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

Commit fde17f0

Browse files
authored
Merge pull request #248 from cdoremus/add-fs-test
Unit tests for shared/fs.ts
2 parents 011eb5e + 9bbd39c commit fde17f0

File tree

2 files changed

+117
-5
lines changed

2 files changed

+117
-5
lines changed

shared/fs.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { dirname } from 'https://deno.land/[email protected]/path/mod.ts'
22
import { ensureDir } from 'https://deno.land/[email protected]/fs/ensure_dir.ts'
33

44
/* check whether or not the given path exists as a directory. */
5-
export async function existsDir(path: string): Promise<boolean> {
5+
export async function existsDir(path: string): Promise<boolean | Error> {
66
try {
77
const fi = await Deno.lstat(path)
88
if (fi.isDirectory) {
@@ -18,7 +18,7 @@ export async function existsDir(path: string): Promise<boolean> {
1818
}
1919

2020
/* check whether or not the given path exists as a directory. */
21-
export function existsDirSync(path: string) {
21+
export function existsDirSync(path: string): boolean | Error {
2222
try {
2323
const fi = Deno.lstatSync(path)
2424
if (fi.isDirectory) {
@@ -34,7 +34,7 @@ export function existsDirSync(path: string) {
3434
}
3535

3636
/* check whether or not the given path exists as regular file. */
37-
export async function existsFile(path: string): Promise<boolean> {
37+
export async function existsFile(path: string): Promise<boolean | Error> {
3838
try {
3939
const fi = await Deno.lstat(path)
4040
if (fi.isFile) {
@@ -50,7 +50,7 @@ export async function existsFile(path: string): Promise<boolean> {
5050
}
5151

5252
/* check whether or not the given path exists as regular file. */
53-
export function existsFileSync(path: string) {
53+
export function existsFileSync(path: string): boolean {
5454
try {
5555
const fi = Deno.lstatSync(path)
5656
if (fi.isFile) {
@@ -73,7 +73,7 @@ export async function ensureTextFile(name: string, content: string): Promise<voi
7373
}
7474

7575
/** remove the file if it exists. */
76-
export async function lazyRemove(name: string, options?: { recursive?: boolean }): Promise<void> {
76+
export async function lazyRemove(name: string, options?: { recursive?: boolean }): Promise<void | Error> {
7777
try {
7878
await Deno.remove(name, options)
7979
} catch (err) {

shared/fs_test.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import {
2+
assert, assertEquals, assertThrows, assertNotEquals
3+
} from 'https://deno.land/[email protected]/testing/asserts.ts'
4+
import { SEP } from "https://deno.land/[email protected]/path/separator.ts"
5+
import {
6+
existsDir, existsDirSync, existsFile, existsFileSync,
7+
ensureTextFile, lazyRemove
8+
} from './fs.ts'
9+
10+
11+
Deno.test(`fs existsDirSync`, () => {
12+
// true test cases
13+
const dir = Deno.makeTempDirSync()
14+
assert(existsDirSync(dir))
15+
assert(existsDirSync(Deno.realPathSync(getStandardFolder())))
16+
// false test cases
17+
assertEquals(existsDirSync(`${dir}${SEP}foobar`), false)
18+
const file = Deno.makeTempFileSync()
19+
assertEquals(existsDirSync(file), false)
20+
// error test cases
21+
assertThrows(() => existsDirSync({} as string), Error)
22+
})
23+
24+
25+
Deno.test(`fs async existsDir`, async () => {
26+
// true test cases
27+
assert(await existsDir(await Deno.realPath(getStandardFolder())))
28+
const dir = await Deno.makeTempDir()
29+
assertEquals(await existsDir(dir), true)
30+
// false test cases
31+
assertEquals(await existsDir(`${dir}${SEP}foobar`), false)
32+
const file = await Deno.makeTempFile()
33+
assertEquals(await existsDir(file), false)
34+
// error test cases
35+
existsDir({} as string).then(err => {
36+
assert(err instanceof Error)
37+
}).catch(e => console.error(e))
38+
})
39+
40+
Deno.test(`fs existsFileSync`, () => {
41+
// true test cases
42+
const file = Deno.makeTempFileSync()
43+
assert(existsFileSync(file))
44+
// false test cases
45+
const dir = Deno.makeTempDirSync()
46+
assert(!existsFileSync(`${dir}`))
47+
assert(!existsFileSync(`${dir}${SEP}llksdafzxc.ts`))
48+
// error test cases
49+
assertThrows(() => existsDirSync({} as string), Error)
50+
})
51+
52+
Deno.test(`fs async existsFile`, async () => {
53+
// true test cases
54+
const file = await Deno.makeTempFile()
55+
assert(await existsFile(file))
56+
// false test cases
57+
const dir = Deno.makeTempDirSync()
58+
assertEquals(await existsFile(dir), false)
59+
assertEquals(await existsFileSync(`${dir}${SEP}llksdafzxc.ts`), false)
60+
// error test cases
61+
existsFile({} as string).then(err => {
62+
assert(err instanceof Error)
63+
}).catch(e => console.error(e))
64+
})
65+
66+
Deno.test('ensureTextFile', async () => {
67+
// true test case
68+
const dirPath = await Deno.makeTempDir()
69+
const textFilePath = `${dirPath}${SEP}test.txt`
70+
const content = 'This is a test'
71+
await ensureTextFile(textFilePath, content)
72+
assert(await existsFile(textFilePath))
73+
const testContent = await Deno.readTextFile(textFilePath)
74+
assertEquals(testContent, content)
75+
// false test case
76+
// illegal folder name
77+
const textFilePath2 = `${SEP}test2.txt`
78+
let testContent2 = ''
79+
try {
80+
await ensureTextFile(textFilePath2, content)
81+
testContent2 = await Deno.readTextFile(textFilePath2)
82+
} catch (error) {
83+
assertNotEquals(testContent2, content)
84+
}
85+
})
86+
87+
Deno.test('lazyRemove', async () => {
88+
// true test case
89+
const filePath = await Deno.makeTempFile()
90+
await lazyRemove(filePath)
91+
assertEquals(existsFileSync(filePath), false)
92+
// false test case
93+
const dirPath = await Deno.makeTempDir()
94+
await lazyRemove(`${dirPath}${SEP}asdfsdf.txt`)
95+
assert(await existsDir(dirPath))
96+
// error test
97+
lazyRemove({} as string).then(err => {
98+
assert(err instanceof Error)
99+
}).catch(e => console.error(e))
100+
101+
})
102+
103+
104+
/**
105+
* Returns an operating system-specific
106+
* example folder.
107+
* @returns 'C:\Windows' for Windows or
108+
* '/tmp' for unix-based operating systems
109+
*/
110+
const getStandardFolder = () => {
111+
return Deno.build.os === 'windows' ? "C:\\Windows" : '/tmp'
112+
}

0 commit comments

Comments
 (0)