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

Uppercase file paths in Xunit reports are changed to lowercase on case sensitive operating systems #2303

Merged
merged 2 commits into from
Jan 25, 2022
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 @@ -19,12 +19,17 @@
*/
package org.sonar.cxx.sensors.tests.xunit;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

import javax.xml.stream.XMLStreamException;

import org.codehaus.staxmate.in.ElementFilter;
import org.codehaus.staxmate.in.SMHierarchicCursor;
import org.codehaus.staxmate.in.SMInputCursor;
Expand All @@ -39,7 +44,7 @@
public class XunitReportParser implements XmlStreamHandler {

private final String baseDir;
private final Map<String, TestFile> testFiles = new HashMap<>();
private final Map<Path, TestFile> testFiles = new HashMap<>();

public XunitReportParser(String baseDir) {
this.baseDir = baseDir;
Expand Down Expand Up @@ -127,15 +132,15 @@ private void parseTestCaseTag(SMInputCursor testCaseCursor, String tsName, Strin
}

private TestFile getTestFile(String filename) {
String absolute = CxxUtils.resolveAntPath(baseDir, filename);
if (absolute != null) {
absolute = absolute.toLowerCase();
}
var file = testFiles.get(absolute);
var absolute = Optional.ofNullable(CxxUtils.resolveAntPath(baseDir, filename))
.map(p -> Paths.get(p));

var file = testFiles.get(absolute.orElse(null));
if (file == null) {
file = new TestFile(absolute);
testFiles.put(absolute, file);
file = new TestFile(absolute.map(Object::toString).orElse(null));
testFiles.put(absolute.orElse(null), file);
}

return file;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@
*/
package org.sonar.cxx.sensors.tests.xunit;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.TreeMap;
import static org.junit.Assert.assertEquals;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.assertj.core.util.Strings;
import org.junit.Test;
import org.sonar.cxx.sensors.utils.StaxParser;
import org.sonar.cxx.sensors.utils.TestUtils;
Expand Down Expand Up @@ -68,4 +76,27 @@ public void shouldThrowWhenGivenInvalidTime() throws javax.xml.stream.XMLStreamE
parser.parse(report);
}

@Test
public void testFilePaths() throws javax.xml.stream.XMLStreamException {
parserHandler = new XunitReportParser("");
parser = new StaxParser(parserHandler, false);
File report = TestUtils.loadResource(pathPrefix + "xunit-result-SAMPLE-inconsistent-case.xml");
parser.parse(report);

var actualPaths = parserHandler.getTestFiles()
.stream()
.map(TestFile::getFilename)
.filter(p -> !Strings.isNullOrEmpty(p))
.map(s -> Paths.get(s))
.collect(Collectors.toList());

var expectPaths = Stream.of(
Paths.get("/test/file.cpp"),
Paths.get("/test/File.cpp"),
Paths.get("/TEST/file.cpp"))
.distinct()
.toArray(n -> new Path[n]);

assertThat(actualPaths).containsExactlyInAnyOrder(expectPaths);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="0" failures="0" tests="3" name="SAMPLEtest">
<properties>
<property value="4.6.2" name="QTestVersion"/>
<property value="4.6.2" name="QtVersion"/>
</properties>
<testcase result="pass" name="initTestCase" filename="test/file.cpp" />
<testcase result="pass" name="testCase1" filename="test/File.cpp" />
<testcase result="pass" name="testCase2" filename="test/File.cpp" />
<testcase result="pass" name="cleanupTestCase" filename="TEST/file.cpp" />
<system-err/>
</testsuite>