@Test public void testExecute() throws IOException { Assume.assumeTrue("Tool exists", new File(Constants.TSQL_CG_PATH_DEFAULT).exists()); SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot()); ctxTester.fileSystem().setWorkDir(folder.getRoot().toPath()); File baseFile = folder.newFile("sample.sql"); FileUtils.copyURLToFile(getClass().getResource("/tsql/sample1.sql"), baseFile); String contents = new String(Files.readAllBytes(baseFile.toPath())); DefaultInputFile ti = new TestInputFileBuilder("test", folder.getRoot(), baseFile).initMetadata(contents) .setLanguage(Constants.languageKey).setContents(contents).setProjectBaseDir(folder.getRoot().toPath()) .build(); ctxTester.fileSystem().add(ti); CGIssuesSensor s = new CGIssuesSensor(temp); s.execute(ctxTester); Assert.assertEquals(1, ctxTester.allExternalIssues().size()); Assert.assertEquals(0, ctxTester.allIssues().size()); Assert.assertEquals(1, ctxTester.allAdHocRules().size()); }
@Override public void execute(SensorContext context) { final String externalTool = context.config().get(Constants.TSQL_CG_PATH).orElse(Constants.TSQL_CG_PATH_DEFAULT) .toLowerCase(); final String sourceDir = context.fileSystem().baseDir().getAbsolutePath(); try { if (!Files.exists(Paths.get(externalTool))) { LOGGER.info("Skip running external tool as executable not found {}", externalTool); return; } final File tempResultsFile = tempFolder.newFile("temp", "results.xml"); final String[] args = new String[] { externalTool, "-source", sourceDir, "-out", tempResultsFile.getAbsolutePath(), "/include:all" }; final Process process = new ProcessBuilder(args).inheritIO().start(); LOGGER.debug("Running SQLCodeGuard with {}", Arrays.toString(args)); final int result = process.waitFor(); if (!tempResultsFile.exists() || tempResultsFile.length() == 0) { LOGGER.warn("SQLCodeGuard returned with '{}'. Arguments were: {}", result, Arrays.toString(args)); return; } try (final BOMInputStream stream = new BOMInputStream(new FileInputStream(tempResultsFile))) { final SqlIssuesList issues = read(stream); addIssues(context, issues, null); } } catch (Throwable e) { LOGGER.warn("Unexpected error", e); } }
CGIssuesSensor extends BaseSensor implements Sensor { @Override public void execute(SensorContext context) { final String externalTool = context.config().get(Constants.TSQL_CG_PATH).orElse(Constants.TSQL_CG_PATH_DEFAULT) .toLowerCase(); final String sourceDir = context.fileSystem().baseDir().getAbsolutePath(); try { if (!Files.exists(Paths.get(externalTool))) { LOGGER.info("Skip running external tool as executable not found {}", externalTool); return; } final File tempResultsFile = tempFolder.newFile("temp", "results.xml"); final String[] args = new String[] { externalTool, "-source", sourceDir, "-out", tempResultsFile.getAbsolutePath(), "/include:all" }; final Process process = new ProcessBuilder(args).inheritIO().start(); LOGGER.debug("Running SQLCodeGuard with {}", Arrays.toString(args)); final int result = process.waitFor(); if (!tempResultsFile.exists() || tempResultsFile.length() == 0) { LOGGER.warn("SQLCodeGuard returned with '{}'. Arguments were: {}", result, Arrays.toString(args)); return; } try (final BOMInputStream stream = new BOMInputStream(new FileInputStream(tempResultsFile))) { final SqlIssuesList issues = read(stream); addIssues(context, issues, null); } } catch (Throwable e) { LOGGER.warn("Unexpected error", e); } } }
CGIssuesSensor extends BaseSensor implements Sensor { @Override public void execute(SensorContext context) { final String externalTool = context.config().get(Constants.TSQL_CG_PATH).orElse(Constants.TSQL_CG_PATH_DEFAULT) .toLowerCase(); final String sourceDir = context.fileSystem().baseDir().getAbsolutePath(); try { if (!Files.exists(Paths.get(externalTool))) { LOGGER.info("Skip running external tool as executable not found {}", externalTool); return; } final File tempResultsFile = tempFolder.newFile("temp", "results.xml"); final String[] args = new String[] { externalTool, "-source", sourceDir, "-out", tempResultsFile.getAbsolutePath(), "/include:all" }; final Process process = new ProcessBuilder(args).inheritIO().start(); LOGGER.debug("Running SQLCodeGuard with {}", Arrays.toString(args)); final int result = process.waitFor(); if (!tempResultsFile.exists() || tempResultsFile.length() == 0) { LOGGER.warn("SQLCodeGuard returned with '{}'. Arguments were: {}", result, Arrays.toString(args)); return; } try (final BOMInputStream stream = new BOMInputStream(new FileInputStream(tempResultsFile))) { final SqlIssuesList issues = read(stream); addIssues(context, issues, null); } } catch (Throwable e) { LOGGER.warn("Unexpected error", e); } } CGIssuesSensor(final TempFolder tempFolder); }
CGIssuesSensor extends BaseSensor implements Sensor { @Override public void execute(SensorContext context) { final String externalTool = context.config().get(Constants.TSQL_CG_PATH).orElse(Constants.TSQL_CG_PATH_DEFAULT) .toLowerCase(); final String sourceDir = context.fileSystem().baseDir().getAbsolutePath(); try { if (!Files.exists(Paths.get(externalTool))) { LOGGER.info("Skip running external tool as executable not found {}", externalTool); return; } final File tempResultsFile = tempFolder.newFile("temp", "results.xml"); final String[] args = new String[] { externalTool, "-source", sourceDir, "-out", tempResultsFile.getAbsolutePath(), "/include:all" }; final Process process = new ProcessBuilder(args).inheritIO().start(); LOGGER.debug("Running SQLCodeGuard with {}", Arrays.toString(args)); final int result = process.waitFor(); if (!tempResultsFile.exists() || tempResultsFile.length() == 0) { LOGGER.warn("SQLCodeGuard returned with '{}'. Arguments were: {}", result, Arrays.toString(args)); return; } try (final BOMInputStream stream = new BOMInputStream(new FileInputStream(tempResultsFile))) { final SqlIssuesList issues = read(stream); addIssues(context, issues, null); } } catch (Throwable e) { LOGGER.warn("Unexpected error", e); } } CGIssuesSensor(final TempFolder tempFolder); @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context); }
CGIssuesSensor extends BaseSensor implements Sensor { @Override public void execute(SensorContext context) { final String externalTool = context.config().get(Constants.TSQL_CG_PATH).orElse(Constants.TSQL_CG_PATH_DEFAULT) .toLowerCase(); final String sourceDir = context.fileSystem().baseDir().getAbsolutePath(); try { if (!Files.exists(Paths.get(externalTool))) { LOGGER.info("Skip running external tool as executable not found {}", externalTool); return; } final File tempResultsFile = tempFolder.newFile("temp", "results.xml"); final String[] args = new String[] { externalTool, "-source", sourceDir, "-out", tempResultsFile.getAbsolutePath(), "/include:all" }; final Process process = new ProcessBuilder(args).inheritIO().start(); LOGGER.debug("Running SQLCodeGuard with {}", Arrays.toString(args)); final int result = process.waitFor(); if (!tempResultsFile.exists() || tempResultsFile.length() == 0) { LOGGER.warn("SQLCodeGuard returned with '{}'. Arguments were: {}", result, Arrays.toString(args)); return; } try (final BOMInputStream stream = new BOMInputStream(new FileInputStream(tempResultsFile))) { final SqlIssuesList issues = read(stream); addIssues(context, issues, null); } } catch (Throwable e) { LOGGER.warn("Unexpected error", e); } } CGIssuesSensor(final TempFolder tempFolder); @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context);  }