Skip to content

Commit 8771afc

Browse files
committed
Include unit tests in dogfooding tests
1 parent e659a3f commit 8771afc

File tree

5 files changed

+119
-43
lines changed

5 files changed

+119
-43
lines changed

acceptance-tests/acceptance-tests-dogfood/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@
4848
<artifactId>slf4j-simple</artifactId>
4949
<scope>test</scope>
5050
</dependency>
51+
52+
<!--
53+
Stuff that JCT uses within tests, we need to copy these across to dogfood them correctly,
54+
since Maven won't pull them in for us.
55+
-->
56+
<dependency>
57+
<groupId>org.awaitility</groupId>
58+
<artifactId>awaitility</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>org.mockito</groupId>
64+
<artifactId>mockito-core</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>org.mockito</groupId>
70+
<artifactId>mockito-junit-jupiter</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
5174
</dependencies>
5275

5376
<build>

acceptance-tests/acceptance-tests-dogfood/src/test/java/io/github/ascopes/jct/acceptancetests/dogfood/JctCompilationConfigurer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void configure(JctCompiler<?, ?> compiler) {
3333
.failOnWarnings(false)
3434
.inheritClassPath(true)
3535
.inheritModulePath(true)
36+
.addCompilerOptions("-implicit:class")
3637
.showWarnings(false) // ignore spam about the testing module being hidden
3738
.locale(Locale.ENGLISH);
3839
}

acceptance-tests/acceptance-tests-dogfood/src/test/java/io/github/ascopes/jct/acceptancetests/dogfood/JctDogfoodTest.java

Lines changed: 91 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.IOException;
2424
import java.nio.file.Files;
2525
import java.nio.file.Path;
26+
import java.util.Set;
27+
import java.util.function.Function;
2628
import java.util.stream.Collectors;
2729
import org.junit.jupiter.api.DisplayName;
2830

@@ -34,6 +36,9 @@
3436
@DisplayName("JCT dogfood acceptance tests")
3537
class JctDogfoodTest {
3638

39+
static final String MAIN_MODULE = "io.github.ascopes.jct";
40+
static final String TEST_MODULE = "io.github.ascopes.jct.testing";
41+
3742
static final Path PROJECT_ROOT = Path.of(System.getProperty("user.dir"))
3843
.getParent()
3944
.getParent()
@@ -46,10 +51,19 @@ class JctDogfoodTest {
4651
.resolve("main")
4752
.resolve("java");
4853

54+
static final Path SRC_TEST_JAVA = PROJECT_ROOT
55+
.resolve("src")
56+
.resolve("test")
57+
.resolve("java");
58+
4959
static final Path TARGET_CLASSES = PROJECT_ROOT
5060
.resolve("target")
5161
.resolve("classes");
5262

63+
static final Path TARGET_TEST_CLASSES = PROJECT_ROOT
64+
.resolve("target")
65+
.resolve("test-classes");
66+
5367
@DisplayName("JCT can compile itself as a legacy module source")
5468
@JavacCompilerTest(minVersion = 11, configurers = JctCompilationConfigurer.class)
5569
void jctCanCompileItselfAsLegacyModule(JctCompiler<?, ?> compiler) throws IOException {
@@ -63,62 +77,96 @@ void jctCanCompileItselfAsLegacyModule(JctCompiler<?, ?> compiler) throws IOExce
6377
var compilation = compiler.compile(workspace);
6478

6579
// Then
66-
try (var walker = Files.walk(TARGET_CLASSES)) {
67-
var expectedFiles = walker
68-
.filter(Files::isRegularFile)
69-
.filter(file -> file.getFileName().endsWith(".class"))
70-
.map(TARGET_CLASSES::relativize)
71-
.map(Path::toString)
72-
.collect(Collectors.toSet());
73-
74-
assertThat(compilation)
75-
.isSuccessful();
76-
77-
assertThat(compilation)
78-
.classOutput()
79-
.packages()
80-
.allFilesExist(expectedFiles);
81-
}
80+
assertThat(compilation)
81+
.isSuccessful();
82+
83+
assertThat(compilation)
84+
.classOutput()
85+
.packages()
86+
.allFilesExist(getClassesFrom(TARGET_CLASSES, null));
8287
}
8388
}
8489

85-
@DisplayName("JCT can compile itself as a multiple-module source")
90+
@DisplayName("JCT can compile its unit tests as a legacy module source")
8691
@JavacCompilerTest(minVersion = 11, configurers = JctCompilationConfigurer.class)
87-
void jctCanCompileItselfAsMultiModule(JctCompiler<?, ?> compiler) throws IOException {
92+
void jctCanCompileUnitTestsAsLegacyModule(JctCompiler<?, ?> compiler) throws IOException {
8893
// Given
8994
try (var workspace = Workspaces.newWorkspace()) {
9095
workspace
91-
.createSourcePathModule("io.github.ascopes.jct")
96+
.createSourcePathPackage()
97+
.copyContentsFrom(SRC_TEST_JAVA);
98+
99+
// When
100+
var compilation = compiler.compile(workspace);
101+
102+
// Then
103+
assertThat(compilation)
104+
.isSuccessful();
105+
106+
assertThat(compilation)
107+
.classOutput()
108+
.packages()
109+
.allFilesExist(getClassesFrom(TARGET_TEST_CLASSES, null));
110+
}
111+
}
112+
113+
@DisplayName("JCT can compile itself and its unit tests as a multiple-module source")
114+
@JavacCompilerTest(minVersion = 11, configurers = JctCompilationConfigurer.class)
115+
void jctCanCompileItselfAndUnitTestsAsMultiModule(JctCompiler<?, ?> compiler) throws IOException {
116+
// Given
117+
try (var workspace = Workspaces.newWorkspace()) {
118+
workspace
119+
.createSourcePathModule(MAIN_MODULE)
92120
.copyContentsFrom(SRC_MAIN_JAVA);
121+
workspace
122+
.createSourcePathModule(TEST_MODULE)
123+
.copyContentsFrom(SRC_TEST_JAVA);
93124

94125
// When
95126
var compilation = compiler.compile(workspace);
96127

97128
// Then
98-
try (var walker = Files.walk(TARGET_CLASSES)) {
99-
var expectedFiles = walker
100-
.filter(Files::isRegularFile)
101-
.filter(file -> file.getFileName().endsWith(".class"))
102-
.map(TARGET_CLASSES::relativize)
103-
.map(Path::toString)
104-
.map("io.github.ascopes.jct/"::concat)
105-
.collect(Collectors.toSet());
106-
107-
assertThat(compilation)
108-
.isSuccessful();
109-
110-
assertThat(compilation)
111-
.diagnostics()
112-
.warnings()
113-
.filteredOn(diag -> !diag.getCode().equals("compiler.warn.module.not.found"))
114-
.withFailMessage("Expected no warnings (other than module.not.found)")
115-
.isEmpty();
116-
117-
assertThat(compilation)
118-
.classOutput()
119-
.packages()
120-
.allFilesExist(expectedFiles);
121-
}
129+
var expectedMainFiles = getClassesFrom(TARGET_CLASSES, MAIN_MODULE);
130+
var expectedTestFiles = getClassesFrom(TARGET_TEST_CLASSES, TEST_MODULE);
131+
132+
assertThat(compilation)
133+
.isSuccessful();
134+
135+
assertThat(compilation)
136+
.diagnostics()
137+
.warnings()
138+
.filteredOn(diag -> !diag.getCode().equals("compiler.warn.module.not.found"))
139+
.withFailMessage("Expected no warnings (other than module.not.found)")
140+
.isEmpty();
141+
142+
assertThat(compilation)
143+
.classOutput()
144+
.packages()
145+
.satisfies(
146+
packages -> assertThat(packages)
147+
.withFailMessage("Missing classes from main source root")
148+
.allFilesExist(expectedMainFiles),
149+
packages -> assertThat(packages)
150+
.withFailMessage("Missing classes from test source root")
151+
.allFilesExist(expectedTestFiles)
152+
);
153+
}
154+
}
155+
156+
private static Set<String> getClassesFrom(
157+
Path location,
158+
/* Nullable */ String moduleNamePrefix
159+
) throws IOException {
160+
try (var walker = Files.walk(location)) {
161+
return walker
162+
.filter(Files::isRegularFile)
163+
.filter(file -> file.getFileName().endsWith(".class"))
164+
.map(location::relativize)
165+
.map(Path::toString)
166+
.map(moduleNamePrefix == null
167+
? Function.identity()
168+
: (moduleNamePrefix + "/")::concat)
169+
.collect(Collectors.toSet());
122170
}
123171
}
124172
}

acceptance-tests/acceptance-tests-dogfood/src/test/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
open module io.github.ascopes.jct.acceptancetests.dogfood {
1717
requires io.github.ascopes.jct;
1818
requires java.compiler;
19+
requires org.assertj.core;
1920
requires transitive org.junit.jupiter.api;
2021
requires transitive org.junit.jupiter.engine;
2122
requires transitive org.junit.jupiter.params;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
junit.jupiter.execution.parallel.enabled=true
2+
junit.jupiter.execution.parallel.mode.classes.default=SAME_THREAD
3+
junit.jupiter.execution.parallel.mode.default=CONCURRENT

0 commit comments

Comments
 (0)