Skip to content

Commit

Permalink
Add setup command tests for Gradle (#2848)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi authored Feb 4, 2025
1 parent 08e8c38 commit 724f0c6
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
57 changes: 56 additions & 1 deletion gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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, "")
Expand All @@ -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()
}
}
20 changes: 20 additions & 0 deletions testdata/gradle/setupcmd/build.gradle
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 2 additions & 0 deletions testdata/gradle/setupcmd/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// settings.gradle.kts
rootProject.name = "test-gradle2"
11 changes: 11 additions & 0 deletions testdata/gradle/setupcmd/src/main/java/com/example/App.java
Original file line number Diff line number Diff line change
@@ -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 ");
}
}

0 comments on commit 724f0c6

Please sign in to comment.