Adding fail-on-warnings command line options.
1.1 --- a/cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java Sat Aug 15 13:14:20 2015 +0200
1.2 +++ b/cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java Sun Sep 20 09:59:26 2015 +0200
1.3 @@ -107,6 +107,7 @@
1.4 import org.netbeans.modules.parsing.impl.indexing.CacheFolder;
1.5 import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdater;
1.6 import org.netbeans.spi.editor.hints.ErrorDescription;
1.7 +import org.netbeans.spi.editor.hints.Severity;
1.8 import org.netbeans.spi.java.classpath.ClassPathProvider;
1.9 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
1.10 import org.netbeans.spi.java.hints.Hint.Kind;
1.11 @@ -130,6 +131,7 @@
1.12
1.13 private static final String OPTION_APPLY = "apply";
1.14 private static final String OPTION_NO_APPLY = "no-apply";
1.15 + private static final String OPTION_FAIL_ON_WARNINGS = "fail-on-warnings";
1.16 private static final String SOURCE_LEVEL_DEFAULT = "1.7";
1.17 private static final String ACCEPTABLE_SOURCE_LEVEL_PATTERN = "(1\\.)?[2-9][0-9]*";
1.18
1.19 @@ -160,6 +162,7 @@
1.20 parser.accepts(OPTION_NO_APPLY, "do not apply changes - only print locations were the hint would be applied");
1.21 parser.accepts(OPTION_APPLY, "apply changes");
1.22 parser.accepts("show-gui", "show configuration dialog");
1.23 + parser.accepts(OPTION_FAIL_ON_WARNINGS, "fail when warnings are detected");
1.24
1.25 OptionSet parsed;
1.26
1.27 @@ -375,11 +378,19 @@
1.28
1.29 if (apply) {
1.30 apply(hints, rootFolders.toArray(new Folder[0]), progress, hintSettings, parsed.valueOf(out));
1.31 +
1.32 + return 0;
1.33 } else {
1.34 - findOccurrences(hints, rootFolders.toArray(new Folder[0]), progress, hintSettings, parsed.valueOf(out));
1.35 + WarningsAndErrors wae = new WarningsAndErrors();
1.36 +
1.37 + findOccurrences(hints, rootFolders.toArray(new Folder[0]), progress, hintSettings, wae);
1.38 +
1.39 + if (wae.errors != 0 || (wae.warnings != 0 && parsed.has(OPTION_FAIL_ON_WARNINGS))) {
1.40 + return 1;
1.41 + } else {
1.42 + return 0;
1.43 + }
1.44 }
1.45 - } catch (Throwable e) {
1.46 - e.printStackTrace();
1.47 } finally {
1.48 for (Object toUnRegister : register2Lookup) {
1.49 MainLookup.unregister(toUnRegister);
1.50 @@ -398,8 +409,6 @@
1.51 }
1.52 }
1.53 }
1.54 -
1.55 - return 0;
1.56 }
1.57
1.58 private static Map<HintMetadata, Collection<? extends HintDescription>> listHints(ClassPath sourceFrom, ClassPath binaryFrom) {
1.59 @@ -465,7 +474,7 @@
1.60 System.setProperty("RepositoryUpdate.increasedLogLevel", "OFF");
1.61 }
1.62
1.63 - private static void findOccurrences(Iterable<? extends HintDescription> descs, Folder[] sourceRoot, ProgressHandleWrapper progress, HintsSettings settings, File out) throws IOException {
1.64 + private static void findOccurrences(Iterable<? extends HintDescription> descs, Folder[] sourceRoot, ProgressHandleWrapper progress, HintsSettings settings, final WarningsAndErrors wae) throws IOException {
1.65 final Map<String, String> id2DisplayName = Utils.computeId2DisplayName(descs);
1.66 ProgressHandleWrapper w = progress.startNextPartWithEmbedding(1, 1);
1.67 BatchResult occurrences = BatchSearch.findOccurrences(descs, Scopes.specifiedFoldersScope(sourceRoot), w, settings);
1.68 @@ -475,7 +484,7 @@
1.69 @Override public void groupStarted() {}
1.70 @Override public boolean spansVerified(CompilationController wc, Resource r, Collection<? extends ErrorDescription> hints) throws Exception {
1.71 for (ErrorDescription ed : hints) {
1.72 - print(ed, id2DisplayName);
1.73 + print(ed, wae, id2DisplayName);
1.74 }
1.75 return true;
1.76 }
1.77 @@ -486,7 +495,7 @@
1.78 }, problems, new AtomicBoolean());
1.79 }
1.80
1.81 - private static void print(ErrorDescription error, Map<String, String> id2DisplayName) throws IOException {
1.82 + private static void print(ErrorDescription error, WarningsAndErrors wae, Map<String, String> id2DisplayName) throws IOException {
1.83 int lineNumber = error.getRange().getBegin().getLine();
1.84 String line = error.getFile().asLines().get(lineNumber);
1.85 int column = error.getRange().getBegin().getColumn();
1.86 @@ -503,7 +512,15 @@
1.87 b.append('^');
1.88
1.89 String idDisplayName = Utils.categoryName(error.getId(), id2DisplayName);
1.90 - System.out.println(FileUtil.getFileDisplayName(error.getFile()) + ":" + (lineNumber + 1) + ": warning: " + idDisplayName + error.getDescription());
1.91 + String severity;
1.92 + if (error.getSeverity() == Severity.ERROR) {
1.93 + severity = "error";
1.94 + wae.errors++;
1.95 + } else {
1.96 + severity = "warning";
1.97 + wae.warnings++;
1.98 + }
1.99 + System.out.println(FileUtil.getFileDisplayName(error.getFile()) + ":" + (lineNumber + 1) + ": " + severity + ": " + idDisplayName + error.getDescription());
1.100 System.out.println(line);
1.101 System.out.println(b);
1.102 }
1.103 @@ -625,6 +642,11 @@
1.104 }
1.105 }
1.106
1.107 + private static final class WarningsAndErrors {
1.108 + private int warnings;
1.109 + private int errors;
1.110 + }
1.111 +
1.112 @ServiceProvider(service=Lookup.class)
1.113 public static final class LookupProviderImpl extends ProxyLookup {
1.114
2.1 --- a/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/CreateToolTest.java Sat Aug 15 13:14:20 2015 +0200
2.2 +++ b/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/CreateToolTest.java Sun Sep 20 09:59:26 2015 +0200
2.3 @@ -71,7 +71,7 @@
2.4 private static File compiler;
2.5
2.6 @Override
2.7 - protected void reallyRunCompiler(File workingDir, String[] output, String... params) throws Exception {
2.8 + protected void reallyRunCompiler(File workingDir, int exitcode, String[] output, String... params) throws Exception {
2.9 assertNotNull(compiler);
2.10 List<String> ll = new LinkedList<String>();
2.11 ll.add("java");
2.12 @@ -90,7 +90,7 @@
2.13 outCopy.start();
2.14 errCopy.start();
2.15
2.16 - assertEquals(0, p.waitFor());
2.17 + assertEquals(exitcode, p.waitFor());
2.18
2.19 outCopy.doJoin();
2.20 errCopy.doJoin();
3.1 --- a/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java Sat Aug 15 13:14:20 2015 +0200
3.2 +++ b/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java Sun Sep 20 09:59:26 2015 +0200
3.3 @@ -399,9 +399,41 @@
3.4 "${workdir}/src/test");
3.5 }
3.6
3.7 + public void testWarningsAreErrors() throws Exception {
3.8 + String code =
3.9 + "package test;\n" +
3.10 + "public class Test {\n" +
3.11 + " private void test(java.util.Collection c) {\n" +
3.12 + " boolean b1 = c.size() == 0;\n" +
3.13 + "\tboolean b2 = c.size() == 0;\n" +
3.14 + " }\n" +
3.15 + "}\n";
3.16 +
3.17 + doRunCompiler(code,
3.18 + "${workdir}/src/test/Test.java:4: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
3.19 + " boolean b1 = c.size() == 0;\n" +
3.20 + " ^\n" +
3.21 + "${workdir}/src/test/Test.java:5: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
3.22 + "\tboolean b2 = c.size() == 0;\n" +
3.23 + "\t ^\n",
3.24 + null,
3.25 + 1,
3.26 + "src/test/Test.java",
3.27 + code,
3.28 + null,
3.29 + "--hint",
3.30 + "Usage of .size() == 0",
3.31 + "--no-apply",
3.32 + "--fail-on-warnings");
3.33 + }
3.34 +
3.35 private static final String DONT_APPEND_PATH = new String("DONT_APPEND_PATH");
3.36
3.37 private void doRunCompiler(String golden, String stdOut, String stdErr, String... fileContentAndExtraOptions) throws Exception {
3.38 + doRunCompiler(golden, stdOut, stdErr, 0, fileContentAndExtraOptions);
3.39 + }
3.40 +
3.41 + private void doRunCompiler(String golden, String stdOut, String stdErr, int exitcode, String... fileContentAndExtraOptions) throws Exception {
3.42 List<String> fileAndContent = new LinkedList<String>();
3.43 List<String> extraOptions = new LinkedList<String>();
3.44 List<String> fileContentAndExtraOptionsList = Arrays.asList(fileContentAndExtraOptions);
3.45 @@ -448,7 +480,7 @@
3.46
3.47 String[] output = new String[2];
3.48
3.49 - reallyRunCompiler(wd, output, options.toArray(new String[0]));
3.50 + reallyRunCompiler(wd, exitcode, output, options.toArray(new String[0]));
3.51
3.52 assertEquals(golden, TestUtilities.copyFileToString(source));
3.53
3.54 @@ -461,7 +493,7 @@
3.55 }
3.56 }
3.57
3.58 - protected void reallyRunCompiler(File workDir, String[] output, String... params) throws Exception {
3.59 + protected void reallyRunCompiler(File workDir, int exitcode, String[] output, String... params) throws Exception {
3.60 String oldUserDir = System.getProperty("user.dir");
3.61
3.62 System.setProperty("user.dir", workDir.getAbsolutePath());
3.63 @@ -475,7 +507,7 @@
3.64 System.setErr(new PrintStream(errData, true, "UTF-8"));
3.65
3.66 try {
3.67 - assertEquals(0, Main.compile(params));
3.68 + assertEquals(exitcode, Main.compile(params));
3.69 } finally {
3.70 System.setProperty("user.dir", oldUserDir);
3.71 System.out.close();