|
| 1 | +/* |
| 2 | + * (c) Copyright 2018 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.baseline; |
| 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.gradle.GradleFile; |
| 24 | +import com.palantir.gradle.testing.junit.DisabledConfigurationCache; |
| 25 | +import com.palantir.gradle.testing.junit.GradlePluginTests; |
| 26 | +import com.palantir.gradle.testing.project.RootProject; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.UncheckedIOException; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.HashSet; |
| 33 | +import java.util.Set; |
| 34 | +import org.junit.jupiter.api.BeforeEach; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +@GradlePluginTests |
| 38 | +@DisabledConfigurationCache |
| 39 | +public class BaselineCircleCiIntegrationTest { |
| 40 | + |
| 41 | + private Path circleArtifactsDir; |
| 42 | + private Path circleTestReportsDir; |
| 43 | + |
| 44 | + private GradleFile standardBuildFile(RootProject rootProject) { |
| 45 | + rootProject.buildGradle().plugins().add("java-library").add("com.palantir.baseline-circleci"); |
| 46 | + |
| 47 | + return rootProject.buildGradle().append(""" |
| 48 | + repositories { |
| 49 | + mavenCentral() |
| 50 | + } |
| 51 | +
|
| 52 | + dependencies { |
| 53 | + testImplementation 'junit:junit:4.12' |
| 54 | + } |
| 55 | + """); |
| 56 | + } |
| 57 | + |
| 58 | + private static final String javaFile = """ |
| 59 | + package test; |
| 60 | +
|
| 61 | + import org.junit.Test; |
| 62 | +
|
| 63 | + public class TestClass { |
| 64 | + @Test |
| 65 | + public void test() {} |
| 66 | + } |
| 67 | + """; |
| 68 | + |
| 69 | + @BeforeEach |
| 70 | + void setup(RootProject rootProject) throws IOException { |
| 71 | + circleArtifactsDir = rootProject.path().resolve("circle-artifacts"); |
| 72 | + circleTestReportsDir = rootProject.path().resolve("circle-test-reports"); |
| 73 | + |
| 74 | + // Clean up directories if they exist |
| 75 | + if (Files.exists(circleArtifactsDir)) { |
| 76 | + deleteDirectory(circleArtifactsDir); |
| 77 | + } |
| 78 | + if (Files.exists(circleTestReportsDir)) { |
| 79 | + deleteDirectory(circleTestReportsDir); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void collects_junit_reports(GradleInvoker gradle, RootProject rootProject) throws IOException { |
| 85 | + standardBuildFile(rootProject); |
| 86 | + rootProject.testSourceSet().java().writeClass(javaFile); |
| 87 | + |
| 88 | + InvocationResult result = gradle.withArgs( |
| 89 | + "test", |
| 90 | + "-P__TESTING=true", |
| 91 | + "-P__TESTING_CIRCLE_TEST_REPORTS=" + circleTestReportsDir.toString()) |
| 92 | + .buildsSuccessfully(); |
| 93 | + |
| 94 | + assertThat(result).task(":test").succeeded(); |
| 95 | + |
| 96 | + Path junitReportDir = circleTestReportsDir.resolve("junit").resolve("test"); |
| 97 | + String[] files = junitReportDir.toFile().list(); |
| 98 | + Set<String> fileSet = new HashSet<>(Arrays.asList(files)); |
| 99 | + org.assertj.core.api.Assertions.assertThat(fileSet).isEqualTo(Set.of("TEST-test.TestClass.xml")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void collects_html_reports(GradleInvoker gradle, RootProject rootProject) throws IOException { |
| 104 | + standardBuildFile(rootProject); |
| 105 | + rootProject.testSourceSet().java().writeClass(javaFile); |
| 106 | + |
| 107 | + InvocationResult result = gradle.withArgs( |
| 108 | + "test", "-P__TESTING=true", "-P__TESTING_CIRCLE_ARTIFACTS=" + circleArtifactsDir.toString()) |
| 109 | + .buildsSuccessfully(); |
| 110 | + |
| 111 | + assertThat(result).task(":test").succeeded(); |
| 112 | + |
| 113 | + Path htmlReportDir = circleArtifactsDir.resolve("junit").resolve("test"); |
| 114 | + String[] files = htmlReportDir.toFile().list(); |
| 115 | + Set<String> fileSet = new HashSet<>(Arrays.asList(files)); |
| 116 | + org.assertj.core.api.Assertions.assertThat(fileSet) |
| 117 | + .isEqualTo(Set.of("classes", "css", "index.html", "js", "packages")); |
| 118 | + } |
| 119 | + |
| 120 | + private void deleteDirectory(Path directory) throws IOException { |
| 121 | + if (Files.exists(directory)) { |
| 122 | + Files.walk(directory) |
| 123 | + .sorted((a, b) -> b.compareTo(a)) // Reverse order to delete files before directories |
| 124 | + .forEach(path -> { |
| 125 | + try { |
| 126 | + Files.delete(path); |
| 127 | + } catch (IOException e) { |
| 128 | + throw new UncheckedIOException(e); |
| 129 | + } |
| 130 | + }); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments