|
| 1 | +/* |
| 2 | + * (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.palantir.gradle.utils.circleciartifacts; |
| 18 | + |
| 19 | +import static com.palantir.gradle.testing.assertion.GradlePluginTestAssertions.assertThat; |
| 20 | + |
| 21 | +import com.palantir.gradle.testing.execution.GradleInvoker; |
| 22 | +import com.palantir.gradle.testing.execution.InvocationResult; |
| 23 | +import com.palantir.gradle.testing.files.Directory; |
| 24 | +import com.palantir.gradle.testing.files.gradle.GradleFile; |
| 25 | +import com.palantir.gradle.testing.junit.DisabledConfigurationCache; |
| 26 | +import com.palantir.gradle.testing.junit.GradlePluginTests; |
| 27 | +import com.palantir.gradle.testing.project.RootProject; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +@GradlePluginTests |
| 32 | +@DisabledConfigurationCache |
| 33 | +class CircleCiArtifactsTest { |
| 34 | + |
| 35 | + @BeforeEach |
| 36 | + void setup(RootProject project) { |
| 37 | + standardBuildFile(project); |
| 38 | + } |
| 39 | + |
| 40 | + private GradleFile standardBuildFile(RootProject project) { |
| 41 | + return project.buildGradle().overwrite(""" |
| 42 | + import com.palantir.gradle.utils.circleciartifacts.CircleCiArtifacts |
| 43 | +
|
| 44 | + public abstract class CircleCiArtifactsTask extends DefaultTask { |
| 45 | + @Nested |
| 46 | + abstract CircleCiArtifacts getCircleCiArtifacts(); |
| 47 | + } |
| 48 | +
|
| 49 | + tasks.register("printCircleCiLocation", CircleCiArtifactsTask) { |
| 50 | + doLast { task -> |
| 51 | + def artifactLocation = task.circleCiArtifacts.resolveArtifactLocation('location/in/artifacts') |
| 52 | + if (artifactLocation.isPresent()) { |
| 53 | + println "Physical path: ${artifactLocation.get().physicalPath()}" |
| 54 | + println "External location: ${artifactLocation.get().externalLocation()}" |
| 55 | + println "Circle link: ${artifactLocation.get().circleLink()}" |
| 56 | + } else { |
| 57 | + println "Not in Circle, empty artifact location" |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + """); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void can_use_circleci_artifacts_when_the_right_environment_variables_are_set( |
| 66 | + GradleInvoker gradle, RootProject project) { |
| 67 | + Directory fakeCircleArtifacts = |
| 68 | + project.directory("build/circle-artifacts").createDirectories(); |
| 69 | + project.gradlePropertiesFile() |
| 70 | + .appendProperty("__TESTING", "true") |
| 71 | + .appendProperty("__TESTING_CI", "true") |
| 72 | + .appendProperty( |
| 73 | + "__TESTING_CIRCLE_ARTIFACTS", |
| 74 | + project.path().relativize(fakeCircleArtifacts.path()).toString()) |
| 75 | + .appendProperty("__TESTING_CIRCLE_PROJECT_USERNAME", "palantir") |
| 76 | + .appendProperty("__TESTING_CIRCLE_PROJECT_REPONAME", "gradle-utils") |
| 77 | + .appendProperty("__TESTING_CIRCLE_BUILD_NUM", "1234") |
| 78 | + .appendProperty("__TESTING_CIRCLE_NODE_INDEX", "2345") |
| 79 | + .appendProperty("__TESTING_CIRCLE_BUILD_URL", "https://circleci.com/gh/palantir/gradle-utils/1234") |
| 80 | + .appendProperty("__TESTING_CIRCLE_WORKFLOW_JOB_ID", "abc-123-def-456"); |
| 81 | + |
| 82 | + InvocationResult result = gradle.withArgs("printCircleCiLocation").buildsSuccessfully(); |
| 83 | + |
| 84 | + assertThat(result) |
| 85 | + .output() |
| 86 | + .containsPattern("Physical path: .*/build/circle-artifacts/location/in/artifacts") |
| 87 | + .containsPattern( |
| 88 | + "External location: palantir/gradle-utils/1234/artifacts/2345/.*/location/in/artifacts") |
| 89 | + .containsPattern("Circle link:" |
| 90 | + + " https://circleci.com/output/job/abc-123-def-456/artifacts/2345/.*/location/in/artifacts"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void empty_property_if_were_not_in_circle(GradleInvoker gradle, RootProject project) { |
| 95 | + project.gradlePropertiesFile().appendProperty("__TESTING", "true"); |
| 96 | + |
| 97 | + InvocationResult result = gradle.withArgs("printCircleCiLocation").buildsSuccessfully(); |
| 98 | + |
| 99 | + assertThat(result).output().contains("Not in Circle, empty artifact location"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void handles_missing_circleci_url_gracefully(GradleInvoker gradle, RootProject project) { |
| 104 | + Directory fakeCircleArtifacts = |
| 105 | + project.directory("build/circle-artifacts").createDirectories(); |
| 106 | + project.gradlePropertiesFile() |
| 107 | + .appendProperty("__TESTING", "true") |
| 108 | + .appendProperty("__TESTING_CI", "true") |
| 109 | + .appendProperty( |
| 110 | + "__TESTING_CIRCLE_ARTIFACTS", |
| 111 | + project.path().relativize(fakeCircleArtifacts.path()).toString()) |
| 112 | + .appendProperty("__TESTING_CIRCLE_PROJECT_USERNAME", "palantir") |
| 113 | + .appendProperty("__TESTING_CIRCLE_PROJECT_REPONAME", "gradle-utils") |
| 114 | + .appendProperty("__TESTING_CIRCLE_BUILD_NUM", "1234") |
| 115 | + .appendProperty("__TESTING_CIRCLE_NODE_INDEX", "2345") |
| 116 | + .appendProperty("__TESTING_CIRCLE_WORKFLOW_JOB_ID", "abc-123-def-456"); |
| 117 | + |
| 118 | + InvocationResult result = gradle.withArgs("printCircleCiLocation").buildsSuccessfully(); |
| 119 | + |
| 120 | + assertThat(result) |
| 121 | + .output() |
| 122 | + .containsPattern("Physical path: .*/build/circle-artifacts/location/in/artifacts") |
| 123 | + .containsPattern( |
| 124 | + "External location: palantir/gradle-utils/1234/artifacts/2345/.*/location/in/artifacts") |
| 125 | + .containsPattern("Circle link:" |
| 126 | + + " https://<circle_url>/output/job/abc-123-def-456/artifacts/2345/.*/location/in/artifacts"); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void handles_custom_circle_home_directory_environment_variable(GradleInvoker gradle, RootProject project) { |
| 131 | + String customHome = "/custom/home/path/"; |
| 132 | + project.gradlePropertiesFile() |
| 133 | + .appendProperty("__TESTING", "true") |
| 134 | + .appendProperty("__TESTING_CI", "true") |
| 135 | + .appendProperty("__TESTING_CIRCLE_ARTIFACTS", customHome + "circle-artifacts") |
| 136 | + .appendProperty("__TESTING_CIRCLE_PROJECT_USERNAME", "palantir") |
| 137 | + .appendProperty("__TESTING_CIRCLE_PROJECT_REPONAME", "gradle-utils") |
| 138 | + .appendProperty("__TESTING_CIRCLE_BUILD_NUM", "1234") |
| 139 | + .appendProperty("__TESTING_CIRCLE_NODE_INDEX", "2345") |
| 140 | + .appendProperty("__TESTING_CIRCLE_BUILD_URL", "https://circleci.com/gh/palantir/gradle-utils/1234") |
| 141 | + .appendProperty("__TESTING_CIRCLE_WORKFLOW_JOB_ID", "abc-123-def-456") |
| 142 | + .appendProperty("__TESTING_CIRCLE_HOME_DIRECTORY", customHome); |
| 143 | + |
| 144 | + InvocationResult result = gradle.withArgs("printCircleCiLocation").buildsSuccessfully(); |
| 145 | + |
| 146 | + assertThat(result) |
| 147 | + .output() |
| 148 | + .contains("Physical path: " + customHome + "circle-artifacts/location/in/artifacts") |
| 149 | + .contains("External location:" |
| 150 | + + " palantir/gradle-utils/1234/artifacts/2345/~/circle-artifacts/location/in/artifacts") |
| 151 | + .contains( |
| 152 | + "Circle link:" |
| 153 | + + " https://circleci.com/output/job/abc-123-def-456/artifacts/2345/~/circle-artifacts/location/in/artifacts"); |
| 154 | + } |
| 155 | +} |
0 commit comments