8000 Pull #16874: api: add new int process(List<Path> files) method to Roo… · checkstyle/checkstyle@3134505 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3134505

Browse files
author
Vincent Potucek
committed
Pull #16874: api: add new int process(List<Path> files) method to RootModule - add api
1 parent b24e789 commit 3134505

File tree

5 files changed

+93
-8
lines changed

5 files changed

+93
-8
lines changed

src/main/java/com/puppycrawl/tools/checkstyle/Checker.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import java.io.UnsupportedEncodingException;
2727
import java.nio.charset.Charset;
2828
import java.nio.charset.StandardCharsets;
29+
import java.nio.file.Path;
2930
import java.util.ArrayList;
31+
import java.util.Collection;
3032
import java.util.List;
3133
import java.util.Locale;
3234
import java.util.Set;
@@ -220,21 +222,23 @@ public int process(List<File> files) throws CheckstyleException {
220222
fsc.beginProcessing(charset);
221223
}
222224

223-
final List<File> targetFiles = files.stream()
225+
processFiles(files.stream()
224226
.filter(file -> CommonUtil.matchesFileExtension(file, fileExtensions))
225-
.collect(Collectors.toUnmodifiableList());
226-
processFiles(targetFiles);
227+
.collect(Collectors.toUnmodifiableList()));
227228

228229
// Finish up
229230
// It may also log!!!
230231
fileSetChecks.forEach(FileSetCheck::finishProcessing);
231-
232-
// It may also log!!!
233232
fileSetChecks.forEach(FileSetCheck::destroy);
234-
235-
final int errorCount = counter.getCount();
236233
fireAuditFinished();
237-
return errorCount;
234+
return counter.getCount();
235+
}
236+
237+
@Override
238+
public int process(Collection<Path> paths) throws CheckstyleException {
239+
return process(paths.stream()
240+
.map(Path::toFile)
241+
.collect(Collectors.toUnmodifiableList()));
238242
}
239243

240244
/**

src/main/java/com/puppycrawl/tools/checkstyle/api/RootModule.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package com.puppycrawl.tools.checkstyle.api;
2121

2222
import java.io.File;
23+
import java.nio.file.Path;
24+
import java.util.Collection;
2325
import java.util.List;
2426

2527
/**
@@ -40,9 +42,23 @@ public interface RootModule extends Configurable {
4042
* @return the total number of audit events with error severity found
4143
* @throws CheckstyleException if error condition within Checkstyle occurs
4244
* @see #destroy()
45+
* @deprecated Use {@link #process(Collection)}
4346
*/
47+
@Deprecated(since = "9.3")
4448
int process(List<File> files) throws CheckstyleException;
4549

50+
/**
51+
* Processes a set of files.
52+
* Once this is done, it is highly recommended to call for
53+
* the destroy method to close and remove the listeners.
54+
*
55+
* @param files the list of files to be audited.
56+
* @return the total number of audit events with error severity found
57+
* @throws CheckstyleException if error condition within Checkstyle occurs
58+
* @see #destroy()
59+
*/
60+
int process(Collection<Path> files) throws CheckstyleException;
61+
4662
/**
4763
* Add the listener that will be used to receive events from the audit.
4864
*

src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java

Lines changed: 48 additions & 0 deletions
< F438 /tr>
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_STARTED_MESSAGE;
2626
import static com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.MSG_KEY_NO_NEWLINE_EOF;
2727
import static com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck.MSG_KEY;
28+
import static org.mockito.Mockito.doReturn;
2829

2930
import java.io.BufferedReader;
3031
import java.io.ByteArrayInputStream;
@@ -41,6 +42,8 @@
4142
import java.lang.reflect.Method;
4243
import java.nio.charset.StandardCharsets;
4344
import java.nio.file.Files;
45+
import java.nio.file.Path;
46+
import java.nio.file.Paths;
4447
import java.util.ArrayList;
4548
import java.util.Arrays;
4649
import java.util.Collections;
@@ -55,8 +58,11 @@
5558
import java.util.UUID;
5659
import java.util.stream.Collectors;
5760

61+
import org.junit.jupiter.api.Nested;
5862
import org.junit.jupiter.api.Test;
5963
import org.junit.jupiter.api.io.TempDir;
64+
import org.mockito.ArgumentCaptor;
65+
import org.mockito.Mockito;
6066

6167
import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions;
6268
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
@@ -1733,6 +1739,48 @@ public void testRelativizedFileExclusion() throws Exception {
17331739
tempFile.getName(), expected);
17341740
}
17351741

1742+
@Nested
1743+
public class TestProcessPathCollection {
1744+
1745+
@Test
1746+
public void testFileExtensionsPath() throws Exception {
1747+
final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
1748+
checkerConfig.addProperty("charset", StandardCharsets.UTF_8.name());
1749+
checkerConfig.addProperty("cacheFile", createTempFile("junit").getAbsolutePath());
1750+
1751+
final Checker checker = new Checker();
1752+
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
1753+
checker.configure(checkerConfig);
1754+
1755+
final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
1756+
checker.addListener(auditAdapter);
1757+
1758+
final List<Path> files = new ArrayList<>();
1759+
files.add(Paths.get("file.pdf"));
1760+
files.add(Paths.get("file.java"));
1761+
checker.setFileExtensions("java", "xml", "properties");
1762+
checker.setCacheFile(createTempFile("junit").getAbsolutePath());
1763+
final int counter = checker.process(files);
1764+
1765+
// comparing to 1 as there is only one legal file in input
1766+
final int numLegalFiles = 1;
1767+
assertWithMessage("There were more legal files than expected")
1768+
.that(counter)
1769+
.isEqualTo(numLegalFiles);
1770+
assertWithMessage("Audit was started on larger amount of files than expected")
1771+
.that(auditAdapter.getNumFilesStarted())
1772+
.isEqualTo(numLegalFiles);
1773+
assertWithMessage("Audit was finished on larger amount of files than expected")
1774+
.that(auditAdapter.getNumFilesFinished())
1775+
.isEqualTo(numLegalFiles);
1776+
assertWithMessage("Cache should not contain any file")
1777+
.that(TestUtil.<PropertyCacheFile>getInternalState(checker, "cacheFile")
1778+
.get(Paths.get("file.java").toAbsolutePath().toString()))
1779+
.isNull();
1780+
}
1781+
1782+
}
1783+
17361784
public static class DefaultLoggerWithCounter extends DefaultLogger {
17371785

17381786
private int fileStartedCount;

src/test/java/com/puppycrawl/tools/checkstyle/internal/testmodules/TestRootModuleChecker.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
package com.puppycrawl.tools.checkstyle.internal.testmodules;
2121

2222
import java.io.File;
23+
import java.nio.file.Path;
2324
import java.util.ArrayList;
25+
import java.util.Collection;
2426
import java.util.Collections;
2527
import java.util.List;
28+
import java.util.stream.Collectors;
2629

2730
import com.puppycrawl.tools.checkstyle.api.AuditListener;
2831
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
@@ -55,6 +58,13 @@ public int process(List<File> files) {
5558
return 0;
5659
}
5760

61+
@Override
62+
public int process(Collection<Path> files) {
63+
return process(files.stream()
64+
.map(Path::toFile)
65+
.collect(Collectors.toUnmodifiableList()));
66+
}
67+
5868
@Override
5969
public void addListener(AuditListener listener) {
6070
// not used

src/test/java/com/puppycrawl/tools/checkstyle/utils/ModuleReflectionUtilTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import java.io.File;
2727
import java.io.IOException;
28+
import java.nio.file.Path;
29+
import java.util.Collection;
2830
import java.util.Collections;
2931
import java.util.List;
3032
import java.util.Set;
@@ -301,6 +303,11 @@ public int process(List<File> files) {
301303
return 0;
302304
}
303305

306+
@Override
307+
public int process(Collection<Path> files) {
308+
return 0;
309+
}
310+
304311
@Override
305312
public void destroy() {
306313
// dummy method

0 commit comments

Comments
 (0)
0