-
Notifications
You must be signed in to change notification settings - Fork 169
/
fs_test.ts
75 lines (70 loc) · 2.11 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
import {
assert,
assertEquals,
assertNotEquals
} from 'std/testing/asserts.ts'
import { SEP } from 'std/path/separator.ts'
import {
existsDir,
existsFile,
ensureTextFile,
lazyRemove
} from './fs.ts'
Deno.test(`fs: existsDir`, async () => {
// true test cases
const dir = await Deno.makeTempDir()
assert(await existsDir(dir))
assert(await existsDir(await Deno.realPath(getStandardFolder())))
// false test cases
const file = await Deno.makeTempFile()
assertEquals(await existsDir(file), false)
assertEquals(await existsDir(`${dir}${SEP}foo${SEP}bar`), false)
})
Deno.test(`fs: existsFile`, async () => {
// true test cases
const file = await Deno.makeTempFile()
assert(await existsFile(file))
// false test cases
const dir = await Deno.makeTempDir()
assert(!await existsFile(`${dir}`))
assert(!await existsFile(`${dir}${SEP}foo${SEP}bar`))
})
Deno.test('fs: 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('fs: lazyRemove', async () => {
// true test case
const filePath = await Deno.makeTempFile()
await lazyRemove(filePath)
assertEquals(await existsFile(filePath), false)
// false test case
const dirPath = await Deno.makeTempDir()
await lazyRemove(`${dirPath}${SEP}foo${SEP}bar.bin`)
assert(await existsDir(dirPath))
})
/**
* 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'
}