forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuget_test.go
167 lines (148 loc) · 5.77 KB
/
nuget_test.go
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
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/jfrog/jfrog-cli-go/artifactory/utils"
"github.com/jfrog/jfrog-cli-go/inttestutils"
"github.com/jfrog/jfrog-cli-go/utils/cliutils"
"github.com/jfrog/jfrog-cli-go/utils/config"
"github.com/jfrog/jfrog-cli-go/utils/tests"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
)
func initNugetTest(t *testing.T) {
if !*tests.TestNuget {
t.Skip("Skipping NuGet test. To run Nuget test add the '-test.nuget=true' option.")
}
if !cliutils.IsWindows() {
t.Skip("Skipping nuget tests, since this is not a Windows machine.")
}
// This is due to Artifactory bug, we cant create remote repository with REST API.
if !isRepoExist(tests.NugetRemoteRepo) {
t.Error("Create nuget remote repository:", tests.NugetRemoteRepo, "in order to run nuget tests")
t.FailNow()
}
createJfrogHomeConfig(t)
}
func TestNugetResolve(t *testing.T) {
initNugetTest(t)
projects := []struct {
name string
project string
moduleId string
args []string
expectedDependencies int
}{
{"packagesconfigwithoutmodulechnage", "packagesconfig", "packagesconfig", []string{"nuget", "restore", tests.NugetRemoteRepo, "--build-name=" + tests.NugetBuildName}, 6},
{"packagesconfigwithmodulechnage", "packagesconfig", ModuleNameJFrogTest, []string{"nuget", "restore", tests.NugetRemoteRepo, "--build-name=" + tests.NugetBuildName, "--module=" + ModuleNameJFrogTest}, 6},
{"referencewithoutmodulechnage", "reference", "reference", []string{"nuget", "restore", tests.NugetRemoteRepo, "--build-name=" + tests.NugetBuildName}, 6},
{"referencewithmodulechnage", "reference", ModuleNameJFrogTest, []string{"nuget", "restore", tests.NugetRemoteRepo, "--build-name=" + tests.NugetBuildName, "--module=" + ModuleNameJFrogTest}, 6},
}
for buildNumber, test := range projects {
t.Run(test.project, func(t *testing.T) {
testNugetCmd(t, createNugetProject(t, test.project), strconv.Itoa(buildNumber), test.moduleId, test.expectedDependencies, test.args, false)
})
}
cleanBuildToolsTest()
}
func TestNativeNugetResolve(t *testing.T) {
initNugetTest(t)
projects := []struct {
name string
project string
moduleId string
args []string
expectedDependencies int
}{
{"packagesconfigwithoutmodulechnage", "packagesconfig", "packagesconfig", []string{"nuget", "restore", "--build-name=" + tests.NugetBuildName}, 6},
{"packagesconfigwithmodulechnage", "packagesconfig", ModuleNameJFrogTest, []string{"nuget", "restore", "--build-name=" + tests.NugetBuildName, "--module=" + ModuleNameJFrogTest}, 6},
{"referencewithoutmodulechnage", "reference", "reference", []string{"nuget", "restore", "--build-name=" + tests.NugetBuildName}, 6},
{"referencewithmodulechnage", "reference", ModuleNameJFrogTest, []string{"nuget", "restore", "--build-name=" + tests.NugetBuildName, "--module=" + ModuleNameJFrogTest}, 6},
}
for buildNumber, test := range projects {
projectPath := createNugetProject(t, test.project)
err := testCreateConfFile([]string{projectPath}, tests.NugetRemoteRepo, "", t, utils.Nuget, false)
if err != nil {
t.Error(err)
}
t.Run(test.project, func(t *testing.T) {
testNugetCmd(t, projectPath, strconv.Itoa(buildNumber), test.moduleId, test.expectedDependencies, test.args, true)
})
}
cleanBuildToolsTest()
}
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)
if err != nil {
t.Error(err)
}
files, err := fileutils.ListFiles(projectSrc, false)
if err != nil {
t.Error(err)
}
for _, v := range files {
err = fileutils.CopyFile(projectTarget, v)
if err != nil {
t.Error(err)
}
}
return projectTarget
}
func TestNuGetWithGlobalConfig(t *testing.T) {
initNugetTest(t)
projectPath := createNugetProject(t, "packagesconfig")
jfrogHomeDir, err := config.GetJfrogHomeDir()
if err != nil {
t.Error(err)
}
err = testCreateConfFile([]string{jfrogHomeDir}, tests.NugetRemoteRepo, "", t, utils.Nuget, true)
if err != nil {
t.Error(err)
}
testNugetCmd(t, projectPath, "1", "packagesconfig", 6, []string{"nuget", "restore", "--build-name=" + tests.NugetBuildName}, true)
cleanBuildToolsTest()
}
func testNugetCmd(t *testing.T, projectPath, buildNumber, module string, expectedDependencies int, args []string, native bool) {
wd, err := os.Getwd()
if err != nil {
t.Error(err)
}
err = os.Chdir(projectPath)
if err != nil {
t.Error(err)
}
args = append(args, "--build-number="+buildNumber)
if native {
runNuGet(t, args...)
} else {
artifactoryCli.Exec(args...)
}
artifactoryCli.Exec("bp", tests.NugetBuildName, buildNumber)
buildInfo := inttestutils.GetBuildInfo(artifactoryDetails.Url, tests.NugetBuildName, buildNumber, t, artHttpDetails)
if buildInfo.Modules == nil || len(buildInfo.Modules) == 0 {
t.Error("Nuget build info was not generated correctly, no modules were created.")
}
if expectedDependencies != len(buildInfo.Modules[0].Dependencies) {
t.Error("Incorrect number of artifacts found in the build-info, expected:", expectedDependencies, " Found:", len(buildInfo.Modules[0].Dependencies))
}
if module != buildInfo.Modules[0].Id {
t.Error(fmt.Errorf("Expected module name %s, got %s", module, buildInfo.Modules[0].Id))
}
err = os.Chdir(wd)
if err != nil {
t.Error(err)
}
// cleanup
inttestutils.DeleteBuild(artifactoryDetails.Url, tests.NugetBuildName, artHttpDetails)
}
func runNuGet(t *testing.T, args ...string) {
artifactoryNuGetCli := tests.NewJfrogCli(execMain, "jfrog rt", "")
err := artifactoryNuGetCli.Exec(args...)
if err != nil {
t.Error(err)
}
}