Skip to content

Commit 93051dd

Browse files
Anmol202005rdiachenko
authored andcommitted
Issue checkstyle#18: Implemented git-diff generation
1 parent c45b52f commit 93051dd

File tree

16 files changed

+601
-0
lines changed

16 files changed

+601
-0
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,29 @@ on:
77
branches: [ main ]
88

99
jobs:
10+
diff-check:
11+
strategy:
12+
matrix:
13+
platform: [ubuntu-latest, macos-latest, windows-latest]
14+
runs-on: ${{ matrix.platform }}
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Generate diffs
20+
shell: bash
21+
run: ./scripts/generate-diffs.sh
22+
23+
- name: Check diff validity
24+
shell: bash
25+
run: |
26+
git diff --exit-code || (
27+
echo "::error::Generated diffs are not valid - please regenerate"
28+
exit 1
29+
)
30+
1031
build:
32+
needs: diff-check
1133
strategy:
1234
matrix:
1335
platform: [ubuntu-latest, macos-latest, windows-latest]

scripts/generate-diffs.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
RESOURCES_DIR="src/test/resources/org/checkstyle/autofix/recipe"
4+
5+
echo "Generating diffs for recipe tests..."
6+
7+
find "$RESOURCES_DIR" -name "Input*" -type f | while read -r input_file; do
8+
dir=$(dirname "$input_file")
9+
basename_input=$(basename "$input_file")
10+
suffix=${basename_input#Input}
11+
dir=${dir%/}
12+
output_file="$dir/Output$suffix"
13+
diff_file="$dir/Diff${suffix%.*}.diff"
14+
15+
if [ -f "$output_file" ]; then
16+
echo "Processing: $input_file -> $output_file"
17+
git diff --no-index --no-prefix "$input_file" "$output_file" > "$diff_file" 2>/dev/null
18+
echo " Diff generated: $diff_file"
19+
fi
20+
done
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
diff --git src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/classfieldtest/InputClassFieldTest.java src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/classfieldtest/OutputClassFieldTest.java
2+
index ac06187..74b379e 100644
3+
--- src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/classfieldtest/InputClassFieldTest.java
4+
+++ src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/classfieldtest/OutputClassFieldTest.java
5+
@@ -14,7 +14,7 @@ import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
-public class InputClassFieldTest {
10+
+public class OutputClassFieldTest {
11+
private String instanceField = "instance";
12+
private int counter = 0;
13+
private List<String> dataList = new ArrayList<>();
14+
@@ -25,10 +25,9 @@ public class InputClassFieldTest {
15+
private static int globalCounter = 0;
16+
17+
public void FieldsParametersLocalsTest(String name, int initialValue, boolean autoStart) {
18+
- String normalizedName = name.trim(); // violation, "should be declared final"
19+
- int validatedValue = Math.max(0, initialValue); // violation, "should be declared final"
20+
- // violation below, "should be declared final"
21+
- String logMessage = "Creating instance with: " + normalizedName;
22+
+ final String normalizedName = name.trim();
23+
+ final int validatedValue = Math.max(0, initialValue);
24+
+ final String logMessage = "Creating instance with: " + normalizedName;
25+
26+
this.instanceField = normalizedName;
27+
this.counter = validatedValue;
28+
@@ -37,14 +36,11 @@ public class InputClassFieldTest {
29+
30+
public void complexMethod(String param1, int param2, List<String> param3,
31+
Map<String, Object> param4, boolean param5) {
32+
- String workingData = param1 + "_processed"; // violation, "should be declared final"
33+
- int calculatedSize = param2 * 2; // violation, "should be declared final"
34+
- // violation below, "should be declared final"
35+
- List<String> filteredList = new ArrayList<>(param3);
36+
- // violation below, "should be declared final"
37+
- Map<String, Object> workingMap = new HashMap<>(param4);
38+
- boolean processFlag = param5 && !param3.isEmpty(); // violation, "should be declared final"
39+
- // violation below, "should be declared final"
40+
- String statusMessage = "Processing " + calculatedSize + " items";
41+
+ final String workingData = param1 + "_processed";
42+
+ final int calculatedSize = param2 * 2;
43+
+ final List<String> filteredList = new ArrayList<>(param3);
44+
+ final Map<String, Object> workingMap = new HashMap<>(param4);
45+
+ final boolean processFlag = param5 && !param3.isEmpty();
46+
+ final String statusMessage = "Processing " + calculatedSize + " items";
47+
}
48+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
diff --git src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/edgecasetest/InputEdgeCaseTest.java src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/edgecasetest/OutputEdgeCaseTest.java
2+
index bb10884..651244a 100644
3+
--- src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/edgecasetest/InputEdgeCaseTest.java
4+
+++ src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/edgecasetest/OutputEdgeCaseTest.java
5+
@@ -17,67 +17,64 @@ import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
9+
-public class InputEdgeCaseTest {
10+
+public class OutputEdgeCaseTest {
11+
public void varDeclarations() {
12+
- var autoString = "inferred string"; // violation, "should be declared final"
13+
- var autoNumber = 42; // violation, "should be declared final"
14+
- var autoList = new ArrayList<String>(); // violation, "should be declared final"
15+
- var autoMap = new HashMap<String, Integer>(); // violation, "should be declared final"
16+
-
17+
- String regularString = "explicit type"; // violation, "should be declared final"
18+
- var anotherAuto = "mixed with regular"; // violation, "should be declared final"
19+
- int regularInt = 100; // violation, "should be declared final"
20+
+ final var autoString = "inferred string";
21+
+ final var autoNumber = 42;
22+
+ final var autoList = new ArrayList<String>();
23+
+ final var autoMap = new HashMap<String, Integer>();
24+
+
25+
+ final String regularString = "explicit type";
26+
+ final var anotherAuto = "mixed with regular";
27+
+ final int regularInt = 100;
28+
}
29+
30+
public void loopVariables() {
31+
- List<String> items = Arrays.asList("a", "b", "c"); // violation, "should be declared final"
32+
+ final List<String> items = Arrays.asList("a", "b", "c");
33+
34+
for (String item : items) {
35+
System.out.println(item);
36+
- String processed = item.toUpperCase(); // violation, "should be declared final"
37+
+ final String processed = item.toUpperCase();
38+
}
39+
40+
for (int i = 0; i < 10; i++) {
41+
- String loopVar = "iteration " + i; // violation, "should be declared final"
42+
- int doubled = i * 2; // violation, "should be declared final"
43+
+ final String loopVar = "iteration " + i;
44+
+ final int doubled = i * 2;
45+
}
46+
47+
int counter = 0;
48+
while (counter < 5) {
49+
- String message = "Count: " + counter; // violation, "should be declared final"
50+
+ final String message = "Count: " + counter;
51+
counter++;
52+
}
53+
}
54+
55+
public void lambdaExpressions() {
56+
- // violation below, "should be declared final"
57+
- List<String> items = Arrays.asList("one", "two", "three");
58+
+ final List<String> items = Arrays.asList("one", "two", "three");
59+
60+
items.forEach(item -> System.out.println(item));
61+
items.stream().map(item -> item.toUpperCase()).forEach(System.out::println);
62+
63+
items.forEach((String item) -> {
64+
- String processed = item.trim(); // violation, "should be declared final"
65+
+ final String processed = item.trim();
66+
System.out.println(processed);
67+
});
68+
69+
- String prefix = "Item: "; // violation, "should be declared final"
70+
+ final String prefix = "Item: ";
71+
items.forEach(item -> System.out.println(prefix + item));
72+
}
73+
74+
public void tryWithResourcesAndExceptions() {
75+
- String filename = "test.txt"; // violation, "should be declared final"
76+
+ final String filename = "test.txt";
77+
78+
try (FileReader reader = new FileReader(filename);
79+
BufferedReader buffered = new BufferedReader(reader)) {
80+
81+
- String line = buffered.readLine(); // violation, "should be declared final"
82+
- // violation below, "should be declared final"
83+
- String processed = line != null ? line.trim() : "";
84+
+ final String line = buffered.readLine();
85+
+ final String processed = line != null ? line.trim() : "";
86+
87+
} catch (IOException e) {
88+
- // violation below, "should be declared final"
89+
- String errorMsg = "Error reading file: " + e.getMessage();
90+
+ final String errorMsg = "Error reading file: " + e.getMessage();
91+
System.err.println(errorMsg);
92+
93+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
diff --git src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/enhancedforloop/InputEnhancedForLoop.java src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/enhancedforloop/OutputEnhancedForLoop.java
2+
index af3995c..cad8af0 100644
3+
--- src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/enhancedforloop/InputEnhancedForLoop.java
4+
+++ src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/enhancedforloop/OutputEnhancedForLoop.java
5+
@@ -12,28 +12,26 @@
6+
7+
package org.checkstyle.autofix.recipe.finallocalvariable.enhancedforloop;
8+
9+
-public class InputEnhancedForLoop {
10+
+public class OutputEnhancedForLoop {
11+
public void method1()
12+
{
13+
final java.util.List<Object> list = new java.util.ArrayList<>();
14+
15+
- for(Object a : list){ // violation, "should be declared final"
16+
+ for(final Object a : list){
17+
}
18+
}
19+
20+
public void method2()
21+
{
22+
final int[] squares = {0, 1, 4, 9, 16, 25};
23+
- int x; // violation, "should be declared final"
24+
- for (int i : squares) { // violation, "should be declared final"
25+
+ final int x;
26+
+ for (final int i : squares) {
27+
}
28+
29+
}
30+
- // violation below, "should be declared final"
31+
- public java.util.List<String> method3(java.util.List<String> snippets) {
32+
- // violation below, "should be declared final"
33+
- java.util.List<String> filteredSnippets = new java.util.ArrayList<>();
34+
- for (String snippet : snippets) { // violation, "should be declared final"
35+
+ public java.util.List<String> method3(final java.util.List<String> snippets) {
36+
+ final java.util.List<String> filteredSnippets = new java.util.ArrayList<>();
37+
+ for (final String snippet : snippets) {
38+
filteredSnippets.add(snippet);
39+
}
40+
if (filteredSnippets.size() == 0) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
diff --git src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/singlelocaltest/InputSingleLocalTest.java src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/singlelocaltest/OutputSingleLocalTest.java
2+
index 357b8f9..163d0a6 100644
3+
--- src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/singlelocaltest/InputSingleLocalTest.java
4+
+++ src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/singlelocaltest/OutputSingleLocalTest.java
5+
@@ -15,36 +15,35 @@ import java.util.Date;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
9+
-public class InputSingleLocalTest {
10+
+public class OutputSingleLocalTest {
11+
public void basicLocalVariables() {
12+
- String name = "John Doe"; // violation, "should be declared final"
13+
- int age = 25; // violation, "should be declared final"
14+
- double salary = 50000.0; // violation, "should be declared final"
15+
- boolean isActive = true; // violation, "should be declared final"
16+
- char grade = 'A'; // violation, "should be declared final"
17+
- Object data = new HashMap<>(); // violation, "should be declared final"
18+
- List<String> items = new ArrayList<>(); // violation, "should be declared final"
19+
- StringBuilder builder = new StringBuilder(); // violation, "should be declared final"
20+
- Date currentDate = new Date(); // violation, "should be declared final"
21+
- File // prefix comment preceding target element
22+
- tempFile = new File("temp.txt"); // violation, "should be declared final"
23+
+ final String name = "John Doe";
24+
+ final int age = 25;
25+
+ final double salary = 50000.0;
26+
+ final boolean isActive = true;
27+
+ final char grade = 'A';
28+
+ final Object data = new HashMap<>();
29+
+ final List<String> items = new ArrayList<>();
30+
+ final StringBuilder builder = new StringBuilder();
31+
+ final Date currentDate = new Date();
32+
+ final File // prefix comment preceding target element
33+
+ tempFile = new File("temp.txt");
34+
}
35+
36+
public void methodWithCalculations() {
37+
- int x = 10; // violation, "should be declared final"
38+
- int y = 20; // violation, "should be declared final"
39+
- int sum = x + y; // violation, "should be declared final"
40+
- int product = x * y; // violation, "should be declared final"
41+
- double average = (x + y) / 2.0; // violation, "should be declared final"
42+
- // violation below, "should be declared final"
43+
- String result = "Sum: " + sum + ", Product: " + product;
44+
- boolean isPositive = sum > 0; // violation, "should be declared final"
45+
+ final int x = 10;
46+
+ final int y = 20;
47+
+ final int sum = x + y;
48+
+ final int product = x * y;
49+
+ final double average = (x + y) / 2.0;
50+
+ final String result = "Sum: " + sum + ", Product: " + product;
51+
+ final boolean isPositive = sum > 0;
52+
}
53+
54+
public String processResult(String input) {
55+
- String trimmed = input.trim(); // violation, "should be declared final"
56+
- String upperCase = trimmed.toUpperCase(); // violation, "should be declared final"
57+
- String formatted = "[" + upperCase + "]"; // violation, "should be declared final"
58+
+ final String trimmed = input.trim();
59+
+ final String upperCase = trimmed.toUpperCase();
60+
+ final String formatted = "[" + upperCase + "]";
61+
return formatted;
62+
}
63+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
diff --git src/test/resources/org/checkstyle/autofix/recipe/header/headerblanklines/InputHeaderBlankLines.java src/test/resources/org/checkstyle/autofix/recipe/header/headerblanklines/OutputHeaderBlankLines.java
2+
index c095e1d..4ebba0c 100644
3+
--- src/test/resources/org/checkstyle/autofix/recipe/header/headerblanklines/InputHeaderBlankLines.java
4+
+++ src/test/resources/org/checkstyle/autofix/recipe/header/headerblanklines/OutputHeaderBlankLines.java
5+
@@ -1,4 +1,11 @@
6+
+///////////////////////////////////////////////////////////////////////////////////////////////
7+
+// Unit test for checkstyle-openrewrite-recipes.
8+
+// Dated: XX.XX.XX
9+
+// Copyright (C) 2025 Authors. Licensed under Apache 2.0.
10+
+// This file is part of the Checkstyle OpenRewrite test suite.
11+
+///////////////////////////////////////////////////////////////////////////////////////////////
12+
+
13+
package org.checkstyle.autofix.recipe.header.headerblanklines;
14+
15+
-public class InputHeaderBlankLines {
16+
+public class OutputHeaderBlankLines {
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
diff --git src/test/resources/org/checkstyle/autofix/recipe/header/headercomments/InputHeaderComments.java src/test/resources/org/checkstyle/autofix/recipe/header/headercomments/OutputHeaderComments.java
2+
index fa0cfcb..3894f18 100644
3+
--- src/test/resources/org/checkstyle/autofix/recipe/header/headercomments/InputHeaderComments.java
4+
+++ src/test/resources/org/checkstyle/autofix/recipe/header/headercomments/OutputHeaderComments.java
5+
@@ -1,3 +1,10 @@
6+
+///////////////////////////////////////////////////////////////////////////////////////////////
7+
+// Unit test for checkstyle-openrewrite-recipes.
8+
+// Dated: XX.XX.XX
9+
+// Copyright (C) 2025 Authors. Licensed under Apache 2.0.
10+
+// This file is part of the Checkstyle OpenRewrite test suite.
11+
+///////////////////////////////////////////////////////////////////////////////////////////////
12+
+
13+
///////////////////////////////////////////////////////////////////////////////////////////////
14+
// Unit test for checkstyle-openrewrite-recipes.
15+
// Dated: 11.07.25
16+
@@ -6,5 +13,5 @@
17+
18+
package org.checkstyle.autofix.recipe.header.headercomments;
19+
20+
-public class InputHeaderComments {
21+
+public class OutputHeaderComments {
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
diff --git src/test/resources/org/checkstyle/autofix/recipe/header/headerincorrect/InputHeaderIncorrect.java src/test/resources/org/checkstyle/autofix/recipe/header/headerincorrect/OutputHeaderIncorrect.java
2+
index d3e9e8c..b1b2ac0 100644
3+
--- src/test/resources/org/checkstyle/autofix/recipe/header/headerincorrect/InputHeaderIncorrect.java
4+
+++ src/test/resources/org/checkstyle/autofix/recipe/header/headerincorrect/OutputHeaderIncorrect.java
5+
@@ -1,8 +1,15 @@
6+
+///////////////////////////////////////////////////////////////////////////////////////////////
7+
+// Unit test for checkstyle-openrewrite-recipes.
8+
+// Dated: XX.XX.XX
9+
+// Copyright (C) 2025 Authors. Licensed under Apache 2.0.
10+
+// This file is part of the Checkstyle OpenRewrite test suite.
11+
+///////////////////////////////////////////////////////////////////////////////////////////////
12+
+
13+
///////////////////////////////////////////////////////////////////////////////////////////////
14+
// This is a test class
15+
///////////////////////////////////////////////////////////////////////////////////////////////
16+
17+
package org.checkstyle.autofix.recipe.header.headerincorrect;
18+
19+
-public class InputHeaderIncorrect {
20+
+public class OutputHeaderIncorrect {
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
diff --git src/test/resources/org/checkstyle/autofix/recipe/redundantimport/complexcase/InputComplexCase.java src/test/resources/org/checkstyle/autofix/recipe/redundantimport/complexcase/OutputComplexCase.java
2+
index 5871f90..bf7c544 100644
3+
--- src/test/resources/org/checkstyle/autofix/recipe/redundantimport/complexcase/InputComplexCase.java
4+
+++ src/test/resources/org/checkstyle/autofix/recipe/redundantimport/complexcase/OutputComplexCase.java
5+
@@ -8,13 +8,9 @@
6+
*/
7+
8+
package org.checkstyle.autofix.recipe.redundantimport.complexcase;
9+
-
10+
-import org.checkstyle.autofix.recipe.redundantimport.complexcase.*; // violation 'Redundant import from the same package'
11+
import java.io.*;
12+
- import java.lang.*; // violation 'Redundant import from the java.lang package'
13+
14+
import java.util.List;
15+
-import java.util.List; // violation 'Duplicate import'
16+
import java.util.Iterator;
17+
import java.util.Enumeration;
18+
import java.util.Arrays;
19+
@@ -26,7 +22,6 @@ import javax.swing.BorderFactory;
20+
21+
import static java.io.File.listRoots;
22+
import static javax.swing.WindowConstants.*;
23+
-import static javax.swing.WindowConstants.*; // violation 'Duplicate import'
24+
import static java.io.File.createTempFile;
25+
import static java.io.File.pathSeparator;
26+
import static org.checkstyle.autofix.recipe.redundantimport.complexcase.InputComplexCase.myStaticMethod;
27+
@@ -41,6 +36,6 @@ import java.util.BitSet;
28+
29+
import static java.lang.Math.PI;
30+
31+
-class InputComplexCase {
32+
+class OutputComplexCase {
33+
public static void myStaticMethod() {}
34+
}

0 commit comments

Comments
 (0)