github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/gradle_test.go (about) 1 package main 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/jfrog/jfrog-cli-go/inttestutils" 11 "github.com/jfrog/jfrog-cli-go/utils/tests" 12 ) 13 14 const gradleFlagName = "gradle" 15 16 func TestGradleBuildWithServerID(t *testing.T) { 17 initGradleTest(t) 18 19 buildGradlePath := createGradleProject(t, "gradleproject") 20 configFilePath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "buildspecs", tests.GradleServerIDConfig) 21 configFilePath, err := tests.ReplaceTemplateVariables(configFilePath, "") 22 assert.NoError(t, err) 23 buildName := "gradle-cli" 24 buildNumber := "1" 25 runAndValidateGradle(buildGradlePath, configFilePath, buildName, buildNumber, t) 26 artifactoryCli.Exec("bp", buildName, buildNumber) 27 buildInfo := inttestutils.GetBuildInfo(artifactoryDetails.Url, buildName, buildNumber, t, artHttpDetails) 28 validateBuildInfo(buildInfo, t, 0, 1, ":minimal-example:1.0") 29 30 cleanBuildToolsTest() 31 } 32 33 func TestNativeGradleBuildWithServerID(t *testing.T) { 34 initGradleTest(t) 35 buildGradlePath := createGradleProject(t, "gradleproject") 36 configFilePath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "buildspecs", tests.GradleConfig) 37 destPath := filepath.Join(filepath.Dir(buildGradlePath), ".jfrog", "projects") 38 createConfigFile(destPath, configFilePath, t) 39 oldHomeDir := changeWD(t, filepath.Dir(buildGradlePath)) 40 buildName := "gradle-cli" 41 buildNumber := "1" 42 buildGradlePath = strings.Replace(buildGradlePath, `\`, "/", -1) // Windows compatibility. 43 runCli(t, "gradle", "clean artifactoryPublish", "-b"+buildGradlePath, "--build-name="+buildName, "--build-number="+buildNumber) 44 err := os.Chdir(oldHomeDir) 45 assert.NoError(t, err) 46 // Validate 47 searchSpec, err := tests.CreateSpec(tests.SearchAllRepo1) 48 assert.NoError(t, err) 49 verifyExistInArtifactory(tests.GetGradleDeployedArtifacts(), searchSpec, t) 50 verifyExistInArtifactoryByProps(tests.GetGradleDeployedArtifacts(), tests.Repo1+"/*", "build.name="+buildName+";build.number="+buildNumber, t) 51 artifactoryCli.Exec("bp", buildName, buildNumber) 52 buildInfo := inttestutils.GetBuildInfo(artifactoryDetails.Url, buildName, buildNumber, t, artHttpDetails) 53 validateBuildInfo(buildInfo, t, 0, 1, ":minimal-example:1.0") 54 cleanBuildToolsTest() 55 } 56 57 func TestGradleBuildWithServerIDWithUsesPlugin(t *testing.T) { 58 initGradleTest(t) 59 60 buildGradlePath := createGradleProject(t, "projectwithplugin") 61 configFilePath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "buildspecs", tests.GradleServerIDUsesPluginConfig) 62 configFilePath, err := tests.ReplaceTemplateVariables(configFilePath, "") 63 assert.NoError(t, err) 64 buildName := "gradle-cli" 65 buildNumber := "1" 66 runAndValidateGradle(buildGradlePath, configFilePath, buildName, buildNumber, t) 67 68 artifactoryCli.Exec("bp", buildName, buildNumber) 69 buildInfo := inttestutils.GetBuildInfo(artifactoryDetails.Url, buildName, buildNumber, t, artHttpDetails) 70 validateBuildInfo(buildInfo, t, 0, 1, ":minimal-example:1.0") 71 cleanBuildToolsTest() 72 } 73 74 func TestGradleBuildWithCredentials(t *testing.T) { 75 if *tests.RtUser == "" || *tests.RtPassword == "" { 76 t.SkipNow() 77 } 78 79 initGradleTest(t) 80 81 buildName := "gradle-cli" 82 buildNumber := "1" 83 buildGradlePath := createGradleProject(t, "gradleproject") 84 srcConfigTemplate := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "buildspecs", tests.GradleUsernamePasswordTemplate) 85 configFilePath, err := tests.ReplaceTemplateVariables(srcConfigTemplate, "") 86 assert.NoError(t, err) 87 88 runAndValidateGradle(buildGradlePath, configFilePath, buildName, buildNumber, t) 89 artifactoryCli.Exec("bp", buildName, buildNumber) 90 buildInfo := inttestutils.GetBuildInfo(artifactoryDetails.Url, buildName, buildNumber, t, artHttpDetails) 91 validateBuildInfo(buildInfo, t, 0, 1, ":minimal-example:1.0") 92 cleanBuildToolsTest() 93 } 94 95 func runAndValidateGradle(buildGradlePath, configFilePath, buildName, buildNumber string, t *testing.T) { 96 runCliWithLegacyBuildtoolsCmd(t, "gradle", "clean artifactoryPublish -b "+buildGradlePath, configFilePath, "--build-name="+buildName, "--build-number="+buildNumber) 97 searchSpec, err := tests.CreateSpec(tests.SearchAllRepo1) 98 assert.NoError(t, err) 99 100 verifyExistInArtifactory(tests.GetGradleDeployedArtifacts(), searchSpec, t) 101 verifyExistInArtifactoryByProps(tests.GetGradleDeployedArtifacts(), tests.Repo1+"/*", "build.name="+buildName+";build.number="+buildNumber, t) 102 } 103 104 func createGradleProject(t *testing.T, projectName string) string { 105 srcBuildFile := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "gradle", projectName, "build.gradle") 106 buildGradlePath, err := tests.ReplaceTemplateVariables(srcBuildFile, "") 107 assert.NoError(t, err) 108 109 srcSettingsFile := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "gradle", projectName, "settings.gradle") 110 _, err = tests.ReplaceTemplateVariables(srcSettingsFile, "") 111 assert.NoError(t, err) 112 113 return buildGradlePath 114 } 115 func initGradleTest(t *testing.T) { 116 if !*tests.TestGradle { 117 t.Skip("Skipping Gradle test. To run Gradle test add the '-test.gradle=true' option.") 118 } 119 createJfrogHomeConfig(t) 120 }