|
| 1 | +/* |
| 2 | + * (c) Copyright 2019 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 | +package com.palantir.javaformat.gradle; |
| 17 | + |
| 18 | +import static com.palantir.gradle.testing.assertion.GradlePluginTestAssertions.assertThat; |
| 19 | + |
| 20 | +import com.palantir.gradle.testing.execution.GradleInvoker; |
| 21 | +import com.palantir.gradle.testing.execution.InvocationResult; |
| 22 | +import com.palantir.gradle.testing.junit.DisabledConfigurationCache; |
| 23 | +import com.palantir.gradle.testing.junit.GradlePluginTests; |
| 24 | +import com.palantir.gradle.testing.project.RootProject; |
| 25 | +import java.io.File; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.ValueSource; |
| 29 | + |
| 30 | +@GradlePluginTests |
| 31 | +@DisabledConfigurationCache |
| 32 | +class SpotlessExcludesTest { |
| 33 | + |
| 34 | + private static final String CLASSPATH_FILE = new File("build/impl.classpath").getAbsolutePath(); |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setup(RootProject project) { |
| 38 | + project.buildGradle() |
| 39 | + .plugins() |
| 40 | + .add("java") |
| 41 | + .add("com.palantir.java-format") |
| 42 | + .add("com.diffplug.spotless"); |
| 43 | + |
| 44 | + project.buildGradle().append(""" |
| 45 | + dependencies { |
| 46 | + palantirJavaFormat files(file("%s").text.split(':')) |
| 47 | + } |
| 48 | + """, CLASSPATH_FILE); |
| 49 | + // Add jvm args to allow spotless and formatter gradle plugins to run with Java 16+ |
| 50 | + project.gradlePropertiesFile() |
| 51 | + .appendProperty( |
| 52 | + "org.gradle.jvmargs", |
| 53 | + "--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED " |
| 54 | + + "--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED " |
| 55 | + + "--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED " |
| 56 | + + "--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED " |
| 57 | + + "--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"); |
| 58 | + } |
| 59 | + |
| 60 | + @ParameterizedTest |
| 61 | + @ValueSource( |
| 62 | + strings = { |
| 63 | + "build/generated/java", |
| 64 | + "src/generated/java", |
| 65 | + "src/main/generated_testsrc", |
| 66 | + "src/main/generated_foo_testsrc", |
| 67 | + "generated_testSrc", |
| 68 | + "build/groovy-dsl-plugins/output" |
| 69 | + }) |
| 70 | + void format_ignores_excluded_directories(String srcDir, GradleInvoker gradle, RootProject project) { |
| 71 | + project.buildGradle().append(""" |
| 72 | + sourceSets { |
| 73 | + main { |
| 74 | + java { srcDir '%s' } |
| 75 | + } |
| 76 | + } |
| 77 | + """, srcDir); |
| 78 | + |
| 79 | + project.file(srcDir + "/test/Test.java").overwrite(""" |
| 80 | + package test; |
| 81 | + import java.lang.Void; |
| 82 | + public class Test { Void test() { return null; } } |
| 83 | + """); |
| 84 | + |
| 85 | + InvocationResult result = gradle.withArgs("spotlessJavaCheck").buildsSuccessfully(); |
| 86 | + |
| 87 | + assertThat(result).task(":spotlessJava").succeeded(); |
| 88 | + } |
| 89 | + |
| 90 | + @ParameterizedTest |
| 91 | + @ValueSource( |
| 92 | + strings = { |
| 93 | + "foo/generated_foo/src/bar", |
| 94 | + "src/main/java/test/generatedNamespace", |
| 95 | + }) |
| 96 | + void format_checks_non_generated_files(String srcDir, GradleInvoker gradle, RootProject project) { |
| 97 | + project.buildGradle().append(""" |
| 98 | + sourceSets { |
| 99 | + main { |
| 100 | + java { srcDir '%s' } |
| 101 | + } |
| 102 | + } |
| 103 | + """, srcDir); |
| 104 | + |
| 105 | + project.file(srcDir + "/Test.java").overwrite(""" |
| 106 | + package test; |
| 107 | + import java.lang.Void; |
| 108 | + public class Test { Void test() { return null; } } |
| 109 | + """); |
| 110 | + |
| 111 | + InvocationResult result = gradle.withArgs("spotlessJavaCheck").buildsWithFailure(); |
| 112 | + |
| 113 | + assertThat(result).task(":spotlessJava").succeeded(); |
| 114 | + } |
| 115 | +} |
0 commit comments