From 724f0c6a6b6e03f3d3d9f63383d2ff02646c20b9 Mon Sep 17 00:00:00 2001 From: Omer Zidkoni <50792403+omerzi@users.noreply.github.com> Date: Tue, 4 Feb 2025 11:24:06 +0200 Subject: [PATCH 1/2] Add setup command tests for Gradle (#2848) --- go.mod | 1 + go.sum | 4 +- gradle_test.go | 57 ++++++++++++++++++- testdata/gradle/setupcmd/build.gradle | 20 +++++++ testdata/gradle/setupcmd/settings.gradle | 2 + .../src/main/java/com/example/App.java | 11 ++++ 6 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 testdata/gradle/setupcmd/build.gradle create mode 100644 testdata/gradle/setupcmd/settings.gradle create mode 100644 testdata/gradle/setupcmd/src/main/java/com/example/App.java diff --git a/go.mod b/go.mod index 88e871e8d..3ee261584 100644 --- a/go.mod +++ b/go.mod @@ -79,6 +79,7 @@ require ( github.com/google/go-github/v56 v56.0.0 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/google/gofuzz v1.2.0 // indirect + github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gookit/color v1.5.4 // indirect github.com/grokify/mogo v0.64.12 // indirect diff --git a/go.sum b/go.sum index cb2442bca..b295d4609 100644 --- a/go.sum +++ b/go.sum @@ -147,8 +147,8 @@ github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y= -github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg= +github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSFBy+X1V0o+l+8NF1avt4HWl7cA= +github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/gradle_test.go b/gradle_test.go index 801647a20..84a060c15 100644 --- a/gradle_test.go +++ b/gradle_test.go @@ -2,7 +2,15 @@ package main import ( "errors" + "fmt" + "github.com/jfrog/gofrog/io" + "github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/gradle" + coretests "github.com/jfrog/jfrog-cli-core/v2/utils/tests" + "github.com/jfrog/jfrog-client-go/http/httpclient" + "github.com/stretchr/testify/require" + "net/http" "os" + "os/exec" "path/filepath" "strings" "testing" @@ -13,7 +21,6 @@ import ( clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests" buildinfo "github.com/jfrog/build-info-go/entities" - "github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/gradle" "github.com/jfrog/jfrog-cli-core/v2/common/build" commonCliUtils "github.com/jfrog/jfrog-cli-core/v2/common/cliutils" "github.com/jfrog/jfrog-cli-core/v2/common/commands" @@ -216,6 +223,37 @@ func TestGradleBuildWithServerIDWithUsesPlugin(t *testing.T) { cleanGradleTest(t) } +func TestSetupGradleCommand(t *testing.T) { + restoreFunc := prepareGradleSetupTest(t) + defer restoreFunc() + // Validate that the module does not exist in the cache before running the test. + client, err := httpclient.ClientBuilder().Build() + assert.NoError(t, err) + + // This module is part of the dependencies in the build.gradle file of the current test project. + // We want to ensure that it is not exist in the cache before running the build command. + moduleCacheUrl := serverDetails.ArtifactoryUrl + tests.GradleRemoteRepo + "-cache/com/fasterxml/jackson/core/jackson-core/2.13.2/jackson-core-2.13.2.jar" + _, _, err = client.GetRemoteFileDetails(moduleCacheUrl, artHttpDetails) + assert.ErrorContains(t, err, "404") + + jfrogCli := coretests.NewJfrogCli(execMain, "jfrog", "") + assert.NoError(t, execGo(jfrogCli, "setup", "gradle", "--repo="+tests.GradleRemoteRepo)) + + // Run `gradle clean` to resolve the artifact from Artifactory and force it to be downloaded. + output, err := exec.Command("gradle", + "clean", + "build", + "--info", + "--refresh-dependencies").CombinedOutput() + assert.NoError(t, err, fmt.Sprintf("%s\n%q", string(output), err)) + + // Validate that the module exists in the cache after running the build command. + _, 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 createGradleProject(t *testing.T, projectName string) string { srcBuildFile := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "gradle", projectName, "build.gradle") buildGradlePath, err := tests.ReplaceTemplateVariables(srcBuildFile, "") @@ -227,9 +265,26 @@ func createGradleProject(t *testing.T, projectName string) string { return buildGradlePath } + func initGradleTest(t *testing.T) { if !*tests.TestGradle { t.Skip("Skipping Gradle test. To run Gradle test add the '-test.gradle=true' option.") } createJfrogHomeConfig(t, true) } + +func prepareGradleSetupTest(t *testing.T) func() { + initGradleTest(t) + gradleHome := t.TempDir() + t.Setenv(gradle.UserHomeEnv, gradleHome) + wd, err := os.Getwd() + assert.NoError(t, err) + gradleProjectDir := t.TempDir() + err = io.CopyDir(filepath.Join(tests.GetTestResourcesPath(), "gradle", "setupcmd"), gradleProjectDir, true, nil) + require.NoError(t, err) + assert.NoError(t, os.Chdir(gradleProjectDir)) + restoreDir := clientTestUtils.ChangeDirWithCallback(t, wd, gradleProjectDir) + return func() { + restoreDir() + } +} diff --git a/testdata/gradle/setupcmd/build.gradle b/testdata/gradle/setupcmd/build.gradle new file mode 100644 index 000000000..eb7202b78 --- /dev/null +++ b/testdata/gradle/setupcmd/build.gradle @@ -0,0 +1,20 @@ +plugins { + id("java") +} + +allprojects { + repositories { + all { repo -> + // This check is for Gradle versions less than 8.0 + if (repo.hasProperty('allowInsecureProtocol')) { + // For Gradle 8.0 and later this flag is mandatory to allow running against local Artifactory instances + repo.allowInsecureProtocol = true + } + } + } +} + +dependencies { + implementation("com.fasterxml.jackson.core:jackson-databind:2.13.2") + implementation("org.apache.commons:commons-lang3:3.12.0") +} \ No newline at end of file diff --git a/testdata/gradle/setupcmd/settings.gradle b/testdata/gradle/setupcmd/settings.gradle new file mode 100644 index 000000000..0f87226f4 --- /dev/null +++ b/testdata/gradle/setupcmd/settings.gradle @@ -0,0 +1,2 @@ +// settings.gradle.kts +rootProject.name = "test-gradle2" \ No newline at end of file diff --git a/testdata/gradle/setupcmd/src/main/java/com/example/App.java b/testdata/gradle/setupcmd/src/main/java/com/example/App.java new file mode 100644 index 000000000..18d94470a --- /dev/null +++ b/testdata/gradle/setupcmd/src/main/java/com/example/App.java @@ -0,0 +1,11 @@ +package com.example; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang3.StringUtils; + +public class App { + public static void main(String[] args) { + ObjectMapper mapper = new ObjectMapper(); + String example = StringUtils.trim(" example "); + } +} \ No newline at end of file From 64c515ed9340f9cb90ff97769f5306bd4c769950 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Tue, 4 Feb 2025 15:17:17 +0200 Subject: [PATCH 2/2] Hide setup command from help (#2863) --- buildtools/cli.go | 2 ++ go.mod | 1 - go.sum | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/buildtools/cli.go b/buildtools/cli.go index ac8f55ef8..cb38e74ce 100644 --- a/buildtools/cli.go +++ b/buildtools/cli.go @@ -75,6 +75,8 @@ const ( func GetCommands() []cli.Command { return cliutils.GetSortedCommands(cli.CommandsByName{ { + // Currently, the setup command is hidden from the help menu, till it will be released as GA. + Hidden: true, Name: "setup", Flags: cliutils.GetCommandFlags(cliutils.Setup), Usage: setupdocs.GetDescription(), diff --git a/go.mod b/go.mod index 3ee261584..88e871e8d 100644 --- a/go.mod +++ b/go.mod @@ -79,7 +79,6 @@ require ( github.com/google/go-github/v56 v56.0.0 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/google/gofuzz v1.2.0 // indirect - github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gookit/color v1.5.4 // indirect github.com/grokify/mogo v0.64.12 // indirect diff --git a/go.sum b/go.sum index b295d4609..cb2442bca 100644 --- a/go.sum +++ b/go.sum @@ -147,8 +147,8 @@ github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSFBy+X1V0o+l+8NF1avt4HWl7cA= -github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144= +github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y= +github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=