ci(danger): exempt comment-only Go changes from tests_with_code (#728 stack: 1/5)
What this changes
The Danger rule tests_with_code no longer counts a production Go file as changed when its diff adds and removes only comment lines and blank lines.
All other behavior of the rule stays the same.
Changed files:
danger/tests_with_code/Dangerfile: the comment-only check.danger/test/helper.rb:check_fail_casesaccepts an optional fifth element, the patch that the stubgit.diff_for_filereturns.danger/test/tests_with_code_test.rb: new cases for the check.docs/dev/conventions.md: the## Danger rulessection states the exemption and the new case element.
Why
!3360 (merged) (!3360 (merged)) added tests_with_code.
The rule fails a feat or fix merge request that changes a production Go file in a directory without a _test.go change in that directory.
A change to a comment does not change behavior, so a test change is not necessary for it.
Merge request !3380 (merged), "fix(datastore): reach remote parent tombstones from three scan levels (S20-A lifecycle purger plan: 20b/24)" (!3380 (merged)), fails the rule for this reason.
In that merge request, internal/managementapi/container_image_delete.go changes only a two-line comment.
This merge request unblocks !3380 (merged) (!3380 (merged)), whose only managementapi change is a comment.
The detection rule
The check reads the diff line by line from git.diff_for_file.
It does not parse Go, so it counts a changed line as code when it is not sure that the line is a comment.
A changed line is a comment line only in these cases:
- The trimmed line is empty.
- The trimmed line starts with
//, then a space, a tab, or the line end, and it does not contain*/. - The trimmed line starts with
/*and ends with its first*/, so the block comment opens and closes on that line.
These changes count as code:
- Directives such as
//go:build,//go:embedand//nolint, because no space follows the//. - A legacy
// +buildconstraint, because it is a build directive. Any whitespace between//and+buildcounts, because Go reads the constraint that way. - The inner lines of a block comment that has more than one line.
A line such as
* textcannot be told from code without the lines around it, for example*p = 1. - A
//line that contains*/, because it can close a block comment and start code. - A line that starts with a string literal, for example
"// a",. - All changes in a file where a line has an odd number of backticks.
Such a file can hold a multi-line raw string, and a
//line in a raw string is string text. - A renamed or copied file, which has a
rename fromorcopy fromline in its diff header, even when its hunks change only comments. A move to another directory moves the file to another Go package. - All changes in a cgo file, which has an
import "C"line or a"C"line in a grouped import. The comments before that import are the cgo preamble, and the preamble is C code. - A file that is not in the checkout, a file with no diff object, and a diff with no changed line, for example a mode change.
The check has these limits:
- A file can have an odd backtick count for other reasons, for example an unpaired backtick in a comment. The rule then requires a test change for a comment-only change in that file.
- A comment-only change to the inner lines of a multi-line block comment still requires a test change.
- A comment-only change to a file renamed or copied inside its own directory still requires a test change.
Tests
The new cases in danger/test/tests_with_code_test.rb cover these results:
| Change | Failures |
|---|---|
A // comment reworded, added with a blank line, or removed |
0 |
A one-line /* ... */ comment changed, or /**/ added |
0 |
A comment-only change beside a _test.go change in the same directory |
0 |
| An inner line of a multi-line block comment changed | 1 |
An unclosed /*/ added |
1 |
| A block comment before code on one line | 1 |
| A comment change and a code change in one file | 1 |
A string literal that contains //, or that starts with // |
1 |
A // line inside a multi-line raw string |
1 |
A //go:build directive added |
1 |
A legacy // +build constraint added, also with two spaces or a tab before +build |
1 |
A cgo preamble line changed, with a single or a grouped "C" import |
1 |
| A comment-only hunk on a renamed file, or on a copied file | 1 |
A // line that contains */ |
1 |
| A mode change with no hunk, no diff object, or a file not in the checkout | 1 |
The existing cases do not change.
Each existing case gets an empty patch, so the comment-only check does not exempt any file in them.
sh scripts/danger-rules-test.sh passes.
No end-to-end scenario is added or affected, because this change applies only to CI review rules. No user documentation is affected, because this change applies only to CI review rules.