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

Improve source file resolving #131

Merged
merged 6 commits into from
Dec 16, 2019
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 @@ -37,8 +37,10 @@
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

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 @@ -74,94 +76,33 @@ public void resolveSourceFiles(Run<?, ?> run, FilePath workspace, TaskListener l
}
}

listener.getLogger().printf("%d source files need to be copied%n", paints.size());
listener.getLogger().printf("%d source files need to be copied.%n", paints.size());

paints.forEach((sourceFilePath, paint) -> {
FilePath[] possibleFiles;
FilePath buildDirSourceFile = new FilePath(new File(runRootDir, DEFAULT_SOURCE_CODE_STORE_DIRECTORY + sanitizeFilename(sourceFilePath)));

try {
if (getPossiblePaths() != null && getPossiblePaths().size() > 0) {
possibleFiles = workspace.act(new FindSourceFileCallable(sourceFilePath, getPossiblePaths()));
} else {
possibleFiles = workspace.act(new FindSourceFileCallable(sourceFilePath));
}
} catch (IOException | InterruptedException e) {
listener.getLogger().println(ExceptionUtils.getFullStackTrace(e));
return;
}
if (possibleFiles != null && possibleFiles.length > 0) {
FilePath source = possibleFiles[0];
FilePath copiedSource = new FilePath(new File(runRootDir, DEFAULT_SOURCE_CODE_STORE_DIRECTORY + sourceFilePath + "_copied"));
try {
source.copyTo(copiedSource);
} catch (IOException | InterruptedException e) {
listener.getLogger().println(ExceptionUtils.getFullStackTrace(e));
return;
listener.getLogger().printf("Starting copy source file %s. %n", sourceFilePath);

Set<String> possibleParentPaths = getPossiblePaths();
if (possibleParentPaths == null) {
possibleParentPaths = Collections.emptySet();
}

FilePath buildDirSourceFile = new FilePath(new File(runRootDir, DEFAULT_SOURCE_CODE_STORE_DIRECTORY + sourceFilePath));
boolean copiedSucceed = workspace.act(new SourceFilePainter(sourceFilePath, paint, buildDirSourceFile, possibleParentPaths));
if (copiedSucceed) {
listener.getLogger().printf("Copied %s. %n", sourceFilePath);

try {
paintSourceCode(copiedSource, paint, buildDirSourceFile);
} catch (CoverageException e) {
listener.getLogger().println(ExceptionUtils.getFullStackTrace(e));
}

deleteFilePathQuietly(copiedSource);
} else {
listener.getLogger().printf("Cannot found source file for %s%n", sourceFilePath);
} catch (IOException | InterruptedException e) {
listener.getLogger().println(ExceptionUtils.getFullStackTrace(e));
}

});
}


private void paintSourceCode(FilePath source, CoveragePaint paint, FilePath canvas) throws CoverageException {
try (BufferedWriter output = new BufferedWriter(new OutputStreamWriter(canvas.write(), StandardCharsets.UTF_8));
BufferedReader input = new BufferedReader(new InputStreamReader(source.read(), StandardCharsets.UTF_8))) {
int line = 0;
String content;
while ((content = input.readLine()) != null) {
line++;

if (paint.isPainted(line)) {
final int hits = paint.getHits(line);
final int branchCoverage = paint.getBranchCoverage(line);
final int branchTotal = paint.getBranchTotal(line);
final int coveragePercent = (hits == 0) ? 0 : (int) (branchCoverage * 100.0 / branchTotal);
if (paint.getHits(line) > 0) {
if (branchTotal == branchCoverage) {
output.write("<tr class=\"coverFull\">\n");
} else {
output.write("<tr class=\"coverPart\" title=\"Line " + line + ": Conditional coverage " + coveragePercent + "% ("
+ branchCoverage + "/" + branchTotal + ")\">\n");
}
} else {
output.write("<tr class=\"coverNone\">\n");
}
output.write("<td class=\"line\"><a name='" + line + "'>" + line + "</a></td>\n");
output.write("<td class=\"hits\">" + hits + "</td>\n");
} else {
output.write("<tr class=\"noCover\">\n");
output.write("<td class=\"line\"><a name='" + line + "'>" + line + "</a></td>\n");
output.write("<td class=\"hits\"></td>\n");
}
output.write("<td class=\"code\">"
+ content.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\n", "").replace("\r", "").replace(" ",
"&nbsp;").replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") + "</td>\n");
output.write("</tr>\n");
}

paint.setTotalLines(line);
} catch (IOException | InterruptedException e) {
throw new CoverageException(e);
}
}

private void deleteFilePathQuietly(FilePath file) {
try {
file.delete();
} catch (IOException | InterruptedException ignore) {
}
private String sanitizeFilename(String inputName) {
return inputName.replaceAll("[^a-zA-Z0-9-_.]", "_");
}


Expand All @@ -183,49 +124,124 @@ public ListBoxModel doFillLevelItems() {
}
}

private static class FindSourceFileCallable extends MasterToSlaveFileCallable<FilePath[]> {
private static class SourceFilePainter extends MasterToSlaveFileCallable<Boolean> {
private static final long serialVersionUID = 6548573019315830249L;

private String sourceFilePath;
private Set<String> possiblePaths;
private CoveragePaint paint;
private FilePath destination;

public FindSourceFileCallable(String sourceFilePath) {
this(sourceFilePath, Collections.emptySet());
}

public FindSourceFileCallable(String sourceFilePath, Set<String> possiblePaths) {
SourceFilePainter(@Nonnull String sourceFilePath, @Nonnull CoveragePaint paint, @Nonnull FilePath destination, @Nonnull Set<String> possiblePaths) {
this.sourceFilePath = sourceFilePath;
this.paint = paint;
this.destination = destination;
this.possiblePaths = possiblePaths;
}

@Override
public FilePath[] invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
public Boolean invoke(File workspace, VirtualChannel channel) throws IOException, InterruptedException {
FilePath sourceFile = tryFindSourceFile(workspace);
if (sourceFile == null) {
throw new IOException(
String.format("Unable to find source file %s in workspace %s", sourceFilePath, workspace.getAbsolutePath()));
}

try {
paintSourceCode(sourceFile, paint, destination);
} catch (CoverageException e) {
throw new IOException(e);
}

return true;
}

private FilePath tryFindSourceFile(File workspace) {
List<File> possibleDirectories = new LinkedList<>();

// guess its parent directory
for (String directory : possiblePaths) {
File pathFromRoot = new File(directory);
if (pathFromRoot.exists() && pathFromRoot.isDirectory()) {
possibleDirectories.add(pathFromRoot);
}

File pathFromWorkDir = new File(f, directory);
File pathFromWorkDir = new File(workspace, directory);
if (pathFromWorkDir.exists() && pathFromWorkDir.isDirectory() && !pathFromWorkDir.equals(pathFromRoot)) {
possibleDirectories.add(pathFromWorkDir);
}
}


File sourceFile = new File(f, sourceFilePath);
if (sourceFile.exists() && sourceFile.isFile() && sourceFile.canRead()) {
return new FilePath[]{new FilePath(sourceFile)};
// check if we can find source file in workspace
File sourceFile = new File(workspace, sourceFilePath);
if (isValidSourceFile(sourceFile)) {
return new FilePath(sourceFile);
}

// check if we can find source file in the possible parent directories
for (File directory : possibleDirectories) {
sourceFile = new File(directory, sourceFilePath);
if (sourceFile.exists() && sourceFile.isFile() && sourceFile.canRead()) {
return new FilePath[]{new FilePath(sourceFile)};
if (isValidSourceFile(sourceFile)) {
return new FilePath(sourceFile);
}
}

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

return new FilePath(f).list("**/" + sourceFilePath);
return null;
}

private boolean isValidSourceFile(File sourceFile) {
return sourceFile.exists() && sourceFile.isFile() && sourceFile.canRead();
}

private void paintSourceCode(FilePath source, CoveragePaint paint, FilePath canvas) throws CoverageException {
try (BufferedWriter output = new BufferedWriter(new OutputStreamWriter(canvas.write(), StandardCharsets.UTF_8));
BufferedReader input = new BufferedReader(new InputStreamReader(source.read(), StandardCharsets.UTF_8))) {
int line = 0;
String content;
while ((content = input.readLine()) != null) {
line++;

if (paint.isPainted(line)) {
final int hits = paint.getHits(line);
final int branchCoverage = paint.getBranchCoverage(line);
final int branchTotal = paint.getBranchTotal(line);
final int coveragePercent = (hits == 0) ? 0 : (int) (branchCoverage * 100.0 / branchTotal);
if (paint.getHits(line) > 0) {
if (branchTotal == branchCoverage) {
output.write("<tr class=\"coverFull\">\n");
} else {
output.write("<tr class=\"coverPart\" title=\"Line " + line + ": Conditional coverage " + coveragePercent + "% ("
+ branchCoverage + "/" + branchTotal + ")\">\n");
}
} else {
output.write("<tr class=\"coverNone\">\n");
}
output.write("<td class=\"line\"><a name='" + line + "'>" + line + "</a></td>\n");
output.write("<td class=\"hits\">" + hits + "</td>\n");
} else {
output.write("<tr class=\"noCover\">\n");
output.write("<td class=\"line\"><a name='" + line + "'>" + line + "</a></td>\n");
output.write("<td class=\"hits\"></td>\n");
}
output.write("<td class=\"code\">"
+ content.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\n", "").replace("\r", "").replace(" ",
"&nbsp;").replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") + "</td>\n");
output.write("</tr>\n");
}

paint.setTotalLines(line);
} catch (IOException | InterruptedException e) {
throw new CoverageException(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package io.jenkins.plugins.coverage.targets;

import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Item;
import hudson.model.Run;
Expand All @@ -40,6 +41,7 @@
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -207,11 +209,26 @@ public void paint(int line, int hits, int branchHits, int branchTotal) {
*/
private File getSourceFile() {
if (hasPermission()) {
return new File(owner.getRootDir(), DefaultSourceFileResolver.DEFAULT_SOURCE_CODE_STORE_DIRECTORY + relativeSourcePath);
File sourceFile = new File(owner.getRootDir(), DefaultSourceFileResolver.DEFAULT_SOURCE_CODE_STORE_DIRECTORY + sanitizeFilename(relativeSourcePath));
if (sourceFile.exists()) {
return sourceFile;
}
// keep compatibility
sourceFile = new File(owner.getRootDir(), DefaultSourceFileResolver.DEFAULT_SOURCE_CODE_STORE_DIRECTORY + relativeSourcePath);
if (sourceFile.exists()) {
return sourceFile;
}

// try to normalize file path
return Paths.get(sourceFile.getPath()).normalize().toFile();
}
return null;
}

private String sanitizeFilename(String inputName) {
return inputName.replaceAll("[^a-zA-Z0-9-_.]", "_");
}

/**
* Getter for property 'changeRequestCoverageDiffWithTargetBranch'.
*
Expand Down
Loading