Skip to content

Commit

Permalink
Add npm publish with workspaces test (#2471)
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea authored Mar 12, 2024
1 parent 3945c98 commit 2f0e0c9
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
78 changes: 78 additions & 0 deletions npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import (
"github.com/stretchr/testify/assert"
)

const (
minimumWorkspacesNpmVersion = "7.24.2"
)

type npmTestParams struct {
testName string
nativeCommand string
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions testdata/npm/npmworkspaces/nested1/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
15 changes: 15 additions & 0 deletions testdata/npm/npmworkspaces/nested2/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
21 changes: 21 additions & 0 deletions testdata/npm/npmworkspaces/package.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit 2f0e0c9

Please sign in to comment.