1
- import fs from 'fs' ;
1
+ import fs , { PathOrFileDescriptor } from 'fs' ;
2
2
3
3
import { CommandInfo } from '../command' ;
4
4
import { ExpandWildcard } from './expand-wildcard' ;
@@ -23,12 +23,53 @@ afterEach(() => {
23
23
} ) ;
24
24
25
25
describe ( 'ExpandWildcard#readDeno' , ( ) => {
26
- it ( 'can read deno' , ( ) => {
26
+ it ( 'can read deno.json ' , ( ) => {
27
27
const expectedDeno = {
28
28
name : 'deno' ,
29
29
version : '1.14.0' ,
30
30
} ;
31
- jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path ) => {
31
+ jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
32
+ return path === 'deno.json' ;
33
+ } ) ;
34
+ jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
35
+ if ( path === 'deno.json' ) {
36
+ return JSON . stringify ( expectedDeno ) ;
37
+ }
38
+ return '' ;
39
+ } ) ;
40
+
41
+ const actualReadDeno = ExpandWildcard . readDeno ( ) ;
42
+ expect ( actualReadDeno ) . toEqual ( expectedDeno ) ;
43
+ } ) ;
44
+
45
+ it ( 'can read deno.jsonc' , ( ) => {
46
+ const expectedDeno = {
47
+ name : 'deno' ,
48
+ version : '1.14.0' ,
49
+ } ;
50
+ jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
51
+ return path === 'deno.jsonc' ;
52
+ } ) ;
53
+ jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
54
+ if ( path === 'deno.jsonc' ) {
55
+ return '/* comment */\n' + JSON . stringify ( expectedDeno ) ;
56
+ }
57
+ return '' ;
58
+ } ) ;
59
+
60
+ const actualReadDeno = ExpandWildcard . readDeno ( ) ;
61
+ expect ( actualReadDeno ) . toEqual ( expectedDeno ) ;
62
+ } ) ;
63
+
64
+ it ( 'prefers deno.json over deno.jsonc' , ( ) => {
65
+ const expectedDeno = {
66
+ name : 'deno' ,
67
+ version : '1.14.0' ,
68
+ } ;
69
+ jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
70
+ return path === 'deno.json' || path === 'deno.jsonc' ;
71
+ } ) ;
72
+ jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
32
73
if ( path === 'deno.json' ) {
33
74
return JSON . stringify ( expectedDeno ) ;
34
75
}
@@ -40,6 +81,7 @@ describe('ExpandWildcard#readDeno', () => {
40
81
} ) ;
41
82
42
83
it ( 'can handle errors reading deno' , ( ) => {
84
+ jest . spyOn ( fs , 'existsSync' ) . mockReturnValue ( true ) ;
43
85
jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( ) => {
44
86
throw new Error ( 'Error reading deno' ) ;
45
87
} ) ;
@@ -55,7 +97,7 @@ describe('ExpandWildcard#readPackage', () => {
55
97
name : 'concurrently' ,
56
98
version : '6.4.0' ,
57
99
} ;
58
- jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path ) => {
100
+ jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( path : PathOrFileDescriptor ) => {
59
101
if ( path === 'package.json' ) {
60
102
return JSON . stringify ( expectedPackage ) ;
61
103
}
@@ -105,7 +147,7 @@ it('expands to nothing if no scripts exist in package.json', () => {
105
147
expect ( parser . parse ( createCommandInfo ( 'npm run foo-*-baz qux' ) ) ) . toEqual ( [ ] ) ;
106
148
} ) ;
107
149
108
- it ( 'expands to nothing if no tasks exist in deno.json and no scripts exist in package.json ' , ( ) => {
150
+ it ( 'expands to nothing if no tasks exist in Deno config and no scripts exist in NodeJS config ' , ( ) => {
109
151
readDeno . mockReturnValue ( { } ) ;
110
152
readPackage . mockReturnValue ( { } ) ;
111
153
@@ -192,7 +234,7 @@ describe.each(['npm run', 'yarn run', 'pnpm run', 'bun run', 'node --run'])(
192
234
expect ( readPackage ) . toHaveBeenCalledTimes ( 1 ) ;
193
235
} ) ;
194
236
195
- it ( "doesn't read deno.json " , ( ) => {
237
+ it ( "doesn't read Deno config " , ( ) => {
196
238
readPackage . mockReturnValue ( { } ) ;
197
239
198
240
parser . parse ( createCommandInfo ( `${ command } foo-*-baz qux` ) ) ;
0 commit comments