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

Skip to content

Commit 9f90965

Browse files
author
Vincent Potucek
committed
Pull checkstyle#16872: api: add new int process(List<Path> files) method to RootModule
1 parent 2aaf914 commit 9f90965

File tree

2 files changed

+41
-51
lines changed

2 files changed

+41
-51
lines changed

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

Expand all lines: src/main/java/com/puppycrawl/tools/checkstyle/Checker.java
Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,44 @@ public void setBasedir(String basedir) {
212212

213213
@Override
214214
public int process(List<File> files) throws CheckstyleException {
215+
return processFilesAudition(files);
216+
}
217+
218+
@Override
219+
public int process(Collection<Path> paths) throws CheckstyleException {
220+
return processFilesAudition(paths.stream()
221+
.map(Path::toFile)
222+
.collect(Collectors.toUnmodifiableList()));
223+
}
224+
225+
/**
226+
* Core method that processes files through all configured checks.
227+
*
228+
* <p>This method:
229+
* <ol>
230+
* <li>Updates cache with external resources if caching is enabled</li>
231+
* <li>Notifies listeners that audit is starting</li>
232+
* <li>Initializes all file set checks</li>
233+
* <li>Processes only files matching configured extensions</li>
234+
* <li>Performs cleanup of file set checks</li>
235+
* <li>Notifies listeners that audit is complete</li>
236+
* </ol>
237+
*
238+
* <p>The actual file processing is handled by {@link #processFiles(List)} which:
239+
* <ul>
240+
* <li>Applies before-execution file filters</li>
241+
* <li>Handles caching of file timestamps</li>
242+
* <li>Processes each file through all checks</li>
243+
* <li>Manages error reporting</li>
244+
* </ul>
245+
*
246+
* @param files List of files to process (non-null, may be empty)
247+
* @return Total count of errors found
248+
* @throws CheckstyleException if an error occurs during processing
249+
* @see #process(List)
250+
* @see #process(Collection)
251+
*/
252+
private int processFilesAudition(List<File> files) throws CheckstyleException {
215253
if (cacheFile != null) {
216254
cacheFile.putExternalResources(getExternalResourceLocations());
217255
}
@@ -222,29 +260,18 @@ public int process(List<File> files) throws CheckstyleException {
222260
fsc.beginProcessing(charset);
223261
}
224262

225-
final List<File> targetFiles = files.stream()
263+
processFiles(files.stream()
226264
.filter(file -> CommonUtil.matchesFileExtension(file, fileExtensions))
227-
.collect(Collectors.toUnmodifiableList());
228-
processFiles(targetFiles);
265+
.collect(Collectors.toUnmodifiableList()));
229266

230267
// Finish up
231268
// It may also log!!!
232269
fileSetChecks.forEach(FileSetCheck::finishProcessing);
233-
234-
// It may also log!!!
235270
fileSetChecks.forEach(FileSetCheck::destroy);
236-
237-
final int errorCount = counter.getCount();
238271
fireAuditFinished();
239-
return errorCount;
272+
return counter.getCount();
240273
}
241274

242-
@Override
243-
public int process(Collection<Path> paths) throws CheckstyleException {
244-
return process(paths.stream()
245-
.map(Path::toFile)
246-
.collect(Collectors.toUnmodifiableList()));
247-
}
248275

249276
/**
250277
* Returns a set of external configuration resource locations which are used by all file set

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,43 +1779,6 @@ public void testFileExtensionsPath() throws Exception {
17791779
.isNull();
17801780
}
17811781

1782-
@Test
1783-
public void testProcessPathCollection() throws Exception {
1784-
final Checker checker = new Checker();
1785-
final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
1786-
checker.addListener(auditAdapter);
1787-
1788-
final File tempFile1 = createTempFile("test1", ".java");
1789-
final File tempFile2 = createTempFile("test2", ".java");
1790-
1791-
final Checker spyChecker = Mockito.spy(checker);
1792-
doReturn(0).when(spyChecker).process(Collections.<File>emptyList());
1793-
1794-
spyChecker.process(Arrays.asList(
1795-
tempFile1.toPath(),
1796-
tempFile2.toPath()
1797-
));
1798-
1799-
final ArgumentCaptor<List<File>> filesCaptor = ArgumentCaptor.forClass(List.class);
1800-
Mockito.verify(spyChecker).process(filesCaptor.capture());
1801-
final List<File> processedFiles = filesCaptor.getValue();
1802-
assertWithMessage("Processed files count mismatch")
1803-
.that(processedFiles)
1804-
.hasSize(2);
1805-
assertWithMessage("First file mismatch")
1806-
.that(processedFiles.get(0))
1807-
.isEqualTo(tempFile1);
1808-
assertWithMessage("Second file mismatch")
1809-
.that(processedFiles.get(1))
1810-
.isEqualTo(tempFile2);
1811-
assertWithMessage("Audit started event not fired")
1812-
.that(auditAdapter.wasCalled())
1813-
.isTrue();
1814-
assertWithMessage("Incorrect number of files processed")
1815-
.that(auditAdapter.getNumFilesStarted())
1816-
.isEqualTo(2);
1817-
}
1818-
18191782
}
18201783

18211784
public static class DefaultLoggerWithCounter extends DefaultLogger {

0 commit comments

Comments
 (0)
0