From 2f0e0c96b8922e14019032ed2352861bc88535d5 Mon Sep 17 00:00:00 2001 From: Eyal Delarea Date: Tue, 12 Mar 2024 17:46:27 +0200 Subject: [PATCH] Add npm publish with workspaces test (#2471) --- go.mod | 2 +- go.sum | 4 +- npm_test.go | 78 +++++++++++++++++++ .../npm/npmworkspaces/nested1/package.json | 15 ++++ .../npm/npmworkspaces/nested2/package.json | 15 ++++ testdata/npm/npmworkspaces/package.json | 21 +++++ 6 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 testdata/npm/npmworkspaces/nested1/package.json create mode 100644 testdata/npm/npmworkspaces/nested2/package.json create mode 100755 testdata/npm/npmworkspaces/package.json diff --git a/go.mod b/go.mod index 2e13062b1..940abb3cf 100644 --- a/go.mod +++ b/go.mod @@ -130,7 +130,7 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240305100201-685c9c30adde +replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240312151007-5979ebb571c7 replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240228121257-3414cc0ffcb6 diff --git a/go.sum b/go.sum index 00df20d8f..b08e4a0c6 100644 --- a/go.sum +++ b/go.sum @@ -127,8 +127,8 @@ github.com/jfrog/gofrog v1.6.3 h1:F7He0+75HcgCe6SGTSHLFCBDxiE2Ja0tekvvcktW6wc= github.com/jfrog/gofrog v1.6.3/go.mod h1:SZ1EPJUruxrVGndOzHd+LTiwWYKMlHqhKD+eu+v5Hqg= github.com/jfrog/jfrog-apps-config v1.0.1 h1:mtv6k7g8A8BVhlHGlSveapqf4mJfonwvXYLipdsOFMY= github.com/jfrog/jfrog-apps-config v1.0.1/go.mod h1:8AIIr1oY9JuH5dylz2S6f8Ym2MaadPLR6noCBO4C22w= -github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240305100201-685c9c30adde h1:e1TWEAPAJZ+HFBxQ2UoJ0aKfj9xJ2XgkFjygsCkMHbg= -github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240305100201-685c9c30adde/go.mod h1:lgwEgVIfazK/BjJarpaS5Mdpw86DWeDCSNDCwJqH8kg= +github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240312151007-5979ebb571c7 h1:bqbrSRA2tONZqm1Y+tuuiCJaYIc9G8RZQHuWjXh9xA0= +github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240312151007-5979ebb571c7/go.mod h1:fTnA9KjwuMEWnqAFPPoLc6IzvYxD8SorqawESk74fP8= github.com/jfrog/jfrog-cli-security v1.0.4-0.20240303113253-d82ecc02d9eb h1:JUqwNOqzwj+1oZQXY1p/xnnH97BEMfOKGG505vGVIe4= github.com/jfrog/jfrog-cli-security v1.0.4-0.20240303113253-d82ecc02d9eb/go.mod h1:pOqg/hGw6eHWNyLDHeOCyq/Ntuc+wLJTV2Z8XuwXspU= github.com/jfrog/jfrog-client-go v1.28.1-0.20240228121257-3414cc0ffcb6 h1:W+79g2W3ARRhIZtBfG0t73fi4IlyiIRWwdm1tajOkkc= diff --git a/npm_test.go b/npm_test.go index 3c0cf1474..d040e3eee 100644 --- a/npm_test.go +++ b/npm_test.go @@ -37,6 +37,10 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + minimumWorkspacesNpmVersion = "7.24.2" +) + type npmTestParams struct { testName string nativeCommand string @@ -274,6 +278,17 @@ func initNpmProjectTest(t *testing.T) (npmProjectPath string) { return } +func initNpmWorkspacesProjectTest(t *testing.T) (npmProjectPath string) { + npmProjectPath = filepath.Dir(createNpmProject(t, "npmworkspaces")) + err := createConfigFileForTest([]string{npmProjectPath}, tests.NpmRemoteRepo, tests.NpmRepo, t, project.Npm, false) + assert.NoError(t, err) + testFolder := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "npm", "npmworkspaces") + err = biutils.CopyDir(testFolder, npmProjectPath, true, []string{}) + assert.NoError(t, err) + prepareArtifactoryForNpmBuild(t, npmProjectPath) + return +} + func initGlobalNpmFilesTest(t *testing.T) (npmProjectPath string) { npmProjectPath = createNpmProject(t, "npmproject") jfrogHomeDir, err := coreutils.GetJfrogHomeDir() @@ -499,6 +514,69 @@ func TestNpmPackInstall(t *testing.T) { assert.Len(t, npmBuildInfo.Modules, 0) } +// Test npm publish --workspaces command +// When using the -w flag npm itself knows to handle multiple modules, +// And the CLI needs to know to publish multiple packages. +// Workspaces has been introduced in npm v7.0.0+ +// Read more about npm workspaces here: https://docs.npmjs.com/cli/v7/using-npm/workspaces +func TestNpmPublishWithWorkspaces(t *testing.T) { + // Check npm version + npmVersion, _, err := buildutils.GetNpmVersionAndExecPath(log.Logger) + if err != nil { + assert.NoError(t, err) + return + } + // In npm under v7 skip test + if npmVersion.Compare(minimumWorkspacesNpmVersion) > 0 { + log.Info("Test skipped as this function in not supported in npm version " + npmVersion.GetVersion()) + return + } + + initNpmTest(t) + defer cleanNpmTest(t) + wd, err := os.Getwd() + assert.NoError(t, err, "Failed to get current dir") + defer clientTestUtils.ChangeDirAndAssert(t, wd) + + // Init npm project & npmp command for testing + npmProjectPath := initNpmWorkspacesProjectTest(t) + configFilePath := filepath.Join(npmProjectPath, ".jfrog", "projects", "npm.yaml") + args := []string{"--detailed-summary=true", "--workspaces", "--verbose"} + npmpCmd := npm.NewNpmPublishCommand() + npmpCmd.SetConfigFilePath(configFilePath).SetArgs(args) + npmpCmd.SetNpmArgs(args) + assert.NoError(t, npmpCmd.Init()) + err = commands.Exec(npmpCmd) + assert.NoError(t, err) + + result := npmpCmd.Result() + assert.NotNil(t, result) + reader := result.Reader() + readerGetErrorAndAssert(t, reader) + defer readerCloseAndAssert(t, reader) + // Read result + var files []clientutils.FileTransferDetails + for transferDetails := new(clientutils.FileTransferDetails); reader.NextRecord(transferDetails) == nil; transferDetails = new(clientutils.FileTransferDetails) { + files = append(files, *transferDetails) + } + if files == nil { + assert.NotNil(t, files) + return + } + + expectedTars := []string{"nested1", "nested2"} + for index, tar := range expectedTars { + // Verify deploy details + tarballName := tar + "-1.0.0.tgz" + expectedSourcePath := filepath.Join(npmProjectPath, tarballName) + expectedTargetPath := serverDetails.ArtifactoryUrl + tests.NpmRepo + "/" + tar + "/-/" + tarballName + assert.Equal(t, expectedSourcePath, files[index].SourcePath, "Summary validation failed - unmatched SourcePath.") + assert.Equal(t, expectedTargetPath, files[index].RtUrl+files[index].TargetPath, "Summary validation failed - unmatched TargetPath.") + assert.Equal(t, len(expectedTars), len(files), "Summary validation failed - two archive should be deployed.") + assert.Len(t, files[index].Sha256, 64) + } +} + func TestYarn(t *testing.T) { initNpmTest(t) defer cleanNpmTest(t) diff --git a/testdata/npm/npmworkspaces/nested1/package.json b/testdata/npm/npmworkspaces/nested1/package.json new file mode 100644 index 000000000..629f660ce --- /dev/null +++ b/testdata/npm/npmworkspaces/nested1/package.json @@ -0,0 +1,15 @@ +{ + "name": "nested1", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "loadash": "1.0.0" + } +} diff --git a/testdata/npm/npmworkspaces/nested2/package.json b/testdata/npm/npmworkspaces/nested2/package.json new file mode 100644 index 000000000..c38fdeaf9 --- /dev/null +++ b/testdata/npm/npmworkspaces/nested2/package.json @@ -0,0 +1,15 @@ +{ + "name": "nested2", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "loadash": "1.0.0" + } +} diff --git a/testdata/npm/npmworkspaces/package.json b/testdata/npm/npmworkspaces/package.json new file mode 100755 index 000000000..2eb0d9c4e --- /dev/null +++ b/testdata/npm/npmworkspaces/package.json @@ -0,0 +1,21 @@ +{ + "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" + }, + "workspaces": [ + "nested1", + "nested2" + ] +}