tempdir-yaml

Build temporary directories and files with YAML definition.


Keywords
tmp, temp, tmpdir, tempdir, yaml, test, testing, nodejs
License
MIT
Install
npm install tempdir-yaml@0.3.0

Documentation

tempdir-yaml

Build Status codecov install size

Build temporary directories and files with YAML definition. Inspired by the filemaker of pydeps.

Installation

npm install -D tempdir-yaml

Usage

const fs = require("fs");
const {withDir} = require("tempdir-yaml");

describe("a test", () => {
  it("a case", () =>
    withDir(`
      - package.json
      - sub-dir:
        - foo.txt: |
           content of foo
    `, resolve => {
      const data = fs.readFileSync(resolve("sub-dir/foo.txt"), "utf-8");
      assert.equal(data, "content of foo\n");
    })
  );
});

Some quick examples:

  • An empty folder:
    - my-dir:
  • A directory containing some files:
    - my-dir:
      - file1
      - file2
  • An empty file:
    - file
  • A file with content:
    - file: |
       the content of the file

API

makeDir

async makeDir(yaml: String) =>
  ({
    resolve: (...args) => absPath: String,
    cleanup: async () => undefined
  })

Create a temporary file tree and return a resolve function and a cleanup function. yaml is dedented before parsed.

The file tree is created according to the type of the value:

  • a list - if the list item is:
    • a string - create an empty file. Use the item as its name.
    • a map - see below.
  • a map - a list of name: data pairs. If data is:
    • a string - create a file that name is the name and data is the content.
    • null - create an empty folder.
    • a list or a map - create a folder and use data as the children.

resolve function resolves relative paths to an absolute path based on the temporary folder. You can get the root path with resolve(".").

cleanup function would remove the temporary folder. If cleanup is not called, the temporary folder would be removed at the "exit" event.

withDir

async withDir(yaml:String, async onReady(resolve))

A wrapper of makeDir that would automatically cleanup when onReady returns/throws.

it("my test", () =>
  withDir(`
    - foo.txt: |
        FOO
    - bar.txt: |
        BAR
  `, resolve => {
    // test something with the files...
  })
);

Changelog

  • 0.3.0 (Jun 5, 2019)

    • Bump dependencies.
    • Breaking: now cleanup is an async function.
  • 0.2.1 (Jun 27, 2018)

    • Fix: ENOENT error when onReady is async in withDir.
  • 0.2.0 (Jun 27, 2018)

    • Change: the signature of makeDir is changed.
    • Add: withDir.
  • 0.1.0 (Jun 26, 2018)

    • First release.