-
Notifications
You must be signed in to change notification settings - Fork 170
/
fs_test.ts
112 lines (101 loc) · 3.48 KB
/
fs_test.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {
assert, assertEquals, assertThrows, assertNotEquals
} from 'std/testing/asserts.ts'
import { SEP } from 'std/path/separator.ts'
import {
existsDir, existsDirSync, existsFile, existsFileSync,
ensureTextFile, lazyRemove
} from './fs.ts'
Deno.test(`fs existsDirSync`, () => {
// true test cases
const dir = Deno.makeTempDirSync()
assert(existsDirSync(dir))
assert(existsDirSync(Deno.realPathSync(getStandardFolder())))
// false test cases
assertEquals(existsDirSync(`${dir}${SEP}foobar`), false)
const file = Deno.makeTempFileSync()
assertEquals(existsDirSync(file), false)
// error test cases
assertThrows(() => existsDirSync({} as string), Error)
})
Deno.test(`fs async existsDir`, async () => {
// true test cases
assert(await existsDir(await Deno.realPath(getStandardFolder())))
const dir = await Deno.makeTempDir()
assertEquals(await existsDir(dir), true)
// false test cases
assertEquals(await existsDir(`${dir}${SEP}foobar`), false)
const file = await Deno.makeTempFile()
assertEquals(await existsDir(file), false)
// error test cases
existsDir({} as string).then(err => {
assert(err instanceof Error)
}).catch(e => console.error(e))
})
Deno.test(`fs existsFileSync`, () => {
// true test cases
const file = Deno.makeTempFileSync()
assert(existsFileSync(file))
// false test cases
const dir = Deno.makeTempDirSync()
assert(!existsFileSync(`${dir}`))
assert(!existsFileSync(`${dir}${SEP}llksdafzxc.ts`))
// error test cases
assertThrows(() => existsDirSync({} as string), Error)
})
Deno.test(`fs async existsFile`, async () => {
// true test cases
const file = await Deno.makeTempFile()
assert(await existsFile(file))
// false test cases
const dir = Deno.makeTempDirSync()
assertEquals(await existsFile(dir), false)
assertEquals(await existsFileSync(`${dir}${SEP}llksdafzxc.ts`), false)
// error test cases
existsFile({} as string).then(err => {
assert(err instanceof Error)
}).catch(e => console.error(e))
})
Deno.test('ensureTextFile', async () => {
// true test case
const dirPath = await Deno.makeTempDir()
const textFilePath = `${dirPath}${SEP}test.txt`
const content = 'This is a test'
await ensureTextFile(textFilePath, content)
assert(await existsFile(textFilePath))
const testContent = await Deno.readTextFile(textFilePath)
assertEquals(testContent, content)
// false test case
// illegal folder name
const textFilePath2 = `${SEP}test2.txt`
let testContent2 = ''
try {
await ensureTextFile(textFilePath2, content)
testContent2 = await Deno.readTextFile(textFilePath2)
} catch (error) {
assertNotEquals(testContent2, content)
}
})
Deno.test('lazyRemove', async () => {
// true test case
const filePath = await Deno.makeTempFile()
await lazyRemove(filePath)
assertEquals(existsFileSync(filePath), false)
// false test case
const dirPath = await Deno.makeTempDir()
await lazyRemove(`${dirPath}${SEP}asdfsdf.txt`)
assert(await existsDir(dirPath))
// error test
lazyRemove({} as string).then(err => {
assert(err instanceof Error)
}).catch(e => console.error(e))
})
/**
* Returns an operating system-specific
* example folder.
* @returns 'C:\Windows' for Windows or
* '/tmp' for unix-based operating systems
*/
const getStandardFolder = () => {
return Deno.build.os === 'windows' ? "C:\\Windows" : '/tmp'
}