Skip to content

Commit

Permalink
added artifactory resolution test for Maven
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Dec 11, 2023
1 parent 60af265 commit b747013
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
6 changes: 6 additions & 0 deletions testdata/maven/mavenproject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion utils/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func GetNonVirtualRepositories() map[*string]string {
TestPip: {&PypiRemoteRepo},
TestPipenv: {&PipenvRemoteRepo},
TestPlugins: {&RtRepo1},
TestXray: {&NpmRemoteRepo, &NugetRemoteRepo, &YarnRemoteRepo, &GradleRemoteRepo},
TestXray: {&NpmRemoteRepo, &NugetRemoteRepo, &YarnRemoteRepo, &GradleRemoteRepo, &MvnRemoteRepo},
TestAccess: {&RtRepo1},
TestTransfer: {&RtRepo1, &RtRepo2, &MvnRepo1, &MvnRemoteRepo, &DockerRemoteRepo},
TestLifecycle: {&RtDevRepo, &RtProdRepo},
Expand Down
40 changes: 31 additions & 9 deletions xray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,12 +990,20 @@ func TestDependencyResolutionFromArtifactory(t *testing.T) {
projectType: artUtils.Yarn,
},
{
testProjectPath: []string{"gradle", "gradleproject"},
resolveRepoName: tests.GradleRemoteRepo,
cacheRepoName: tests.GradleRemoteRepo,
projectType: artUtils.Gradle,
},
*/

{
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,
},
}

Expand Down Expand Up @@ -1027,11 +1035,7 @@ 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)

if projectType == artUtils.Dotnet {
// In Nuget/Dotnet projects we need to clear local caches so we will resolve dependencies from Artifactory
_, err = exec.Command("dotnet", "nuget", "locals", "all", "--clear").CombinedOutput()
assert.NoError(t, err)
}
deleteLocalCacheIfNeeded(t, projectType)

// 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"))
Expand All @@ -1041,3 +1045,21 @@ func testSingleTechDependencyResolution(t *testing.T, testProjectPartialPath []s
// After the resolution from Artifactory, we verify whether the repository's cache is filled with artifacts.
assert.NotEqual(t, "[]\n", output)
}

// In order to ensure dependencies resolution from Artifactory, some package managers require deletion of their local cache
func deleteLocalCacheIfNeeded(t *testing.T, projectType artUtils.ProjectType) {
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)
}
}
}

0 comments on commit b747013

Please sign in to comment.