From 42e821c9948d2759c066fa35ab59ebf7a42ce70d Mon Sep 17 00:00:00 2001 From: Paul O'Neill Date: Sat, 4 Feb 2017 05:00:06 -0800 Subject: [PATCH] Fix issue #86 - failure to handle LCOV for Angular CLI projects on Windows (#89) * Fix issue #86 - Angular CLI LCOV crashing on Windows --- pom.xml | 2 +- .../com/pablissimo/sonar/LCOVParserImpl.java | 18 ++++++++++++++++-- .../pablissimo/sonar/LCOVParserImplTest.java | 5 +++++ .../resources/lcov/angularwindowspaths.lcov | 3 +++ 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/lcov/angularwindowspaths.lcov diff --git a/pom.xml b/pom.xml index 07ff0f4..e5d92a4 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.pablissimo.sonar sonar-typescript-plugin sonar-plugin - 0.99-SNAPSHOT + 0.99.1-SNAPSHOT TypeScript Analyse TypeScript projects diff --git a/src/main/java/com/pablissimo/sonar/LCOVParserImpl.java b/src/main/java/com/pablissimo/sonar/LCOVParserImpl.java index 7648560..60439d7 100644 --- a/src/main/java/com/pablissimo/sonar/LCOVParserImpl.java +++ b/src/main/java/com/pablissimo/sonar/LCOVParserImpl.java @@ -24,6 +24,8 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; @@ -148,14 +150,25 @@ private static void logWrongDataWarning(String dataType, String lineNumber, Ille private FileData loadCurrentFileData(final Map files, String line) { String filePath = line.substring(SF.length()); FileData fileData = null; - // some tools (like Istanbul, Karma) provide relative paths, so let's consider them relative to project directory - InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath)); + // some tools (like Istanbul, Karma) provide relative paths, so let's consider them relative to project directory + InputFile inputFile = null; + try { + Paths.get(filePath); + inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath)); + } + catch (InvalidPathException ex) { + LOG.debug("LCOV file referred to path that appears invalid (not just not on disk): " + filePath); + } + // Try to accommodate Angular projects that, when the angular template loader's used // by checking for a ! in the filepath if the path isn't found - have a bash at seeking // everything after the ! as a second fallback pass if (inputFile == null && filePath.contains("!") && (filePath.indexOf("!") + 1) < filePath.length()) { String amendedPath = filePath.substring(filePath.indexOf("!") + 1); + + LOG.debug("Failed to resolve " + filePath + " as a valid source file, so attempting " + amendedPath + " instead"); + inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(amendedPath)); } @@ -166,6 +179,7 @@ private FileData loadCurrentFileData(final Map files, Strin files.put(inputFile, fileData); } } else { + LOG.debug("Failed to resolve path " + filePath + " to a file in the analysis set"); unresolvedPaths.add(filePath); } return fileData; diff --git a/src/test/java/com/pablissimo/sonar/LCOVParserImplTest.java b/src/test/java/com/pablissimo/sonar/LCOVParserImplTest.java index f793ce2..e37a886 100644 --- a/src/test/java/com/pablissimo/sonar/LCOVParserImplTest.java +++ b/src/test/java/com/pablissimo/sonar/LCOVParserImplTest.java @@ -119,6 +119,11 @@ public void parseFileThrowsWhenFileDoesNotExist() { parser.parseFile(nonExistent); } + @Test + public void doesNotThrow_withWindowsStyleAngularCLIPaths() { + executeForTestCase("angularwindowspaths"); + } + private Map executeForTestCase(String testName) { File lcovFile = resource(testName); LCOVParser parser = getParser(lcovFile); diff --git a/src/test/resources/lcov/angularwindowspaths.lcov b/src/test/resources/lcov/angularwindowspaths.lcov new file mode 100644 index 0000000..80c0f74 --- /dev/null +++ b/src/test/resources/lcov/angularwindowspaths.lcov @@ -0,0 +1,3 @@ +SF:C:/path/to/template/loader.js!C:\path\to\file.ts +DA:1,3 +end_of_record \ No newline at end of file