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

Commit 9bbd39c

Browse files
Craig DoremusCraig Doremus
authored andcommitted
test: cleaned up
1 parent 3283217 commit 9bbd39c

File tree

1 file changed

+36
-94
lines changed

1 file changed

+36
-94
lines changed

shared/fs_test.ts

Lines changed: 36 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
2-
assert, assertEquals, assertThrows,
3-
assertExists, assertNotEquals
2+
assert, assertEquals, assertThrows, assertNotEquals
43
} from 'https://deno.land/[email protected]/testing/asserts.ts'
54
import { SEP } from "https://deno.land/[email protected]/path/separator.ts"
65
import {
@@ -10,24 +9,28 @@ import {
109

1110

1211
Deno.test(`fs existsDirSync`, () => {
13-
// console.log('getStandardFolder', getStandardFolder())
1412
// true test cases
15-
assert(existsDirSync(getAbsolutePath(`.${SEP}shared`)))
16-
assert(existsDir(getAbsolutePath(getStandardFolder())))
13+
const dir = Deno.makeTempDirSync()
14+
assert(existsDirSync(dir))
15+
assert(existsDirSync(Deno.realPathSync(getStandardFolder())))
1716
// false test cases
18-
assertEquals(existsDirSync(getAbsolutePath(`.${SEP}foobar`)), false)
19-
assertEquals(existsDirSync(getAbsolutePath(`.${SEP}shared${SEP}fs.ts`)), false)
17+
assertEquals(existsDirSync(`${dir}${SEP}foobar`), false)
18+
const file = Deno.makeTempFileSync()
19+
assertEquals(existsDirSync(file), false)
2020
// error test cases
2121
assertThrows(() => existsDirSync({} as string), Error)
2222
})
2323

24+
2425
Deno.test(`fs async existsDir`, async () => {
2526
// true test cases
26-
assertEquals(await existsDir(getAbsolutePath(getStandardFolder())), true)
27-
assertEquals(await existsDir(getAbsolutePath(`.${SEP}shared`)), true)
27+
assert(await existsDir(await Deno.realPath(getStandardFolder())))
28+
const dir = await Deno.makeTempDir()
29+
assertEquals(await existsDir(dir), true)
2830
// false test cases
29-
assertEquals(await existsDir(getAbsolutePath(`.${SEP}foobar`)), false)
30-
assertEquals(await existsDir(getAbsolutePath(`.${SEP}shared${SEP}fs.ts`)), false)
31+
assertEquals(await existsDir(`${dir}${SEP}foobar`), false)
32+
const file = await Deno.makeTempFile()
33+
assertEquals(await existsDir(file), false)
3134
// error test cases
3235
existsDir({} as string).then(err => {
3336
assert(err instanceof Error)
@@ -36,19 +39,24 @@ Deno.test(`fs async existsDir`, async () => {
3639

3740
Deno.test(`fs existsFileSync`, () => {
3841
// true test cases
39-
assert(existsFileSync(getAbsolutePath(`.${SEP}shared${SEP}fs.ts`)))
42+
const file = Deno.makeTempFileSync()
43+
assert(existsFileSync(file))
4044
// false test cases
41-
assert(!existsFileSync(getAbsolutePath(`.${SEP}shared`)))
42-
assert(!existsFileSync(getAbsolutePath(`.${SEP}shared${SEP}baz.ts`)))
45+
const dir = Deno.makeTempDirSync()
46+
assert(!existsFileSync(`${dir}`))
47+
assert(!existsFileSync(`${dir}${SEP}llksdafzxc.ts`))
4348
// error test cases
4449
assertThrows(() => existsDirSync({} as string), Error)
4550
})
4651

4752
Deno.test(`fs async existsFile`, async () => {
4853
// true test cases
49-
assert(await existsFile(getAbsolutePath(`.${SEP}shared${SEP}fs.ts`)))
54+
const file = await Deno.makeTempFile()
55+
assert(await existsFile(file))
5056
// false test cases
51-
assertEquals(await existsFile(getAbsolutePath(`.${SEP}shared${SEP}foobar.ts`)), false)
57+
const dir = Deno.makeTempDirSync()
58+
assertEquals(await existsFile(dir), false)
59+
assertEquals(await existsFileSync(`${dir}${SEP}llksdafzxc.ts`), false)
5260
// error test cases
5361
existsFile({} as string).then(err => {
5462
assert(err instanceof Error)
@@ -64,24 +72,24 @@ Deno.test('ensureTextFile', async () => {
6472
assert(await existsFile(textFilePath))
6573
const testContent = await Deno.readTextFile(textFilePath)
6674
assertEquals(testContent, content)
67-
// FIXME: false test case
75+
// false test case
6876
// illegal folder name
69-
// const textFilePath2 = `${SEP}test2.txt`
70-
// let testContent2 = ''
71-
// try {
72-
// await ensureTextFile(textFilePath2, content)
73-
// testContent2 = await Deno.readTextFile(textFilePath2)
74-
// } catch (error) {
75-
// assertNotEquals(testContent2, content)
76-
// }
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+
}
7785
})
7886

7987
Deno.test('lazyRemove', async () => {
80-
// true test
88+
// true test case
8189
const filePath = await Deno.makeTempFile()
8290
await lazyRemove(filePath)
8391
assertEquals(existsFileSync(filePath), false)
84-
// false test
92+
// false test case
8593
const dirPath = await Deno.makeTempDir()
8694
await lazyRemove(`${dirPath}${SEP}asdfsdf.txt`)
8795
assert(await existsDir(dirPath))
@@ -92,79 +100,13 @@ Deno.test('lazyRemove', async () => {
92100

93101
})
94102

95-
/**
96-
* Test of local function getAbsolutePath
97-
*/
98-
Deno.test('getAbsolutePath', () => {
99-
// folder
100-
let path = 'shared'
101-
let absPath = getAbsolutePath(path)
102-
assert(Deno.lstatSync(absPath).isDirectory)
103-
path = `.${SEP}shared`
104-
absPath = getAbsolutePath(path)
105-
assert(Deno.lstatSync(absPath).isDirectory)
106-
// file
107-
path = `shared${SEP}fs.ts`
108-
absPath = getAbsolutePath(path)
109-
assert(Deno.lstatSync(absPath).isFile)
110-
path = `.${SEP}shared${SEP}fs.ts`
111-
absPath = getAbsolutePath(path)
112-
assert(Deno.lstatSync(absPath).isFile)
113-
})
114103

115104
/**
116105
* Returns an operating system-specific
117106
* example folder.
118-
* @returns 'C:\Program Files' for Windows or
107+
* @returns 'C:\Windows' for Windows or
119108
* '/tmp' for unix-based operating systems
120109
*/
121110
const getStandardFolder = () => {
122111
return Deno.build.os === 'windows' ? "C:\\Windows" : '/tmp'
123112
}
124-
125-
126-
/**
127-
* This function is designed to be used in this module
128-
* for test cases involving a file or directory. It
129-
* takes a path to a folder or file and converts it to an
130-
* absolute path. Designed to be os-agnostic by using
131-
* the SEP path separator from the Deno standard (std)
132-
* library (separator module).
133-
*
134-
* <strong>Note:</strong> This function might need to
135-
* be modified when the test is run in a CI/CD environment
136-
* depending where the tests are run. The current
137-
* implementation assumes that the tests are being
138-
* run from the repo's root folder.
139-
*
140-
* @param path relative or absolute path string to a folder
141-
* or file. If the string starts with a operating-system
142-
* agnostic slash, then it is assumed to be a full path;
143-
* if the path starts with a dot slash (./) or no
144-
* slash, then the path argument is assumed to be
145-
* a relative path
146-
* @returns the full path to the folder or file
147-
*/
148-
const getAbsolutePath = (path: string): string => {
149-
const cwd = Deno.cwd()
150-
let fullRelativePath
151-
let absolutePath
152-
if (path.startsWith(`.${SEP}`)) { // dot slash
153-
// path == local relative path
154-
fullRelativePath = path.substring(1)
155-
absolutePath = `${cwd}${fullRelativePath}`
156-
// absolutePath = Deno.realPathSync(path)
157-
// console.log('REAL PATH: ', absolutePath)
158-
} else if (path.startsWith(SEP)) { // slash
159-
// path === absolute path
160-
absolutePath = Deno.realPathSync(path)
161-
} else if (path.startsWith('C:\\')) { // windows full path
162-
// path === absolute path
163-
absolutePath = Deno.realPathSync(path)
164-
} else { // no dot or slash at start of path
165-
// path == local relative path
166-
fullRelativePath = path
167-
absolutePath = `${cwd}${SEP}${fullRelativePath}`
168-
}
169-
return absolutePath
170-
}

0 commit comments

Comments
 (0)