-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
61 lines (51 loc) · 1.49 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* @japa/file-system
*
* (c) Japa.dev
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare module '@japa/runner/core' {
interface TestContext {
fs: FileSystem
}
}
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { createId } from '@paralleldrive/cuid2'
import type { PluginFn } from '@japa/runner/types'
import { TestContext, Test } from '@japa/runner/core'
import './src/assert.js'
import { FileSystem } from './src/file_system.js'
/**
* The filesystem plugin for Japa
*/
export function fileSystem(options?: { basePath?: string | URL; autoClean?: boolean }) {
const normalizeOptions = Object.assign(
{ basePath: join(tmpdir(), createId()), autoClean: true },
options
)
/**
* Create the base root path for the file system class
*/
const baseRoot =
typeof normalizeOptions.basePath === 'string'
? normalizeOptions.basePath
: fileURLToPath(normalizeOptions.basePath)
const fsPlugin: PluginFn = function () {
TestContext.getter('fs', () => new FileSystem(baseRoot), true)
Test.executing((test) => {
/**
* Share fs property with assert module
*/
test.context.assert.fs = test.context.fs
if (normalizeOptions.autoClean) {
test.context.cleanup(() => test.context.fs.cleanup())
}
})
}
return fsPlugin
}
export { FileSystem } from './src/file_system.js'