Skip to content

Commit

Permalink
Fix: cannot hanle absolute path in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
cizezsy committed Dec 11, 2019
1 parent 01cdd69 commit 229e59d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import javax.annotation.Nonnull;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.*;

public class DefaultSourceFileResolver extends SourceFileResolver {
Expand Down Expand Up @@ -186,12 +187,11 @@ private FilePath tryFindSourceFile(File workspace) {
}

// if sourceFilePath is a absolute path check if it is under the workspace directory
if (sourceFilePath.startsWith(File.separator)) {
if (sourceFilePath.startsWith(workspace.getAbsolutePath())) {
sourceFile = new File(sourceFilePath);
if (sourceFile.exists() && sourceFile.isFile() && sourceFile.canRead()) {
return new FilePath(sourceFile);
}
if (Paths.get(sourceFilePath).isAbsolute()
&& Paths.get(sourceFilePath).normalize().startsWith(workspace.getAbsolutePath())) {
sourceFile = new File(sourceFilePath);
if (isValidSourceFile(sourceFile)) {
return new FilePath(sourceFile);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public void testAbsolutePathSourceFile() throws Exception {
String sourceFileContent = workspace
.child("cobertura-coverage.xml")
.readToString()
.replaceAll("cc.js", absoluteSourceFilePath);
.replace("filename=\"cc.js\"", "filename=\"" + absoluteSourceFilePath + "\"");

workspace.child("cobertura-coverage.xml")
.write(sourceFileContent, "utf-8");
Expand All @@ -304,7 +304,12 @@ public void testAbsolutePathSourceFile() throws Exception {

File sourceFile = new File(r.getRootDir(), DefaultSourceFileResolver.DEFAULT_SOURCE_CODE_STORE_DIRECTORY + absoluteSourceFilePath.replaceAll("[^a-zA-Z0-9-_.]", "_"));

Assert.assertTrue(String.format("Source file path %s", absoluteSourceFilePath), sourceFile.exists());
System.out.println("Listing file below source files directory");
for (File file : Objects.requireNonNull(sourceFile.getParentFile().listFiles())) {
System.out.println(file.getAbsolutePath());
}

Assert.assertTrue(String.format("Source file path %s .%nDestination file path %s", absoluteSourceFilePath, sourceFile), sourceFile.exists());
}

@Test
Expand Down

0 comments on commit 229e59d

Please sign in to comment.