diff --git a/build-logic/src/main/java/dev/caskeleton/buildlogic/architecture/ArchitecturePlugin.java b/build-logic/src/main/java/dev/caskeleton/buildlogic/architecture/ArchitecturePlugin.java index 4e5491b4..34e2d3e7 100644 --- a/build-logic/src/main/java/dev/caskeleton/buildlogic/architecture/ArchitecturePlugin.java +++ b/build-logic/src/main/java/dev/caskeleton/buildlogic/architecture/ArchitecturePlugin.java @@ -26,6 +26,11 @@ public final class ArchitecturePlugin implements Plugin { private static final List APPLICATION_CLASSPATHS = List.of( "compileClasspath", "runtimeClasspath", "testCompileClasspath", "testRuntimeClasspath"); + // Present only where a pure module applies java-test-fixtures. The test classpaths above see + // testFixturesImplementation, but not testFixturesCompileOnly: a framework declared there + // compiled into the shared fixtures and passed the purity check. + private static final List TEST_FIXTURES_CLASSPATHS = + List.of("testFixturesCompileClasspath", "testFixturesRuntimeClasspath"); @Override public void apply(Project project) { @@ -171,6 +176,13 @@ public final class ArchitecturePlugin implements Plugin { .getResolutionResult() .getRootComponent()); } + for (String name : TEST_FIXTURES_CLASSPATHS) { + Configuration fixtures = application.getConfigurations().findByName(name); + if (fixtures != null) { + classpathRoots.put( + name, fixtures.getIncoming().getResolutionResult().getRootComponent()); + } + } String applicationPath = module.gradlePath(); application .getTasks() diff --git a/build-logic/src/test/java/dev/caskeleton/buildlogic/architecture/PureModuleTestFixturesPurityFunctionalTest.java b/build-logic/src/test/java/dev/caskeleton/buildlogic/architecture/PureModuleTestFixturesPurityFunctionalTest.java new file mode 100644 index 00000000..00517d81 --- /dev/null +++ b/build-logic/src/test/java/dev/caskeleton/buildlogic/architecture/PureModuleTestFixturesPurityFunctionalTest.java @@ -0,0 +1,125 @@ +package dev.caskeleton.buildlogic.architecture; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** The purity check covers a pure module's test fixtures, not only its main and test classpaths. */ +class PureModuleTestFixturesPurityFunctionalTest { + + @Test + @DisplayName("순수 모듈의 testFixturesCompileOnly에 Spring을 두면 순수성 검사가 실패한다") + void aFrameworkInAPureModulesFixturesFailsThePurityCheck(@TempDir Path temporaryRoot) + throws IOException { + // given + Path project = + fixture( + temporaryRoot, "testFixturesCompileOnly 'org.springframework:spring-context:1.0.0'"); + + // when + BuildResult result = runner(project, "verifyApplicationCoreDependencyPurity").buildAndFail(); + + // then + assertThat(result.getOutput()) + .contains("testFixturesCompileClasspath: forbidden resolved dependency") + .contains("org.springframework:spring-context:1.0.0"); + } + + @Test + @DisplayName("순수 모듈의 testFixtures가 framework를 쓰지 않으면 순수성 검사를 통과한다") + void pureFixturesPassThePurityCheck(@TempDir Path temporaryRoot) throws IOException { + // given + Path project = fixture(temporaryRoot, "testFixturesImplementation 'org.example:other:1.0.0'"); + + // when + BuildResult result = runner(project, "verifyApplicationCoreDependencyPurity").build(); + + // then + assertThat(result.getOutput()).contains("verifyApplicationCoreDependencyPurity: OK"); + } + + private static Path fixture(Path temporaryRoot, String fixturesDeclaration) throws IOException { + Path project = temporaryRoot.resolve("repository"); + Files.createDirectories(project.resolve("gradle")); + Files.createDirectories(project.resolve("application-demo")); + Path maven = temporaryRoot.resolve("maven"); + pom(maven, "org.springframework", "spring-context"); + pom(maven, "org.example", "other"); + Files.writeString( + project.resolve("settings.gradle"), + """ + rootProject.name = 'main' + include 'application-demo' + """, + UTF_8); + Files.writeString( + project.resolve("build.gradle"), + """ + plugins { id 'ca.architecture' } + subprojects { + apply plugin: 'java-library' + apply plugin: 'java-test-fixtures' + repositories { maven { url = '%s' } } + } + project(':application-demo') { + dependencies { + %s + } + } + """ + .formatted(maven.toUri(), fixturesDeclaration), + UTF_8); + Files.writeString( + project.resolve("gradle/modules.json"), + """ + { + "library_groups": ["dev.cakit"], + "modules": [ + { + "id": "application-demo", + "gradle_path": ":application-demo", + "source_path": "application-demo", + "allowed_dependencies": [] + } + ] + } + """, + UTF_8); + return project; + } + + private static void pom(Path maven, String group, String artifact) throws IOException { + Path directory = maven.resolve(group.replace('.', '/')).resolve(artifact).resolve("1.0.0"); + Files.createDirectories(directory); + Files.writeString( + directory.resolve(artifact + "-1.0.0.pom"), + """ + + 4.0.0 + %s%s1.0.0 + pom + + """ + .formatted(group, artifact), + UTF_8); + } + + private static GradleRunner runner(Path project, String... arguments) { + String[] fullArguments = new String[arguments.length + 2]; + fullArguments[0] = "--console=plain"; + fullArguments[1] = "--stacktrace"; + System.arraycopy(arguments, 0, fullArguments, 2, arguments.length); + return GradleRunner.create() + .withProjectDir(project.toFile()) + .withPluginClasspath() + .withArguments(fullArguments); + } +}