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