https://github.com/eight04/tempdir-yaml
Build temporary directories and files with YAML definition.
https://github.com/eight04/tempdir-yaml
nodejs tempdir test
Last synced: 4 months ago
JSON representation
Build temporary directories and files with YAML definition.
- Host: GitHub
- URL: https://github.com/eight04/tempdir-yaml
- Owner: eight04
- License: mit
- Created: 2018-06-25T16:46:35.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-06-05T10:09:25.000Z (almost 7 years ago)
- Last Synced: 2025-01-28T16:48:37.360Z (over 1 year ago)
- Topics: nodejs, tempdir, test
- Language: JavaScript
- Size: 64.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
tempdir-yaml
============
[](https://travis-ci.org/eight04/tempdir-yaml)
[](https://codecov.io/gh/eight04/tempdir-yaml)
[](https://packagephobia.now.sh/result?p=tempdir-yaml)
Build temporary directories and files with YAML definition. Inspired by [the filemaker of pydeps](https://github.com/thebjorn/pydeps/blob/83762459eed1d199af8ac580b2882189cbca1624/tests/filemaker.py).
Installation
------------
```
npm install -D tempdir-yaml
```
Usage
-----
```js
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:
```yaml
- my-dir:
```
* A directory containing some files:
```yaml
- my-dir:
- file1
- file2
```
* An empty file:
```yaml
- file
```
* A file with content:
```yaml
- file: |
the content of the file
```
API
----
### makeDir
```js
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 [dedent](https://www.npmjs.com/package/dedent)ed 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
```js
async withDir(yaml:String, async onReady(resolve))
```
A wrapper of `makeDir` that would automatically cleanup when `onReady` returns/throws.
```js
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.