Skip to content

Commit 8223905

Browse files
committed
chore: add test to verify execution layout
1 parent e51e5de commit 8223905

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2025 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.spotless.cli.core;
17+
18+
import java.nio.file.Path;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.mockito.Mockito;
22+
23+
import com.diffplug.spotless.ResourceHarness;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
class ExecutionLayoutTest extends ResourceHarness {
28+
29+
@Test
30+
void itResolvesGradleBuildDir() {
31+
setFile("settings.gradle").toLines("rootProject.name = 'test'");
32+
ExecutionLayout layout = ExecutionLayout.create(fileResolver(), commandLineStream());
33+
34+
Path buildDir = layout.buildDir();
35+
36+
assertThat(buildDir.toString())
37+
.matches("\\Q%s\\E.*\\Q%s\\E.*"
38+
.formatted(
39+
rootFolder().toPath().toString(),
40+
Path.of("build", "spotless-cli").toString()));
41+
}
42+
43+
@Test
44+
void itResolvesMavenBuildDir() {
45+
setFile("pom.xml").toLines("<project><modelVersion>4.0.0</modelVersion></project>");
46+
ExecutionLayout layout = ExecutionLayout.create(fileResolver(), commandLineStream());
47+
48+
Path buildDir = layout.buildDir();
49+
50+
assertThat(buildDir.toString())
51+
.matches("\\Q%s\\E.*\\Q%s\\E.*"
52+
.formatted(
53+
rootFolder().toPath().toString(),
54+
Path.of("target", "spotless-cli").toString()));
55+
}
56+
57+
@Test
58+
void itResolvesTmpBuildDir() {
59+
ExecutionLayout layout = ExecutionLayout.create(fileResolver(), commandLineStream());
60+
61+
Path buildDir = layout.buildDir();
62+
63+
assertThat(buildDir.toString())
64+
.doesNotStartWith(rootFolder().toPath().toString())
65+
.startsWith(System.getProperty("java.io.tmpdir"));
66+
}
67+
68+
private FileResolver fileResolver() {
69+
return new FileResolver(rootFolder().toPath());
70+
}
71+
72+
private SpotlessCommandLineStream commandLineStream() {
73+
return Mockito.mock(SpotlessCommandLineStream.class);
74+
}
75+
}

build-logic/src/main/groovy/buildlogic.java-common-conventions.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ plugins {
99
id 'java'
1010
id 'buildlogic.spotless-java-conventions'
1111
id 'buildlogic.java-selfie-tests-conventions'
12+
id 'buildlogic.java-test-mockito-conventions'
1213
}
1314

1415
repositories {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
configurations {
6+
mockitoAgent
7+
}
8+
dependencies {
9+
testImplementation(libs.mockito)
10+
mockitoAgent(libs.mockito) {
11+
transitive = false
12+
}
13+
}
14+
15+
tasks.withType(Test).configureEach { Test task ->
16+
task.jvmArgs("-javaagent:${configurations.mockitoAgent.asPath}")
17+
}

gradle/libs.versions.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ assertj-core = "3.27.3"
44
diff-utils = "1.3.0"
55
durian = "1.2.0"
66
junit = "5.8.1"
7+
mockito = "5.17.0"
78
native-include-googleJavaFormat = "1.24.0"
89
picocli = "4.7.6"
910
selfie = "2.5.1"
@@ -19,6 +20,7 @@ durian-io = { module = "com.diffplug.durian:durian-io", version.ref = "durian" }
1920
durian-collect = { module = "com.diffplug.durian:durian-collect", version.ref = "durian" }
2021
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit" }
2122
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" }
23+
mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" }
2224
native-include-googleJavaFormat = { module = "com.google.googlejavaformat:google-java-format", version.ref = "native-include-googleJavaFormat" }
2325
picocli = { module = "info.picocli:picocli", version.ref = "picocli" }
2426
picocli-codegen = { module = "info.picocli:picocli-codegen", version.ref = "picocli" }
@@ -34,4 +36,4 @@ native-includes = [
3436
"native-include-googleJavaFormat"
3537
]
3638
spotless-libs = ["spotless-lib", "spotless-lib-extra"]
37-
test-libs = ["assertj-core", "junit-jupiter-api", "selfie"]
39+
test-libs = ["assertj-core", "junit-jupiter-api", "selfie", "mockito"]

0 commit comments

Comments
 (0)