1
1
import {
2
- assert , assertEquals , assertThrows ,
3
- assertExists , assertNotEquals
2
+ assert , assertEquals , assertThrows , assertNotEquals
4
3
} from 'https://deno.land/[email protected] /testing/asserts.ts'
5
4
import { SEP } from "https://deno.land/[email protected] /path/separator.ts"
6
5
import {
@@ -10,24 +9,28 @@ import {
10
9
11
10
12
11
Deno . test ( `fs existsDirSync` , ( ) => {
13
- // console.log('getStandardFolder', getStandardFolder())
14
12
// 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 ( ) ) ) )
17
16
// 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 )
20
20
// error test cases
21
21
assertThrows ( ( ) => existsDirSync ( { } as string ) , Error )
22
22
} )
23
23
24
+
24
25
Deno . test ( `fs async existsDir` , async ( ) => {
25
26
// 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 )
28
30
// 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 )
31
34
// error test cases
32
35
existsDir ( { } as string ) . then ( err => {
33
36
assert ( err instanceof Error )
@@ -36,19 +39,24 @@ Deno.test(`fs async existsDir`, async () => {
36
39
37
40
Deno . test ( `fs existsFileSync` , ( ) => {
38
41
// true test cases
39
- assert ( existsFileSync ( getAbsolutePath ( `.${ SEP } shared${ SEP } fs.ts` ) ) )
42
+ const file = Deno . makeTempFileSync ( )
43
+ assert ( existsFileSync ( file ) )
40
44
// 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` ) )
43
48
// error test cases
44
49
assertThrows ( ( ) => existsDirSync ( { } as string ) , Error )
45
50
} )
46
51
47
52
Deno . test ( `fs async existsFile` , async ( ) => {
48
53
// true test cases
49
- assert ( await existsFile ( getAbsolutePath ( `.${ SEP } shared${ SEP } fs.ts` ) ) )
54
+ const file = await Deno . makeTempFile ( )
55
+ assert ( await existsFile ( file ) )
50
56
// 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 )
52
60
// error test cases
53
61
existsFile ( { } as string ) . then ( err => {
54
62
assert ( err instanceof Error )
@@ -64,24 +72,24 @@ Deno.test('ensureTextFile', async () => {
64
72
assert ( await existsFile ( textFilePath ) )
65
73
const testContent = await Deno . readTextFile ( textFilePath )
66
74
assertEquals ( testContent , content )
67
- // FIXME: false test case
75
+ // false test case
68
76
// 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
+ }
77
85
} )
78
86
79
87
Deno . test ( 'lazyRemove' , async ( ) => {
80
- // true test
88
+ // true test case
81
89
const filePath = await Deno . makeTempFile ( )
82
90
await lazyRemove ( filePath )
83
91
assertEquals ( existsFileSync ( filePath ) , false )
84
- // false test
92
+ // false test case
85
93
const dirPath = await Deno . makeTempDir ( )
86
94
await lazyRemove ( `${ dirPath } ${ SEP } asdfsdf.txt` )
87
95
assert ( await existsDir ( dirPath ) )
@@ -92,79 +100,13 @@ Deno.test('lazyRemove', async () => {
92
100
93
101
} )
94
102
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
- } )
114
103
115
104
/**
116
105
* Returns an operating system-specific
117
106
* example folder.
118
- * @returns 'C:\Program Files ' for Windows or
107
+ * @returns 'C:\Windows ' for Windows or
119
108
* '/tmp' for unix-based operating systems
120
109
*/
121
110
const getStandardFolder = ( ) => {
122
111
return Deno . build . os === 'windows' ? "C:\\Windows" : '/tmp'
123
112
}
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