DEV Community

Zarah Dominguez
Zarah Dominguez

Posted on • Originally published at zarah.dev on

Enforcing Team Rules with Lint: Tests ๐Ÿง

A few months ago, my team came upon an agreement that when leaving a TODO anywhere in our code, we need to always provide several things:

  • the person who is expected to address the TODO
  • date when the TODO was left
  • a comment or explanation on what needs to be done

I created a live template to support adherence to this rule, but why not go one step further and integrate the rule into our daily workflow?

We have previously seen:


Having tests for our custom Lint rule is really important. We do not want Lint to flag errors, uhm, erroneously. It results in frustration and users might just turn off our rule.

Serving up files ๐Ÿ’

Lint checks run on files, so for each of our test cases we need to provide mock files.

Lint provides a TestFile API that allows us to create these mock files inline.

TestFiles.java(
    """
        package test.pkg;
        public class TestClass1 {
            // In a comment, mentioning "lint" has no effect
         }
    """
)


TestFiles.kotlin(
     """
        package test.pkg
        class TestClass {
            // In a comment, mentioning "lint" has no effect
        }
    """

Enter fullscreen mode Exit fullscreen mode

I am using raw strings so that I do not have to worry about escaping special characters.

It also gives us very nice syntax highlighting!




Furthermore, if you choose โ€œEdit Kotlin Fragmentโ€ from the hint, Android Studio will open up a file editor. Any changes you make in this editor will immediately reflect in your TestFile. Pretty cool!




Letโ€™s get testing ๐Ÿ”ฌ

The gateway to our test cases is TestLintTask. We need to provide it the following:

  • files we want to run our checks on
  • the issue we are testing against
  • expected result
@Test
fun testKotlinFileNormalComment() {
    TestLintTask.lint()
        .files(
            TestFiles.kotlin(
                """
                    package test.pkg

                    class TestClass {
                        // In a comment, mentioning "lint" has no effect
                    }
                """
            )
        )
        .issues(TodoDetector.ISSUE)
        .run()
        .expect("No warnings.")
}

Enter fullscreen mode Exit fullscreen mode

Note that we have the option here of using expect("No warnings.") or expectClean().

For the test cases where we expect an error to occur, we need to put in the text that Lint spits out (i.e. similar to what you see in the console when running .gradlew :app:lintDebug). The trickiest thing about this is that the string has to match exactly, including where the squiggly lines are.

The easiest way to do this is to pass an empty string to expect() and let the test fail. You can then copy-paste the error message into your test.


Retrieving the message for an error scenario

I wrote a few tests for the detector covering Java and Kotlin files, incorrect date formats, and โ€œTODOโ€ casing. You can find them all here.

Bringing it all together ๐Ÿค

Now that we have written our tests, itโ€™s finally time to integrate our Lint rule into our app!

First we need to create our IssueRegistry to let Lint know about our custom rule. We also need to provide an API value; for custom rules we can use the constant defined by the Lint API.

@Suppress("UnstableApiUsage")
class IssueRegistry : IssueRegistry() {
    override val issues: List<Issue> = listOf(
        TodoDetector.ISSUE
    )

    override val api = CURRENT_API
}

Enter fullscreen mode Exit fullscreen mode

We then register our IssueRegistry by creating a file with the fully qualified name of our file under a META-INF/services/ folder:

src/main/resources/META-INF/services/dev.zarah.lint.checks.IssueRegistry

Enter fullscreen mode Exit fullscreen mode

๐Ÿ™‹ Most posts mention that adding this registry is enough, and I am probably doing something wrong but I found out that for my project, I still have to include my IssueRegistry in the Manifest by adding this to my build.gradle.kts file:

tasks {
  jar {
    manifest {
      attributes(
          "Lint-Registry-v2" to "dev.zarah.lint.checks.IssueRegistry"
      )
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

We have set up everything and now itโ€™s time to consume our custom lint rule! In the modules where you want the rules applied, add a lintChecks entry in the dependencies closure, build your project, and everything should be good to go! ๐Ÿƒโ€โ™€๏ธ

dependencies {
    lintChecks project(':checks')
}

Enter fullscreen mode Exit fullscreen mode

Seeing it in action ๐Ÿ“ฝ๏ธ

Finally! We have come to the end of our journey! Hereโ€™s our detector in action:


Custom lint rule with quickfix

The source code for this Lint series is available on Github (diff).


๐Ÿ™‡โ€โ™€๏ธ I have learned so many things whilst I was doing this task.

As I mentioned on Twitter, thereโ€™s barely any public documentation at all. The talks Iโ€™ve seen are way too advanced for me (for example, I needed to understand what PSI and UAST were before it clicked and most talks barely even define them).

There was a lot of trial and error, and so. much. guesswork. It was incredibly frustrating. Major props to Mike Evans who patiently guided me through the hours and hours of pair programming that we did. If he didnโ€™t help, I wouldnโ€™t even know where to start. Sure I can copy-paste from samples but I want to understand why things are done the way they are โ€“ thatโ€™s how I learn.

I probably have made wrong assumptions whilst writing these posts ๐Ÿคทโ€โ™€๏ธ but again, no documentation so this is the best I could do. ๐Ÿ˜…

Anyway, what I want to say is, I usually only see the final output of other peopleโ€™s work and sometimes I cannot help but feel jealous โ€“ how come they find it super easy whilst Iโ€™m out here crying because I donโ€™t understand anything? I needed to remind myself that itโ€™s okay to be frustrated, itโ€™s okay to take your time to learn, and itโ€™s okay to ask for help.

Further reading (and/or watching) ๐Ÿ“–

If youโ€™re keen to learn more, here are some resources about Lint and custom Lint rules:

Top comments (0)