1
+ import * as os from 'os'
1
2
import * as path from 'path'
2
3
import * as TestContext from '../test-context'
3
4
import { normalizePathsInData , Param1 } from '../utils'
4
5
import { detectExecLayout } from './detect-exec-layout'
5
6
7
+ const isWindows = os . platform ( ) === 'win32'
8
+
6
9
const ctx = TestContext . compose ( TestContext . tmpDir ( ) , TestContext . fs ( ) , ( ctx ) => {
7
10
return {
8
11
detectExecLayout : ( input ?: Partial < Param1 < typeof detectExecLayout > > ) => {
@@ -24,13 +27,25 @@ function nodeProject() {
24
27
ctx . fs . write ( 'package.json' , '{}' )
25
28
}
26
29
function installTool ( ) {
27
- ctx . fs . write ( 'package.json' , '{ "dependencies": { "a": "foo" } }' )
30
+ ctx . fs . write ( 'package.json' , {
31
+ dependencies : { a : 'foo' } ,
32
+ } )
28
33
ctx . fs . write ( 'node_modules/a/index.js' , '' )
29
- ctx . fs . symlink ( ctx . fs . path ( 'node_modules/a/index.js' ) , 'node_modules/.bin/a' )
34
+ ctx . fs . write ( 'node_modules/a/package.json' , {
35
+ name : 'a' ,
36
+ bin : {
37
+ a : 'index.js' ,
38
+ } ,
39
+ } )
40
+ if ( isWindows ) {
41
+ ctx . fs . write ( 'node_modules/.bin/a' , '' )
42
+ } else {
43
+ ctx . fs . symlink ( ctx . fs . path ( 'node_modules/a/index.js' ) , 'node_modules/.bin/a' )
44
+ }
30
45
}
31
46
32
47
beforeEach ( ( ) => {
33
- ctx . fs . dir ( 'some/other/bin/a' )
48
+ ctx . fs . write ( 'some/other/bin/a' , ' ')
34
49
ctx . fs . dir ( 'node_modules/.bin' )
35
50
} )
36
51
@@ -105,28 +120,46 @@ describe('local available detection', () => {
105
120
runningLocalTool : false ,
106
121
} )
107
122
} )
108
- it ( 'if just node_module/dir missing, discounts being available' , ( ) => {
109
- ctx . fs . remove ( 'node_modules/a' )
110
- expect ( ctx . detectExecLayout ( ) ) . toMatchObject ( {
111
- nodeProject : true ,
112
- toolProject : true ,
113
- toolCurrentlyPresentInNodeModules : false ,
114
- runningLocalTool : false ,
115
- } )
116
- } )
123
+ // todo | Test disabled becuase on posix the symlink in node_modules/.bin
124
+ // todo | dir is deleted when the node_modules package it links to is deleted
125
+ // todo | meaning that the test is testing nothing.
126
+ // todo | Bring back just for windows?
127
+ // it('if just node_module/dir missing, discounts being available', () => {
128
+ // ctx.fs.remove('node_modules/a')
129
+ // expect(ctx.detectExecLayout()).toMatchObject({
130
+ // nodeProject: true,
131
+ // toolProject: true,
132
+ // toolCurrentlyPresentInNodeModules: false,
133
+ // runningLocalTool: false,
134
+ // })
135
+ // })
117
136
} )
118
137
119
138
describe ( 'running locally detection' , ( ) => {
120
139
beforeEach ( installTool )
121
- it ( 'if process script path matches path to tool in project bin then considered running locally' , ( ) => {
122
- expect ( ctx . detectExecLayout ( { scriptPath : ctx . fs . path ( 'node_modules/.bin/a' ) } ) ) . toMatchObject ( {
140
+
141
+ describe ( 'if process tool path matches project tool path in dot-bin then considered running locally' , ( ) => {
142
+ const runningLocalToolResult = {
123
143
nodeProject : true ,
124
144
toolProject : true ,
125
145
toolCurrentlyPresentInNodeModules : true ,
126
146
runningLocalTool : true ,
147
+ }
148
+ it ( 'process tool path as direct to script' , ( ) => {
149
+ expect ( ctx . detectExecLayout ( { scriptPath : ctx . fs . path ( 'node_modules/a/index.js' ) } ) ) . toMatchObject (
150
+ runningLocalToolResult
151
+ )
127
152
} )
153
+ if ( ! isWindows ) {
154
+ it ( 'on posix: process tool path as dot-bin path (b/c argv[0] symlink not followed in some cases)' , ( ) => {
155
+ expect ( ctx . detectExecLayout ( { scriptPath : ctx . fs . path ( 'node_modules/.bin/a' ) } ) ) . toMatchObject (
156
+ runningLocalToolResult
157
+ )
158
+ } )
159
+ }
128
160
} )
129
- it ( 'if process script path does not match path to tool in project bin then not considered running locally' , ( ) => {
161
+
162
+ it ( 'if process tool path does not match project tool path in dot-bin then not considered running locally' , ( ) => {
130
163
expect ( ctx . detectExecLayout ( { scriptPath : ctx . fs . path ( 'some/other/bin/a' ) } ) ) . toMatchObject ( {
131
164
nodeProject : true ,
132
165
toolProject : true ,
@@ -136,34 +169,28 @@ describe('running locally detection', () => {
136
169
} )
137
170
} )
138
171
139
- describe ( 'this process analysis ' , ( ) => {
172
+ describe ( 'analysis about " this process" ' , ( ) => {
140
173
beforeEach ( ( ) => {
141
174
ctx . fs . write ( 'a/b/c/real.js' , '' )
142
- ctx . fs . symlink ( ctx . fs . path ( 'a/b/c/real.js' ) , 'x/y/z/fake' )
175
+ if ( isWindows ) {
176
+ ctx . fs . write ( 'x/y/z/fake' , '' )
177
+ } else {
178
+ ctx . fs . symlink ( ctx . fs . path ( 'a/b/c/real.js' ) , 'x/y/z/fake' )
179
+ }
143
180
} )
144
181
it ( 'finds the real path of the script node executed' , ( ) => {
145
182
const data = ctx . detectExecLayout ( { scriptPath : ctx . fs . path ( 'x/y/z/fake' ) } )
146
- expect ( data . thisProcessToolBin ) . toMatchInlineSnapshot ( `
147
- Object {
148
- "dir": "/__dynamic__/x/y/z",
149
- "name": "fake",
150
- "path": "/__dynamic__/x/y/z/fake",
151
- "realDir": "/__dynamic__/a/b/c",
152
- "realPath": "/__dynamic__/a/b/c/real.js",
153
- }
154
- ` )
183
+
184
+ if ( isWindows ) {
185
+ expect ( data . process . toolPath ) . toMatchInlineSnapshot ( `"/__dynamic__/x/y/z/fake"` )
186
+ } else {
187
+ expect ( data . process . toolPath ) . toMatchInlineSnapshot ( `"/__dynamic__/a/b/c/real.js"` )
188
+ }
155
189
} )
190
+
156
191
it ( 'supports node running script without extension' , ( ) => {
157
192
const data = ctx . detectExecLayout ( { scriptPath : ctx . fs . path ( 'a/b/c/real' ) } )
158
- expect ( data . thisProcessToolBin ) . toMatchInlineSnapshot ( `
159
- Object {
160
- "dir": "/__dynamic__/a/b/c",
161
- "name": "real.js",
162
- "path": "/__dynamic__/a/b/c/real.js",
163
- "realDir": "/__dynamic__/a/b/c",
164
- "realPath": "/__dynamic__/a/b/c/real.js",
165
- }
166
- ` )
193
+ expect ( data . process . toolPath ) . toMatchInlineSnapshot ( `"/__dynamic__/a/b/c/real.js"` )
167
194
} )
168
195
} )
169
196
@@ -177,13 +204,20 @@ describe('project analysis', () => {
177
204
nodeProject ( )
178
205
const data = ctx . detectExecLayout ( )
179
206
expect ( data . nodeProject ) . toEqual ( true )
207
+ // expect(data.project).toMatchInlineSnapshot(`
208
+ // Object {
209
+ // "binDir": "/__dynamic__/node_modules/.bin",
210
+ // "dir": "/__dynamic__",
211
+ // "nodeModulesDir": "/__dynamic__/node_modules",
212
+ // "toolBinPath": "/__dynamic__/node_modules/.bin/a",
213
+ // "toolBinRealPath": null,
214
+ // }
215
+ // `)
180
216
expect ( data . project ) . toMatchInlineSnapshot ( `
181
217
Object {
182
- "binDir": "/__dynamic__/node_modules/.bin",
183
218
"dir": "/__dynamic__",
184
219
"nodeModulesDir": "/__dynamic__/node_modules",
185
- "toolBinPath": "/__dynamic__/node_modules/.bin/a",
186
- "toolBinRealPath": null,
220
+ "toolPath": null,
187
221
}
188
222
` )
189
223
} )
@@ -192,13 +226,11 @@ describe('project analysis', () => {
192
226
const data = ctx . detectExecLayout ( )
193
227
expect ( data . toolProject ) . toEqual ( true )
194
228
expect ( data . project ) . toMatchInlineSnapshot ( `
195
- Object {
196
- "binDir": "/__dynamic__/node_modules/.bin",
197
- "dir": "/__dynamic__",
198
- "nodeModulesDir": "/__dynamic__/node_modules",
199
- "toolBinPath": "/__dynamic__/node_modules/.bin/a",
200
- "toolBinRealPath": "/__dynamic__/node_modules/a/index.js",
201
- }
202
- ` )
229
+ Object {
230
+ "dir": "/__dynamic__",
231
+ "nodeModulesDir": "/__dynamic__/node_modules",
232
+ "toolPath": "/__dynamic__/node_modules/a/index.js",
233
+ }
234
+ ` )
203
235
} )
204
236
} )
0 commit comments