file

package module
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

xk6-file

k6 extension for writing files, implemented using the xk6 system.

Build

xk6 build v2.1.0 --with github.com/avitalique/xk6-file@v1.7.0

Example

import http from 'k6/http';
import { check } from 'k6';
import file from 'k6/x/file';

const filepath = 'sample-output.txt';
const binaryFilepath = 'sample-image.jpg';
const dirPath = 'test-dir';

export default function () {
    // Write/append string to file
    file.writeString(filepath, 'New file. First line.\n');
    file.appendString(filepath, `Second line. VU: ${__VU}  -  ITER: ${__ITER}`);

    // Read file
    const fileContent = file.readFile(filepath);
    check(fileContent, {
        "file content is correct": (content) =>
            content.includes("New file. First line.") &&
            content.includes(`Second line. VU: ${__VU}  -  ITER: ${__ITER}`),
    });

    // Remove rows from text file/clear file content/delete file
    file.removeRowsBetweenValues(filepath, 2, 2);
    file.clearFile(filepath);
    file.deleteFile(filepath);

    // Write binary file
    let response = http.get("https://upload.wikimedia.org/wikipedia/commons/3/3f/JPEG_example_flower.jpg", {
        responseType: "binary",
    });
    check(response, { 'status was 200': response.status === 200 });
    file.writeBytes(binaryFilepath, Array.from(new Uint8Array(response.body)));

    // Rename file
    file.renameFile(binaryFilepath, 'renamed-image.jpg')

    // Create directory
    file.createDirectory(dirPath);
    check(file.writeString(`${dirPath}/test-file.txt`, "Testing directory creation.") === undefined, {
        "directory created": (result) => result,
    });

    // Delete directory
    file.deleteDirectory(dirPath);
    check(
        (() => {
            try {
                file.writeString(`${dirPath}/test-file.txt`, "This should fail.");
                return false;
            } catch (e) {
                return true;
            }
        })(),
        {
            "directory deleted": (result) => result,
        }
    );
}

Run sample script

./k6 run examples/sample-script.js

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FILE

type FILE struct{}

FILE is the k6 extension

func (*FILE) AppendString

func (*FILE) AppendString(path string, s string) error

AppendString appends string to file

func (*FILE) ClearFile added in v1.3.0

func (*FILE) ClearFile(path string) error

ClearFile removes all the contents of a file

func (*FILE) CreateDirectory added in v1.5.0

func (*FILE) CreateDirectory(path string) error

CreateDirectory creates a new directory at the specified path

func (*FILE) DeleteDirectory added in v1.5.0

func (*FILE) DeleteDirectory(path string) error

DeleteDirectory deletes the directory at the specified path

func (*FILE) DeleteFile added in v1.3.0

func (*FILE) DeleteFile(path string) error

DeleteFile deletes file

func (*FILE) ReadFile added in v1.5.0

func (*FILE) ReadFile(path string) (string, error)

ReadFile reads the contents of a file and returns it as a string

func (*FILE) RemoveRowsBetweenValues added in v1.3.0

func (*FILE) RemoveRowsBetweenValues(path string, start, end int) error

RemoveRowsBetweenValues removes the rows from a file between start and end (inclusive)

func (FILE) RenameFile added in v1.4.0

func (FILE) RenameFile(oldPath string, newPath string) error

RenameFile renames file from oldPath to newPath

func (*FILE) WriteBytes added in v1.3.0

func (*FILE) WriteBytes(path string, b []byte) error

WriteBytes writes binary file

func (*FILE) WriteString

func (*FILE) WriteString(path string, s string) error

WriteString writes string to file

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL