Search in sources :

Example 1 with CFLintConfiguration

use of com.cflint.config.CFLintConfiguration in project CFLint by cflint.

the class TestFiles method test.

@Test
public void test() throws IOException, URISyntaxException, JAXBException, TransformerException {
    final String inputString = FileUtil.loadFile(sourceFile);
    final File expectedFile = new File(sourceFile.getPath().replaceAll("\\.cf.", ".expected.txt"));
    final String expectedFileText = expectedFile.exists() ? FileUtil.loadFile(expectedFile) : null;
    String expectedText = expectedFileText;
    final CFLintConfiguration config = loadPluginInfo(sourceFile.getParentFile());
    CFLint cflint = new CFLint(config);
    cflint.setVerbose(true);
    cflint.setLogError(true);
    cflint.addExceptionListener(new CFLintExceptionListener() {

        @Override
        public void exceptionOccurred(Throwable exception, String messageCode, String filename, Integer line, Integer column, String functionName, String expression) {
            exception.printStackTrace();
            fail("Error scanning " + filename);
        }
    });
    cflint.process(inputString, sourceFile.getPath());
    //List<BugInfo> result = cflint.getBugs().getFlatBugList();
    StringWriter writer = new StringWriter();
    new JSONOutput().output(cflint.getBugs(), writer, false);
    String actualTree = writer.toString();
    if (expectedText == null || expectedText.trim().length() == 0) {
        writeExpectFile(expectedFile, actualTree);
        System.out.println("Tree written to " + expectedFile);
    } else {
        if (autoReplaceFailed && !actualTree.equals(expectedText)) {
            System.out.println("Replaced content of " + expectedFile);
            expectedText = actualTree;
            writeExpectFile(expectedFile, actualTree);
        }
        assertEquals(expectedText.replaceAll("\\\\", "/").replaceAll("/+", "/").replaceAll("\r\n", "\n"), actualTree.replaceAll("\\\\", "/").replaceAll("/+", "/").replaceAll("\r\n", "\n"));
    }
}
Also used : CFLintExceptionListener(com.cflint.plugins.exceptions.CFLintExceptionListener) StringWriter(java.io.StringWriter) CFLint(com.cflint.CFLint) CFLintConfiguration(com.cflint.config.CFLintConfiguration) JSONOutput(com.cflint.JSONOutput) File(java.io.File) Test(org.junit.Test)

Example 2 with CFLintConfiguration

use of com.cflint.config.CFLintConfiguration in project CFLint by cflint.

the class CFLintTask method execute.

@Override
public void execute() {
    FileInputStream fis = null;
    try {
        CFLintConfiguration config = null;
        if (configFile != null) {
            if (configFile.getName().toLowerCase().endsWith(".xml")) {
                config = ConfigUtils.unmarshal(new FileInputStream(configFile), CFLintConfig.class);
            } else {
                config = ConfigUtils.unmarshalJson(new FileInputStream(configFile), CFLintConfig.class);
            }
        }
        CFLintConfiguration cmdLineConfig = null;
        if ((excludeRule != null && excludeRule.trim().length() > 0) || (includeRule != null && includeRule.trim().length() > 0)) {
            cmdLineConfig = new CFLintConfig();
            if (includeRule != null && includeRule.trim().length() > 0) {
                for (final String code : includeRule.trim().split(",")) {
                    cmdLineConfig.addInclude(new PluginMessage(code));
                }
            }
            if (excludeRule != null && excludeRule.trim().length() > 0) {
                for (final String code : excludeRule.trim().split(",")) {
                    cmdLineConfig.addExclude(new PluginMessage(code));
                }
            }
        }
        // TODO combine configs
        final CFLint cflint = new CFLint(config);
        cflint.setVerbose(verbose);
        cflint.setQuiet(quiet);
        if (extensions != null && extensions.trim().length() > 0) {
            cflint.setAllowedExtensions(Arrays.asList(extensions.trim().split(",")));
        }
        CFLintFilter filter = CFLintFilter.createFilter(verbose);
        if (filterFile != null) {
            final File ffile = filterFile;
            if (ffile.exists()) {
                fis = new FileInputStream(ffile);
                final byte[] b = new byte[fis.available()];
                fis.read(b);
                filter = CFLintFilter.createFilter(new String(b), verbose);
            }
        }
        cflint.getBugs().setFilter(filter);
        if (xmlFile == null && htmlFile == null && textFile == null) {
            xmlFile = new File("cflint-result.xml");
        }
        if (xmlFile != null) {
            if (verbose) {
                System.out.println("Style:" + xmlStyle);
            }
            if ("findbugs".equalsIgnoreCase(xmlStyle)) {
                new XMLOutput().outputFindBugs(cflint.getBugs(), createWriter(xmlFile, StandardCharsets.UTF_8), showStats);
            } else {
                new DefaultCFlintResultMarshaller().output(cflint.getBugs(), createWriter(xmlFile, StandardCharsets.UTF_8), showStats);
            }
        }
        if (textFile != null) {
            final Writer textwriter = textFile != null ? new FileWriter(textFile) : new OutputStreamWriter(System.out);
            new TextOutput().output(cflint.getBugs(), textwriter, showStats);
        }
        if (htmlFile != null) {
            try {
                new HTMLOutput(htmlStyle).output(cflint.getBugs(), new FileWriter(htmlFile), showStats);
            } catch (final TransformerException e) {
                throw new IOException(e);
            }
        }
        for (final FileSet fileset : filesets) {
            int progress = 1;
            // 3
            final DirectoryScanner ds = fileset.getDirectoryScanner(getProject());
            final ProgressMonitor progressMonitor = showProgress && !filesets.isEmpty() ? new ProgressMonitor(null, "CFLint", "", 1, ds.getIncludedFilesCount()) : null;
            final String[] includedFiles = ds.getIncludedFiles();
            for (final String includedFile : includedFiles) {
                if (progressMonitor.isCanceled()) {
                    throw new RuntimeException("CFLint scan cancelled");
                }
                final String filename = ds.getBasedir() + File.separator + includedFile;
                progressMonitor.setNote("scanning " + includedFile);
                cflint.scan(filename);
                progressMonitor.setProgress(progress++);
            }
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (final IOException e) {
        }
    }
}
Also used : TextOutput(com.cflint.TextOutput) CFLintConfiguration(com.cflint.config.CFLintConfiguration) FileWriter(java.io.FileWriter) XMLOutput(com.cflint.XMLOutput) CFLint(com.cflint.CFLint) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) PluginMessage(com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage) DefaultCFlintResultMarshaller(com.cflint.xml.stax.DefaultCFlintResultMarshaller) TransformerException(javax.xml.transform.TransformerException) FileSet(org.apache.tools.ant.types.FileSet) HTMLOutput(com.cflint.HTMLOutput) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CFLint(com.cflint.CFLint) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ProgressMonitor(javax.swing.ProgressMonitor) CFLintConfig(com.cflint.config.CFLintConfig) CFLintFilter(com.cflint.tools.CFLintFilter) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 3 with CFLintConfiguration

use of com.cflint.config.CFLintConfiguration in project CFLint by cflint.

the class Test_Switch_Default_Checker method setUp.

@Before
public void setUp() throws IOException {
    final CFLintConfiguration conf = CFLintConfig.createDefaultLimited("CFSwitchDefaultChecker");
    cfBugs = new CFLint(conf);
}
Also used : CFLintConfiguration(com.cflint.config.CFLintConfiguration) Before(org.junit.Before)

Example 4 with CFLintConfiguration

use of com.cflint.config.CFLintConfiguration in project CFLint by cflint.

the class TestAbortChecker method setUp.

@Before
public void setUp() throws Exception {
    final CFLintConfiguration conf = CFLintConfig.createDefaultLimited("AbortChecker");
    cfBugs = new CFLint(conf);
}
Also used : CFLintConfiguration(com.cflint.config.CFLintConfiguration) Before(org.junit.Before)

Example 5 with CFLintConfiguration

use of com.cflint.config.CFLintConfiguration in project CFLint by cflint.

the class TestArgumentNames method setUp.

@Before
public void setUp() throws Exception {
    final CFLintConfiguration conf = CFLintConfig.createDefaultLimited("ArgumentNameChecker");
    cfBugs = new CFLint(conf);
}
Also used : CFLintConfiguration(com.cflint.config.CFLintConfiguration) Before(org.junit.Before)

Aggregations

CFLintConfiguration (com.cflint.config.CFLintConfiguration)7 Before (org.junit.Before)4 File (java.io.File)3 CFLint (com.cflint.CFLint)2 CFLintConfig (com.cflint.config.CFLintConfig)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ParseException (cfml.parsing.reporting.ParseException)1 HTMLOutput (com.cflint.HTMLOutput)1 JSONOutput (com.cflint.JSONOutput)1 TextOutput (com.cflint.TextOutput)1 XMLOutput (com.cflint.XMLOutput)1 CFLintChainedConfig (com.cflint.config.CFLintChainedConfig)1 PluginMessage (com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage)1 CFLintExceptionListener (com.cflint.plugins.exceptions.CFLintExceptionListener)1 CFLintFilter (com.cflint.tools.CFLintFilter)1 DefaultCFlintResultMarshaller (com.cflint.xml.stax.DefaultCFlintResultMarshaller)1 FileWriter (java.io.FileWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1