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

Allow different naming conventions for tests #24

Closed
scottkennedy opened this issue May 1, 2017 · 13 comments
Closed

Allow different naming conventions for tests #24

scottkennedy opened this issue May 1, 2017 · 13 comments
Projects
Milestone

Comments

@scottkennedy
Copy link
Contributor

In tests, I like to have very specific method names that are human readable.

For example:

fun `With input X, the output should be Y`()

However, this is not valid in Android apps.

As such, it would be nice if I could specify a different configuration for the tests in my project.

@MyDogTom
Copy link
Contributor

MyDogTom commented May 2, 2017

You can achieve this by having two different Gradle tasks with different filters and config values. Let's assume that all your tests are placed in directories with test suffix (eg. src/test, src/sharedTest, src/androidTest etc)

Here is example of configuration for all files except tests

    task detektCheck(type: JavaExec) {
        main = "io.gitlab.arturbosch.detekt.cli.Main"
        classpath = configurations.detekt
        def input = "$project.projectDir.absolutePath"
        def config = "$project.projectDir/../config/detekt/detekt_configuration_main.yml"
        def filters = ".*src/\\w*[Tt]est/.*"
        def rulesets = ""
        def params = ['-p', input, '-c', config, '-f', filters, '-r', rulesets]
        args(params)
    }

Configuration for files in "test" directories

    task detektCheckTests(type: JavaExec) {
        main = "io.gitlab.arturbosch.detekt.cli.Main"
        classpath = configurations.detekt
        def input = "$project.projectDir.absolutePath"
        def config = "$project.projectDir/../config/detekt/detekt_configuration_tests.yml"
        def filters = ".*src/(?!(\\w*[Tt]est))"
        def rulesets = ""
        def params = ['-p', input, '-c', config, '-f', filters, '-r', rulesets]
        args(params)
    }

As I said, difference in filters and config fields.

Disadvantage of this approach is that you have to duplicate your configuration, because mostly it will be identical (except naming configuration)

@scottkennedy
Copy link
Contributor Author

Awesome, that will work well. Maybe a different suggestion would be to allow multiple config files (like how Proguard works), so the common stuff could be in one place, and my tests could override an option?

@arturbosch
Copy link
Member

arturbosch commented May 4, 2017

It makes perfectly sense to have different conditions for production code and test code etc. For example we could allow higher thresholds for e.g. LongMethod in test cases and naming conventions like @scottkennedy suggested.

I just have not figure out the best way to support this. I don't want to have some extra logic to filter for test paths and main paths in the core module. Also something like 'profiles' for different environments would be nice. Different gradle tasks with different configuration is now possible but needs to much code. Extra config files which override some values of the 'main'-config sound good to me.

To decrease the parameters in the task definitions, I'm thinking about a kotlin DSL ( #19 ), so the only parameter for the CLI would be the path to the config path. But maybe just extending the yaml format would also be sufficient for now and spend the time on other features :)

PS: today I implemented the '@Suppress' feature ( #6 ) which would be another cumbersome way to archive different naming conventions :D

@arturbosch arturbosch added this to the 1.0.0 milestone May 4, 2017
@arturbosch arturbosch added this to Todo in Next Jun 4, 2017
@jaredsburrows
Copy link

@scottkennedy You got this working for Android?

@scottkennedy
Copy link
Contributor Author

scottkennedy commented Jun 4, 2017

Yes, this is how I have it set up:

task detektApp(type: JavaExec) {
    main = "io.gitlab.arturbosch.detekt.cli.Main"
    classpath = configurations.detekt
    def input = "$project.projectDir.absolutePath"
    def output = "$project.projectDir.absolutePath/build/reports/detekt/"
    def config = "$project.rootDir/reporters/detekt/detekt.yml"
    def filters = ".*src/test/.*"
    def rulesets = ""
    def params = ['-p', input, '-c', config, '-f', filters, '-r', rulesets, '-o', '-rp', output]
    args(params)
}

task detektTests(type: JavaExec) {
    main = "io.gitlab.arturbosch.detekt.cli.Main"
    classpath = configurations.detekt
    def input = "$project.projectDir.absolutePath"
    def output = "$project.projectDir.absolutePath/build/reports/detekt/"
    def config = "$project.rootDir/reporters/detekt/detekt_tests.yml"
    def filters = ".*src/(?!(test))/.*"
    def rulesets = ""
    def params = ['-p', input, '-c', config, '-f', filters, '-r', rulesets, '-o', '-rp', output]
    args(params)
}

Hmmm I'm thinking I have this backwards now, but you get the idea.

@jaredsburrows
Copy link

@scottkennedy Thanks for the fast reply! I tried to apply it to an existing project and it failed: #69.

  • What android gradle plugin are you using?
  • What kotlin plugin version are you using?
  • What detekt plugin version are you using?

@scottkennedy
Copy link
Contributor Author

Android Gradle 2.3.2
Kotlin 1.1.2-4
detekt 1.0.0.M10.1

@scottkennedy
Copy link
Contributor Author

Oh but I'm not using the detekt Gradle plugin.

@jaredsburrows
Copy link

@scottkennedy Applying classpath "gradle.plugin.io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.0.M10.1" instead of classpath "gradle.plugin.io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.0.M10.3" fails:

$ gradlew assembleDebug
Download https://plugins.gradle.org/m2/gradle/plugin/io/gitlab/arturbosch/detekt/detekt-gradle-plugin/1.0.0.M10.1/detekt-gradle-plugin-1.0.0.M10.1.pom
Download https://plugins.gradle.org/m2/io/gitlab/arturbosch/detekt/detekt-cli/1.0.0.M10.1/detekt-cli-1.0.0.M10.1.pom
Download https://plugins.gradle.org/m2/io/gitlab/arturbosch/detekt/detekt-api/1.0.0.M10.1/detekt-api-1.0.0.M10.1.pom
Download https://plugins.gradle.org/m2/io/gitlab/arturbosch/detekt/detekt-core/1.0.0.M10.1/detekt-core-1.0.0.M10.1.po
Download https://plugins.gradle.org/m2/io/gitlab/arturbosch/detekt/detekt-rules/1.0.0.M10.1/detekt-rules-1.0.0.M10.1.pom

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'android-gif-example'.
> Could not resolve all files for configuration ':classpath'.
   > Could not find io.gitlab.arturbosch:codesmell-baseline-format:1.0.0.
     Searched in the following locations:
         https://plugins.gradle.org/m2/io/gitlab/arturbosch/codesmell-baseline-format/1.0.0/codesmell-baseline-format-1.0.0.pom
         https://plugins.gradle.org/m2/io/gitlab/arturbosch/codesmell-baseline-format/1.0.0/codesmell-baseline-format-1.0.0.jar
     Required by:
         project : > gradle.plugin.io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.0.M10.1 > io.gitlab.arturbosch.detekt:detekt-cli:1.0.0.M10.1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED in 4s

@jaredsburrows
Copy link

@scottkennedy What do you mean? You have custom tasks and only have it on the classpath?

@scottkennedy
Copy link
Contributor Author

I'm using it the same way I used it before there was a Gradle plugin. I couldn't get the plugin to work, and haven't had time to investigate.

So yes, just the tasks, along with:

configurations {
    detekt
}

dependencies {
    detekt dep.detekt_cli
    detekt dep.detekt_formatting
}

@jaredsburrows
Copy link

@scottkennedy Thank you. This is what I meant, you aren't "applying" it the to project itself, just using the jars. I can't get it to work either but I will try your way!

arturbosch added a commit that referenced this issue Jun 16, 2017
…erent parameters for different project modules - closes #24, #88
@arturbosch arturbosch moved this from Todo to Next in Next Jun 17, 2017
@arturbosch arturbosch moved this from Next to Done in Next Jun 19, 2017
@arturbosch arturbosch modified the milestones: M12, 1.0.0 Jun 22, 2017
arturbosch added a commit that referenced this issue Jun 26, 2017
…erent parameters for different project modules - closes #24, #88
@lock
Copy link

lock bot commented Jun 20, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Next
Done
Development

No branches or pull requests

4 participants