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); } }