@@ -9,6 +9,14 @@ interface ITestData {
99}
1010
1111describe ( "helpers" , ( ) => {
12+ let originalProcessEnvNpmConfig : any = null ;
13+ beforeEach ( ( ) => {
14+ originalProcessEnvNpmConfig = process . env . npm_config_argv ;
15+ } ) ;
16+
17+ afterEach ( ( ) => {
18+ process . env . npm_config_argv = originalProcessEnvNpmConfig ;
19+ } ) ;
1220
1321 const assertTestData = ( testData : ITestData , method : Function ) => {
1422 const actualResult = method ( testData . input ) ;
@@ -699,4 +707,122 @@ describe("helpers", () => {
699707 } ) ;
700708 } ) ;
701709 } ) ;
710+
711+ const setNpmConfigArgv = ( original : string [ ] ) : void => {
712+ process . env . npm_config_argv = JSON . stringify ( { original } ) ;
713+ } ;
714+
715+ describe ( "doesCurrentNpmCommandMatch" , ( ) => {
716+ describe ( "when searching for global flag (--global or -g)" , ( ) => {
717+ [
718+ {
719+ name : "returns true when `--global` is passed on terminal" ,
720+ input : [ "install" , "--global" , "nativescript" ] ,
721+ expectedOutput : true
722+ } ,
723+ {
724+ name : "returns true when `-g` is passed on terminal" ,
725+ input : [ "install" , "-g" , "nativescript" ] ,
726+ expectedOutput : true
727+ } ,
728+ {
729+ name : "returns false neither -g/--global are passed on terminal" ,
730+ input : [ "install" , "nativescript" ] ,
731+ expectedOutput : false
732+ } ,
733+ {
734+ name : "returns false when neither -g/--global are passed on terminal, but similar flag is passed" ,
735+ input : [ "install" , "nativescript" , "--globalEnv" ] ,
736+ expectedOutput : false
737+ } ,
738+ {
739+ name : "returns false when neither -g/--global are passed on terminal, but trying to install global package" ,
740+ input : [ "install" , "global" ] ,
741+ expectedOutput : false
742+ }
743+ ] . forEach ( testCase => {
744+ it ( testCase . name , ( ) => {
745+ setNpmConfigArgv ( testCase . input ) ;
746+ const result = helpers . doesCurrentNpmCommandMatch ( [ / ^ - - g l o b a l $ / , / ^ - g $ / ] ) ;
747+ assert . equal ( result , testCase . expectedOutput ) ;
748+ } ) ;
749+ } ) ;
750+ } ) ;
751+ } ) ;
752+
753+ describe ( "isInstallingNativeScriptGlobally" , ( ) => {
754+ const installationFlags = [ "install" , "i" ] ;
755+ const globalFlags = [ "--global" , "-g" ] ;
756+ const validNativeScriptPackageNames = [ "nativescript" , "nativescript@1.0.1" , "nativescript@next" ] ;
757+
758+ it ( "returns true when installing nativescript globally with npm" , ( ) => {
759+ validNativeScriptPackageNames . forEach ( nativescript => {
760+ installationFlags . forEach ( install => {
761+ globalFlags . forEach ( globalFlag => {
762+ const npmArgs = [ install , nativescript , globalFlag ] ;
763+ setNpmConfigArgv ( npmArgs ) ;
764+ const result = helpers . isInstallingNativeScriptGlobally ( ) ;
765+ assert . isTrue ( result ) ;
766+ } ) ;
767+ } ) ;
768+ } ) ;
769+ } ) ;
770+
771+ it ( "returns true when installing nativescript globally with yarn" , ( ) => {
772+ validNativeScriptPackageNames . forEach ( nativescript => {
773+ const npmArgs = [ "global" , "add" , nativescript ] ;
774+ setNpmConfigArgv ( npmArgs ) ;
775+ const result = helpers . isInstallingNativeScriptGlobally ( ) ;
776+ assert . isTrue ( result ) ;
777+ } ) ;
778+ } ) ;
779+
780+ const invalidInstallationFlags = [ "installpackage" , "is" ] ;
781+ const invalidGlobalFlags = [ "--globalEnv" , "" ] ;
782+ const invalidNativeScriptPackageNames = [ "nativescript" , "nativescript-facebook" , "nativescript-facebook@1.0.1" , "kinvey-nativescript-plugin" ] ;
783+
784+ it ( `returns false when command does not install nativescript globally` , ( ) => {
785+ invalidInstallationFlags . forEach ( nativescript => {
786+ invalidGlobalFlags . forEach ( install => {
787+ invalidNativeScriptPackageNames . forEach ( globalFlag => {
788+ const npmArgs = [ install , nativescript , globalFlag ] ;
789+ setNpmConfigArgv ( npmArgs ) ;
790+ const result = helpers . isInstallingNativeScriptGlobally ( ) ;
791+ assert . isFalse ( result ) ;
792+ } ) ;
793+ } ) ;
794+ } ) ;
795+ } ) ;
796+ } ) ;
797+
798+ describe ( "getCurrentNpmCommandArgv" , ( ) => {
799+ it ( "returns the value of process.env.npm_config_argv.original" , ( ) => {
800+ const command = [ "install" , "nativescript" ] ;
801+ process . env . npm_config_argv = JSON . stringify ( { someOtherProp : 1 , original : command } ) ;
802+ const actualCommand = helpers . getCurrentNpmCommandArgv ( ) ;
803+ assert . deepEqual ( actualCommand , command ) ;
804+ } ) ;
805+
806+ describe ( "returns empty array" , ( ) => {
807+ const assertResultIsEmptyArray = ( ) => {
808+ const actualCommand = helpers . getCurrentNpmCommandArgv ( ) ;
809+ assert . deepEqual ( actualCommand , [ ] ) ;
810+ } ;
811+
812+ it ( "when npm_config_argv is not populated" , ( ) => {
813+ delete process . env . npm_config_argv ;
814+ assertResultIsEmptyArray ( ) ;
815+ } ) ;
816+
817+ it ( "when npm_config_argv is not a valid json" , ( ) => {
818+ process . env . npm_config_argv = "invalid datas" ;
819+ assertResultIsEmptyArray ( ) ;
820+ } ) ;
821+
822+ it ( "when npm_config_argv.original is null" , ( ) => {
823+ process . env . npm_config_argv = JSON . stringify ( { original : null } ) ;
824+ assertResultIsEmptyArray ( ) ;
825+ } ) ;
826+ } ) ;
827+ } ) ;
702828} ) ;
0 commit comments