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; class CleanArchitectureResolvedClasspathFunctionalTest { @Test void transitiveApiProjectMustBeRegisteredForTheCompileVisibleConsumer(@TempDir Path temporaryRoot) throws IOException { Path project = fixture(temporaryRoot, false); BuildResult result = runner(project, "verifyCleanArchitectureDependencies").buildAndFail(); assertThat(result.getOutput()) .contains("Module ':c' has forbidden project dependencies [a]") .contains( "Direct and compile-visible production project edges must be explicitly registered"); } @Test void aSecondCompositionRootIsGovernedByTheSameDependencyPolicy( @TempDir Path temporaryRoot) throws IOException { Path repository = temporaryRoot.resolve("repository"); Path project = repository; Files.createDirectories(project.resolve("gradle")); Files.createDirectories(project.resolve("a")); Files.createDirectories(project.resolve("example-app")); Files.writeString( project.resolve("settings.gradle"), """ rootProject.name = 'main' include 'a', 'example-app' """, UTF_8); Files.writeString( project.resolve("build.gradle"), """ plugins { id 'ca.architecture' } subprojects { apply plugin: 'java-library' } project(':example-app') { dependencies { implementation project(':a') } } """, UTF_8); Files.writeString( project.resolve("gradle/modules.json"), """ { "modules": [ { "id": "a", "gradle_path": ":a", "source_path": "a", "allowed_dependencies": [] }, { "id": "example-app", "gradle_path": ":example-app", "source_path": "example-app", "allowed_dependencies": [] } ] } """, UTF_8); BuildResult result = runner(project, "verifyCleanArchitectureDependencies").buildAndFail(); assertThat(result.getOutput()) .contains("Module ':example-app' has forbidden project dependencies [a]"); } @Test void explicitlyRegisteredCompileVisibleTransitiveProjectPasses(@TempDir Path temporaryRoot) throws IOException { Path project = fixture(temporaryRoot, true); BuildResult result = runner(project, "verifyCleanArchitectureDependencies").build(); assertThat(result.getOutput()).contains("BUILD SUCCESSFUL"); } @Test @DisplayName("registry에 등록하지 않은 leaf 디렉터리에 build 스크립트가 있으면 방향 게이트가 그 경로로 실패한다") void aLeafDirectoryMissingFromTheRegistryFailsTheGate(@TempDir Path temporaryRoot) throws IOException { // given Path project = fixture(temporaryRoot, true); Files.createDirectories(project.resolve("d")); Files.writeString(project.resolve("d/build.gradle"), "plugins { id 'java' }\n", UTF_8); // when BuildResult result = runner(project, "verifyCleanArchitectureDependencies").buildAndFail(); // then assertThat(result.getOutput()).contains("d/build.gradle").contains("no registry entry"); } @Test @DisplayName("소유 group 라이브러리를 runtimeOnly로만 실어도 allowed_libraries에 없으면 방향 게이트가 실패한다") void aRuntimeOnlyOwnedLibraryMissingFromAllowedLibrariesFailsTheGate( @TempDir Path temporaryRoot) throws IOException { // given Path project = libraryFixture(temporaryRoot, "runtimeOnly 'dev.cakit.demo:demo-starter:1.0.0'", "[]"); // when BuildResult result = runner(project, "verifyCleanArchitectureDependencies").buildAndFail(); // then assertThat(result.getOutput()) .contains("Module ':c' has forbidden library dependencies [dev.cakit.demo:demo-starter]") .contains("allowed_libraries"); } @Test @DisplayName("소유 group 라이브러리가 api로 내보내 컴파일에 보이는 모듈도 allowed_libraries에 있어야 한다") void aCompileVisibleOwnedLibraryMustBeListedToo(@TempDir Path temporaryRoot) throws IOException { // given: demo-api's POM brings demo-core in compile scope, so c compiles against both. Path project = libraryFixture( temporaryRoot, "implementation 'dev.cakit.demo:demo-api:1.0.0'", "[\"dev.cakit.demo:demo-api\"]"); // when BuildResult result = runner(project, "verifyCleanArchitectureDependencies").buildAndFail(); // then assertThat(result.getOutput()) .contains("Module ':c' has forbidden library dependencies [dev.cakit.demo:demo-core]"); } @Test void ownedLibrariesListedInAllowedLibrariesPass(@TempDir Path temporaryRoot) throws IOException { // given Path project = libraryFixture( temporaryRoot, "implementation 'dev.cakit.demo:demo-api:1.0.0'\n" + " runtimeOnly 'dev.cakit.demo:demo-starter:1.0.0'", "[\"dev.cakit.demo:demo-api\", \"dev.cakit.demo:demo-core\"," + " \"dev.cakit.demo:demo-starter\"]"); // when BuildResult result = runner(project, "verifyCleanArchitectureDependencies").build(); // then assertThat(result.getOutput()).contains("BUILD SUCCESSFUL"); } @Test void aLibraryOutsideTheOwnedGroupsIsNotGoverned(@TempDir Path temporaryRoot) throws IOException { // given Path project = libraryFixture(temporaryRoot, "implementation 'org.example:other:1.0.0'", "[]"); // when BuildResult result = runner(project, "verifyCleanArchitectureDependencies").build(); // then assertThat(result.getOutput()).contains("BUILD SUCCESSFUL"); } @Test @DisplayName("--include-build로 끼운 라이브러리 모듈은 프로젝트가 아니라 그 좌표로 allowed_libraries와 대조한다") void aLibrarySubstitutedFromAnIncludedBuildIsCheckedByItsCoordinate(@TempDir Path temporaryRoot) throws IOException { // given: the library's own build next to the repository; demo-included exports demo-extra. Path project = libraryFixture( temporaryRoot, "implementation 'dev.cakit.demo:demo-included:1.0.0'", "[\"dev.cakit.demo:demo-included\"]"); Path library = includedLibrary(temporaryRoot); // when BuildResult result = runner( project, "--include-build", library.toString(), "verifyCleanArchitectureDependencies") .buildAndFail(); // then: the module the included project exports is named as a library, not a project edge. assertThat(result.getOutput()) .contains("Module ':c' has forbidden library dependencies [dev.cakit.demo:demo-extra]") .doesNotContain("forbidden project dependencies"); } @Test void anIncludedLibraryWhoseModulesAreAllListedPasses(@TempDir Path temporaryRoot) throws IOException { // given Path project = libraryFixture( temporaryRoot, "implementation 'dev.cakit.demo:demo-included:1.0.0'", "[\"dev.cakit.demo:demo-included\", \"dev.cakit.demo:demo-extra\"]"); Path library = includedLibrary(temporaryRoot); // when BuildResult result = runner( project, "--include-build", library.toString(), "verifyCleanArchitectureDependencies") .build(); // then assertThat(result.getOutput()).contains("BUILD SUCCESSFUL"); } @Test @DisplayName("계층 leaf(domain-core 등) 행에 allowed_libraries를 적으면 registry 위반으로 실패한다") void aLayerModuleThatAllowsALibraryIsARegistryViolation(@TempDir Path temporaryRoot) throws IOException { // given Path project = temporaryRoot.resolve("repository"); Files.createDirectories(project.resolve("gradle")); Files.createDirectories(project.resolve("domain-core")); Files.writeString( project.resolve("settings.gradle"), "rootProject.name = 'main'\ninclude 'domain-core'\n", UTF_8); Files.writeString( project.resolve("build.gradle"), "plugins { id 'ca.architecture' }\nsubprojects { apply plugin: 'java-library' }\n", UTF_8); Files.writeString( project.resolve("gradle/modules.json"), """ { "library_groups": ["dev.cakit"], "modules": [ { "id": "domain-core", "gradle_path": ":domain-core", "source_path": "domain-core", "allowed_dependencies": [], "allowed_libraries": ["dev.cakit.demo:demo-api"] } ] } """, UTF_8); // when BuildResult result = runner(project, "verifyCleanArchitectureDependencies").buildAndFail(); // then assertThat(result.getOutput()) .contains("'domain-core' is a layer module") .contains("[dev.cakit.demo:demo-api]"); } /** A library build of two projects in group dev.cakit.demo; demo-included exports demo-extra. */ private static Path includedLibrary(Path temporaryRoot) throws IOException { Path library = temporaryRoot.resolve("library"); Files.createDirectories(library.resolve("demo-included")); Files.createDirectories(library.resolve("demo-extra")); Files.writeString( library.resolve("settings.gradle"), "rootProject.name = 'library'\ninclude 'demo-included', 'demo-extra'\n", UTF_8); Files.writeString( library.resolve("build.gradle"), """ subprojects { apply plugin: 'java-library' group = 'dev.cakit.demo' version = '1.0.0' } project(':demo-included') { dependencies { api project(':demo-extra') } } """, UTF_8); return library; } /** * One module {@code c} that declares {@code declaration} against a file repository of POM-only * modules: {@code dev.cakit.demo:demo-api} (bringing {@code demo-core} in compile scope), * {@code demo-starter}, and {@code org.example:other}. The direction check reads the resolved * graph, not the artifacts, so no jar is needed. */ private static Path libraryFixture(Path temporaryRoot, String declaration, String cLibraries) throws IOException { Path repository = temporaryRoot.resolve("repository"); Path project = repository; Files.createDirectories(project.resolve("gradle")); Files.createDirectories(project.resolve("c")); Path maven = temporaryRoot.resolve("maven"); pom(maven, "dev.cakit.demo", "demo-core", ""); pom( maven, "dev.cakit.demo", "demo-api", """ dev.cakit.demodemo-core\ 1.0.0compile"""); pom(maven, "dev.cakit.demo", "demo-starter", ""); pom(maven, "org.example", "other", ""); Files.writeString( project.resolve("settings.gradle"), """ rootProject.name = 'main' include 'c' """, UTF_8); Files.writeString( project.resolve("build.gradle"), """ plugins { id 'ca.architecture' } subprojects { apply plugin: 'java-library' repositories { maven { url = '%s' } } } project(':c') { dependencies { %s } } """ .formatted(maven.toUri(), declaration), UTF_8); Files.writeString( project.resolve("gradle/modules.json"), """ { "library_groups": ["dev.cakit"], "modules": [ { "id": "c", "gradle_path": ":c", "source_path": "c", "allowed_dependencies": [], "allowed_libraries": %s } ] } """ .formatted(cLibraries), UTF_8); return project; } private static void pom(Path maven, String group, String artifact, String dependencies) 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 %s """ .formatted(group, artifact, dependencies), UTF_8); } private static Path fixture(Path temporaryRoot, boolean allowTransitiveA) throws IOException { Path repository = temporaryRoot.resolve("repository"); Path project = repository; Files.createDirectories(project.resolve("gradle")); for (String module : new String[] {"a", "b", "c"}) { Files.createDirectories(project.resolve(module)); } Files.writeString( project.resolve("settings.gradle"), """ rootProject.name = 'main' include 'a', 'b', 'c' """, UTF_8); Files.writeString( project.resolve("build.gradle"), """ plugins { id 'ca.architecture' } subprojects { apply plugin: 'java-library' } project(':b') { dependencies { api project(':a') } } project(':c') { dependencies { implementation project(':b') } } """, UTF_8); String cAllowed = allowTransitiveA ? "[\"b\", \"a\"]" : "[\"b\"]"; Files.writeString( project.resolve("gradle/modules.json"), """ { "modules": [ { "id": "a", "gradle_path": ":a", "source_path": "a", "allowed_dependencies": [] }, { "id": "b", "gradle_path": ":b", "source_path": "b", "allowed_dependencies": ["a"] }, { "id": "c", "gradle_path": ":c", "source_path": "c", "allowed_dependencies": %s } ] } """ .formatted(cAllowed), UTF_8); return project; } 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); } }