Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit b5ad290

Browse files
Craig DoremusCraig Doremus
authored andcommitted
test: added tests for ensureTextFile & lazyRemove
1 parent 3b00b79 commit b5ad290

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

shared/fs_test.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { assert, assertEquals, assertThrows } from 'https://deno.land/[email protected]/testing/asserts.ts'
1+
import {
2+
assert, assertEquals, assertThrows,
3+
assertExists, assertNotEquals
4+
} from 'https://deno.land/[email protected]/testing/asserts.ts'
25
import { SEP } from "https://deno.land/[email protected]/path/separator.ts"
3-
import { existsDir, existsDirSync, existsFile, existsFileSync } from './fs.ts'
6+
import {
7+
existsDir, existsDirSync, existsFile, existsFileSync,
8+
ensureTextFile, lazyRemove
9+
} from './fs.ts'
410

511

612
Deno.test(`fs existsDirSync`, () => {
@@ -45,16 +51,45 @@ Deno.test(`fs async existsFile`, async () => {
4551
// false test cases
4652
assertEquals(await existsFile(getAbsolutePath(`.${SEP}shared${SEP}foobar.ts`)), false)
4753
// error test cases
48-
existsDir({} as string).then(err => {
54+
existsFile({} as string).then(err => {
4955
assert(err instanceof Error)
5056
}).catch(e => console.error(e))
5157
})
5258

53-
Deno.test('ensureTextFile', () => {
54-
59+
Deno.test('ensureTextFile', async () => {
60+
// true test case
61+
const dirPath = await Deno.makeTempDir()
62+
const textFilePath = `${dirPath}${SEP}test.txt`
63+
const content = 'This is a test'
64+
await ensureTextFile(textFilePath, content)
65+
assert(await existsFile(textFilePath))
66+
const testContent = await Deno.readTextFile(textFilePath)
67+
assertEquals(testContent, content)
68+
// FIXME: false test case
69+
// illegal folder name
70+
// const textFilePath2 = `${SEP}test2.txt`
71+
// let testContent2 = ''
72+
// try {
73+
// await ensureTextFile(textFilePath2, content)
74+
// testContent2 = await Deno.readTextFile(textFilePath2)
75+
// } catch (error) {
76+
// assertNotEquals(testContent2, content)
77+
// }
5578
})
5679

57-
Deno.test('lazyRemove', () => {
80+
Deno.test('lazyRemove', async () => {
81+
// true test
82+
const filePath = await Deno.makeTempFile()
83+
await lazyRemove(filePath)
84+
assertEquals(existsFileSync(filePath), false)
85+
// false test
86+
const dirPath = await Deno.makeTempDir()
87+
await lazyRemove(`${dirPath}${SEP}asdfsdf.txt`)
88+
assert(await existsDir(dirPath))
89+
// error test
90+
lazyRemove({} as string).then(err => {
91+
assert(err instanceof Error)
92+
}).catch(e => console.error(e))
5893

5994
})
6095

0 commit comments

Comments
 (0)