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