@@ -15,6 +15,7 @@ let fs = require("fs"),
1515const originalExecSync = childProcess . execSync ;
1616const originalConsoleError = console . error ;
1717const originalExistsSync = fs . existsSync ;
18+ const originalReadFileSync = fs . readFileSync ;
1819
1920describe ( "getPath" , ( ) => {
2021
@@ -23,6 +24,7 @@ describe("getPath", () => {
2324 childProcess . execSync = originalExecSync ;
2425 console . error = originalConsoleError ;
2526 fs . existsSync = originalExistsSync ;
27+ fs . readFileSync = originalReadFileSync ;
2628 } ) ;
2729
2830 // platform independant tests. Execute them by simulating all supported platforms, but results should be the same.
@@ -173,13 +175,77 @@ describe("getPath", () => {
173175 assert . deepEqual ( result , path . join ( executableDirName , "node_modules" , packageName ) ) ;
174176 } ) ;
175177
178+ it ( "returns correct result when where result is correct, and package is added to PATH via its bin dir" , ( ) => {
179+ const packageName = "test1" ,
180+ executableName = "test1.js" ,
181+ executableDirName = path . join ( "C:" , "Users" , "username" , "nativescript-cli" , "bin" ) ,
182+ whereResult = path . join ( executableDirName , executableName ) ;
183+
184+ fs . existsSync = ( filePath ) => {
185+ return filePath . indexOf ( "package.json" ) !== - 1 ;
186+ } ;
187+
188+ fs . readFileSync = ( filePath ) => {
189+ if ( filePath . indexOf ( "package.json" ) !== - 1 ) {
190+ return JSON . stringify ( {
191+ "name" : packageName
192+ } ) ;
193+ }
194+
195+ return "" ;
196+ } ;
197+
198+ childProcess . execSync = ( command ) => {
199+ if ( command . indexOf ( "where" ) !== - 1 ) {
200+ return whereResult ;
201+ }
202+
203+ return null ;
204+ } ;
205+
206+ const result = index . getPath ( packageName , executableName ) ;
207+ assert . deepEqual ( result , path . join ( path . dirname ( executableDirName ) ) ) ;
208+ } ) ;
209+
210+ it ( "returns null when package is added to PATH via its bin dir, but the name in package.json is incorrect" , ( ) => {
211+ const packageName = "test1" ,
212+ executableName = "test1.js" ,
213+ executableDirName = path . join ( "C:" , "Users" , "username" , "nativescript-cli" , "bin" ) ,
214+ whereResult = path . join ( executableDirName , executableName ) ;
215+
216+ fs . existsSync = ( filePath ) => {
217+ return filePath . indexOf ( "package.json" ) !== - 1 ;
218+ } ;
219+
220+ fs . readFileSync = ( filePath ) => {
221+ if ( filePath . indexOf ( "package.json" ) !== - 1 ) {
222+ return JSON . stringify ( {
223+ "name" : "invalidName"
224+ } ) ;
225+ }
226+
227+ return "" ;
228+ } ;
229+
230+ childProcess . execSync = ( command ) => {
231+ if ( command . indexOf ( "where" ) !== - 1 ) {
232+ return whereResult ;
233+ }
234+
235+ return null ;
236+ } ;
237+
238+ const result = index . getPath ( packageName , executableName ) ;
239+ assert . deepEqual ( result , null ) ;
240+ } ) ;
241+
176242 it ( "returns correct result when where result returns multiple lines correct" , ( ) => {
177243 const packageName = "test1" ,
178244 executableName = "test1.js" ,
179245 executableDirName = path . join ( "C:" , "Users" , "username" , "AppData" , "Roaming" , "npm" ) ,
180246 invalidName = "invalidName" ,
181- invalidLineOfWhereResult = path . join ( executableDirName , invalidName , executableName ) ,
182- whereResult = invalidLineOfWhereResult + "\n" + invalidLineOfWhereResult + "\r\n" + path . join ( executableDirName , executableName ) ;
247+ invalidLineOfWhereResult = path . join ( executableDirName , invalidName , invalidName , executableName ) ,
248+ whereResult = invalidLineOfWhereResult + "\n" + invalidLineOfWhereResult + "\r\n" + path . join ( executableDirName , executableName ) ;
183249
184250 fs . existsSync = ( filePath ) => {
185251 if ( filePath && filePath . indexOf ( invalidName ) !== - 1 ) {
@@ -348,14 +414,90 @@ describe("getPath", () => {
348414 const result = index . getPath ( packageName , executableName ) ;
349415 assert . deepEqual ( result , null ) ;
350416 } ) ;
417+
418+ it ( "returns correct result when which result is correct, and package is added to PATH via its bin dir" , ( ) => {
419+ const packageName = "test1" ,
420+ executableName = "test1.js" ,
421+ executableDirName = path . join ( "/usr" , "username" , "repository_name" , "bin" ) ,
422+ whichResult = path . join ( executableDirName , executableName ) ,
423+ lsLResult = `lrwxrwxrwx 1 rvladimirov rvladimirov 52 Oct 20 14:51 ${ whichResult } -> incorrect` ;
424+
425+ fs . existsSync = ( filePath ) => {
426+ return filePath . indexOf ( "package.json" ) !== - 1 ;
427+ } ;
428+
429+ childProcess . execSync = ( command ) => {
430+
431+ if ( command . indexOf ( "ls -l" ) !== - 1 ) {
432+ return lsLResult ;
433+ }
434+
435+ if ( command . indexOf ( "which" ) !== - 1 ) {
436+ return whichResult ;
437+ }
438+
439+ return null ;
440+ } ;
441+
442+ fs . readFileSync = ( filePath ) => {
443+ if ( filePath . indexOf ( "package.json" ) !== - 1 ) {
444+ return JSON . stringify ( {
445+ "name" : packageName
446+ } ) ;
447+ }
448+
449+ return "" ;
450+ } ;
451+
452+ const result = index . getPath ( packageName , executableName ) ;
453+ assert . deepEqual ( result , path . dirname ( executableDirName ) ) ;
454+ } ) ;
455+
456+ it ( "returns null when package is added to PATH via its bin dir, but the name in package.json is incorrect" , ( ) => {
457+ const packageName = "test1" ,
458+ executableName = "test1.js" ,
459+ executableDirName = path . join ( "/usr" , "username" , "repository_name" , "bin" ) ,
460+ whichResult = path . join ( executableDirName , executableName ) ,
461+ lsLResult = `lrwxrwxrwx 1 rvladimirov rvladimirov 52 Oct 20 14:51 ${ whichResult } -> incorrect` ;
462+
463+ fs . existsSync = ( filePath ) => {
464+ return filePath . indexOf ( "package.json" ) !== - 1 ;
465+ } ;
466+
467+ childProcess . execSync = ( command ) => {
468+
469+ if ( command . indexOf ( "ls -l" ) !== - 1 ) {
470+ return lsLResult ;
471+ }
472+
473+ if ( command . indexOf ( "which" ) !== - 1 ) {
474+ return whichResult ;
475+ }
476+
477+ return null ;
478+ } ;
479+
480+ fs . readFileSync = ( filePath ) => {
481+ if ( filePath . indexOf ( "package.json" ) !== - 1 ) {
482+ return JSON . stringify ( {
483+ "name" : "invalid name"
484+ } ) ;
485+ }
486+
487+ return "" ;
488+ } ;
489+
490+ const result = index . getPath ( packageName , executableName ) ;
491+ assert . deepEqual ( result , null ) ;
492+ } ) ;
351493 }
352494 } ) ;
353495 } ) ;
354496 } ) ;
355497
356498 it ( "throws error when process.platform is not valid" , ( ) => {
357499 require ( "../lib/process-wrapper" ) . getProcessPlatform = ( ) => "1" ;
358- assert . throws ( ( ) => index . getPath ( "test1" , "test1" ) , "OS '1' is not supported" ) ;
500+ assert . throws ( ( ) => index . getPath ( "test1" , "test1" ) , "OS '1' is not supported" ) ;
359501 } ) ;
360502
361503} ) ;
0 commit comments