6
6
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
7
7
"github.com/jfrog/jfrog-client-go/http/httpclient"
8
8
"github.com/stretchr/testify/require"
9
+ "gopkg.in/yaml.v2"
9
10
"net/http"
10
11
"os"
11
12
"os/exec"
@@ -669,7 +670,7 @@ func TestYarn(t *testing.T) {
669
670
testDataTarget := filepath .Join (tempDirPath , tests .Out , "yarn" )
670
671
assert .NoError (t , biutils .CopyDir (testDataSource , testDataTarget , true , nil ))
671
672
672
- yarnProjectPath := filepath .Join (testDataTarget , "yarnproject " )
673
+ yarnProjectPath := filepath .Join (testDataTarget , "yarnprojectV2 " )
673
674
assert .NoError (t , createConfigFileForTest ([]string {yarnProjectPath }, tests .NpmRemoteRepo , "" , t , project .Yarn , false ))
674
675
675
676
wd , err := os .Getwd ()
@@ -714,6 +715,169 @@ func TestYarn(t *testing.T) {
714
715
inttestutils .DeleteBuild (serverDetails .ArtifactoryUrl , tests .YarnBuildName , artHttpDetails )
715
716
}
716
717
718
+ func TestYarnSetVersion (t * testing.T ) {
719
+ initNpmTest (t )
720
+ defer cleanNpmTest (t )
721
+
722
+ // Temporarily change the cache folder to a temporary folder - to make sure the cache is clean and dependencies will be downloaded from Artifactory
723
+ tempDirPath , createTempDirCallback := coretests .CreateTempDirWithCallbackAndAssert (t )
724
+ defer createTempDirCallback ()
725
+
726
+ testDataSource := filepath .Join (filepath .FromSlash (tests .GetTestResourcesPath ()), "yarn" )
727
+ testDataTarget := filepath .Join (tempDirPath , tests .Out , "yarn" )
728
+ assert .NoError (t , biutils .CopyDir (testDataSource , testDataTarget , true , nil ))
729
+
730
+ yarnProjectPath := filepath .Join (testDataTarget , "yarnprojectV2" )
731
+ assert .NoError (t , createConfigFileForTest ([]string {yarnProjectPath }, tests .NpmRemoteRepo , "" , t , project .Yarn , false ))
732
+
733
+ wd , err := os .Getwd ()
734
+ assert .NoError (t , err , "Failed to get current dir" )
735
+ chdirCallback := clientTestUtils .ChangeDirWithCallback (t , wd , yarnProjectPath )
736
+ defer chdirCallback ()
737
+ cleanUpYarnGlobalFolder := clientTestUtils .SetEnvWithCallbackAndAssert (t , "YARN_GLOBAL_FOLDER" , tempDirPath )
738
+ defer cleanUpYarnGlobalFolder ()
739
+
740
+ // Add "localhost" to http whitelist
741
+ yarnExecPath , err := exec .LookPath ("yarn" )
742
+ assert .NoError (t , err )
743
+ // Get original http white list config
744
+ origWhitelist , err := yarn .ConfigGet ("unsafeHttpWhitelist" , yarnExecPath , true )
745
+ assert .NoError (t , err )
746
+ assert .NoError (t , yarn .ConfigSet ("unsafeHttpWhitelist" , "[\" localhost\" ]" , yarnExecPath , true ))
747
+ defer func () {
748
+ // Restore original whitelist config
749
+ assert .NoError (t , yarn .ConfigSet ("unsafeHttpWhitelist" , origWhitelist , yarnExecPath , true ))
750
+ }()
751
+
752
+ jfrogCli := coretests .NewJfrogCli (execMain , "jfrog" , "" )
753
+ err = jfrogCli .Exec ("yarn" , "set" , "version" , "3.2.1" )
754
+ assert .NoError (t , err )
755
+ modifyExistingYarnRc (t , "3.2.1" )
756
+ }
757
+
758
+ func TestYarnUpgradeToV4 (t * testing.T ) {
759
+ initNpmTest (t )
760
+ defer cleanNpmTest (t )
761
+
762
+ // Temporarily change the cache folder to a temporary folder - to make sure the cache is clean and dependencies will be downloaded from Artifactory
763
+ tempDirPath , createTempDirCallback := coretests .CreateTempDirWithCallbackAndAssert (t )
764
+ defer createTempDirCallback ()
765
+
766
+ testDataSource := filepath .Join (filepath .FromSlash (tests .GetTestResourcesPath ()), "yarn" )
767
+ testDataTarget := filepath .Join (tempDirPath , tests .Out , "yarn" )
768
+ assert .NoError (t , biutils .CopyDir (testDataSource , testDataTarget , true , nil ))
769
+
770
+ yarnProjectPath := filepath .Join (testDataTarget , "yarnprojectV2" )
771
+ assert .NoError (t , createConfigFileForTest ([]string {yarnProjectPath }, tests .NpmRemoteRepo , "" , t , project .Yarn , false ))
772
+
773
+ wd , err := os .Getwd ()
774
+ assert .NoError (t , err , "Failed to get current dir" )
775
+ chdirCallback := clientTestUtils .ChangeDirWithCallback (t , wd , yarnProjectPath )
776
+ defer chdirCallback ()
777
+ cleanUpYarnGlobalFolder := clientTestUtils .SetEnvWithCallbackAndAssert (t , "YARN_GLOBAL_FOLDER" , tempDirPath )
778
+ defer cleanUpYarnGlobalFolder ()
779
+
780
+ // Add "localhost" to http whitelist
781
+ yarnExecPath , err := exec .LookPath ("yarn" )
782
+ assert .NoError (t , err )
783
+ // Get original http white list config
784
+ origWhitelist , err := yarn .ConfigGet ("unsafeHttpWhitelist" , yarnExecPath , true )
785
+ assert .NoError (t , err )
786
+ assert .NoError (t , yarn .ConfigSet ("unsafeHttpWhitelist" , "[\" localhost\" ]" , yarnExecPath , true ))
787
+ defer func () {
788
+ // Restore original whitelist config
789
+ assert .NoError (t , yarn .ConfigSet ("unsafeHttpWhitelist" , origWhitelist , yarnExecPath , true ))
790
+ }()
791
+
792
+ jfrogCli := coretests .NewJfrogCli (execMain , "jfrog" , "" )
793
+ err = jfrogCli .Exec ("yarn" , "set" , "version" , "4.0.1" )
794
+ assert .Error (t , err )
795
+ }
796
+
797
+ func TestYarnInV4 (t * testing.T ) {
798
+ initNpmTest (t )
799
+ defer cleanNpmTest (t )
800
+
801
+ // Temporarily change the cache folder to a temporary folder - to make sure the cache is clean and dependencies will be downloaded from Artifactory
802
+ tempDirPath , createTempDirCallback := coretests .CreateTempDirWithCallbackAndAssert (t )
803
+ defer createTempDirCallback ()
804
+
805
+ testDataSource := filepath .Join (filepath .FromSlash (tests .GetTestResourcesPath ()), "yarn" )
806
+ testDataTarget := filepath .Join (tempDirPath , tests .Out , "yarn" )
807
+ assert .NoError (t , biutils .CopyDir (testDataSource , testDataTarget , true , nil ))
808
+
809
+ wd , err := os .Getwd ()
810
+ assert .NoError (t , err , "Failed to get current dir" )
811
+
812
+ yarnProjectPath := filepath .Join (testDataTarget , "yarnprojectV4" )
813
+ assert .NoError (t , createConfigFileForTest ([]string {yarnProjectPath }, tests .NpmRemoteRepo , "" , t , project .Yarn , false ))
814
+ chdirCallback := clientTestUtils .ChangeDirWithCallback (t , wd , yarnProjectPath )
815
+ defer chdirCallback ()
816
+ cleanUpYarnGlobalFolder := clientTestUtils .SetEnvWithCallbackAndAssert (t , "YARN_GLOBAL_FOLDER" , tempDirPath )
817
+ defer cleanUpYarnGlobalFolder ()
818
+
819
+ jfrogCli := coretests .NewJfrogCli (execMain , "jfrog" , "" )
820
+ err = jfrogCli .Exec ("yarn" , "install" )
821
+ assert .Error (t , err )
822
+ }
823
+
824
+ func TestYarnChangeVersionInV4 (t * testing.T ) {
825
+ initNpmTest (t )
826
+ defer cleanNpmTest (t )
827
+
828
+ // Temporarily change the cache folder to a temporary folder - to make sure the cache is clean and dependencies will be downloaded from Artifactory
829
+ tempDirPath , createTempDirCallback := coretests .CreateTempDirWithCallbackAndAssert (t )
830
+ defer createTempDirCallback ()
831
+
832
+ testDataSource := filepath .Join (filepath .FromSlash (tests .GetTestResourcesPath ()), "yarn" )
833
+ testDataTarget := filepath .Join (tempDirPath , tests .Out , "yarn" )
834
+ assert .NoError (t , biutils .CopyDir (testDataSource , testDataTarget , true , nil ))
835
+
836
+ yarnProjectPath := filepath .Join (testDataTarget , "yarnprojectV4" )
837
+ assert .NoError (t , createConfigFileForTest ([]string {yarnProjectPath }, tests .NpmRemoteRepo , "" , t , project .Yarn , false ))
838
+
839
+ wd , err := os .Getwd ()
840
+ assert .NoError (t , err , "Failed to get current dir" )
841
+ chdirCallback := clientTestUtils .ChangeDirWithCallback (t , wd , yarnProjectPath )
842
+ defer chdirCallback ()
843
+ cleanUpYarnGlobalFolder := clientTestUtils .SetEnvWithCallbackAndAssert (t , "YARN_GLOBAL_FOLDER" , tempDirPath )
844
+ defer cleanUpYarnGlobalFolder ()
845
+
846
+ // Add "localhost" to http whitelist
847
+ yarnExecPath , err := exec .LookPath ("yarn" )
848
+ assert .NoError (t , err )
849
+
850
+ yarnrcPath := ".yarnrc.yml"
851
+ data , err := os .ReadFile (yarnrcPath )
852
+ assert .NoError (t , err )
853
+ // Parse YAML
854
+ var config = make (map [string ]any )
855
+ err = yaml .Unmarshal (data , & config )
856
+ if err != nil {
857
+ assert .NoError (t , err )
858
+ }
859
+ config ["unsafeHttpWhitelist" ] = []string {"localhost" }
860
+ updatedYamlData , err := yaml .Marshal (& config )
861
+ assert .NoError (t , err )
862
+ err = os .WriteFile (yarnrcPath , updatedYamlData , 0644 )
863
+ assert .NoError (t , err )
864
+
865
+ assert .NoError (t , err )
866
+ defer func () {
867
+ // Restore original whitelist config
868
+ assert .NoError (t , yarn .ConfigSet ("unsafeHttpWhitelist" , "[]" , yarnExecPath , true ))
869
+ }()
870
+
871
+ jfrogCli := coretests .NewJfrogCli (execMain , "jfrog" , "" )
872
+
873
+ err = jfrogCli .Exec ("yarn" , "set" , "version" , "3.2.1" )
874
+ assert .NoError (t , err )
875
+ modifyExistingYarnRc (t , "3.2.1" )
876
+
877
+ err = jfrogCli .Exec ("yarn" , "--version" )
878
+ assert .NoError (t , err )
879
+ }
880
+
717
881
// Checks if the expected dependencies match the actual dependencies. Only the dependencies' IDs and scopes (not more than one scope) are compared.
718
882
func equalDependenciesSlices (t * testing.T , expectedDependencies []expectedDependency , actualDependencies []buildinfo.Dependency ) {
719
883
assert .Equal (t , len (expectedDependencies ), len (actualDependencies ))
@@ -733,6 +897,16 @@ func equalDependenciesSlices(t *testing.T, expectedDependencies []expectedDepend
733
897
}
734
898
}
735
899
900
+ func modifyExistingYarnRc (t * testing.T , version string ) {
901
+ yarnConfig := make (map [string ]any )
902
+ yarnRcPath := ".yarnrc.yml"
903
+ yarnConfig ["yarnPath" ] = ".yarn/releases/yarn-" + version + ".cjs"
904
+ updatedYamlData , err := yaml .Marshal (& yarnConfig )
905
+ assert .NoError (t , err )
906
+ err = os .WriteFile (yarnRcPath , updatedYamlData , 0644 )
907
+ assert .NoError (t , err )
908
+ }
909
+
736
910
func isNpm7 (npmVersion * version.Version ) bool {
737
911
return npmVersion .Compare ("7.0.0" ) <= 0
738
912
}
0 commit comments