|
| 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.assertj.core.api.Assertions; |
| 21 | +import org.gradle.testkit.runner.GradleRunner; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import com.diffplug.common.base.StringPrinter; |
| 25 | + |
| 26 | +class IsolatedProjectTest extends GradleIntegrationHarness { |
| 27 | + private static final int N = 10; |
| 28 | + |
| 29 | + @Override |
| 30 | + public GradleRunner gradleRunner() throws IOException { |
| 31 | + setFile("gradle.properties").toContent("org.gradle.unsafe.isolated-projects=true"); |
| 32 | + return super.gradleRunner(); |
| 33 | + } |
| 34 | + |
| 35 | + private void createNSubprojects() throws IOException { |
| 36 | + for (int i = 0; i < N; i++) { |
| 37 | + createSubproject(Integer.toString(i)); |
| 38 | + } |
| 39 | + String settings = StringPrinter.buildString(printer -> { |
| 40 | + for (int i = 0; i < N; i++) { |
| 41 | + printer.println("include '" + i + "'"); |
| 42 | + } |
| 43 | + }); |
| 44 | + setFile("settings.gradle").toContent(settings); |
| 45 | + } |
| 46 | + |
| 47 | + void createSubproject(String name) throws IOException { |
| 48 | + setFile(name + "/build.gradle").toLines( |
| 49 | + "plugins {", |
| 50 | + " id 'com.diffplug.spotless'", |
| 51 | + "}", |
| 52 | + "repositories { mavenCentral() }", |
| 53 | + "spotless {", |
| 54 | + " kotlin {", |
| 55 | + " target file('Test.kt')", |
| 56 | + " ktlint()", |
| 57 | + " }", |
| 58 | + "}"); |
| 59 | + setFile(name + "/Test.kt").toResource("kotlin/ktlint/basic.dirty"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void rootIsSupported() throws IOException { |
| 64 | + setFile("build.gradle").toLines( |
| 65 | + "plugins {", |
| 66 | + " id 'com.diffplug.spotless'", |
| 67 | + "}", |
| 68 | + "repositories { mavenCentral() }", |
| 69 | + "spotless {", |
| 70 | + " kotlin {", |
| 71 | + " target file('Test.kt')", |
| 72 | + " ktlint()", |
| 73 | + " }", |
| 74 | + "}"); |
| 75 | + setFile("Test.kt").toResource("kotlin/ktlint/basic.dirty"); |
| 76 | + createNSubprojects(); |
| 77 | + gradleRunner().withArguments("spotlessApply").build(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void noRootIsSupported() throws IOException { |
| 82 | + setFile("build.gradle").toLines(); |
| 83 | + createNSubprojects(); |
| 84 | + gradleRunner().withArguments("spotlessApply").build(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void predeclaredIsUnsupported() throws IOException { |
| 89 | + setFile("build.gradle").toLines( |
| 90 | + "plugins {", |
| 91 | + " id 'com.diffplug.spotless'", |
| 92 | + "}", |
| 93 | + "repositories { mavenCentral() }", |
| 94 | + "spotless { predeclareDeps() }", |
| 95 | + "spotlessPredeclare {", |
| 96 | + " kotlin { ktlint() }", |
| 97 | + "}"); |
| 98 | + createNSubprojects(); |
| 99 | + Assertions.assertThat(gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput()) |
| 100 | + .containsAnyOf("Cannot access project", "cannot access 'Project.tasks'"); |
| 101 | + } |
| 102 | +} |
0 commit comments