enofex/taikai

Block imports in Test classes

Closed this issue · 4 comments

Hi, apologies for another question. I have a lot of JUnit 5 tests written in Kotlin, and some of these tests are using Hamcrest, some are using kotlin.test, and some of them AssertJ. I'd like to block the use of Hamcrest and Kotlin test, but I don't see how I can do this.

I've attempted to add it here:

                .java(java -> java
                        .imports(imports -> imports
                                .shouldNotImport("org.hamcrest"))

But that didn't do anything. I also tried it on the test block:

                .test(test -> test
                        .junit5(junit5 -> junit5
                                .classesShouldNotBeAnnotatedWithDisabled()
                                .methodsShouldNotBeAnnotatedWithDisabled()
                        ))

But I couldn't find a shouldNotImport method on either the test or junit method.

Any way to address this?

There is a shouldNotImport rule (detailed in the documentation), which applies to main classes, not test classes.

However, you can provide a custom configuration as described here.

For instance, the regex configuration would look like this: .shouldNotImport("..shaded..").

Thanks, that looks interesting. However, I'm not sure if the code example isn't correct or the code isn't, but the linked code example doesn't work:

ImportsConfigurer configurer = new ImportsConfigurer();
configurer.shouldNotImport("com.example.package", Configuration.of("com.example.namespace", Namespace.IMPORT.WITHOUT_TESTS));

The problem is that the constructor of the ImportsConfigurer is package protected, so I can't instantiate it as per the example.

I checked the rest of the code, but it seems the NamingConfigurer also has package protected class, not sure if intentional or not.

Also, bit of a sidenote, but have you seen https://docs.konsist.lemonappdev.com/ by any chance?

Thanks pointing it out, fixed the incorrect doc with e3ab724.

Thanks for the information about this library!

Hi @mnhock , thanks for updating the documentation. There's a small typo in the updated documentation though, I left a comment on the PR.