fs-fixture 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +121 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# fs-fixture
|
|
2
|
+
|
|
3
|
+
Easily create test fixtures at a temporary file-system path.
|
|
4
|
+
|
|
5
|
+
<sub>Support this project by ⭐️ starring and sharing it. [Follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️</sub>
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
### JSON input
|
|
10
|
+
|
|
11
|
+
Pass in an object representing the test fixture.
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createFixture } from 'fs-fixture'
|
|
15
|
+
|
|
16
|
+
test('my test using json fixture', async () => {
|
|
17
|
+
// Pass in a JSON representing the test fixture
|
|
18
|
+
const fixture = await createFixture({
|
|
19
|
+
// Nested directory syntax
|
|
20
|
+
directoryA: {
|
|
21
|
+
directoryB: {
|
|
22
|
+
fileNameA: 'fileContent'
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
// Directory path syntax - Same as above
|
|
27
|
+
'directoryA/directoryB/fileNameB': 'fileContent'
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
/*
|
|
31
|
+
Your test code here...
|
|
32
|
+
|
|
33
|
+
// Log fixture path
|
|
34
|
+
console.log(fixture.path)
|
|
35
|
+
|
|
36
|
+
// Check if relative path exists
|
|
37
|
+
console.log(await fixture.exists('./file'))
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
// Cleanup fixture
|
|
41
|
+
await fixture.rm()
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Template path input
|
|
46
|
+
|
|
47
|
+
Pass in a path to a test fixture template directory to make a copy of it.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
test('my test using template path', async () => {
|
|
51
|
+
// Pass in a path to a fixture template path, and it will make a copy of it
|
|
52
|
+
const fixture = await createFixture('./fixtures/template-a')
|
|
53
|
+
|
|
54
|
+
/* Your test code here... */
|
|
55
|
+
|
|
56
|
+
// Cleanup fixture
|
|
57
|
+
await fixture.rm()
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## API
|
|
62
|
+
|
|
63
|
+
### createFixture(source)
|
|
64
|
+
|
|
65
|
+
An async function that creates a fixture from the `source` you pass in, and returns a `FsFixture` instance.
|
|
66
|
+
|
|
67
|
+
#### source
|
|
68
|
+
Type: `string | FileTree`
|
|
69
|
+
|
|
70
|
+
Path to a template fixture path, or a `FileTree` object that represents the fixture content.
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
### Types
|
|
74
|
+
#### FileTree
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
type FileTree = {
|
|
78
|
+
[path: string]: string | FileTree
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### FsFixture
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
class FsFixture {
|
|
86
|
+
/**
|
|
87
|
+
Path to the fixture directory.
|
|
88
|
+
*/
|
|
89
|
+
path: string
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
Create a Fixture instance from a path. Does not create the fixture directory.
|
|
93
|
+
*/
|
|
94
|
+
constructor(fixturePath: string)
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
Check if the fixture exists. Pass in a subpath to check if it exists.
|
|
98
|
+
*/
|
|
99
|
+
exists(subpath?: string): Promise<boolean>
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
Delete the fixture directory. Pass in a subpath to delete it.
|
|
103
|
+
*/
|
|
104
|
+
rm(subpath?: string): Promise<void>
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
Create a file in the fixture directory.
|
|
108
|
+
*/
|
|
109
|
+
writeFile(filePath: string, content: string): Promise<void>
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
Create a JSON file in the fixture directory.
|
|
113
|
+
*/
|
|
114
|
+
writeJson(filePath: string, json: any): Promise<void>
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
Read a file from the fixture directory.
|
|
118
|
+
*/
|
|
119
|
+
readFile(filePath: string, encoding?: BufferEncoding): Promise<string | Buffer>
|
|
120
|
+
}
|
|
121
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
declare class FsFixture {
|
|
2
|
+
/**
|
|
3
|
+
Path to the fixture directory.
|
|
4
|
+
*/
|
|
5
|
+
path: string;
|
|
6
|
+
/**
|
|
7
|
+
Create a Fixture instance from a path. Does not create the fixture directory.
|
|
8
|
+
*/
|
|
9
|
+
constructor(fixturePath: string);
|
|
10
|
+
/**
|
|
11
|
+
Check if the fixture exists. Pass in a subpath to check if it exists.
|
|
12
|
+
*/
|
|
13
|
+
exists(subpath?: string): Promise<boolean>;
|
|
14
|
+
/**
|
|
15
|
+
Delete the fixture directory. Pass in a subpath to delete it.
|
|
16
|
+
*/
|
|
17
|
+
rm(subpath?: string): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
Create a file in the fixture directory.
|
|
20
|
+
*/
|
|
21
|
+
writeFile(filePath: string, content: string): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
Create a JSON file in the fixture directory.
|
|
24
|
+
*/
|
|
25
|
+
writeJson(filePath: string, json: any): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
Read a file from the fixture directory.
|
|
28
|
+
*/
|
|
29
|
+
readFile(filePath: string, encoding?: BufferEncoding): Promise<string | Buffer>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
declare type FileTree = {
|
|
33
|
+
[path: string]: string | FileTree;
|
|
34
|
+
};
|
|
35
|
+
declare function createFixture(source: string | FileTree): Promise<FsFixture>;
|
|
36
|
+
|
|
37
|
+
export { FileTree, FsFixture, createFixture };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var a=require("fs"),c=require("path"),d=require("os");function u(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var h=u(a),i=u(c),m=u(d);class w{path;constructor(t){this.path=t}exists(t=""){return a.promises.access(i.default.join(this.path,t)).then(()=>!0,()=>!1)}rm(t=""){return a.promises.rm(i.default.join(this.path,t),{recursive:!0,force:!0})}writeFile(t,r){return a.promises.writeFile(i.default.join(this.path,t),r)}writeJson(t,r){return this.writeFile(t,JSON.stringify(r,null,2))}readFile(t,r){return a.promises.readFile(i.default.join(this.path,t),r)}}const o=i.default.join(h.default.realpathSync(m.default.tmpdir()),"test-fixtures",Date.now().toString());let l=0;function f(){return l+=1,l}const{hasOwnProperty:y}=Object.prototype,j=(e,t)=>y.call(e,t);function p(e,t){const r=[];for(const s in e){if(!j(e,s))continue;const n=e[s];typeof n=="string"?r.push({path:i.default.join(t,s),content:n}):r.push(...p(n,i.default.join(t,s)))}return r}async function v(e){let t;return typeof e=="string"?(t=i.default.join(o,`${i.default.basename(e)}-${f()}`),await a.promises.mkdir(t,{recursive:!0}),await a.promises.cp(e,t,{recursive:!0})):(t=i.default.join(o,`fixture-${f()}`),await Promise.all(p(e,t).map(async r=>{await a.promises.mkdir(i.default.dirname(r.path),{recursive:!0}),await a.promises.writeFile(r.path,r.content)}))),new w(t)}exports.createFixture=v;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import h,{promises as n}from"fs";import i from"path";import l from"os";class f{path;constructor(t){this.path=t}exists(t=""){return n.access(i.join(this.path,t)).then(()=>!0,()=>!1)}rm(t=""){return n.rm(i.join(this.path,t),{recursive:!0,force:!0})}writeFile(t,e){return n.writeFile(i.join(this.path,t),e)}writeJson(t,e){return this.writeFile(t,JSON.stringify(e,null,2))}readFile(t,e){return n.readFile(i.join(this.path,t),e)}}const a=i.join(h.realpathSync(l.tmpdir()),"test-fixtures",Date.now().toString());let u=0;function c(){return u+=1,u}const{hasOwnProperty:m}=Object.prototype,w=(r,t)=>m.call(r,t);function p(r,t){const e=[];for(const o in r){if(!w(r,o))continue;const s=r[o];typeof s=="string"?e.push({path:i.join(t,o),content:s}):e.push(...p(s,i.join(t,o)))}return e}async function j(r){let t;return typeof r=="string"?(t=i.join(a,`${i.basename(r)}-${c()}`),await n.mkdir(t,{recursive:!0}),await n.cp(r,t,{recursive:!0})):(t=i.join(a,`fixture-${c()}`),await Promise.all(p(r,t).map(async e=>{await n.mkdir(i.dirname(e.path),{recursive:!0}),await n.writeFile(e.path,e.content)}))),new f(t)}export{j as createFixture};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "fs-fixture",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Easily create test fixtures at a temporary file-system path",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"test",
|
|
7
|
+
"fixture",
|
|
8
|
+
"temporary",
|
|
9
|
+
"file-system",
|
|
10
|
+
"disk",
|
|
11
|
+
"json",
|
|
12
|
+
"object",
|
|
13
|
+
"template"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": "privatenumber/fs-fixture",
|
|
17
|
+
"funding": "https://github.com/privatenumber/fs-fixture?sponsor=1",
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "Hiroki Osame",
|
|
20
|
+
"email": "hiroki.osame@gmail.com"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"module": "./dist/index.mjs",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
"require": "./dist/index.js",
|
|
30
|
+
"import": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"imports": {
|
|
34
|
+
"#fs-fixture": {
|
|
35
|
+
"types": "./src/index.ts",
|
|
36
|
+
"development": "./src/index.ts",
|
|
37
|
+
"default": "./dist/index.mjs"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|