diff --git a/cxx-lint/pom.xml b/cxx-lint/pom.xml index f448c75e92..76a32a782a 100644 --- a/cxx-lint/pom.xml +++ b/cxx-lint/pom.xml @@ -19,6 +19,10 @@ cxx-checks ${project.version} + + ch.qos.logback + logback-classic + org.slf4j slf4j-api @@ -67,6 +71,11 @@ 1.3 test + + org.assertj + assertj-core + test + diff --git a/cxx-lint/src/main/java/org/sonar/cxx/cxxlint/CxxLint.java b/cxx-lint/src/main/java/org/sonar/cxx/cxxlint/CxxLint.java index 7450d9c560..451884bdb5 100644 --- a/cxx-lint/src/main/java/org/sonar/cxx/cxxlint/CxxLint.java +++ b/cxx-lint/src/main/java/org/sonar/cxx/cxxlint/CxxLint.java @@ -109,8 +109,8 @@ public static void main(String[] args) { Options options = createCommandLineOptions(); CommandLine parsedArgs = null; String settingsFile = ""; - String fileToAnalyse = ""; String encodingOfFile = "UTF-8"; + File targetFile = null; boolean isNotOptionS = true; try { @@ -118,9 +118,8 @@ public static void main(String[] args) { if (!parsedArgs.hasOption("f")) { throw new ParseException("f option mandatory"); } else { - fileToAnalyse = parsedArgs.getOptionValue("f"); - File f = new File(fileToAnalyse); - if (!f.exists()) { + targetFile = new File(parsedArgs.getOptionValue("f")); + if (!targetFile.exists()) { throw new ParseException("file to analysis not found"); } } @@ -145,27 +144,24 @@ public static void main(String[] args) { } - String fileName = new File(fileToAnalyse).getName(); - SensorContextTester sensorContext = SensorContextTester.create(new File(fileToAnalyse).getParentFile().toPath()); + SensorContextTester sensorContext = SensorContextTester.create(targetFile.getParentFile().toPath()); CxxConfiguration configuration = new CxxConfiguration(Charset.forName(encodingOfFile), new CppLanguage(sensorContext.config())); try { - String content = new String(Files.readAllBytes(Paths.get(fileToAnalyse)), encodingOfFile); - sensorContext.fileSystem().add(TestInputFileBuilder.create("ProjectKey", fileName).initMetadata(content).build()); + sensorContext.fileSystem().add(TestInputFileBuilder.create("", targetFile.getParentFile(), targetFile).build()); InputFile cxxFile = sensorContext.fileSystem().inputFile(sensorContext.fileSystem().predicates() - .hasPath(fileName)); - + .hasPath(targetFile.getName())); List rulesData = new ArrayList<>(); if (!"".equals(settingsFile)) { JsonParser parser = new JsonParser(); String fileContent = readFile(settingsFile); // get basic information - String platformToolset = getJsonStringValue(parser, fileContent, "platformToolset"); - String platform = getJsonStringValue(parser, fileContent, "platform"); - String projectFile = getJsonStringValue(parser, fileContent, "projectFile"); + String platformToolset = getJsonStringValue(parser, fileContent, "platformToolset"); + String platform = getJsonStringValue(parser, fileContent, "platform"); + String projectFile = getJsonStringValue(parser, fileContent, "projectFile"); JsonElement rules = parser.parse(fileContent).getAsJsonObject().get("rules"); if (rules != null) { @@ -195,8 +191,8 @@ public static void main(String[] args) { } } - handleVCppAdditionalOptions(platformToolset, platform, elementsOfAdditionalOptions + " ", - projectFile, fileToAnalyse, configuration); + handleVCppAdditionalOptions(platformToolset, platform, elementsOfAdditionalOptions + " ", + projectFile, targetFile.getName(), configuration); } @@ -248,7 +244,7 @@ public static void main(String[] args) { LOG.info("LOC: {}", file.getInt(CxxMetric.LINES_OF_CODE)); LOG.info("COMPLEXITY: {}", file.getInt(CxxMetric.COMPLEXITY)); - } catch (IOException|InstantiationException|IllegalAccessException ex) { + } catch (InstantiationException|IllegalAccessException ex) { LOG.error("{}", ex); } @@ -333,18 +329,14 @@ private static void createCheckerRules(List rulesData, JsonElement for (JsonElement rule : rules.getAsJsonArray()) { JsonObject data = rule.getAsJsonObject(); String ruleId = data.get("ruleId").getAsString(); - + String templateKey = ""; - try { + if (data.has("templateKeyId")) { templateKey = data.get("templateKeyId").getAsString(); - } catch(Exception ex) { - if (LOG.isDebugEnabled()) { - LOG.debug("CxxLint exception in createCheckerRules {}", ex); - } } - + String enabled = data.get("status").getAsString(); - + CheckerData check = new CheckerData(); check.setId(ruleId); check.setTemplateId(templateKey); diff --git a/cxx-lint/src/test/java/org/codehaus/sonarplugins/cxx/cxxlint/CxxLintTest.java b/cxx-lint/src/test/java/org/codehaus/sonarplugins/cxx/cxxlint/CxxLintTest.java index ce2e58e66b..74feb1b26b 100644 --- a/cxx-lint/src/test/java/org/codehaus/sonarplugins/cxx/cxxlint/CxxLintTest.java +++ b/cxx-lint/src/test/java/org/codehaus/sonarplugins/cxx/cxxlint/CxxLintTest.java @@ -19,48 +19,56 @@ */ package org.codehaus.sonarplugins.cxx.cxxlint; +import java.io.File; + +import org.junit.Test; +import org.sonar.cxx.cxxlint.CxxLint; + + +import static org.assertj.core.api.Assertions.assertThat; /** * * @author jocs */ public class CxxLintTest { - +// private static final Logger LOG = Loggers.get(CxxLintTest.class); + /** * Test of main method, of class CxxLint. */ -// @Test -// public void runsToolWithoutSettingsWithoutExceptions() throws IllegalAccessException, IOException, Exception { -// ClassLoader classLoader = getClass().getClassLoader(); -// File fileToAnalyse = new File(classLoader.getResource("PathHandle.cpp").getFile()); -// -// String[] args = new String[2]; -// args[0] = "-f"; -// args[1] = fileToAnalyse.getAbsolutePath(); -// -// CxxLint.main(args); -// assertTrue(true); -// } + @Test + public void runsToolWithoutSettingsWithoutExceptions() { + ClassLoader classLoader = getClass().getClassLoader(); + File fileToAnalyse = new File(classLoader.getResource("PathHandle.cpp").getFile()); + + String[] args = new String[2]; + args[0] = "-f"; + args[1] = fileToAnalyse.getAbsolutePath(); + CxxLint.main(args); + assertThat(true); + } /** * Test of main method, of class CxxLint. */ -// @Test -// public void runsToolWithSettingsWithoutExceptions() { -// ClassLoader classLoader = getClass().getClassLoader(); -// File fileToAnalyse = new File(classLoader.getResource("PathHandle.cpp").getFile()); -// File settingsFile = new File(classLoader.getResource("4b4b9c5c-05f3-42e1-b94f-4c74b53241e3.json").getFile()); -// -// String[] args = new String[4]; -// args[0] = "-f"; -// args[1] = fileToAnalyse.getAbsolutePath(); -// args[2] = "-s"; -// args[3] = settingsFile.getAbsolutePath(); -// + @Test + public void runsToolWithSettingsWithoutExceptions() { + ClassLoader classLoader = getClass().getClassLoader(); + File fileToAnalyse = new File(classLoader.getResource("PathHandle.cpp").getFile()); + File settingsFile = new File(classLoader.getResource("4b4b9c5c-05f3-42e1-b94f-4c74b53241e3.json").getFile()); + + String[] args = new String[4]; + args[0] = "-f"; + args[1] = fileToAnalyse.getAbsolutePath(); + args[2] = "-s"; + args[3] = settingsFile.getAbsolutePath(); + // try { -// CxxLint.main(args); -// assertTrue(true); + CxxLint.main(args); + assertThat(true); // } catch (Exception ex) { -// assertTrue("Exception Found: " + ex.getMessage(), false); +// LOG.info("Exception Found: " + ex); +// assertThat(false); // } -// } + } } diff --git a/cxx-lint/src/test/resources/4b4b9c5c-05f3-42e1-b94f-4c74b53241e3.json b/cxx-lint/src/test/resources/4b4b9c5c-05f3-42e1-b94f-4c74b53241e3.json index e67920dc6f..3b6026d69a 100644 --- a/cxx-lint/src/test/resources/4b4b9c5c-05f3-42e1-b94f-4c74b53241e3.json +++ b/cxx-lint/src/test/resources/4b4b9c5c-05f3-42e1-b94f-4c74b53241e3.json @@ -7,9 +7,7 @@ "C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/ATLMFC/INCLUDE", "C:/Program Files (x86)/Windows Kits/8.1/include/shared", "C:/Program Files (x86)/Windows Kits/8.1/include/um", - "C:/Program Files (x86)/Windows Kits/8.1/include/winrt", - "D:/prod/structures/Packages/gtestmock.1.7.5/build/native/include/", - "D:/prod/structures/Packages/gtestmock.1.7.5/build/native/include/googletest" + "C:/Program Files (x86)/Windows Kits/8.1/include/winrt" ], "defines": [ "_MBCS",