From dfa0ee87513f564b5c936eed7065ab9df15eaba2 Mon Sep 17 00:00:00 2001 From: Pawan Hegde Date: Tue, 28 May 2019 16:21:10 +0530 Subject: [PATCH] Make LintRunner accept multiple targets - Added Unit test - Make LintRunner not fail on creation if path doesn't contain valid JSON files --- build.gradle | 1 + .../com/zachary_moore/runner/LintRunner.java | 82 +++++++++---------- .../runner/LintRunnerShould.java | 15 ++++ 3 files changed, 56 insertions(+), 42 deletions(-) diff --git a/build.gradle b/build.gradle index 3df7ec4..5fa7773 100644 --- a/build.gradle +++ b/build.gradle @@ -92,6 +92,7 @@ test { } dependencies { + implementation group: 'commons-io', name: 'commons-io', version: '2.6' implementation group: 'org.json', name: 'json', version: '20180813' implementation group: 'com.j2html', name: 'j2html', version: '1.4.0' diff --git a/src/main/java/com/zachary_moore/runner/LintRunner.java b/src/main/java/com/zachary_moore/runner/LintRunner.java index ae3c2de..bad5c11 100644 --- a/src/main/java/com/zachary_moore/runner/LintRunner.java +++ b/src/main/java/com/zachary_moore/runner/LintRunner.java @@ -5,78 +5,74 @@ import com.zachary_moore.lint.LintRegister; import com.zachary_moore.lint.LintRule; import com.zachary_moore.objects.JSONFile; - import java.io.File; import java.io.IOException; -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.logging.Logger; import java.util.stream.Collectors; +import org.apache.commons.io.FileUtils; + public class LintRunner { - private List filesToLint; + private String[] basePaths; private Map>> lintOutput; private final LintRegister lintRegister; /** * Create a LintRunner * @param lintRegister representation of {@link LintRule} to run against - * @param basePath Base Path to setup {@link JSONFile} from + * @param basePaths Paths to folders or files to fetch {@link JSONFile}(s) from */ public LintRunner(LintRegister lintRegister, - String basePath) { + String... basePaths) { this.lintRegister = lintRegister; - - File basePathFile = new File(basePath); - if (basePathFile.isFile()) { - try { - this.filesToLint = new ArrayList<>(Collections. - singletonList(new JSONFile(basePathFile))); - } catch (IOException e) { - throw new RuntimeException(e); - } - } else if (basePathFile.isDirectory()){ - this.filesToLint = getAllFiles(basePath).stream() - .map(f -> { - try { - return new JSONFile(f); - } catch (IOException e) { - throw new RuntimeException(e); - } - }) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - } else { - throw new RuntimeException("Input path is neither a file nor directory"); - } + this.basePaths = basePaths; } - private List getAllFiles(String basePath) { - List files = new ArrayList<>(); - File directory = new File(basePath); + private Set getFilesToLint() { + Set files = new HashSet<>(); - File[] fList = directory.listFiles(); - if (fList != null) { - for (File file : fList) { - if (file.isFile()) { - files.add(file); - } else if (file.isDirectory()) { - files.addAll(getAllFiles(file.getAbsolutePath())); - } + for (String basePath : this.basePaths) { + File file = new File(basePath); + + if (file.isFile()) { + files.add(file); + } else if (file.isDirectory()) { + files.addAll(FileUtils.listFiles(file, null, true)); + } else { + // TODO: Add proper logging + Logger.getGlobal().warning(basePath + " is not a valid file path"); } } - return files; + + return files.stream().map(file -> { + try { + return new JSONFile(file); + } catch (IOException e) { + // Is not a JSON file + return null; + } + }).filter(Objects::nonNull).collect(Collectors.toSet()); } /** - * Lint all files in given path in constructor + * Lint all files in given path in constructor and store the output * @return Representation of any lint issues */ public Map>> lint() { Map>> lintOutput = new HashMap<>(); + Set filesToLint = getFilesToLint(); + for (LintRule lintRule : lintRegister.getLintRules()) { try { if (lintRule.getLevel() != LintLevel.IGNORE) { - Map> lintReports = lintRule.lint(filesToLint.toArray(new JSONFile[filesToLint.size()])); + Map> lintReports = lintRule.lint(filesToLint.toArray(new JSONFile[0])); if (lintReports.size() != 0) { lintOutput.put(lintRule, lintReports); } @@ -87,6 +83,7 @@ public Map>> lint() { } } this.lintOutput = lintOutput; + return lintOutput; } @@ -105,6 +102,7 @@ public int analyzeLintAndGiveExitCode() { return 1; } } + return 0; } } diff --git a/src/test/java/com/zachary_moore/runner/LintRunnerShould.java b/src/test/java/com/zachary_moore/runner/LintRunnerShould.java index 708742a..c475962 100644 --- a/src/test/java/com/zachary_moore/runner/LintRunnerShould.java +++ b/src/test/java/com/zachary_moore/runner/LintRunnerShould.java @@ -74,4 +74,19 @@ public void lintRunnerShouldGiveExitStatus0WithIgnore() throws Exception { lintRunner.lint(); assert(lintRunner.analyzeLintAndGiveExitCode() == 0); } + + @Test + public void lintRunnerShouldTakeMultipleFiles() throws Exception { + LintRule lintRule = builder.setLevel(LintLevel.ERROR).build(); + this.lintRegister.register(lintRule); + LintRunner lintRunner = + new LintRunner( + this.lintRegister, + "./src/test/resources/test-2.json", + "./src/test/resources/test-file.pdsc"); + Map>> lintOutput = lintRunner.lint(); + + assert(lintRunner.analyzeLintAndGiveExitCode() == 1); + assert(lintOutput.get(lintRule).size() == 2); + } }