Skip to content

Commit 5d2a1b2

Browse files
Anmol202005timurt
authored andcommitted
Issue checkstyle#18: Implemented Diff generation with build
1 parent 93051dd commit 5d2a1b2

File tree

19 files changed

+117
-90
lines changed

19 files changed

+117
-90
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,7 @@ 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-
3110
build:
32-
needs: diff-check
3311
strategy:
3412
matrix:
3513
platform: [ubuntu-latest, macos-latest, windows-latest]

config/import-control-test.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
<allow pkg="java.nio"/>
1313
<allow pkg="java.lang"/>
1414
<allow pkg="javax.xml.stream"/>
15+
<allow pkg="org.eclipse.jgit.diff"/>
1516
<allow pkg="com.puppycrawl.tools.checkstyle"/>
1617
</import-control>

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
<version>${rewrite.version}</version>
6565
</dependency>
6666

67+
<!-- JGit for generating diffs without external Git dependency -->
68+
<dependency>
69+
<groupId>org.eclipse.jgit</groupId>
70+
<artifactId>org.eclipse.jgit</artifactId>
71+
<version>7.3.0.202506031305-r</version>
72+
</dependency>
73+
6774
<!-- Test dependencies -->
6875
<dependency>
6976
<groupId>com.google.truth</groupId>

scripts/generate-diffs.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite.
3+
// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
///////////////////////////////////////////////////////////////////////////////////////////////
17+
18+
package org.checkstyle.autofix.generator;
19+
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.util.List;
26+
import java.util.logging.Logger;
27+
import java.util.stream.Stream;
28+
29+
import org.eclipse.jgit.diff.DiffFormatter;
30+
import org.eclipse.jgit.diff.HistogramDiff;
31+
import org.eclipse.jgit.diff.RawText;
32+
import org.eclipse.jgit.diff.RawTextComparator;
33+
import org.junit.jupiter.api.Test;
34+
35+
public class GenerateDiffFilesTest {
36+
37+
private static final Logger LOGGER = Logger.getLogger(GenerateDiffFilesTest.class.getName());
38+
private static final String RESOURCES_DIR = "src/test/resources/org/checkstyle/autofix/recipe";
39+
40+
@Test
41+
void generateDiffs() throws IOException, InterruptedException {
42+
LOGGER.info("Generating diffs for recipe tests....");
43+
44+
try (Stream<Path> pathStream = Files.walk(Paths.get(RESOURCES_DIR))) {
45+
final List<Path> inputFiles = pathStream
46+
.filter(Files::isRegularFile)
47+
.filter(path -> path.getFileName().toString().startsWith("Input"))
48+
.toList();
49+
50+
for (Path inputFile : inputFiles) {
51+
createDiff(inputFile);
52+
}
53+
}
54+
LOGGER.info("Files generated successfully.");
55+
}
56+
57+
private void createDiff(Path inputFile) throws IOException, InterruptedException {
58+
final String inputFileName = inputFile.getFileName().toString();
59+
final String suffix = inputFileName.substring(5);
60+
61+
final Path outputFile = inputFile.getParent().resolve("Output" + suffix);
62+
final String diffFileName = "Diff" + suffix.replaceFirst("\\.[^.]*$", ".diff");
63+
final Path diffFile = inputFile.getParent().resolve(diffFileName);
64+
65+
if (!Files.exists(outputFile)) {
66+
throw new IOException("Output file does not exist: " + outputFile);
67+
}
68+
69+
final ByteArrayOutputStream out = new ByteArrayOutputStream();
70+
71+
final String oldPath = inputFile.toString().replace('\\', '/');
72+
final String newPath = outputFile.toString().replace('\\', '/');
73+
out.write(String.format("--- %s%n", oldPath).getBytes());
74+
out.write(String.format("+++ %s%n", newPath).getBytes());
75+
76+
try (DiffFormatter formatter = new DiffFormatter(out)) {
77+
78+
final RawText oldText = new RawText(Files.readAllBytes(inputFile));
79+
final RawText newText = new RawText(Files.readAllBytes(outputFile));
80+
81+
formatter.format(
82+
new HistogramDiff().diff(RawTextComparator.DEFAULT, oldText, newText),
83+
oldText, newText);
84+
}
85+
86+
final String diff = out.toString();
87+
Files.writeString(diffFile, diff);
88+
}
89+
}

src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/classfieldtest/DiffClassFieldTest.diff

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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
31
--- src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/classfieldtest/InputClassFieldTest.java
42
+++ src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/classfieldtest/OutputClassFieldTest.java
5-
@@ -14,7 +14,7 @@ import java.util.HashMap;
3+
@@ -14,7 +14,7 @@
64
import java.util.List;
75
import java.util.Map;
86

@@ -11,7 +9,7 @@ index ac06187..74b379e 100644
119
private String instanceField = "instance";
1210
private int counter = 0;
1311
private List<String> dataList = new ArrayList<>();
14-
@@ -25,10 +25,9 @@ public class InputClassFieldTest {
12+
@@ -25,10 +25,9 @@
1513
private static int globalCounter = 0;
1614

1715
public void FieldsParametersLocalsTest(String name, int initialValue, boolean autoStart) {
@@ -25,7 +23,7 @@ index ac06187..74b379e 100644
2523

2624
this.instanceField = normalizedName;
2725
this.counter = validatedValue;
28-
@@ -37,14 +36,11 @@ public class InputClassFieldTest {
26+
@@ -37,14 +36,11 @@
2927

3028
public void complexMethod(String param1, int param2, List<String> param3,
3129
Map<String, Object> param4, boolean param5) {

src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/edgecasetest/DiffEdgeCaseTest.diff

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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
31
--- src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/edgecasetest/InputEdgeCaseTest.java
42
+++ src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/edgecasetest/OutputEdgeCaseTest.java
5-
@@ -17,67 +17,64 @@ import java.util.Arrays;
3+
@@ -17,67 +17,64 @@
64
import java.util.HashMap;
75
import java.util.List;
86

@@ -13,15 +11,14 @@ index bb10884..651244a 100644
1311
- var autoNumber = 42; // violation, "should be declared final"
1412
- var autoList = new ArrayList<String>(); // violation, "should be declared final"
1513
- 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"
2014
+ final var autoString = "inferred string";
2115
+ final var autoNumber = 42;
2216
+ final var autoList = new ArrayList<String>();
2317
+ final var autoMap = new HashMap<String, Integer>();
24-
+
18+
19+
- String regularString = "explicit type"; // violation, "should be declared final"
20+
- var anotherAuto = "mixed with regular"; // violation, "should be declared final"
21+
- int regularInt = 100; // violation, "should be declared final"
2522
+ final String regularString = "explicit type";
2623
+ final var anotherAuto = "mixed with regular";
2724
+ final int regularInt = 100;

src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/enhancedforloop/DiffEnhancedForLoop.diff

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
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
31
--- src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/enhancedforloop/InputEnhancedForLoop.java
42
+++ src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/enhancedforloop/OutputEnhancedForLoop.java
53
@@ -12,28 +12,26 @@

src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/singlelocaltest/DiffSingleLocalTest.diff

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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
31
--- src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/singlelocaltest/InputSingleLocalTest.java
42
+++ src/test/resources/org/checkstyle/autofix/recipe/finallocalvariable/singlelocaltest/OutputSingleLocalTest.java
5-
@@ -15,36 +15,35 @@ import java.util.Date;
3+
@@ -15,36 +15,35 @@
64
import java.util.HashMap;
75
import java.util.List;
86

src/test/resources/org/checkstyle/autofix/recipe/header/headerblanklines/DiffHeaderBlankLines.diff

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
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
31
--- src/test/resources/org/checkstyle/autofix/recipe/header/headerblanklines/InputHeaderBlankLines.java
42
+++ src/test/resources/org/checkstyle/autofix/recipe/header/headerblanklines/OutputHeaderBlankLines.java
53
@@ -1,4 +1,11 @@

0 commit comments

Comments
 (0)