Skip to content

Commit

Permalink
Added additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxxBot committed Feb 19, 2025
1 parent d5f8ced commit 269a3f9
Show file tree
Hide file tree
Showing 10 changed files with 1,891 additions and 2 deletions.
137 changes: 135 additions & 2 deletions npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-client-go/http/httpclient"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -669,7 +670,7 @@ func TestYarn(t *testing.T) {
testDataTarget := filepath.Join(tempDirPath, tests.Out, "yarn")
assert.NoError(t, biutils.CopyDir(testDataSource, testDataTarget, true, nil))

yarnProjectPath := filepath.Join(testDataTarget, "yarnproject")
yarnProjectPath := filepath.Join(testDataTarget, "yarnprojectV2")
assert.NoError(t, createConfigFileForTest([]string{yarnProjectPath}, tests.NpmRemoteRepo, "", t, project.Yarn, false))

wd, err := os.Getwd()
Expand Down Expand Up @@ -714,6 +715,46 @@ func TestYarn(t *testing.T) {
inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, tests.YarnBuildName, artHttpDetails)
}

func TestYarnSetVersion(t *testing.T) {
initNpmTest(t)
defer cleanNpmTest(t)

// Temporarily change the cache folder to a temporary folder - to make sure the cache is clean and dependencies will be downloaded from Artifactory
tempDirPath, createTempDirCallback := coretests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()

testDataSource := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "yarn")
testDataTarget := filepath.Join(tempDirPath, tests.Out, "yarn")
assert.NoError(t, biutils.CopyDir(testDataSource, testDataTarget, true, nil))

yarnProjectPath := filepath.Join(testDataTarget, "yarnprojectV2")
assert.NoError(t, createConfigFileForTest([]string{yarnProjectPath}, tests.NpmRemoteRepo, "", t, project.Yarn, false))

wd, err := os.Getwd()
assert.NoError(t, err, "Failed to get current dir")
chdirCallback := clientTestUtils.ChangeDirWithCallback(t, wd, yarnProjectPath)
defer chdirCallback()
cleanUpYarnGlobalFolder := clientTestUtils.SetEnvWithCallbackAndAssert(t, "YARN_GLOBAL_FOLDER", tempDirPath)
defer cleanUpYarnGlobalFolder()

// Add "localhost" to http whitelist
yarnExecPath, err := exec.LookPath("yarn")
assert.NoError(t, err)
// Get original http white list config
origWhitelist, err := yarn.ConfigGet("unsafeHttpWhitelist", yarnExecPath, true)
assert.NoError(t, err)
assert.NoError(t, yarn.ConfigSet("unsafeHttpWhitelist", "[\"localhost\"]", yarnExecPath, true))
defer func() {
// Restore original whitelist config
assert.NoError(t, yarn.ConfigSet("unsafeHttpWhitelist", origWhitelist, yarnExecPath, true))
}()

jfrogCli := coretests.NewJfrogCli(execMain, "jfrog", "")
err = jfrogCli.Exec("yarn", "set", "version", "3.2.1")
assert.NoError(t, err)
modifyExistingYarnRc(t, "3.2.1")
}

func TestYarnUpgradeToV4(t *testing.T) {
initNpmTest(t)
defer cleanNpmTest(t)
Expand All @@ -726,7 +767,7 @@ func TestYarnUpgradeToV4(t *testing.T) {
testDataTarget := filepath.Join(tempDirPath, tests.Out, "yarn")
assert.NoError(t, biutils.CopyDir(testDataSource, testDataTarget, true, nil))

yarnProjectPath := filepath.Join(testDataTarget, "yarnproject")
yarnProjectPath := filepath.Join(testDataTarget, "yarnprojectV2")
assert.NoError(t, createConfigFileForTest([]string{yarnProjectPath}, tests.NpmRemoteRepo, "", t, project.Yarn, false))

wd, err := os.Getwd()
Expand All @@ -753,6 +794,88 @@ func TestYarnUpgradeToV4(t *testing.T) {
assert.Error(t, err)
}

func TestYarnInV4(t *testing.T) {
initNpmTest(t)
defer cleanNpmTest(t)

// Temporarily change the cache folder to a temporary folder - to make sure the cache is clean and dependencies will be downloaded from Artifactory
tempDirPath, createTempDirCallback := coretests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()

testDataSource := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "yarn")
testDataTarget := filepath.Join(tempDirPath, tests.Out, "yarn")
assert.NoError(t, biutils.CopyDir(testDataSource, testDataTarget, true, nil))

yarnProjectPath := filepath.Join(testDataTarget, "yarnprojectV4")
assert.NoError(t, createConfigFileForTest([]string{yarnProjectPath}, tests.NpmRemoteRepo, "", t, project.Yarn, false))

wd, err := os.Getwd()
assert.NoError(t, err, "Failed to get current dir")
chdirCallback := clientTestUtils.ChangeDirWithCallback(t, wd, yarnProjectPath)
defer chdirCallback()
cleanUpYarnGlobalFolder := clientTestUtils.SetEnvWithCallbackAndAssert(t, "YARN_GLOBAL_FOLDER", tempDirPath)
defer cleanUpYarnGlobalFolder()

// Add "localhost" to http whitelist
yarnExecPath, err := exec.LookPath("yarn")
assert.NoError(t, err)
// Get original http white list config
origWhitelist, err := yarn.ConfigGet("unsafeHttpWhitelist", yarnExecPath, true)
if err != nil {
log.Error(err)
}
assert.NoError(t, err)
assert.NoError(t, yarn.ConfigSet("unsafeHttpWhitelist", "[\"localhost\"]", yarnExecPath, true))
defer func() {
// Restore original whitelist config
assert.NoError(t, yarn.ConfigSet("unsafeHttpWhitelist", origWhitelist, yarnExecPath, true))
}()

jfrogCli := coretests.NewJfrogCli(execMain, "jfrog", "")
err = jfrogCli.Exec("yarn", "install")
assert.Error(t, err)
}

func TestYarnChangeVersionInV4(t *testing.T) {
initNpmTest(t)
defer cleanNpmTest(t)

// Temporarily change the cache folder to a temporary folder - to make sure the cache is clean and dependencies will be downloaded from Artifactory
tempDirPath, createTempDirCallback := coretests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()

testDataSource := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "yarn")
testDataTarget := filepath.Join(tempDirPath, tests.Out, "yarn")
assert.NoError(t, biutils.CopyDir(testDataSource, testDataTarget, true, nil))

yarnProjectPath := filepath.Join(testDataTarget, "yarnprojectV4")
assert.NoError(t, createConfigFileForTest([]string{yarnProjectPath}, tests.NpmRemoteRepo, "", t, project.Yarn, false))

wd, err := os.Getwd()
assert.NoError(t, err, "Failed to get current dir")
chdirCallback := clientTestUtils.ChangeDirWithCallback(t, wd, yarnProjectPath)
defer chdirCallback()
cleanUpYarnGlobalFolder := clientTestUtils.SetEnvWithCallbackAndAssert(t, "YARN_GLOBAL_FOLDER", tempDirPath)
defer cleanUpYarnGlobalFolder()

// Add "localhost" to http whitelist
yarnExecPath, err := exec.LookPath("yarn")
assert.NoError(t, err)
// Get original http white list config
origWhitelist, err := yarn.ConfigGet("unsafeHttpWhitelist", yarnExecPath, true)
assert.NoError(t, err)
assert.NoError(t, yarn.ConfigSet("unsafeHttpWhitelist", "[\"localhost\"]", yarnExecPath, true))
defer func() {
// Restore original whitelist config
assert.NoError(t, yarn.ConfigSet("unsafeHttpWhitelist", origWhitelist, yarnExecPath, true))
}()

jfrogCli := coretests.NewJfrogCli(execMain, "jfrog", "")
err = jfrogCli.Exec("yarn", "set", "version", "3.2.1")
assert.NoError(t, err)
modifyExistingYarnRc(t, "3.2.1")
}

// Checks if the expected dependencies match the actual dependencies. Only the dependencies' IDs and scopes (not more than one scope) are compared.
func equalDependenciesSlices(t *testing.T, expectedDependencies []expectedDependency, actualDependencies []buildinfo.Dependency) {
assert.Equal(t, len(expectedDependencies), len(actualDependencies))
Expand All @@ -772,6 +895,16 @@ func equalDependenciesSlices(t *testing.T, expectedDependencies []expectedDepend
}
}

func modifyExistingYarnRc(t *testing.T, version string) {
yarnConfig := make(map[string]any)
yarnRcPath := filepath.Join(".yarnrc.yml")

Check failure on line 900 in npm_test.go

View workflow job for this annotation

GitHub Actions / Static Check ubuntu-latest

badCall: suspicious Join on 1 argument (gocritic)
yarnConfig["yarnPath"] = ".yarn/releases/yarn-" + version + ".cjs"
updatedYamlData, err := yaml.Marshal(&yarnConfig)
assert.NoError(t, err)
err = os.WriteFile(yarnRcPath, updatedYamlData, 0644)
assert.NoError(t, err)
}

func isNpm7(npmVersion *version.Version) bool {
return npmVersion.Compare("7.0.0") <= 0
}
Expand Down
File renamed without changes.
File renamed without changes.
786 changes: 786 additions & 0 deletions testdata/yarn/yarnprojectV3/.yarn/releases/yarn-3.2.1.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions testdata/yarn/yarnprojectV3/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: .yarn/releases/yarn-3.2.1.cjs
17 changes: 17 additions & 0 deletions testdata/yarn/yarnprojectV3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "jfrog-cli-tests",
"version": "v1.0.0",
"description": "test package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"xml": "1.0.1"
},
"devDependencies": {
"json": "9.0.6"
}
}
Loading

0 comments on commit 269a3f9

Please sign in to comment.