@@ -2,7 +2,15 @@ package main
22
33import (
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+
219257func 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+
230269func 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+ }
0 commit comments