Skip to content

Commit ecfd291

Browse files
committed
added tests
1 parent 2ef169d commit ecfd291

File tree

8 files changed

+172
-11
lines changed

8 files changed

+172
-11
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ require (
189189
sigs.k8s.io/yaml v1.4.0 // indirect
190190
)
191191

192-
replace github.com/jfrog/jfrog-cli-artifactory => github.com/fluxxBot/jfrog-cli-artifactory v0.0.0-20250415004838-3b7657e999ac
192+
replace github.com/jfrog/jfrog-cli-artifactory => github.com/fluxxBot/jfrog-cli-artifactory v0.0.0-20250421001920-75f0f06037b7
193193

194-
replace github.com/jfrog/jfrog-cli-core/v2 => github.com/fluxxBot/jfrog-cli-core/v2 v2.31.1-0.20250413220436-b46ff5daaf5e
194+
replace github.com/jfrog/jfrog-cli-core/v2 => github.com/fluxxBot/jfrog-cli-core/v2 v2.31.1-0.20250420214742-c12c4050641f
195195

196196
replace github.com/jfrog/jfrog-client-go => github.com/fluxxBot/jfrog-client-go v1.28.1-0.20250413214704-1b6cdf0520c6
197197

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
9292
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
9393
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
9494
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
95-
github.com/fluxxBot/jfrog-cli-artifactory v0.0.0-20250415004838-3b7657e999ac h1:457XUQMd/eXCSPaC13M65qP4th56RzWrfrGLWu0c0dA=
96-
github.com/fluxxBot/jfrog-cli-artifactory v0.0.0-20250415004838-3b7657e999ac/go.mod h1:IIF1t7YQnV9jYGYcEyNzusZMiibqG8hG1cPbRDYKTEg=
97-
github.com/fluxxBot/jfrog-cli-core/v2 v2.31.1-0.20250413220436-b46ff5daaf5e h1:q0BinSmz7ZhYbkuow5I+R3DhOIHFIcKk/ZxkE/GSW1w=
98-
github.com/fluxxBot/jfrog-cli-core/v2 v2.31.1-0.20250413220436-b46ff5daaf5e/go.mod h1:eV3CTP/F4nEYE7p4WojZc4ruU1/3sXj0o/ctnjmO5fw=
95+
github.com/fluxxBot/jfrog-cli-artifactory v0.0.0-20250421001920-75f0f06037b7 h1:fZXGWJGy+eMCfA/qeCJCtVviQcS9YJ6EvduPZ1H8+KA=
96+
github.com/fluxxBot/jfrog-cli-artifactory v0.0.0-20250421001920-75f0f06037b7/go.mod h1:uDQCzRFo8n90WXEIYeOKc8ed2TT/pJ3iOQdImw6vgkY=
97+
github.com/fluxxBot/jfrog-cli-core/v2 v2.31.1-0.20250420214742-c12c4050641f h1:SdMa2iO2PqNegvh57uHkWy/00RySEO/CeiZCSa9rT8w=
98+
github.com/fluxxBot/jfrog-cli-core/v2 v2.31.1-0.20250420214742-c12c4050641f/go.mod h1:nrKlwWRT7BT9TqE51WSIpUlO8uQR6qWHlp3UiKqC4nc=
9999
github.com/fluxxBot/jfrog-client-go v1.28.1-0.20250413214704-1b6cdf0520c6 h1:krAN+7hGggGDXfAlcC8eni2QxWfW7I5JVNPvhI3vS4U=
100100
github.com/fluxxBot/jfrog-client-go v1.28.1-0.20250413214704-1b6cdf0520c6/go.mod h1:uRmT8Q1SJymIzId01v0W1o8mGqrRfrwUF53CgEMsH0U=
101101
github.com/forPelevin/gomoji v1.3.0 h1:WPIOLWB1bvRYlKZnSSEevLt3IfKlLs+tK+YA9fFYlkE=

npm_test.go

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/generic"
6+
utils2 "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
67
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
78
"github.com/jfrog/jfrog-client-go/http/httpclient"
89
"github.com/stretchr/testify/require"
@@ -148,12 +149,119 @@ func testNpm(t *testing.T, isLegacy bool) {
148149
inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, tests.NpmBuildName, artHttpDetails)
149150
}
150151

152+
func TestNpmPublishWithNpmrc(t *testing.T) {
153+
testNpmPublishWithNpmrc(t, validateNpmPublish, "npmpublishrcproject", tests.NpmRepo, false)
154+
}
155+
156+
func TestNpmPublishWithNpmrcScoped(t *testing.T) {
157+
testNpmPublishWithNpmrc(t, validateNpmScopedPublish, "npmpublishrcscopedproject", tests.NpmScopedRepo, true)
158+
}
159+
160+
func testNpmPublishWithNpmrc(t *testing.T, validationFunc func(t *testing.T, npmTest npmTestParams, isNpm7 bool), projectName string, repoName string, isScoped bool) {
161+
initNpmTest(t)
162+
defer cleanNpmTest(t)
163+
wd, err := os.Getwd()
164+
assert.NoError(t, err, "Failed to get current dir")
165+
defer clientTestUtils.ChangeDirAndAssert(t, wd)
166+
buildNumber := strconv.Itoa(1)
167+
npmVersion, _, err := buildutils.GetNpmVersionAndExecPath(log.Logger)
168+
if err != nil {
169+
assert.NoError(t, err)
170+
return
171+
}
172+
173+
// Init npm project & npmp command for testing
174+
npmProjectPath := initNpmPublishRcProjectTest(t, projectName)
175+
configFilePath := filepath.Join(npmProjectPath, ".jfrog", "projects", "npm.yaml")
176+
177+
// fetch module id
178+
packageJsonPath := npmProjectPath + "/package.json"
179+
moduleName := readModuleId(t, packageJsonPath, npmVersion)
180+
181+
err = createNpmrcForTesting(configFilePath)
182+
assert.NoError(t, err)
183+
184+
if isScoped {
185+
addNpmScopeRegistryToNpmRc(t, npmProjectPath, packageJsonPath, npmVersion)
186+
}
187+
188+
npmpCmd, err := publishUsingNpmrc(configFilePath, buildNumber)
189+
assert.NoError(t, err)
190+
191+
result := npmpCmd.Result()
192+
assert.NotNil(t, result)
193+
194+
validateNpmLocalBuildInfo(t, tests.NpmBuildName, buildNumber, moduleName)
195+
assert.NoError(t, artifactoryCli.Exec("bp", tests.NpmBuildName, buildNumber))
196+
197+
// validation
198+
testParams := npmTestParams{testName: "npm p",
199+
nativeCommand: "npm publish",
200+
legacyCommand: "rt npm-publish",
201+
repo: repoName,
202+
wd: npmProjectPath,
203+
validationFunc: validateNpmPublish,
204+
buildNumber: buildNumber,
205+
moduleName: moduleName,
206+
}
207+
validationFunc(t, testParams, false)
208+
}
209+
210+
func createNpmrcForTesting(configFilePath string) (err error) {
211+
//Creation of npmrc - npmCommand.CreateTempNpmrc() function is used to create a npmrc file used for uploading
212+
npmCommand := npm.NewNpmCommand("install", true)
213+
npmCommand.SetConfigFilePath(configFilePath)
214+
npmCommand.SetServerDetails(serverDetails)
215+
err = npmCommand.PreparePrerequisites(tests.NpmRepo)
216+
if err != nil {
217+
return
218+
}
219+
err = npmCommand.CreateTempNpmrc()
220+
return
221+
}
222+
223+
func publishUsingNpmrc(configFilePath string, buildNumber string) (npm.NpmPublishCommand, error) {
224+
args := []string{"--use-npmrc=true", "--build-name=" + tests.NpmBuildName, "--build-number=" + buildNumber}
225+
npmpCmd := npm.NewNpmPublishCommand()
226+
npmpCmd.SetConfigFilePath(configFilePath).SetArgs(args)
227+
err := npmpCmd.Init()
228+
if err != nil {
229+
return *npmpCmd, err
230+
}
231+
err = commands.Exec(npmpCmd)
232+
if err != nil {
233+
return *npmpCmd, err
234+
}
235+
return *npmpCmd, err
236+
}
237+
151238
func readModuleId(t *testing.T, wd string, npmVersion *version.Version) string {
152239
packageInfo, err := buildutils.ReadPackageInfoFromPackageJsonIfExists(filepath.Dir(wd), npmVersion)
153240
assert.NoError(t, err)
154241
return packageInfo.BuildInfoModuleId()
155242
}
156243

244+
func addNpmScopeRegistryToNpmRc(t *testing.T, projectPath string, packageJsonPath string, npmVersion *version.Version) {
245+
scope := getScopeFromPackageJson(t, packageJsonPath, npmVersion)
246+
authConfig, err := serverDetails.CreateArtAuthConfig()
247+
assert.NoError(t, err)
248+
_, registry, err := utils2.GetArtifactoryNpmRepoDetails(tests.NpmScopedRepo, authConfig, false)
249+
assert.NoError(t, err)
250+
scopedRegistry := scope + ":registry=" + registry
251+
npmrcFilePath := filepath.Join(projectPath, ".npmrc")
252+
npmrcFile, err := os.OpenFile(npmrcFilePath, os.O_APPEND|os.O_WRONLY, 0644)
253+
assert.NoError(t, err)
254+
defer npmrcFile.Close()
255+
_, err = npmrcFile.WriteString(scopedRegistry)
256+
assert.NoError(t, err)
257+
}
258+
259+
func getScopeFromPackageJson(t *testing.T, wd string, npmVersion *version.Version) string {
260+
packageInfo, err := buildutils.ReadPackageInfoFromPackageJsonIfExists(filepath.Dir(wd), npmVersion)
261+
assert.NoError(t, err)
262+
return packageInfo.Scope
263+
}
264+
157265
func TestNpmWithGlobalConfig(t *testing.T) {
158266
initNpmTest(t)
159267
defer cleanNpmTest(t)
@@ -285,6 +393,14 @@ func initNpmProjectTest(t *testing.T) (npmProjectPath string) {
285393
return
286394
}
287395

396+
func initNpmPublishRcProjectTest(t *testing.T, projectName string) (npmProjectPath string) {
397+
npmProjectPath = filepath.Dir(createNpmProject(t, projectName))
398+
err := createConfigFileForTest([]string{npmProjectPath}, tests.NpmRemoteRepo, tests.NpmRepo, t, project.Npm, false)
399+
assert.NoError(t, err)
400+
prepareArtifactoryForNpmBuild(t, npmProjectPath)
401+
return
402+
}
403+
288404
func initNpmWorkspacesProjectTest(t *testing.T) (npmProjectPath string) {
289405
npmProjectPath = filepath.Dir(createNpmProject(t, "npmworkspaces"))
290406
err := createConfigFileForTest([]string{npmProjectPath}, tests.NpmRemoteRepo, tests.NpmRepo, t, project.Npm, false)
@@ -361,8 +477,8 @@ func validateNpmPublish(t *testing.T, npmTestParams npmTestParams, isNpm7 bool)
361477
}
362478

363479
func validateNpmScopedPublish(t *testing.T, npmTestParams npmTestParams, isNpm7 bool) {
364-
verifyExistInArtifactoryByProps(tests.GetNpmDeployedScopedArtifacts(isNpm7),
365-
tests.NpmRepo+"/*",
480+
verifyExistInArtifactoryByProps(tests.GetNpmDeployedScopedArtifacts(npmTestParams.repo, isNpm7),
481+
npmTestParams.repo+"/*",
366482
fmt.Sprintf("build.name=%v;build.number=%v;build.timestamp=*", tests.NpmBuildName, npmTestParams.buildNumber), t)
367483
validateNpmCommonPublish(t, npmTestParams, isNpm7, true)
368484
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "jfrog-cli-tests",
3+
"version": "1.0.0",
4+
"description": "test package",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"xml": "1.0.1"
13+
},
14+
"devDependencies": {
15+
"json": "9.0.6"
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@jscope/jfrog-cli-tests",
3+
"version": "1.0.0",
4+
"description": "test package",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"xml": "1.0.1"
13+
},
14+
"devDependencies": {
15+
"json": "9.0.6"
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"key": "${NPM_SCOPED_REPO}",
3+
"rclass": "local",
4+
"packageType": "npm"
5+
}

utils/tests/consts.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const (
8888
MavenWithoutDeployerConfig = "maven_without_deployer.yaml"
8989
MoveCopySpecExclusions = "move_copy_spec_exclusions.json"
9090
NpmLocalRepositoryConfig = "npm_local_repository_config.json"
91+
NpmLocalScopedRespositoryConfig = "npm_local_scoped_repository_config.json"
9192
NpmRemoteRepositoryConfig = "npm_remote_repository_config.json"
9293
NugetRemoteRepositoryConfig = "nuget_remote_repository_config.json"
9394
Out = "out"
@@ -165,6 +166,7 @@ const (
165166
XrayEndpoint = "xray/"
166167
YarnRemoteRepositoryConfig = "yarn_remote_repository_config.json"
167168
ReleaseLifecycleImportDependencySpec = "import_bundle_repo_dependency.json"
169+
UseNpmRcFlag = "--use-npmrc"
168170
)
169171

170172
var (
@@ -181,6 +183,7 @@ var (
181183
MvnRepo1 = "cli-mvn1"
182184
MvnRepo2 = "cli-mvn2"
183185
NpmRepo = "cli-npm"
186+
NpmScopedRepo = "cli-npm-scoped"
184187
NpmRemoteRepo = "cli-npm-remote"
185188
NugetRemoteRepo = "cli-nuget-remote"
186189
YarnRemoteRepo = "cli-yarn-remote"
@@ -1797,8 +1800,8 @@ func GetGradleDeployedArtifacts() []string {
17971800
}
17981801
}
17991802

1800-
func GetNpmDeployedScopedArtifacts(isNpm7 bool) []string {
1801-
path := NpmRepo + "/@jscope/jfrog-cli-tests/-/@jscope/"
1803+
func GetNpmDeployedScopedArtifacts(npmRepo string, isNpm7 bool) []string {
1804+
path := npmRepo + "/@jscope/jfrog-cli-tests/-/@jscope/"
18021805
path += GetNpmArtifactName(isNpm7, true)
18031806
return []string{
18041807
path,

utils/tests/utils.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ var reposConfigMap = map[*string]string{
262262
&MvnRemoteRepo: MavenRemoteRepositoryConfig,
263263
&GradleRemoteRepo: GradleRemoteRepositoryConfig,
264264
&NpmRepo: NpmLocalRepositoryConfig,
265+
&NpmScopedRepo: NpmLocalScopedRespositoryConfig,
265266
&NpmRemoteRepo: NpmRemoteRepositoryConfig,
266267
&NugetRemoteRepo: NugetRemoteRepositoryConfig,
267268
&YarnRemoteRepo: YarnRemoteRepositoryConfig,
@@ -325,7 +326,7 @@ func GetNonVirtualRepositories() map[*string]string {
325326
TestGo: {&GoRepo, &GoRemoteRepo},
326327
TestGradle: {&GradleRepo, &GradleRemoteRepo},
327328
TestMaven: {&MvnRepo1, &MvnRepo2, &MvnRemoteRepo},
328-
TestNpm: {&NpmRepo, &NpmRemoteRepo},
329+
TestNpm: {&NpmRepo, &NpmScopedRepo, &NpmRemoteRepo},
329330
TestNuget: {&NugetRemoteRepo},
330331
TestPip: {&PypiLocalRepo, &PypiRemoteRepo},
331332
TestPipenv: {&PipenvRemoteRepo},
@@ -419,6 +420,7 @@ func getSubstitutionMap() map[string]string {
419420
"${GRADLE_REMOTE_REPO}": GradleRemoteRepo,
420421
"${GRADLE_REPO}": GradleRepo,
421422
"${NPM_REPO}": NpmRepo,
423+
"${NPM_SCOPED_REPO}": NpmScopedRepo,
422424
"${NPM_REMOTE_REPO}": NpmRemoteRepo,
423425
"${NUGET_REMOTE_REPO}": NugetRemoteRepo,
424426
"${YARN_REMOTE_REPO}": YarnRemoteRepo,
@@ -488,6 +490,7 @@ func AddTimestampToGlobalVars() {
488490
MvnRepo1 += uniqueSuffix
489491
MvnRepo2 += uniqueSuffix
490492
NpmRepo += uniqueSuffix
493+
NpmScopedRepo += uniqueSuffix
491494
NpmRemoteRepo += uniqueSuffix
492495
NugetRemoteRepo += uniqueSuffix
493496
YarnRemoteRepo += uniqueSuffix

0 commit comments

Comments
 (0)