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

Commit 3b00b79

Browse files
Craig DoremusCraig Doremus
authored andcommitted
test: first fs.ts tests of exists functions
1 parent 12d136c commit 3b00b79

File tree

2 files changed

+141
-5
lines changed

2 files changed

+141
-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: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { assert, assertEquals, assertThrows } from 'https://deno.land/[email protected]/testing/asserts.ts'
2+
import { SEP } from "https://deno.land/[email protected]/path/separator.ts"
3+
import { existsDir, existsDirSync, existsFile, existsFileSync } from './fs.ts'
4+
5+
6+
Deno.test(`fs existsDirSync`, () => {
7+
// true test cases
8+
assert(existsDirSync(getAbsolutePath(`.${SEP}shared`)))
9+
assert(existsDir(getAbsolutePath(getStandardFolder())))
10+
// false test cases
11+
assertEquals(existsDirSync(getAbsolutePath(`.${SEP}foobar`)), false)
12+
assertEquals(existsDirSync(getAbsolutePath(`.${SEP}shared${SEP}fs.ts`)), false)
13+
assertEquals(existsDirSync(getAbsolutePath('&*^--%$#@')), false)
14+
// error test cases
15+
assertThrows(() => existsDirSync({} as string), Error)
16+
})
17+
18+
Deno.test(`fs async existsDir`, async () => {
19+
// true test cases
20+
assertEquals(await existsDir(getAbsolutePath(getStandardFolder())), true)
21+
assertEquals(await existsDir(getAbsolutePath(`.${SEP}shared`)), true)
22+
// false test cases
23+
assertEquals(await existsDir(getAbsolutePath(`.${SEP}foobar`)), false)
24+
assertEquals(await existsDir(getAbsolutePath(`.${SEP}shared${SEP}fs.ts`)), false)
25+
assertEquals(await existsDir(getAbsolutePath('&*^--%$#@')), false)
26+
// error test cases
27+
existsDir({} as string).then(err => {
28+
assert(err instanceof Error)
29+
}).catch(e => console.error(e))
30+
})
31+
32+
Deno.test(`fs existsFileSync`, () => {
33+
// true test cases
34+
assert(existsFileSync(getAbsolutePath(`.${SEP}shared${SEP}fs.ts`)))
35+
// false test cases
36+
assert(!existsFileSync(getAbsolutePath(`.${SEP}shared`)))
37+
assert(!existsFileSync(getAbsolutePath(`.${SEP}shared${SEP}baz.ts`)))
38+
// error test cases
39+
assertThrows(() => existsDirSync({} as string), Error)
40+
})
41+
42+
Deno.test(`fs async existsFile`, async () => {
43+
// true test cases
44+
assert(await existsFile(getAbsolutePath(`.${SEP}shared${SEP}fs.ts`)))
45+
// false test cases
46+
assertEquals(await existsFile(getAbsolutePath(`.${SEP}shared${SEP}foobar.ts`)), false)
47+
// error test cases
48+
existsDir({} as string).then(err => {
49+
assert(err instanceof Error)
50+
}).catch(e => console.error(e))
51+
})
52+
53+
Deno.test('ensureTextFile', () => {
54+
55+
})
56+
57+
Deno.test('lazyRemove', () => {
58+
59+
})
60+
61+
/**
62+
* Test of local function getAbsolutePath
63+
*/
64+
Deno.test('getAbsolutePath', () => {
65+
// folder
66+
let path = 'shared'
67+
let absPath = getAbsolutePath(path)
68+
assert(Deno.lstatSync(absPath).isDirectory)
69+
path = `.${SEP}shared`
70+
absPath = getAbsolutePath(path)
71+
assert(Deno.lstatSync(absPath).isDirectory)
72+
// file
73+
path = `shared${SEP}fs.ts`
74+
absPath = getAbsolutePath(path)
75+
assert(Deno.lstatSync(absPath).isFile)
76+
path = `.${SEP}shared${SEP}fs.ts`
77+
absPath = getAbsolutePath(path)
78+
assert(Deno.lstatSync(absPath).isFile)
79+
})
80+
81+
/**
82+
* Returns an operating system-specific
83+
* example folder.
84+
* @returns 'C:\Program Files' for Windows or
85+
* '/tmp' for unix-based operating systems
86+
*/
87+
const getStandardFolder = () => {
88+
return Deno.build.os === 'windows' ? 'C:\Program Files' : '/tmp'
89+
}
90+
91+
92+
/**
93+
* This function is designed to be used in this module
94+
* for test cases involving a file or directory. It
95+
* takes a path to a folder or file and converts it to an
96+
* absolute path. Designed to be os-agnostic by using
97+
* the SEP path separator from the Deno standard (std)
98+
* library (separator module).
99+
*
100+
* <strong>Note:</strong> This function might need to
101+
* be modified when the test is run in a CI/CD environment
102+
* depending where the tests are run. The current
103+
* implementation assumes that the tests are being
104+
* run from the repo's root folder.
105+
*
106+
* @param path relative or absolute path string to a folder
107+
* or file. If the string starts with a operating-system
108+
* agnostic slash, then it is assumed to be a full path;
109+
* if the path starts with a dot slash (./) or no
110+
* slash, then the path argument is assumed to be
111+
* a relative path
112+
* @returns the full path to the folder or file
113+
*/
114+
const getAbsolutePath = (path: string): string => {
115+
const cwd = Deno.cwd()
116+
let fullRelativePath
117+
let absolutePath
118+
if (path.startsWith(`.${SEP}`)) { // dot slash
119+
// path == local relative path
120+
fullRelativePath = path.substring(1)
121+
absolutePath = `${cwd}${fullRelativePath}`
122+
// absolutePath = Deno.realPathSync(path)
123+
// console.log('REAL PATH: ', absolutePath)
124+
} else if (path.startsWith(SEP)) { // slash
125+
// path === absolute path
126+
absolutePath = Deno.realPathSync(path)
127+
} else if (path.startsWith('C:\\')) { // windows full path
128+
// path === absolute path
129+
absolutePath = Deno.realPathSync(path)
130+
} else { // no dot or slash at start of path
131+
// path == local relative path
132+
fullRelativePath = path
133+
absolutePath = `${cwd}${SEP}${fullRelativePath}`
134+
}
135+
return absolutePath
136+
}

0 commit comments

Comments
 (0)