SONARJAVA-6196 S1451 should provide a default template for headers#5578
SONARJAVA-6196 S1451 should provide a default template for headers#5578NoemieBenard wants to merge 7 commits intomasterfrom
Conversation
SummaryChanged S1451 (file header check) to provide a default header template instead of an empty default. The check previously required users to manually configure a The implementation also distinguishes between "no header required" (explicit empty string) and "default header" (the new template), ensuring that when What reviewers should knowKey changes to review:
Why this matters:
Watch for:
|
|
| check = new FileHeaderCheck(); | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Class6.java")) | ||
| .withCheck(check) | ||
| .verifyNoIssues(); |
There was a problem hiding this comment.
The test verifies that Class6.java (which contains the default header) is compliant — but there is no negative test asserting that a file without the default header gets flagged.
This gap matters concretely: if DEFAULT_HEADER_FORMAT ever became empty (e.g., due to a text-block indentation accident), the headerFormat.isEmpty() branch would fire, set expectedLines = {}, and matches() would return true for any file. The verifyNoIssues() call would still pass, giving a false sense of correctness.
Add a complementary case that runs the default FileHeaderCheck() against a file that lacks the header and asserts an issue is raised — something like:
check = new FileHeaderCheck();
CheckVerifier.newVerifier()
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Class1.java"))
.withCheck(check)
.verifyIssueOnFile("Add or update the header of this file.");- Mark as noise
There was a problem hiding this comment.
Let's use desciprive file names for test samples.
| private String getHeaderFormat() { | ||
| String format = headerFormat; | ||
| if(format.charAt(0) != '^') { | ||
| if (format.charAt(0) != '^') { |
There was a problem hiding this comment.
IMO it's a bad practice to do unintentional chages, because they make it hard to understand what is happening, if someone is reading a PR for some reason in the future.
| .onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Class3.java")) | ||
| .withCheck(check) | ||
| .verifyNoIssues(); | ||
|
|
There was a problem hiding this comment.
Are these a part of this PR or is it a copied from the other changes?
| defaultValue = "false") | ||
| public boolean isRegularExpression = false; | ||
|
|
||
| private String[] expectedLines; |
There was a problem hiding this comment.
Let's remove expectedLines.
| return format; | ||
| } | ||
|
|
||
| private void visitFile() { |
There was a problem hiding this comment.
Let's remove this method.
| if (isRegularExpression) { | ||
| checkRegularExpression(context.getFileContent()); | ||
| } else { | ||
| if (!matches(expectedLines, context.getFileLines())) { |
There was a problem hiding this comment.
This should be extracted to checkExpectedLines




Add default template for headerFormat in S1451.