|
| 1 | +/* |
| 2 | + * Copyright 2026 DiffPlug |
| 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.diffplug.gradle.spotless; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +class TargetExcludeDirectoryTest extends GradleIntegrationHarness { |
| 23 | + @Test |
| 24 | + void targetExcludeWithDirectory() throws IOException { |
| 25 | + setFile("build.gradle").toLines( |
| 26 | + "plugins { id 'com.diffplug.spotless' }", |
| 27 | + "spotless {", |
| 28 | + " format 'toLower', {", |
| 29 | + " target '**/*.md'", |
| 30 | + " targetExclude layout.projectDirectory.dir('generated')", |
| 31 | + " custom 'lowercase', { str -> str.toLowerCase() }", |
| 32 | + " }", |
| 33 | + "}"); |
| 34 | + setFile("generated/excluded.md").toLines("A B C"); |
| 35 | + setFile("generated/nested/excluded.md").toLines("A B C"); |
| 36 | + setFile("manual.md").toLines("A B C"); |
| 37 | + |
| 38 | + gradleRunner().withArguments("spotlessApply").build(); |
| 39 | + |
| 40 | + assertFile("generated/excluded.md").hasLines("A B C"); |
| 41 | + assertFile("generated/nested/excluded.md").hasLines("A B C"); |
| 42 | + assertFile("manual.md").hasLines("a b c"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void targetExcludeWithDirectoryProperty() throws IOException { |
| 47 | + setFile("build.gradle").toLines( |
| 48 | + "plugins { id 'com.diffplug.spotless' }", |
| 49 | + "def excluded = objects.directoryProperty()", |
| 50 | + "excluded.set(layout.projectDirectory.dir('generated'))", |
| 51 | + "spotless {", |
| 52 | + " format 'toLower', {", |
| 53 | + " target '**/*.md'", |
| 54 | + " targetExclude excluded", |
| 55 | + " custom 'lowercase', { str -> str.toLowerCase() }", |
| 56 | + " }", |
| 57 | + "}"); |
| 58 | + setFile("generated/excluded.md").toLines("A B C"); |
| 59 | + setFile("manual.md").toLines("A B C"); |
| 60 | + |
| 61 | + gradleRunner().withArguments("spotlessApply").build(); |
| 62 | + |
| 63 | + assertFile("generated/excluded.md").hasLines("A B C"); |
| 64 | + assertFile("manual.md").hasLines("a b c"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void targetExcludeWithDirectoryProvider() throws IOException { |
| 69 | + setFile("build.gradle").toLines( |
| 70 | + "plugins { id 'com.diffplug.spotless' }", |
| 71 | + "spotless {", |
| 72 | + " format 'toLower', {", |
| 73 | + " target '**/*.md'", |
| 74 | + " targetExclude providers.provider { layout.projectDirectory.dir('generated') }", |
| 75 | + " custom 'lowercase', { str -> str.toLowerCase() }", |
| 76 | + " }", |
| 77 | + "}"); |
| 78 | + setFile("generated/excluded.md").toLines("A B C"); |
| 79 | + setFile("manual.md").toLines("A B C"); |
| 80 | + |
| 81 | + gradleRunner().withArguments("spotlessApply").build(); |
| 82 | + |
| 83 | + assertFile("generated/excluded.md").hasLines("A B C"); |
| 84 | + assertFile("manual.md").hasLines("a b c"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void targetExcludeWithDirectoryAndConfigurationCache() throws IOException { |
| 89 | + setFile("gradle.properties").toLines("org.gradle.configuration-cache=true"); |
| 90 | + setFile("build.gradle").toLines( |
| 91 | + "plugins { id 'com.diffplug.spotless' }", |
| 92 | + "spotless {", |
| 93 | + " format 'toLower', {", |
| 94 | + " target '**/*.md'", |
| 95 | + " targetExclude layout.projectDirectory.dir('generated')", |
| 96 | + " custom 'lowercase', { str -> str.toLowerCase() }", |
| 97 | + " }", |
| 98 | + "}"); |
| 99 | + setFile("generated/excluded.md").toLines("A B C"); |
| 100 | + setFile("manual.md").toLines("A B C"); |
| 101 | + |
| 102 | + gradleRunner().withArguments("spotlessApply").build(); |
| 103 | + assertFile("generated/excluded.md").hasLines("A B C"); |
| 104 | + assertFile("manual.md").hasLines("a b c"); |
| 105 | + |
| 106 | + // the exclusion still applies on a second run with the cache enabled |
| 107 | + setFile("manual.md").toLines("D E F"); |
| 108 | + gradleRunner().withArguments("spotlessApply").build(); |
| 109 | + assertFile("generated/excluded.md").hasLines("A B C"); |
| 110 | + assertFile("manual.md").hasLines("d e f"); |
| 111 | + } |
| 112 | + |
| 113 | +} |