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

ERROR: sonar.cxx.other.xslt.1.inputs file is not defined #1367

Merged
merged 4 commits into from
Dec 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
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