@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"fmt"
5
5
"github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/generic"
6
+ utils2 "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
6
7
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
7
8
"github.com/jfrog/jfrog-client-go/http/httpclient"
8
9
"github.com/stretchr/testify/require"
@@ -148,12 +149,119 @@ func testNpm(t *testing.T, isLegacy bool) {
148
149
inttestutils .DeleteBuild (serverDetails .ArtifactoryUrl , tests .NpmBuildName , artHttpDetails )
149
150
}
150
151
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
+
151
238
func readModuleId (t * testing.T , wd string , npmVersion * version.Version ) string {
152
239
packageInfo , err := buildutils .ReadPackageInfoFromPackageJsonIfExists (filepath .Dir (wd ), npmVersion )
153
240
assert .NoError (t , err )
154
241
return packageInfo .BuildInfoModuleId ()
155
242
}
156
243
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
+
157
265
func TestNpmWithGlobalConfig (t * testing.T ) {
158
266
initNpmTest (t )
159
267
defer cleanNpmTest (t )
@@ -285,6 +393,14 @@ func initNpmProjectTest(t *testing.T) (npmProjectPath string) {
285
393
return
286
394
}
287
395
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
+
288
404
func initNpmWorkspacesProjectTest (t * testing.T ) (npmProjectPath string ) {
289
405
npmProjectPath = filepath .Dir (createNpmProject (t , "npmworkspaces" ))
290
406
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)
361
477
}
362
478
363
479
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 + "/*" ,
366
482
fmt .Sprintf ("build.name=%v;build.number=%v;build.timestamp=*" , tests .NpmBuildName , npmTestParams .buildNumber ), t )
367
483
validateNpmCommonPublish (t , npmTestParams , isNpm7 , true )
368
484
}
0 commit comments