Skip to content

Commit 724f0c6

Browse files
authored
Add setup command tests for Gradle (#2848)
1 parent 08e8c38 commit 724f0c6

File tree

6 files changed

+92
-3
lines changed

6 files changed

+92
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ require (
7979
github.com/google/go-github/v56 v56.0.0 // indirect
8080
github.com/google/go-querystring v1.1.0 // indirect
8181
github.com/google/gofuzz v1.2.0 // indirect
82+
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 // indirect
8283
github.com/google/uuid v1.6.0 // indirect
8384
github.com/gookit/color v1.5.4 // indirect
8485
github.com/grokify/mogo v0.64.12 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17
147147
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
148148
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
149149
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
150-
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y=
151-
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg=
150+
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSFBy+X1V0o+l+8NF1avt4HWl7cA=
151+
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
152152
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
153153
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
154154
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

gradle_test.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ package main
22

33
import (
44
"errors"
5+
"fmt"
6+
"github.com/jfrog/gofrog/io"
7+
"github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/gradle"
8+
coretests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
9+
"github.com/jfrog/jfrog-client-go/http/httpclient"
10+
"github.com/stretchr/testify/require"
11+
"net/http"
512
"os"
13+
"os/exec"
614
"path/filepath"
715
"strings"
816
"testing"
@@ -13,7 +21,6 @@ import (
1321
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
1422

1523
buildinfo "github.com/jfrog/build-info-go/entities"
16-
"github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/gradle"
1724
"github.com/jfrog/jfrog-cli-core/v2/common/build"
1825
commonCliUtils "github.com/jfrog/jfrog-cli-core/v2/common/cliutils"
1926
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
@@ -216,6 +223,37 @@ func TestGradleBuildWithServerIDWithUsesPlugin(t *testing.T) {
216223
cleanGradleTest(t)
217224
}
218225

226+
func TestSetupGradleCommand(t *testing.T) {
227+
restoreFunc := prepareGradleSetupTest(t)
228+
defer restoreFunc()
229+
// Validate that the module does not exist in the cache before running the test.
230+
client, err := httpclient.ClientBuilder().Build()
231+
assert.NoError(t, err)
232+
233+
// This module is part of the dependencies in the build.gradle file of the current test project.
234+
// We want to ensure that it is not exist in the cache before running the build command.
235+
moduleCacheUrl := serverDetails.ArtifactoryUrl + tests.GradleRemoteRepo + "-cache/com/fasterxml/jackson/core/jackson-core/2.13.2/jackson-core-2.13.2.jar"
236+
_, _, err = client.GetRemoteFileDetails(moduleCacheUrl, artHttpDetails)
237+
assert.ErrorContains(t, err, "404")
238+
239+
jfrogCli := coretests.NewJfrogCli(execMain, "jfrog", "")
240+
assert.NoError(t, execGo(jfrogCli, "setup", "gradle", "--repo="+tests.GradleRemoteRepo))
241+
242+
// Run `gradle clean` to resolve the artifact from Artifactory and force it to be downloaded.
243+
output, err := exec.Command("gradle",
244+
"clean",
245+
"build",
246+
"--info",
247+
"--refresh-dependencies").CombinedOutput()
248+
assert.NoError(t, err, fmt.Sprintf("%s\n%q", string(output), err))
249+
250+
// Validate that the module exists in the cache after running the build command.
251+
_, res, err := client.GetRemoteFileDetails(moduleCacheUrl, artHttpDetails)
252+
if assert.NoError(t, err, "Failed to find the artifact in the cache: "+moduleCacheUrl) {
253+
assert.Equal(t, http.StatusOK, res.StatusCode)
254+
}
255+
}
256+
219257
func createGradleProject(t *testing.T, projectName string) string {
220258
srcBuildFile := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "gradle", projectName, "build.gradle")
221259
buildGradlePath, err := tests.ReplaceTemplateVariables(srcBuildFile, "")
@@ -227,9 +265,26 @@ func createGradleProject(t *testing.T, projectName string) string {
227265

228266
return buildGradlePath
229267
}
268+
230269
func initGradleTest(t *testing.T) {
231270
if !*tests.TestGradle {
232271
t.Skip("Skipping Gradle test. To run Gradle test add the '-test.gradle=true' option.")
233272
}
234273
createJfrogHomeConfig(t, true)
235274
}
275+
276+
func prepareGradleSetupTest(t *testing.T) func() {
277+
initGradleTest(t)
278+
gradleHome := t.TempDir()
279+
t.Setenv(gradle.UserHomeEnv, gradleHome)
280+
wd, err := os.Getwd()
281+
assert.NoError(t, err)
282+
gradleProjectDir := t.TempDir()
283+
err = io.CopyDir(filepath.Join(tests.GetTestResourcesPath(), "gradle", "setupcmd"), gradleProjectDir, true, nil)
284+
require.NoError(t, err)
285+
assert.NoError(t, os.Chdir(gradleProjectDir))
286+
restoreDir := clientTestUtils.ChangeDirWithCallback(t, wd, gradleProjectDir)
287+
return func() {
288+
restoreDir()
289+
}
290+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
plugins {
2+
id("java")
3+
}
4+
5+
allprojects {
6+
repositories {
7+
all { repo ->
8+
// This check is for Gradle versions less than 8.0
9+
if (repo.hasProperty('allowInsecureProtocol')) {
10+
// For Gradle 8.0 and later this flag is mandatory to allow running against local Artifactory instances
11+
repo.allowInsecureProtocol = true
12+
}
13+
}
14+
}
15+
}
16+
17+
dependencies {
18+
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.2")
19+
implementation("org.apache.commons:commons-lang3:3.12.0")
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// settings.gradle.kts
2+
rootProject.name = "test-gradle2"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.apache.commons.lang3.StringUtils;
5+
6+
public class App {
7+
public static void main(String[] args) {
8+
ObjectMapper mapper = new ObjectMapper();
9+
String example = StringUtils.trim(" example ");
10+
}
11+
}

0 commit comments

Comments
 (0)