Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
import java.io.LineNumberReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
Expand All @@ -48,6 +49,14 @@ abstract class AbstractPatchFilterEvaluationTest extends AbstractModuleTestSuppo

private static final String CONTEXT_CONFIG_PATTERN = "(default|zero)ContextConfig.xml";

/**
* Pattern to match inline violation comments in test files.
* Matches: // violation 'message'
* or: // filtered violation below 'message'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we reuse this functionality from main library.
I thought autofix recipes project doing this ,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so - yes we can, i need to check it. Can you ping the reference?

*/
private static final Pattern VIOLATION_PATTERN = Pattern.compile(
"//\\s*(?:filtered\\s+)?violation(?:\\s+below)?\\s+'([^']+)'");

protected abstract String getPatchFileLocation();

protected void testByConfig(String configPath)
Expand Down Expand Up @@ -95,18 +104,81 @@ private void assertResults(String configPath, String path, int errorCounter,
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
LineNumberReader lnr = new LineNumberReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
final String expectedFile = configPath.replaceFirst(CONTEXT_CONFIG_PATTERN,
"expected.txt");
final Path expectedFilePath = Paths.get(getPath(expectedFile));
final List<String> expected = Files.readAllLines(expectedFilePath);
final List<String> actuals = lnr.lines().toList();

final List<String> expected = extractExpectedViolations(path);
final List<String> actuals = lnr.lines()
.filter(line -> !line.equals("Audit done."))
.toList();

assertEquals(expected.size(), actuals.size(),
"Number of violations does not match");

for (int index = 0; index < expected.size(); index++) {
final String expectedResult = path + File.separator + expected.get(index);
assertEquals(expectedResult, actuals.get(index),
"error message " + index + ". Expected file: " + expectedFilePath);
final String expectedViolation = expected.get(index);
final String actualViolation = actuals.get(index);
final String expectedResult = path + File.separator + expectedViolation;
assertEquals(expectedResult, actualViolation, "error message " + index);
}
assertEquals(expected.size(), errorCounter, "unexpected output: " + lnr.readLine());
assertEquals(expected.size(), errorCounter,
"unexpected output: " + lnr.readLine());
}
}

/**
* Extracts expected violation messages from inline comments in test files.
* Falls back to expected.txt if no inline violations are found.
* Parses comments like: // violation 'message'
*
* @param path the directory path containing test files
* @return list of expected violation messages in format "filename:line: message"
* @throws IOException if file reading fails
*/
private List<String> extractExpectedViolations(String path) throws IOException {
final List<String> violations = new ArrayList<>();
final File directory = new File(path);
final File[] files = directory.listFiles((dir, name) -> {
return name.endsWith(".java") || name.endsWith(".properties");
});

if (files != null) {
Arrays.sort(files);

for (File file : files) {
final List<String> lines =
Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
for (int index = 0; index < lines.size(); index++) {
final String line = lines.get(index);
final Matcher matcher = VIOLATION_PATTERN.matcher(line);
if (matcher.find()) {
final String violationMessage = matcher.group(1);
final int lineNumber = index + 1;
final String formattedMessage;
if (violationMessage.matches("\\d+:.*")) {
formattedMessage = ":" + violationMessage;
}
else {
formattedMessage = ": " + violationMessage;
}
violations.add(file.getName() + ":" + lineNumber + formattedMessage);
}
}
}
}

final List<String> result;
if (violations.isEmpty()) {
final File expectedFile = new File(path, "expected.txt");
if (expectedFile.exists()) {
result = Files.readAllLines(expectedFile.toPath(), StandardCharsets.UTF_8);
}
else {
result = violations;
}
}
else {
result = violations;
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package Checker.FileTabCharacter;

public class Test2 {
// Contains a tab -> <- //warn // violation without filter
// Contains a tab -> <- //warn // violation '25: Line contains a tab character.'
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package Checker.FileTabCharacter;

public class Test2 {
// Contains a tab -> <- //warn // violation without filter
// Contains a tab -> <- //warn // violation '25: Line contains a tab character.'
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Test {



// Contains a tab -> <- //warns // violation without filter
// Contains a tab -> <- //warns // violation '25: Line contains a tab character.'
// Long line ----------------------------------------------------------------
// Long line ----------------------------------------------------------------
// Contains a tab -> <- //warns
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Test {



// Contains a tab -> <- //warns // violation without filter
// Contains a tab -> <- //warns // violation '25: Line contains a tab character.'
// Long line ----------------------------------------------------------------
// Long line ----------------------------------------------------------------
// Contains a tab -> <- //warns
Expand Down

This file was deleted.

Loading