diff --git a/testdata/go/simple-project/go.mod b/testdata/go/simple-project/go.mod new file mode 100644 index 000000000..309e9d797 --- /dev/null +++ b/testdata/go/simple-project/go.mod @@ -0,0 +1,9 @@ +module github.com/you/hello + +go 1.20 + +require rsc.io/quote v1.5.2 + +require ( + rsc.io/sampler v1.3.0 // indirect +) diff --git a/testdata/go/simple-project/go.sum b/testdata/go/simple-project/go.sum new file mode 100644 index 000000000..4a8bcd704 --- /dev/null +++ b/testdata/go/simple-project/go.sum @@ -0,0 +1,6 @@ +golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8= +golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y= +rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0= +rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4= +rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/testdata/go/simple-project/hello.go b/testdata/go/simple-project/hello.go new file mode 100644 index 000000000..0a29866c4 --- /dev/null +++ b/testdata/go/simple-project/hello.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + + "rsc.io/quote" +) + +func main() { + fmt.Println(quote.Hello()) +} diff --git a/xray_test.go b/xray_test.go index e3d3a6936..797f7e565 100644 --- a/xray_test.go +++ b/xray_test.go @@ -47,6 +47,12 @@ import ( "github.com/urfave/cli" ) +const ( + jvmLaunchEnvVar = "MAVEN_OPTS" + mavenCacheRedirectionVal = "-Dmaven.repo.local=" + goCacheEnvVar = "GOMODCACHE" +) + var ( xrayDetails *config.ServerDetails xrayAuth auth.ServiceDetails @@ -970,39 +976,36 @@ func TestDependencyResolutionFromArtifactory(t *testing.T) { cacheRepoName string projectType artUtils.ProjectType }{ - /* - { - testProjectPath: []string{"npm", "npmproject"}, - resolveRepoName: tests.NpmRemoteRepo, - cacheRepoName: tests.NpmRemoteRepo, - projectType: artUtils.Npm, - }, - { - testProjectPath: []string{"nuget", "simple-dotnet"}, - resolveRepoName: tests.NugetRemoteRepo, - cacheRepoName: tests.NugetRemoteRepo, - projectType: artUtils.Dotnet, - }, - { - testProjectPath: []string{"yarn", "yarnproject"}, - resolveRepoName: tests.YarnRemoteRepo, - cacheRepoName: tests.YarnRemoteRepo, - projectType: artUtils.Yarn, - }, - { - testProjectPath: []string{"gradle", "gradleproject"}, - resolveRepoName: tests.GradleRemoteRepo, - cacheRepoName: tests.GradleRemoteRepo, - projectType: artUtils.Gradle, - }, - { - testProjectPath: []string{"maven", "mavenproject"}, - resolveRepoName: tests.MvnRemoteRepo, - cacheRepoName: tests.MvnRemoteRepo, - projectType: artUtils.Maven, - }, - - */ + { + testProjectPath: []string{"npm", "npmproject"}, + resolveRepoName: tests.NpmRemoteRepo, + cacheRepoName: tests.NpmRemoteRepo, + projectType: artUtils.Npm, + }, + { + testProjectPath: []string{"nuget", "simple-dotnet"}, + resolveRepoName: tests.NugetRemoteRepo, + cacheRepoName: tests.NugetRemoteRepo, + projectType: artUtils.Dotnet, + }, + { + testProjectPath: []string{"yarn", "yarnproject"}, + resolveRepoName: tests.YarnRemoteRepo, + cacheRepoName: tests.YarnRemoteRepo, + projectType: artUtils.Yarn, + }, + { + testProjectPath: []string{"gradle", "gradleproject"}, + resolveRepoName: tests.GradleRemoteRepo, + cacheRepoName: tests.GradleRemoteRepo, + projectType: artUtils.Gradle, + }, + { + testProjectPath: []string{"maven", "mavenproject"}, + resolveRepoName: tests.MvnRemoteRepo, + cacheRepoName: tests.MvnRemoteRepo, + projectType: artUtils.Maven, + }, { testProjectPath: []string{"go", "simple-project"}, resolveRepoName: tests.GoVirtualRepo, @@ -1041,7 +1044,12 @@ func testSingleTechDependencyResolution(t *testing.T, testProjectPartialPath []s // Before the resolution from Artifactory, we verify whether the repository's cache is empty. assert.Equal(t, "[]\n", output) - clearLocalCacheIfNeeded(t, projectType) + callbackFunc := clearOrRedirectLocalCacheIfNeeded(t, projectType) + if callbackFunc != nil { + defer func() { + assert.NoError(t, callbackFunc()) + }() + } // We execute 'audit' command on a project that hasn't been installed. With the Artifactory server and repository configuration, our expectation is that dependencies will be resolved from there assert.NoError(t, xrayCli.Exec("audit")) @@ -1050,35 +1058,32 @@ func testSingleTechDependencyResolution(t *testing.T, testProjectPartialPath []s output = artifactoryCli.RunCliCmdWithOutput(t, "s", artifactoryPathToSearch, "--fail-no-op") // After the resolution from Artifactory, we verify whether the repository's cache is filled with artifacts. assert.NotEqual(t, "[]\n", output) - // Change for push } // In order to ensure dependencies resolution from Artifactory, some package managers require deletion of their local cache -func clearLocalCacheIfNeeded(t *testing.T, projectType artUtils.ProjectType) { +func clearOrRedirectLocalCacheIfNeeded(t *testing.T, projectType artUtils.ProjectType) (callbackFunc func() error) { switch projectType { case artUtils.Dotnet: _, err := exec.Command("dotnet", "nuget", "locals", "all", "--clear").CombinedOutput() assert.NoError(t, err) case artUtils.Maven: - homeDir := fileutils.GetHomeDir() - mvnCacheFullPath := filepath.Join(homeDir, ".m2", "repository") - cacheExists, err := fileutils.IsDirExists(mvnCacheFullPath, false) - assert.NoError(t, err) - if cacheExists { - err = os.RemoveAll(mvnCacheFullPath) - assert.NoError(t, err) + mavenCacheTempPath, callback := coretests.CreateTempDirWithCallbackAndAssert(t) + clientTestUtils.SetEnvAndAssert(t, jvmLaunchEnvVar, mavenCacheRedirectionVal+mavenCacheTempPath) + callbackFunc = func() error { + callback() + return os.Unsetenv(jvmLaunchEnvVar) } case artUtils.Go: - // In Go, we don't want to clear the entire cache so we delete a specific cached package in order to re-download it from Artifactory - homeDir := fileutils.GetHomeDir() - cachedPackagePath := filepath.Join(homeDir, "go", "pkg", "mod", "cache", "download", "rsc.io") - cachedPackagePathExists, err := fileutils.IsDirExists(cachedPackagePath, false) + goTempCachePath, err := fileutils.CreateTempDir() assert.NoError(t, err) - if cachedPackagePathExists { - err = os.RemoveAll(cachedPackagePath) - assert.NoError(t, err) + clientTestUtils.SetEnvAndAssert(t, goCacheEnvVar, goTempCachePath) + callbackFunc = func() error { + clientTestUtils.UnSetEnvAndAssert(t, "GOMODCACHE") + err = os.RemoveAll(goTempCachePath) + return nil } case artUtils.Pip: - // TODO ass command 'pip cache purge' + // TODO add command 'pip cache purge' } + return }