Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix CxxLint with unit tests #1355

Merged
merged 2 commits into from
Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cxx-lint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<artifactId>cxx-checks</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down Expand Up @@ -67,6 +71,11 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
39 changes: 16 additions & 23 deletions cxx-lint/src/main/java/org/sonar/cxx/cxxlint/CxxLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,17 @@ 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 {
parsedArgs = commandlineParser.parse(createCommandLineOptions(), 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");
}
}
Expand All @@ -145,27 +144,25 @@ public static void main(String[] args) {
}


String fileName = new File(fileToAnalyse).getName();
SensorContextTester sensorContext = SensorContextTester.create(new File(fileToAnalyse).getParentFile().toPath());
String fileName = targetFile.getName();
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));

List<CheckerData> 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) {
Expand Down Expand Up @@ -195,8 +192,8 @@ public static void main(String[] args) {
}
}

handleVCppAdditionalOptions(platformToolset, platform, elementsOfAdditionalOptions + " ",
projectFile, fileToAnalyse, configuration);
handleVCppAdditionalOptions(platformToolset, platform, elementsOfAdditionalOptions + " ",
projectFile, targetFile.getName(), configuration);
}


Expand Down Expand Up @@ -248,7 +245,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);
}

Expand Down Expand Up @@ -333,18 +330,14 @@ private static void createCheckerRules(List<CheckerData> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// }
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down