Marco Eckstein
Marco Eckstein

Reputation: 4758

How to allow only certain cyclic dependencies in ArchUnit?

In ArchUnit, I can check that packages .should().beFreeOfCycles(). How can I specify exceptions to this rule for certain cycles?

E.g., given these packages and their dependencies:

A <-> B <-> C

How can I allow A <-> B, but still forbid A and B being part of any other cycle, e.g. B <-> C?

Upvotes: 0

Views: 1568

Answers (2)

Marco Eckstein
Marco Eckstein

Reputation: 4758

Based on Manfred's answer, here is a solution that seems to work fine (implemented in Kotlin):

fun test() {
    SlicesRuleDefinition.slices().matching("(com.mypackage.*..)")
        .should()
        // Using an extension method which allows us to specify allowed
        // cycles succinctly:
        .beFreeOfCyclesExcept("com.mypackage.a" to "com.mypackage.b")
        .check(someClasses)
}

fun SlicesShould.beFreeOfCyclesExcept(
    vararg allowed: Pair<String, String>
): ArchRule =
    FreezingArchRule
        // In case you are not familiar with Kotlin:
        // We are in an extension method. 'this' will be substituted with
        // 'SlicesRuleDefinition.slices().matching("(com.mypackage.*..)")
        // .should()';
        .freeze(this.beFreeOfCycles())
        .persistIn(
            // Using a custom ViolationStore instead of the default 
            // TextFileBasedViolationStore so we can configure the
            // allowed violations in code instead of a text file:
            object : ViolationStore {

                override fun initialize(properties: Properties) {}

                override fun contains(rule: ArchRule): Boolean = true

                override fun save(rule: ArchRule, violations: List<String>) {
                    // Doing nothing here because we do not want ArchUnit 
                    // to add any additional allowed violations.
                }

                override fun getViolations(rule: ArchRule): List<String> =
                    allowed
                        // ArchUnit records cycles in the form 
                        // A -> B -> A. I.e., A -> B -> A and 
                        // B -> A -> B are different violations.
                        // We add the reverse cycle to make sure
                        // both directions are allowed:
                        .flatMap { pair -> 
                            listOf(pair, Pair(pair.second, pair.first)) 
                        }
                        // .distinct() is not necessary, but using it is
                        // cleaner because by adding the reverse cycles
                        // we may possibly have added duplicates:
                        .distinct()
                        .map { (sliceA, sliceB) ->
                            // This is a prefix of the format that
                            // ArchUnit uses:
                            "Cycle detected: Slice $sliceA -> \n" +
                                "                Slice $sliceB -> \n" +
                                "                Slice $sliceA\n"
                        }
            }
        )
        // The lines that ArchUnit uses are very specific, including 
        // info about which methods etc. create the cycle. That is 
        // exactly what is desirable when establishing a baseline for 
        // legacy code. But we want to permanently allow certain 
        // cycles, regardless of which current or future code creates 
        // the cycle. Thus, we only compare the prefixes of violation
        // lines:
        .associateViolationLinesVia { 
            lineFromFirstViolation,
            lineFromSecondViolation ->
                lineFromFirstViolation.startsWith(lineFromSecondViolation)
        }

Note that I have tested this only on a small project.

Upvotes: 1

Manfred
Manfred

Reputation: 3142

Freezing Arch Rules is always an option to allow for certain violations, but catch others.
Would that be feasible in your case?

Upvotes: 1

Related Questions