TNG / ArchUnit

A Java architecture test library, to specify and assert architecture rules in plain Java
http://archunit.org
Apache License 2.0
3.19k stars 288 forks source link

Improve reporting of ignored violations #66

Open codecholeric opened 6 years ago

codecholeric commented 6 years ago

Typically in legacy code bases there will be a ton of violations, when architecture rules are introduced for the first time. ArchUnit supports the 'archunit_ignore_patterns.txt' file, to exclude messages based on regular expressions, but once violations are ignored, there is no further feedback. The following is a list of suggestions, to improve the overview:

bfassbender commented 5 years ago

What I did with a similar challenge regarding PMD rules violations in a large legacy project was to introduce thresholds per priority. I believe it was a custom Ant Task back in the days, but it worked.

So we could capture the status quo without ignoring rules. This enabled us to have regular conversations in the team on where to clean up next. After cleaning up, the violations decreased every time. So we celebrated and lowered the thresholds to match the new (lower) values. Step by step we cleaned up until our target was achieved (0 critical and major of our PMD rules being violated).

Of course we had rare cases where a new violation had been introduced while an old one was fixed. But this part of the tradeoff and did not hinder us from reaching our overall goal.

How do you think about this?

I agree with your last point from above, that a generator for a full report would better fit a separate module instead of the core. The concise scope of ArchUnit's core is a thing I really love.

codecholeric commented 5 years ago

Yes, that sounds like a good idea!! Thresholds like this would definitely be useful. I wish I could split myself into several devs, then I'd already be working on this :slightly_frowning_face:

bfassbender commented 5 years ago

Well, we could stage this. First: we look into a simple report generator for all ignored violations as a separate module.

This gives transparency and we see how far this will help.

If we can agree on you giving me quick 1:1 intro on the inner structure of ArchUnit and point me in the right direction, I could give it a try and then come back with a pull request.

codecholeric commented 5 years ago

Sure, for mail check https://github.com/codecholeric or @codecholeric on Twitter. I you want, we can schedule some video conference to check out the code. I don't know, if the report is the 'quickest win' though, or if it would be cooler to pimp archunit.properties to allow priority.HIGH.threshold = 50 or something like that :wink: In the end that might be enough to say "every week we lower the threshold by 10" and start to fix this over time...

bfassbender commented 5 years ago

That would be the sweet solution. I’m optimistic, we‘ll figure something out in the 1:1 I‘ll get back to you.

codecholeric commented 5 years ago

To conclude our video conference:

Thanks for your time and the brain storming @bfassbender :smiley:

kapexx commented 5 years ago

Would it be possible to report patterns that didn't match anything?

I worked on a large legacy code base where we had a big archunit_ignore_patterns file. It always was a bit hard to maintain the file because of the size and since many developers didn't bother to update it after refactoring, there were a lot unnecessary patterns in there. To remove obsolete ignore patterns, we sometimes deleted all patterns, executed the tests and then converted the log output of violations into patterns with search/replace in a tetx editor and then dumped those patterns into the archunit_ignore_patterns file. If unused patterns were reported too, we could have identified those more easily and remove them by hand more frequently.

I also toyed with the idea to run tests with a special flag which would generate a configurable pattern for each rule violation (e.g. a combination of rule and class name) and then either report all patterns so that they can be copied to the ignore file manually or even automatically re-generate the archunit_ignore_patterns file. That would have been helpful for our workflow but at some point we reduced the amount of ignore patterns to a manageable level so I didn't implement anything like that.

hankem commented 5 years ago

You'll probably like #181. ;)

codecholeric commented 5 years ago

I do agree that there is some room for optimization of the ignore pattern handling. My thoughts when I originally implemented it was, that maybe you have one central ignore file and want to reuse if within different sub projects, no matter if all the patterns are matched. Meanwhile I also don't know if people use it that way. But I guess it wouldn't be too hard to log out the patterns that didn't match anything. The question would be at what level the logging should be done. If you assume it's a misuse, then you could log a warning. But then on the other hand you could also raise an exception. The other possibility would be info or debug logging, just for cases where you want to continuously clean up that file manually.

As @hankem pointed out, instead of investing more energy to create ignore patterns automatically, I would recommend to use the new FreezingArchRule with the next release, which will not only provide a convenient way to ignore the current state of a rule, but moreover automatically reduces the threshold if violations are fixed in time. It was pretty much designed for exactly that use case (it just needs some more testing if it works sufficiently well).