Skip to content

Commit

Permalink
Merge pull request #1367 from guwirth/fix-1277
Browse files Browse the repository at this point in the history
ERROR: sonar.cxx.other.xslt.1.inputs file is not defined
  • Loading branch information
guwirth authored Dec 31, 2017
2 parents 5267e4b + ec01e77 commit 6878757
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -88,24 +89,17 @@ public void processReport(final SensorContext context, File report) throws XMLSt
URISyntaxException, TransformerException {
LOG.debug("Parsing 'other' format");

StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() {
StaxParser parser = new StaxParser((SMHierarchicCursor rootCursor) -> {
rootCursor.advance();

/**
* {@inheritDoc}
*/
@Override
public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
rootCursor.advance();
SMInputCursor errorCursor = rootCursor.childElementCursor("error");
while (errorCursor.getNext() != null) {
String file = errorCursor.getAttrValue("file");
String line = errorCursor.getAttrValue("line");
String id = errorCursor.getAttrValue("id");
String msg = errorCursor.getAttrValue("msg");

SMInputCursor errorCursor = rootCursor.childElementCursor("error");
while (errorCursor.getNext() != null) {
String file = errorCursor.getAttrValue("file");
String line = errorCursor.getAttrValue("line");
String id = errorCursor.getAttrValue("id");
String msg = errorCursor.getAttrValue("msg");

saveUniqueViolation(context, CxxOtherRepository.KEY, file, line, id, msg);
}
saveUniqueViolation(context, CxxOtherRepository.KEY, file, line, id, msg);
}
});

Expand All @@ -118,45 +112,34 @@ protected String getSensorKey() {
}

public void transformFiles(final File baseDir, SensorContext context) {
boolean goOn = true;
for (int i = 1; (i < MAX_STYLESHEETS) && goOn; i++) {
for (int i = 1; i < MAX_STYLESHEETS; i++) {
String stylesheetKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + STYLESHEET_KEY);
String inputKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + INPUT_KEY);
String outputKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + OUTPUT_KEY);

if (stylesheetKey == null) {
LOG.error("'{}' is not defined.", OTHER_XSLT_KEY + i + STYLESHEET_KEY);
break;
String stylesheet = stylesheetKey == null ? null : resolveFilename(baseDir.getAbsolutePath(), context.config().get(stylesheetKey).orElse(null));
List<File> inputs = inputKey == null ? new ArrayList<>() : getReports(context.config(), baseDir, inputKey);
String[] outputStrings = outputKey == null ? null : context.config().getStringArray(outputKey);
List<String> outputs = outputStrings == null ? new ArrayList<>() : Arrays.asList(outputStrings);

if ((stylesheet == null) && (inputs.isEmpty()) && (outputs.isEmpty())) {
break; // no or last item
}
String stylesheet = resolveFilename(baseDir.getAbsolutePath(), context.config().get(stylesheetKey).orElse(null));

List<File> inputs = getReports(context.config(), baseDir, inputKey);
String[] outputStrings = null;
if (outputKey != null) {
outputStrings = context.config().getStringArray(outputKey);
if (stylesheet == null) {
LOG.error(stylesheetKey + " is not defined.");
break;
}
List<String> outputs = Arrays.asList((outputStrings != null) ? outputStrings : new String[]{});

if (stylesheet == null && inputKey == null && outputKey == null) {
goOn = false;
} else {
if (stylesheet == null) {
LOG.error(stylesheetKey + " is not defined.");
goOn = false;
} else {
goOn = checkInput(inputKey, outputKey, inputs, outputs);
}
if (!checkInput(inputKey, outputKey, inputs, outputs)) {
break;
}

if (goOn) {
if (LOG.isDebugEnabled()) {
LOG.debug("Converting " + stylesheet + " with " + inputs + " to " + outputs + ".");
}
File stylesheetFile = new File(stylesheet);
if (stylesheetFile.isAbsolute()) {
transformFileList(baseDir.getAbsolutePath(), stylesheetFile, inputs, outputs);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Converting " + stylesheet + " with " + inputs + " to " + outputs + ".");
}

transformFileList(baseDir.getAbsolutePath(), stylesheet, inputs, outputs);
}
}

Expand Down Expand Up @@ -213,11 +196,11 @@ private static boolean isValidInput(@Nullable String inputKey, @Nullable List<Fi
return true;
}

private void transformFileList(final String baseDir, File stylesheetFile, List<File> inputs, List<String> outputs) {
private void transformFileList(final String baseDir, String stylesheet, List<File> inputs, List<String> outputs) {
for (int j = 0; j < inputs.size(); j++) {
try {
String normalizedOutputFilename = resolveFilename(baseDir, outputs.get(j));
CxxUtils.transformFile(new StreamSource(stylesheetFile), inputs.get(j), new File(normalizedOutputFilename));
CxxUtils.transformFile(new StreamSource(new File(stylesheet)), inputs.get(j), new File(normalizedOutputFilename));
} catch (TransformerException e) {
String msg = new StringBuilder()
.append("Cannot transform report files: '")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ public void shouldReportOnlyOneViolationAndRemoveDuplicates() {
assertThat(context.allIssues()).hasSize(1);
}

@Test
public void shouldReportNothing() {
SensorContextTester context = SensorContextTester.create(fs.baseDir());
when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("other.xslt.1.stylesheet");
when(language.getPluginProperty("other.xslt.1.inputs")).thenReturn("other.xslt.1.inputs");
when(language.getPluginProperty("other.xslt.1.outputs")).thenReturn("other.xslt.1.outputs");

sensor = new CxxOtherSensor(language);
sensor.execute(context);
assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
}

@Test
public void shouldNotCreateMessage() {
SensorContextTester context = SensorContextTester.create(fs.baseDir());
Expand All @@ -166,9 +178,10 @@ public void shouldCreateMissingStylesheetMessage() {
logTester.clear();
SensorContextTester context = SensorContextTester.create(fs.baseDir());
when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("something");
when(language.getPluginProperty("other.xslt.1.outputs")).thenReturn("something");
when(language.getPluginProperty("other.xslt.1.outputs")).thenReturn("outputs");

settings.setProperty(language.getPluginProperty(CxxOtherSensor.REPORT_PATH_KEY), "externalrules-reports/externalrules-with-duplicates.xml");
settings.setProperty("outputs", "outputs");
context.setSettings(settings);

context.fileSystem().add(TestInputFileBuilder.create("ProjectKey", "sources/utils/code_chunks.cpp")
Expand Down

0 comments on commit 6878757

Please sign in to comment.