forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnuget_test.go
More file actions
536 lines (461 loc) · 22.5 KB
/
nuget_test.go
File metadata and controls
536 lines (461 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package main
import (
"encoding/xml"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
"github.com/jfrog/jfrog-client-go/http/httpclient"
"github.com/jfrog/jfrog-client-go/utils/io"
dotnetUtils "github.com/jfrog/build-info-go/build/utils/dotnet"
buildInfo "github.com/jfrog/build-info-go/entities"
biutils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/dotnet"
"github.com/jfrog/jfrog-cli-core/v2/common/project"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
"github.com/jfrog/jfrog-cli/inttestutils"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)
func initNugetTest(t *testing.T) {
if !*tests.TestNuget {
t.Skip("Skipping NuGet test. To run Nuget test add the '-test.nuget=true' option.")
}
createJfrogHomeConfig(t, true)
}
type testDescriptor struct {
name string
project string
args []string
expectedModules []string
expectedDependencies []int
}
func TestNugetResolve(t *testing.T) {
uniqueNugetTests := []testDescriptor{
{"nugetargswithspaces", "packagesconfig", []string{dotnetUtils.Nuget.String(), "restore", "-PackagesDirectory", "./packages dir with spaces"}, []string{"packagesconfig"}, []int{6}},
{"packagesconfigwithoutmodulechange", "packagesconfig", []string{dotnetUtils.Nuget.String(), "restore"}, []string{"packagesconfig"}, []int{6}},
{"packagesconfigwithmodulechange", "packagesconfig", []string{dotnetUtils.Nuget.String(), "restore", "--module=" + ModuleNameJFrogTest}, []string{ModuleNameJFrogTest}, []int{6}},
{"packagesconfigwithconfigpath", "packagesconfig", []string{dotnetUtils.Nuget.String(), "restore", "./packages.config", "-SolutionDirectory", "."}, []string{"packagesconfig"}, []int{6}},
{"multipackagesconfigwithoutmodulechange", "multipackagesconfig", []string{dotnetUtils.Nuget.String(), "restore"}, []string{"proj1", "proj2", "proj3"}, []int{4, 3, 2}},
{"multipackagesconfigwithmodulechange", "multipackagesconfig", []string{dotnetUtils.Nuget.String(), "restore", "--module=" + ModuleNameJFrogTest}, []string{ModuleNameJFrogTest}, []int{8}},
{"multipackagesconfigwithslnPath", "multipackagesconfig", []string{dotnetUtils.Nuget.String(), "restore", "./multipackagesconfig.sln"}, []string{"proj1", "proj2", "proj3"}, []int{4, 3, 2}},
{"multipackagesconfigsingleprojectdir", "multipackagesconfig", []string{dotnetUtils.Nuget.String(), "restore", "./proj2/", "-SolutionDirectory", "."}, []string{"proj2"}, []int{3}},
{"multipackagesconfigsingleprojectconfig", "multipackagesconfig", []string{dotnetUtils.Nuget.String(), "restore", "./proj1/packages.config", "-SolutionDirectory", "."}, []string{"proj1"}, []int{4}},
}
testNativeNugetDotnetResolve(t, uniqueNugetTests, tests.NuGetBuildName, project.Nuget)
}
func TestDotnetResolve(t *testing.T) {
uniqueDotnetTests := []testDescriptor{
{"dotnetargswithspaces", "multireference", []string{dotnetUtils.DotnetCore.String(), "restore", "src/multireference.proj1/", "--packages", "./packages dir with spaces"}, []string{"proj1"}, []int{5}},
{"multireferencesingleprojectdir", "multireference", []string{dotnetUtils.DotnetCore.String(), "restore", "src/multireference.proj1/"}, []string{"proj1"}, []int{5}},
}
testNativeNugetDotnetResolve(t, uniqueDotnetTests, tests.DotnetBuildName, project.Dotnet)
}
func testNativeNugetDotnetResolve(t *testing.T, uniqueTests []testDescriptor, buildName string, projectType project.ProjectType) {
initNugetTest(t)
testDescriptors := append(slices.Clone(uniqueTests), []testDescriptor{
{"referencewithoutmodulechange", "reference", []string{projectType.String(), "restore"}, []string{"reference"}, []int{6}},
{"referencewithmodulechange", "reference", []string{projectType.String(), "restore", "--module=" + ModuleNameJFrogTest}, []string{ModuleNameJFrogTest}, []int{6}},
{"multireferencewithoutmodulechange", "multireference", []string{projectType.String(), "restore"}, []string{"proj1", "proj2"}, []int{5, 3}},
{"multireferencewithmodulechange", "multireference", []string{projectType.String(), "restore", "--module=" + ModuleNameJFrogTest}, []string{ModuleNameJFrogTest}, []int{6}},
{"multireferencewithslnpath", "multireference", []string{projectType.String(), "restore", "src/multireference.sln"}, []string{"proj1", "proj2"}, []int{5, 3}},
{"multireferencewithslndir", "multireference", []string{projectType.String(), "restore", "src/"}, []string{"proj1", "proj2"}, []int{5, 3}},
{"multireferencesingleprojectcsproj", "multireference", []string{projectType.String(), "restore", "src/multireference.proj2/proj2.csproj"}, []string{"proj2"}, []int{3}},
{"sln_and_proj_different_locations", "differentlocations", []string{projectType.String(), "restore", "solutions/differentlocations.sln"}, []string{"proj1", "proj2"}, []int{5, 3}},
}...)
for buildNumber, test := range testDescriptors {
projectPath := createNugetProject(t, test.project)
err := createConfigFileForTest([]string{projectPath}, tests.NugetRemoteRepo, "", t, projectType, false)
if err != nil {
assert.NoError(t, err)
return
}
t.Run(test.name, func(t *testing.T) {
testNugetCmd(t, projectPath, buildName, strconv.Itoa(buildNumber), test.expectedModules, test.args, test.expectedDependencies)
})
}
cleanTestsHomeEnv()
}
func createNugetProject(t *testing.T, projectName string) string {
projectSrc := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "nuget", projectName)
projectTarget := filepath.Join(tests.Out, projectName)
err := fileutils.CreateDirIfNotExist(projectTarget)
assert.NoError(t, err)
err = biutils.CopyDir(projectSrc, projectTarget, true, nil)
assert.NoError(t, err)
return projectTarget
}
func TestNuGetWithGlobalConfig(t *testing.T) {
initNugetTest(t)
projectPath := createNugetProject(t, "packagesconfig")
jfrogHomeDir, err := coreutils.GetJfrogHomeDir()
assert.NoError(t, err)
err = createConfigFileForTest([]string{jfrogHomeDir}, tests.NugetRemoteRepo, "", t, project.Nuget, true)
assert.NoError(t, err)
// allow insecure connection for testings to work with localhost server
testNugetCmd(t, projectPath, tests.NuGetBuildName, "1", []string{"packagesconfig"}, []string{"nuget", "restore"}, []int{6})
cleanTestsHomeEnv()
}
func testNugetCmd(t *testing.T, projectPath, buildName, buildNumber string, expectedModule, args []string, expectedDependencies []int) {
wd, err := os.Getwd()
assert.NoError(t, err, "Failed to get current dir")
chdirCallback := clientTestUtils.ChangeDirWithCallback(t, wd, projectPath)
defer chdirCallback()
allowInsecureConnectionForTests(&args)
args = append(args, "--build-name="+buildName, "--build-number="+buildNumber)
err = runNuGet(t, args...)
if err != nil {
return
}
inttestutils.ValidateGeneratedBuildInfoModule(t, buildName, buildNumber, "", expectedModule, buildInfo.Nuget)
assert.NoError(t, artifactoryCli.Exec("bp", buildName, buildNumber))
publishedBuildInfo, found, err := tests.GetBuildInfo(serverDetails, buildName, buildNumber)
if err != nil {
assert.NoError(t, err)
return
}
if !found {
assert.True(t, found, "build info was expected to be found")
return
}
bi := publishedBuildInfo.BuildInfo
require.NotEmpty(t, bi.Modules, buildName+" build info was not generated correctly, no modules were created.")
for i, module := range bi.Modules {
assert.Equal(t, expectedModule[i], bi.Modules[i].Id, "Unexpected module name")
assert.Len(t, module.Dependencies, expectedDependencies[i], "Incorrect number of artifacts found in the build-info")
if strings.HasSuffix(projectPath, "multipackagesconfig") {
assertNugetMultiPackagesConfigDependencies(t, module, expectedModule[i])
} else {
assertNugetDependencies(t, module, expectedModule[i])
}
}
chdirCallback()
// cleanup
inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, buildName, artHttpDetails)
}
// Add allow insecure connection for testings to work with localhost server
func allowInsecureConnectionForTests(args *[]string) {
*args = append(*args, "--allow-insecure-connections")
}
func assertNugetDependencies(t *testing.T, module buildInfo.Module, moduleName string) {
for _, dependency := range module.Dependencies {
switch dependency.Id {
case "Microsoft.Web.Xdt:2.1.0", "Microsoft.Web.Xdt:2.1.1":
assert.EqualValues(t, [][]string{{"NuGet.Core:2.14.0", moduleName}}, dependency.RequestedBy)
case "popper.js:1.12.9", "jQuery:3.0.0":
assert.EqualValues(t, [][]string{{"bootstrap:4.0.0", moduleName}}, dependency.RequestedBy)
case "bootstrap:4.0.0", "Newtonsoft.Json:11.0.2", "NuGet.Core:2.14.0":
assert.EqualValues(t, [][]string{{moduleName}}, dependency.RequestedBy)
default:
assert.Fail(t, "Unexpected dependency "+dependency.Id)
}
}
}
func assertNugetMultiPackagesConfigDependencies(t *testing.T, module buildInfo.Module, moduleName string) {
for _, dependency := range module.Dependencies {
switch dependency.Id {
case "Microsoft.Web.Xdt:2.1.0", "Microsoft.Web.Xdt:2.1.1":
assert.EqualValues(t, [][]string{{"NuGet.Core:2.14.0", moduleName}}, dependency.RequestedBy)
case "jQuery:3.0.0":
assert.EqualValues(t, [][]string{{"bootstrap:4.0.0", moduleName}}, dependency.RequestedBy)
case "bootstrap:4.0.0", "Newtonsoft.Json:11.0.2", "NuGet.Core:2.14.0", "StyleCop.Analyzers:1.0.2",
"Microsoft.VisualStudio.Setup.Configuration.Interop:1.11.2290", "popper.js:1.12.9":
assert.EqualValues(t, [][]string{{moduleName}}, dependency.RequestedBy)
default:
assert.Fail(t, "Unexpected dependency "+dependency.Id)
}
}
}
func runNuGet(t *testing.T, args ...string) error {
artifactoryNuGetCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
err := artifactoryNuGetCli.Exec(args...)
assert.NoError(t, err)
return err
}
type testInitNewConfigDescriptor struct {
testName string
useNugetV2 bool
expectedSourceUrl string
}
func TestInitNewConfig(t *testing.T) {
// jfrog-ignore test url
baseRtUrl := "http://some/url"
expectedV2Url := baseRtUrl + "/api/nuget"
expectedV3Url := baseRtUrl + "/api/nuget/v3/index.json"
testsSuites := []testInitNewConfigDescriptor{
{"useNugetAddSourceV2", true, expectedV2Url},
{"useNugetAddSourceV3", false, expectedV3Url},
}
for _, test := range testsSuites {
t.Run(test.testName, func(t *testing.T) {
runInitNewConfig(t, test, baseRtUrl)
})
}
}
func runInitNewConfig(t *testing.T, testSuite testInitNewConfigDescriptor, baseRtUrl string) {
initNugetTest(t)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
if tempDirPath == "" {
return
}
params := &dotnet.DotnetCommand{}
server := &config.ServerDetails{ArtifactoryUrl: baseRtUrl, User: "user", Password: "password"}
params.SetServerDetails(server).
SetUseNugetV2(testSuite.useNugetV2).
SetAllowInsecureConnections(true)
// Prepare the config file with NuGet authentication
configFile, err := dotnet.InitNewConfig(tempDirPath, "", server, testSuite.useNugetV2, true)
if err != nil {
assert.NoError(t, err)
return
}
content, err := os.ReadFile(configFile.Name()) //#nosec G703 -- test code, path from temp file
if err != nil {
assert.NoError(t, err)
return
}
nugetConfig := NugetConfig{}
err = xml.Unmarshal(content, &nugetConfig)
if err != nil {
assert.NoError(t, err, "unmarshalling failed with an error")
return
}
assert.Len(t, nugetConfig.PackageSources, 1)
for _, packageSource := range nugetConfig.PackageSources {
assert.Equal(t, dotnet.SourceName, packageSource.Key)
assert.Equal(t, testSuite.expectedSourceUrl, packageSource.Value)
}
assert.Len(t, nugetConfig.PackageSourceCredentials, 1)
assert.Len(t, nugetConfig.PackageSourceCredentials[0].JFrogCli, 2)
}
type NugetConfig struct {
XMLName xml.Name `xml:"configuration"`
PackageSources []PackageSources `xml:"packageSources>add"`
PackageSourceCredentials []PackageSourceCredentials `xml:"packageSourceCredentials"`
}
type PackageSources struct {
Key string `xml:"key,attr"`
Value string `xml:"value,attr"`
}
type PackageSourceCredentials struct {
JFrogCli []PackageSources `xml:">add"`
}
func TestSetupNugetCommand(t *testing.T) {
testSetupCommand(t, project.Nuget)
}
func TestSetupDotnetCommand(t *testing.T) {
testSetupCommand(t, project.Dotnet)
}
func testSetupCommand(t *testing.T, packageManager project.ProjectType) {
initNugetTest(t)
restoreFunc := prepareSetupTest(t, packageManager)
defer func() {
restoreFunc()
}()
// Validate that the package does not exist in the cache before running the test.
client, err := httpclient.ClientBuilder().Build()
assert.NoError(t, err)
// We use different versions of the Nunit package for Nuget and Dotnet to differentiate between the two tests.
version := "4.0.0"
if packageManager == project.Dotnet {
version = "4.1.0"
}
moduleCacheUrl := serverDetails.ArtifactoryUrl + tests.NugetRemoteRepo + "-cache/nunit." + version + ".nupkg"
_, _, err = client.GetRemoteFileDetails(moduleCacheUrl, artHttpDetails)
assert.ErrorContains(t, err, "404")
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
require.NoError(t, execGo(jfrogCli, "setup", packageManager.String(), "--repo="+tests.NugetRemoteRepo))
// Run install some random (Nunit) package to test the setup command.
var output []byte
if packageManager == project.Dotnet {
output, err = exec.Command(packageManager.String(), "add", "package", "NUnit", "--version", version).Output()
} else {
output, err = exec.Command(packageManager.String(), "install", "NUnit", "-Version", version, "-OutputDirectory", t.TempDir(), "-NoHttpCache").Output()
}
assert.NoError(t, err, fmt.Sprintf("%s\n%q", string(output), err))
// Validate that the package exists in the cache after running the test.
// That means that the setup command worked and the package resolved from Artifactory.
_, res, err := client.GetRemoteFileDetails(moduleCacheUrl, artHttpDetails)
if assert.NoError(t, err, "Failed to find the artifact in the cache: "+moduleCacheUrl) {
assert.Equal(t, http.StatusOK, res.StatusCode)
}
}
func prepareSetupTest(t *testing.T, packageManager project.ProjectType) func() {
homeDir, err := os.UserHomeDir()
assert.NoError(t, err)
var nugetConfigDir string
switch {
case io.IsWindows():
nugetConfigDir = filepath.Join("AppData", "Roaming")
case packageManager == project.Nuget:
nugetConfigDir = ".config"
default:
nugetConfigDir = ".nuget"
}
wd, err := os.Getwd()
assert.NoError(t, err)
restoreDir := clientTestUtils.ChangeDirWithCallback(t, wd, t.TempDir())
// Back up the existing NuGet.config and ensure restoration after the test.
restoreConfigFunc, err := ioutils.BackupFile(filepath.Join(homeDir, nugetConfigDir, "NuGet", "NuGet.Config"), packageManager.String()+".config.backup")
require.NoError(t, err)
if packageManager == project.Dotnet {
// Dotnet requires creating a new project to install packages.
assert.NoError(t, exec.Command(packageManager.String(), "new", "console").Run())
// Clear the NuGet cache to ensure the package is resolved from Artifactory.
assert.NoError(t, exec.Command(packageManager.String(), "nuget", "locals", "all", "--clear").Run())
// Remove the default nuget.org source to force resolving the package from Artifactory.
// We ignore the error since the source might not exist.
_ = exec.Command(packageManager.String(), "nuget", "remove", "source", "nuget.org").Run()
} else {
// Remove the default nuget.org source to force resolving the package from Artifactory.
// We ignore the error since the source might not exist.
_ = exec.Command(packageManager.String(), "sources", "remove", "-name", "nuget.org").Run()
}
return func() {
assert.NoError(t, restoreConfigFunc())
restoreDir()
}
}
// TestDotnetRequestedByDeterminism verifies that the RequestedBy paths in build-info
// are deterministic across multiple runs. This addresses the bug where dependencies
// could flip between "direct" and "transitive" attribution due to non-deterministic
// map iteration order in Go.
//
// The test uses the "determinism" project which has dependencies that are BOTH
// direct AND transitive (e.g., Newtonsoft.Json is a direct dep and also a transitive
// dep of NuGet.Core). This creates multiple RequestedBy paths that must be
// consistently sorted across runs.
func TestDotnetRequestedByDeterminism(t *testing.T) {
t.Skip("Skipping test temporarily")
initNugetTest(t)
const numRuns = 5
buildName := tests.DotnetBuildName + "-determinism"
// Use "determinism" project which has deps that are both direct AND transitive
projectPath := createNugetProject(t, "determinism")
err := createConfigFileForTest([]string{projectPath}, tests.NugetRemoteRepo, "", t, project.Dotnet, false)
require.NoError(t, err)
wd, err := os.Getwd()
require.NoError(t, err)
chdirCallback := clientTestUtils.ChangeDirWithCallback(t, wd, projectPath)
defer chdirCallback()
// Store RequestedBy maps from each run for comparison
var allRunsRequestedBy []map[string][][]string
for i := 1; i <= numRuns; i++ {
buildNumber := strconv.Itoa(i)
args := []string{dotnetUtils.DotnetCore.String(), "restore"}
allowInsecureConnectionForTests(&args)
args = append(args, "--build-name="+buildName, "--build-number="+buildNumber)
err := runNuGet(t, args...)
require.NoError(t, err, "Run %d failed", i)
// Publish build info
require.NoError(t, artifactoryCli.Exec("bp", buildName, buildNumber))
// Get published build info
publishedBuildInfo, found, err := tests.GetBuildInfo(serverDetails, buildName, buildNumber)
require.NoError(t, err)
require.True(t, found, "Build info not found for run %d", i)
bi := publishedBuildInfo.BuildInfo
require.NotEmpty(t, bi.Modules, "No modules in build info for run %d", i)
// Extract RequestedBy for each dependency
requestedByMap := make(map[string][][]string)
for _, module := range bi.Modules {
for _, dep := range module.Dependencies {
requestedByMap[dep.Id] = dep.RequestedBy
}
}
allRunsRequestedBy = append(allRunsRequestedBy, requestedByMap)
// Clean up this build number
inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, buildName, artHttpDetails)
}
// Compare all runs - they should be identical
firstRun := allRunsRequestedBy[0]
for i := 1; i < numRuns; i++ {
currentRun := allRunsRequestedBy[i]
// Check that all dependencies have the same RequestedBy paths
for depId, expectedRequestedBy := range firstRun {
actualRequestedBy, exists := currentRun[depId]
assert.True(t, exists, "Dependency %s missing in run %d", depId, i+1)
assert.Equal(t, expectedRequestedBy, actualRequestedBy,
"RequestedBy mismatch for %s between run 1 and run %d.\nExpected: %v\nActual: %v",
depId, i+1, expectedRequestedBy, actualRequestedBy)
}
// Also check for any extra dependencies in current run
for depId := range currentRun {
_, exists := firstRun[depId]
assert.True(t, exists, "Extra dependency %s found in run %d but not in run 1", depId, i+1)
}
}
// Verify that dependencies with multiple paths have direct path first
// Newtonsoft.Json should have direct path ["determinism"] before transitive paths
for depId, requestedBy := range firstRun {
if len(requestedBy) > 1 {
// Direct paths (length 1) should come before transitive paths (length > 1)
for j := 1; j < len(requestedBy); j++ {
assert.LessOrEqual(t, len(requestedBy[j-1]), len(requestedBy[j]),
"RequestedBy paths not sorted by length for %s: path %d has length %d, path %d has length %d",
depId, j-1, len(requestedBy[j-1]), j, len(requestedBy[j]))
}
t.Logf("Dependency %s has %d RequestedBy paths (verified sorted)", depId, len(requestedBy))
}
}
t.Logf("Successfully verified RequestedBy determinism across %d runs", numRuns)
cleanTestsHomeEnv()
}
// TestNugetBuildPublishWithCIVcsProps tests that CI VCS properties integration works
// with NuGet in a CI environment (GitHub Actions).
// Note: NuGet restore only has dependencies (not artifacts), so this test validates
// that build-info collection works correctly in CI environment. CI VCS properties
// are set on artifacts via build-publish when packages are pushed (nuget push).
func TestNugetBuildPublishWithCIVcsProps(t *testing.T) {
initNugetTest(t)
defer cleanTestsHomeEnv()
buildName := tests.NuGetBuildName + "-civcs"
buildNumber := "1"
// Setup GitHub Actions environment (uses real env vars on CI, mock values locally)
cleanupEnv, actualOrg, actualRepo := tests.SetupGitHubActionsEnv(t)
defer cleanupEnv()
// Clean old build
inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, buildName, artHttpDetails)
defer inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, buildName, artHttpDetails)
// Create NuGet project and config
projectPath := createNugetProject(t, "reference")
err := createConfigFileForTest([]string{projectPath}, tests.NugetRemoteRepo, "", t, project.Nuget, false)
require.NoError(t, err)
wd, err := os.Getwd()
require.NoError(t, err)
chdirCallback := clientTestUtils.ChangeDirWithCallback(t, wd, projectPath)
defer chdirCallback()
// Run NuGet restore with build info collection
args := []string{dotnetUtils.Nuget.String(), "restore"}
allowInsecureConnectionForTests(&args)
args = append(args, "--build-name="+buildName, "--build-number="+buildNumber)
err = runNuGet(t, args...)
require.NoError(t, err)
// Publish build info
require.NoError(t, artifactoryCli.Exec("bp", buildName, buildNumber))
// Get the published build info
publishedBuildInfo, found, err := tests.GetBuildInfo(serverDetails, buildName, buildNumber)
require.NoError(t, err)
require.True(t, found, "Build info was not found")
// Validate build info was created correctly
require.NotEmpty(t, publishedBuildInfo.BuildInfo.Modules, "Build info should have modules")
assert.Greater(t, len(publishedBuildInfo.BuildInfo.Modules[0].Dependencies), 0, "Build info should have dependencies")
assert.Equal(t, buildName, publishedBuildInfo.BuildInfo.Name, "Build name should match")
assert.Equal(t, buildNumber, publishedBuildInfo.BuildInfo.Number, "Build number should match")
// Verify CI environment was detected (for nuget push scenarios, CI VCS props would be set)
assert.NotEmpty(t, actualOrg, "CI org should be detected")
assert.NotEmpty(t, actualRepo, "CI repo should be detected")
t.Logf("NuGet build info created with %d dependencies in CI environment (org=%s, repo=%s)",
len(publishedBuildInfo.BuildInfo.Modules[0].Dependencies), actualOrg, actualRepo)
}