From f96ad200c0df6492a25dd5f15185a2e1bb993950 Mon Sep 17 00:00:00 2001 From: guwirth Date: Fri, 22 Oct 2021 10:58:28 +0200 Subject: [PATCH] support Cobertura coverage with drive letter only in source - source tag: drive letter without file separator was not supported in the past - close #1682 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sample: ´´´XML f: ... ... ´´´ --- .../coverage/cobertura/CoberturaParser.java | 19 ++++--- .../coverage/CxxCoberturaSensorTest.java | 57 +++++++++++-------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/cobertura/CoberturaParser.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/cobertura/CoberturaParser.java index cccb8ba01a..0148216813 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/cobertura/CoberturaParser.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/cobertura/CoberturaParser.java @@ -49,13 +49,13 @@ public class CoberturaParser implements CoverageParser { /** * Join two paths * - * path1 | path2 | result + * path1 | path2 | result * ---------|----------|------- - * empty | empty | empty - * empty | absolute | absolute path2 - * empty | relative | relative path2 - * absolute | empty | empty - * relative | empty | empty + * empty | empty | empty + * empty | absolute | absolute path2 + * empty | relative | relative path2 + * absolute | empty | empty + * relative | empty | empty * absolute | absolute | absolute path2 * absolute | relative | absolute path1 + relative path2 * relative | absolute | absolute path2 @@ -70,7 +70,12 @@ public static String join(Path path1, Path path2) { return ""; } if (!path1.isAbsolute()) { - path1 = Paths.get(".", path1.toString()); + var root = path1.getRoot(); + if (root != null && !root.toString().endsWith(File.separator)) { // special case drive letter only, e.g. c: + path1 = Paths.get(path1.toString(), File.separator); + } else { + path1 = Paths.get(".", path1.toString()); + } } if (!path2.isAbsolute()) { path2 = Paths.get(".", path2.toString()); diff --git a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/coverage/CxxCoberturaSensorTest.java b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/coverage/CxxCoberturaSensorTest.java index 252ad21e7b..c2cf7b0eb0 100644 --- a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/coverage/CxxCoberturaSensorTest.java +++ b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/coverage/CxxCoberturaSensorTest.java @@ -75,70 +75,79 @@ public void testPathJoin() { if (TestUtils.isWindows()) { // Windows - var abs1 = Paths.get("c:\\test1"); - var rel1 = Paths.get("\\test1"); - var abs2 = Paths.get("c:\\test2\\report.txt"); - var rel2 = Paths.get("\\test2\\report.txt"); + var p1_abs1 = Paths.get("c:\\test1"); + var p1_abs2 = Paths.get("c:"); + var p1_abs3 = Paths.get("c:\\"); + var p1_rel1 = Paths.get("\\test1"); + var p2_abs1 = Paths.get("c:\\test2\\report.txt"); + var p2_rel1 = Paths.get("\\test2\\report.txt"); + var p2_rel2 = Paths.get("test2\\report.txt"); result = CoberturaParser.join(empty, empty); assertThat(result).isEmpty(); - result = CoberturaParser.join(empty, abs2); + result = CoberturaParser.join(empty, p2_abs1); assertThat(result).isEqualTo("c:\\test2\\report.txt"); - result = CoberturaParser.join(empty, rel2); + result = CoberturaParser.join(empty, p2_rel1); assertThat(result).isEqualTo(".\\test2\\report.txt"); - result = CoberturaParser.join(abs1, empty); + result = CoberturaParser.join(p1_abs1, empty); assertThat(result).isEmpty(); - result = CoberturaParser.join(rel1, empty); + result = CoberturaParser.join(p1_rel1, empty); assertThat(result).isEmpty(); - result = CoberturaParser.join(abs1, abs2); + result = CoberturaParser.join(p1_abs1, p2_abs1); assertThat(result).isEqualTo("c:\\test2\\report.txt"); - result = CoberturaParser.join(abs1, rel2); + result = CoberturaParser.join(p1_abs1, p2_rel1); assertThat(result).isEqualTo("c:\\test1\\test2\\report.txt"); - result = CoberturaParser.join(rel1, abs2); + result = CoberturaParser.join(p1_rel1, p2_abs1); assertThat(result).isEqualTo("c:\\test2\\report.txt"); - result = CoberturaParser.join(rel1, rel2); + result = CoberturaParser.join(p1_rel1, p2_rel1); assertThat(result).isEqualTo(".\\test1\\test2\\report.txt"); + + result = CoberturaParser.join(p1_abs2, p2_rel2); + assertThat(result).isEqualTo("c:\\test2\\report.txt"); + + result = CoberturaParser.join(p1_abs3, p2_rel2); + assertThat(result).isEqualTo("c:\\test2\\report.txt"); } else { // Linux - var abs1 = Paths.get("/home/test1"); - var rel1 = Paths.get("test1"); - var abs2 = Paths.get("/home/test2/report.txt"); - var rel2 = Paths.get("test2/report.txt"); + var p1_abs1 = Paths.get("/home/test1"); + var p1_rel1 = Paths.get("test1"); + var p2_abs1 = Paths.get("/home/test2/report.txt"); + var p2_rel1 = Paths.get("test2/report.txt"); result = CoberturaParser.join(empty, empty); assertThat(result).isEmpty(); - result = CoberturaParser.join(empty, abs2); + result = CoberturaParser.join(empty, p2_abs1); assertThat(result).isEqualTo("/home/test2/report.txt"); - result = CoberturaParser.join(empty, rel2); + result = CoberturaParser.join(empty, p2_rel1); assertThat(result).isEqualTo("./test2/report.txt"); - result = CoberturaParser.join(abs1, empty); + result = CoberturaParser.join(p1_abs1, empty); assertThat(result).isEmpty(); - result = CoberturaParser.join(rel1, empty); + result = CoberturaParser.join(p1_rel1, empty); assertThat(result).isEmpty(); - result = CoberturaParser.join(abs1, abs2); + result = CoberturaParser.join(p1_abs1, p2_abs1); assertThat(result).isEqualTo("/home/test2/report.txt"); - result = CoberturaParser.join(abs1, rel2); + result = CoberturaParser.join(p1_abs1, p2_rel1); assertThat(result).isEqualTo("/home/test1/test2/report.txt"); - result = CoberturaParser.join(rel1, abs2); + result = CoberturaParser.join(p1_rel1, p2_abs1); assertThat(result).isEqualTo("/home/test2/report.txt"); - result = CoberturaParser.join(rel1, rel2); + result = CoberturaParser.join(p1_rel1, p2_rel1); assertThat(result).isEqualTo("./test1/test2/report.txt"); }