Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the possibility to allow empty should per rule #816

Merged
merged 10 commits into from
Feb 27, 2022
Merged

add the possibility to allow empty should per rule #816

merged 10 commits into from
Feb 27, 2022

Conversation

oberprah
Copy link
Collaborator

@oberprah oberprah commented Feb 26, 2022

This will allow to override the configuration property archRule.failOnEmptyShould on a per-rule basis. We overlooked the use case for certain rules that simply expect to have no match when we introduced failOnEmptyShould. An example would be rules like classes().that(areUnwanted()).should().containNumberOfElements(equalTo(0)). So making this only configurable on a global level renders it useless for a certain set of users. Also, we broke optionalLayers in LayeredArchitecture and OnionArchitecture with the default configuration, since individual access checks are modelled as rules. LayeredArchitecture and OnionArchitecture already have the concept of optionalLayers which is pretty much the same as expressed by allowEmptyShould. We now simply translate allowEmptyShould to the existing concept, otherwise the two concepts clash (e.g. declare an optionalLayer for which the allowed accesses are internally modelled as separate rule, and it still rejects empty classes if failOnEmptyShould is set to true).

Resolves: #803
Resolves: #808
Resolves: #809

actions-user and others added 4 commits February 20, 2022 06:29
Signed-off-by: codecholeric <action@github.com>
Signed-off-by: codecholeric <action@github.com>
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
This task depends on the compiled output of the `compileJava` task, so we should explicitly declare this dependency. Otherwise, if you just run `compileJdk9mainJava` on a clean build it breaks.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
In the end this is not relevant for most users which will be happy with the default configuration and do not need to get into the depths of tweaking specific resolutions.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
@oberprah oberprah marked this pull request as draft February 26, 2022 21:59
@codecholeric codecholeric marked this pull request as ready for review February 27, 2022 05:21
Copy link
Collaborator

@codecholeric codecholeric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your support 😄 As discussed I just took it and fixed up some parts this time without prior discussion so we can release this ASAP 👍 Feel free to comment on things you notice on my updated version!!

With ArchUnit 0.23.0 we fixed an ambiguity problem when resolving method call origins (bridge methods would cause two methods with same name and parameter types, so we would pick one randomly, which could be the wrong/synthetic one). Unfortunately, this broke some Kotlin use cases, because inline functions would cause the compiler to create synthetic classes with methods where the `signature` and the `descriptor` do not match (e.g. the signature says the return type is `String`, but the descriptor claims `Object`). In these cases the erasure of the generic type does not match the raw type. But so far for the return type we derived the raw return type as erasure from the generic return type.
This is now fixed by keeping raw and generic return type completely separate, since we obviously cannot derive one from the other (the JVM spec also clearly states that this is a valid case, that descriptor and signature do not need to match exactly and are not validated against each other).

Note that for parameter types we have already separated this a while ago, so there it never was a problem.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
Since we meanwhile have the descriptor both on the search target and the code unit candidates, we can simply compare that instead of comparing raw parameter types and return type individually.

Note that this by itself would have already fixed the bug where call origins cannot be identified if descriptor and signature mismatch, but nevertheless the API would have been behaving wrongly when deriving the raw return type from the erasure of the generic return type.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
codecholeric and others added 4 commits February 27, 2022 18:30
…smatch #812

With ArchUnit `0.23.0` we fixed an ambiguity problem when resolving method call origins (bridge methods would cause two methods with same name and parameter types, so we would pick one randomly, which could be the wrong/synthetic one). Unfortunately, this broke some Kotlin use cases, because inline functions would cause the compiler to create synthetic classes with methods where the signature and the descriptor do not match (e.g. the signature says the return type is String, but the descriptor claims Object). In these cases the erasure of the generic type does not match the raw type. But so far for the return type we derived the raw return type as erasure from the generic return type.
This is now fixed by keeping raw and generic return type completely separate, since we obviously cannot derive one from the other (the JVM spec also clearly states that this is a valid case, that descriptor and signature do not need to match exactly and are not validated against each other).
For all properties except for `optionalLayers` we have followed the pattern of immutability, we should do the same here. Technically this is a breaking change, but since this immutability is the usual pattern that ArchUnit follows everywhere else, and the fluent API invites to simply chain these methods anyway, the risk is probably low.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
It turned out to be a bad idea to override the default configuration for `archRule.failOnEmptyShould=false` for all tests, since that covered up the fact that the default configuration actually breaks `optionalLayers` for `LayeredArchitecture` and `OnionArchitecture`. We now also use the default configuration for tests and override it on an individual basis. Note that we should use the default config for `ArchitecturesTest`, but at the moment the tests would be broken. So we deactivate it for now and fix it together with making `failOnEmptyShould` configurable on a per-rule basis.

Note that this also showed some tests that were a little shady, i.e. tests that checked for `assertThatMembers(..).matchInAnyOrderMembersOf(..)`, but actually those members were an empty set, at least with certain JDK versions. For members declared in meta-annotated types, this actually also hits the annotation type annotated with the meta-annotation, because meta-annotated counts direct annotations as well. Since the behavior always was like this we adjust the test to match reality in this case.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
This will allow to override the configuration property `archRule.failOnEmptyShould` on a per-rule basis. We overlooked the use case for certain rules that simply expect to have no match when we introduced `failOnEmptyShould`. An example would be rules like `classes().that(areUnwanted()).should().containNumberOfElements(equalTo(0))`. So making this only configurable on a global level renders it useless for a certain set of users. Also, we broke `optionalLayers` in `LayeredArchitecture` and `OnionArchitecture` with the default configuration, since individual access checks are modelled as rules. `LayeredArchitecture` and `OnionArchitecture` already have the concept of `optionalLayers` which is pretty much the same as expressed by `allowEmptyShould`. We now simply translate `allowEmptyShould` to the existing concept, otherwise the two concepts clash (e.g. declare an `optionalLayer` for which the allowed accesses are internally modelled as separate rule, and it still rejects empty classes if `failOnEmptyShould` is set to `true).`

Signed-off-by: Hannes Oberprantacher <h.oberprantacher@gmail.com>
Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
@codecholeric codecholeric merged commit 538242f into TNG:main Feb 27, 2022
codecholeric added a commit that referenced this pull request Feb 27, 2022
This will allow to override the configuration property `archRule.failOnEmptyShould` on a per-rule basis. We overlooked the use case for certain rules that simply expect to have no match when we introduced `failOnEmptyShould`. An example would be rules like `classes().that(areUnwanted()).should().containNumberOfElements(equalTo(0))`. So making this only configurable on a global level renders it useless for a certain set of users. Also, we broke `optionalLayers` in `LayeredArchitecture` and `OnionArchitecture` with the default configuration, since individual access checks are modelled as rules. `LayeredArchitecture` and `OnionArchitecture` already have the concept of `optionalLayers` which is pretty much the same as expressed by `allowEmptyShould`. We now simply translate `allowEmptyShould` to the existing concept, otherwise the two concepts clash (e.g. declare an `optionalLayer` for which the allowed accesses are internally modelled as separate rule, and it still rejects empty classes if `failOnEmptyShould` is set to `true`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants