8000 Issue #16857: api: add new int process(List<Path> files) method to Ro… · checkstyle/checkstyle@9884cc3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9884cc3

Browse files
author
Vincent Potucek
committed
Issue #16857: api: add new int process(List<Path> files) method to RootModule - add api
1 parent 497d26a commit 9884cc3

File tree

11 files changed

+239
-38
lines changed

11 files changed

+239
-38
lines changed

config/jsoref-spellchecker/whitelist.words

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ stylesheet
12811281
subdir
12821282
subelements
12831283
subext
1284+
subpath
12841285
subscope
12851286
sudo
12861287
suitebuilder

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

Lines changed: 9 additions & 0 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;
@@ -208,6 +210,13 @@ public void setBasedir(String basedir) {
208210
this.basedir = basedir;
209211
}
210212

213+
@Override
214+
public int process(Collection<Path> files) throws CheckstyleException {
215+
return process(files.stream()
216+
.map(Path::toFile)
217+
.collect(Collectors.toUnmodifiableList()));
218+
}
219+
211220
@Override
212221
public int process(List<File> files) throws CheckstyleException {
213222
if (cacheFile != null) {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,12 @@ else if (hasSuppressionLineColumnNumber) {
344344
}
345345

346346
// run Checker
347-
result = runCheckstyle(options, filesToProcess);
347+
result = runCheckstyle(
348+
options,
349+
filesToProcess
350+
.stream()
351+
.map(File::toPath)
352+
.collect(Collectors.toUnmodifiableList()));
348353
}
349354

350355
return result;
@@ -361,7 +366,7 @@ else if (hasSuppressionLineColumnNumber) {
361366
* @throws CheckstyleException
362367
* when properties file could not be loaded
363368
*/
364-
private static int runCheckstyle(CliOptions options, List<File> filesToProcess)
369+
private static int runCheckstyle(CliOptions options, List<Path> filesToProcess)
365370
throws CheckstyleException, IOException {
366371
// setup the properties
367372
final Properties props;

src/main/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTask.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private void realExecute(String checkstyleVersion) {
330330
private void processFiles(RootModule rootModule, final SeverityLevelCounter warningCounter,
331331
final String checkstyleVersion) {
332332
final long startTime = System.currentTimeMillis();
333-
final List<File> files = getFilesToCheck();
333+
final List<Path> files = getFilesToCheck();
334334
final long endTime = System.currentTimeMillis();
335335
log("To locate the files took " + (endTime - startTime) + TIME_SUFFIX,
336336
Project.MSG_VERBOSE);
@@ -485,21 +485,20 @@ private AuditListener[] getListeners() {
485485
*
486486
* @return the list of files included via the fileName, filesets and paths.
487487
*/
488-
private List<File> getFilesToCheck() {
489-
final List<File> allFiles = new ArrayList<>();
488+
private List<Path> getFilesToCheck() {
489+
final List<Path> allFiles = new ArrayList<>();
490490
if (fileName != null) {
491491
// oops, we've got an additional one to process, don't
492492
// forget it. No sweat, it's fully resolved via the setter.
493493
log("Adding standalone file for audit", Project.MSG_VERBOSE);
494-
allFiles.add(Path.of(fileName).toFile());
494+
allFiles.add(Path.of(fileName));
495495
}
496496

497-
final List<File> filesFromFileSets = scanFileSets();
497+
final List<Path> filesFromFileSets = scanFileSets();
498498
allFiles.addAll(filesFromFileSets);
499499

500500
final List<Path> filesFromPaths = scanPaths();
501501
allFiles.addAll(filesFromPaths.stream()
502-
.map(Path::toFile)
503502
.collect(Collectors.toUnmodifiableList()));
504503

505504
return allFiles;
@@ -563,7 +562,7 @@ private List<Path> scanPath(org.apache.tools.ant.types.Path path, int pathIndex)
563562
*
564563
* @return the list of files included via the filesets.
565564
*/
566-
protected List<File> scanFileSets() {
565+
protected List<Path> scanFileSets() {
567566
final List<Path> allFiles = new ArrayList<>();
568567

569568
for (int i = 0; i < fileSets.size(); i++) {
@@ -574,7 +573,6 @@ protected List<File> scanFileSets() {
574573
}
575574

576575
return allFiles.stream()
577-
.map(Path::toFile)
578576
.collect(Collectors.toUnmodifiableList());
579577
}
580578

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

Lines changed: 14 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
/**
@@ -43,6 +45,18 @@ public interface RootModule extends Configurable {
4345
*/
4446
int process(List<File> files) throws CheckstyleException;
4547

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

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,11 @@ protected final void verify(Checker checker,
490490
throws Exception {
491491
stream.flush();
492492
stream.reset();
493-
final List<File> theFiles = new ArrayList<>();
494-
Collections.addAll(theFiles, processedFiles);
495-
final int errs = checker.process(theFiles);
493+
final int errs = checker.process(
494+
Arrays
495+
.stream(processedFiles)
496+
.map(File::toPath)
497+
.collect(Collectors.toUnmodifiableList()));
496498

497499
// process each of the lines
498500
final Map<String, List<String>> actualViolations = getActualViolations(errs);
@@ -540,8 +542,8 @@ protected final void verifyWithLimitedResources(String fileName, String... expec
540542
*/
541543
protected final void execute(Configuration config, String... filenames) throws Exception {
542544
final Checker checker = createChecker(config);
543-
final List<File> files = Arrays.stream(filenames)
544-
.map(File::new)
545+
final List<Path> files = Arrays.stream(filenames)
546+
.map(Path::of)
545547
.collect(Collectors.toUnmodifiableList());
546548
checker.process(files);
547549
checker.destroy();
@@ -555,8 +557,8 @@ protected final void execute(Configuration config, String... filenames) throws E
555557
* @throws Exception if there is a problem during checker configuration
556558
*/
557559
protected static void execute(Checker checker, String... filenames) throws Exception {
558-
final List<File> files = Arrays.stream(filenames)
559-
.map(File::new)
560+
final List<Path> files = Arrays.stream(filenames)
561+
.map(Path::of)
560562
.collect(Collectors.toUnmodifiableList());
561563
checker.process(files);
562564
checker.destroy();
@@ -649,7 +651,7 @@ private List<String> getActualViolationsForFile(Configuration config,
649651
String file) throws Exception {
650652
stream.flush();
651653
stream.reset();
652-
final List<File> files = Collections.singletonList(new File(file));
654+
final List<Path> files = Collections.singletonList(Path.of(file));
653655
final Checker checker = createChecker(config);
654656
final Map<String, List<String>> actualViolations =
655657
getActualViolations(checker.process(files));

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package com.puppycrawl.tools.checkstyle;
2121

22+
import static com.google.common.truth.Truth.assertThat;
2223
import static com.google.common.truth.Truth.assertWithMessage;
2324
import static com.puppycrawl.tools.checkstyle.Checker.EXCEPTION_MSG;
2425
import static com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_FINISHED_MESSAGE;
@@ -55,8 +56,10 @@
5556
import java.util.UUID;
5657
import java.util.stream.Collectors;
5758

59+
import org.apache.commons.lang3.RandomUtils;
5860
import org.junit.jupiter.api.Test;
5961
import org.junit.jupiter.api.io.TempDir;
62+
import org.mockito.Mockito;
6063

6164
import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions;
6265
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
@@ -132,6 +135,16 @@ protected String getPackageLocation() {
132135
return "com/puppycrawl/tools/checkstyle/checker";
133136
}
134137

138+
@Test
139+
public void testPath() throws Exception {
140+
final Checker checker = Mockito.spy(new Checker());
141+
final File file = new File("");
142+
final int count = RandomUtils.nextInt();
143+
Mockito.when(checker.process(List.of(file))).thenReturn(count);
144+
Mockito.verify(checker).process(List.of(file));
145+
assertThat(checker.process(Set.of(file.toPath()))).isEqualTo(count);
146+
}
147+
135148
@Test
136149
public void testDestroy() throws Exception {
137150
final Checker checker = new Checker();

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package com.puppycrawl.tools.checkstyle.internal.testmodules;
2121

22-
import java.io.File;
22+
import java.nio.file.Path;
2323
import java.util.Collections;
2424
import java.util.List;
2525

@@ -28,25 +28,8 @@
2828
public class CheckstyleAntTaskStub extends CheckstyleAntTask {
2929

3030
@Override
31-
protected List<File> scanFileSets() {
32-
return Collections.singletonList(new MockFile());
33-
}
34-
35-
private static final class MockFile extends File {
36-
37-
/** A unique serial version identifier. */
38-
private static final long serialVersionUID = -2903929010510199407L;
39-
40-
private MockFile() {
41-
super("mock");
42-
}
43-
44-
/** This method is overridden to simulate an exception. */
45-
@Override
46-
public long lastModified() {
47-
throw new SecurityException("mock");
48-
}
49-
31+
protected List<Path> scanFileSets() {
32+
return Collections.singletonList(new PathMock());
5033
}
5134

5235
}

0 commit comments

Comments
 (0)
0