Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
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 @@ -21,6 +21,7 @@
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
Expand Down Expand Up @@ -385,10 +386,12 @@ else if (sourceDirectories.size() == 1) {
try {
FilePath outputFolder = workspace.child(directory);
outputFolder.mkdirs();
Path temporaryFolder = Files.createTempDirectory(directory);

Charset charset = getCharset();
int count = paintedFiles.parallelStream()
.mapToInt(file -> paintSource(file, workspace, sourceDirectories, charset, log))
.mapToInt(
file -> paintSource(file, workspace, temporaryFolder, sourceDirectories, charset, log))
.sum();

if (count == paintedFiles.size()) {
Expand All @@ -402,6 +405,8 @@ else if (sourceDirectories.size() == 1) {
FilePath zipFile = workspace.child(COVERAGE_SOURCES_ZIP);
outputFolder.zip(zipFile);
log.logInfo("-> zipping sources from folder '%s' as '%s'", outputFolder, zipFile);

deleteFolder(temporaryFolder.toFile(), log);
}
catch (IOException exception) {
log.logException(exception,
Expand All @@ -425,23 +430,23 @@ private Set<String> filterSourceDirectories(final File workspace, final Filtered
permittedSourceDirectories, requestedSourceDirectories, log);
}

private int paintSource(final PaintedNode fileNode, final FilePath workspace,
private int paintSource(final PaintedNode fileNode, final FilePath workspace, final Path temporaryFolder,
final Set<String> sourceSearchDirectories, final Charset sourceEncoding, final FilteredLog log) {
String relativePathIdentifier = fileNode.getNode().getPath();
FilePath paintedFilesDirectory = workspace.child(directory);
return findSourceFile(workspace, relativePathIdentifier, sourceSearchDirectories, log)
.map(resolvedPath -> paint(fileNode.getPaint(), relativePathIdentifier, resolvedPath,
paintedFilesDirectory,
sourceEncoding, log))
paintedFilesDirectory, temporaryFolder, sourceEncoding, log))
.orElse(0);
}

private int paint(final CoveragePaint paint, final String relativePathIdentifier, final FilePath resolvedPath,
final FilePath paintedFilesDirectory, final Charset charset, final FilteredLog log) {
final FilePath paintedFilesDirectory, final Path temporaryFolder,
final Charset charset, final FilteredLog log) {
String sanitizedFileName = sanitizeFilename(relativePathIdentifier);
FilePath zipOutputPath = paintedFilesDirectory.child(sanitizedFileName + ZIP_FILE_EXTENSION);
try {
Path paintedFilesFolder = Files.createTempDirectory(directory);
Path paintedFilesFolder = Files.createTempDirectory(temporaryFolder, directory);
Path fullSourcePath = paintedFilesFolder.resolve(sanitizedFileName);
try (BufferedWriter output = Files.newBufferedWriter(fullSourcePath)) {
List<String> lines = Files.readAllLines(Paths.get(resolvedPath.getRemote()), charset);
Expand All @@ -452,6 +457,7 @@ private int paint(final CoveragePaint paint, final String relativePathIdentifier
paint.setTotalLines(lines.size());
}
new FilePath(fullSourcePath.toFile()).zip(zipOutputPath);
FileUtils.deleteDirectory(paintedFilesFolder.toFile());
return 1;
}
catch (IOException | InterruptedException exception) {
Expand Down Expand Up @@ -540,6 +546,24 @@ private Optional<FilePath> enforcePermissionFor(final FilePath absolutePath, fin
return Optional.empty();
}

/**
* Deletes a folder.
*
* @param folder The directory to be deleted
* @param log The log
*/
private void deleteFolder(final File folder, final FilteredLog log) {
if (folder.isDirectory()) {
try {
FileUtils.deleteDirectory(folder);
}
catch (IOException e) {
log.logError("The folder '%s' could not be deleted",
folder.getAbsolutePath());
}
}
}

private static class PaintedNode implements Serializable {
private static final long serialVersionUID = -6044649044983631852L;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.jenkins.plugins.coverage.model;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -115,14 +116,17 @@ private String createExternalSourceFolder() throws IOException {
WorkflowJob job = createPipelineWithWorkspaceFiles(ACU_COBOL_PARSER_COVERAGE_REPORT);
copyFileToWorkspace(job, SOURCE_FILE, checkoutDirectory + PACKAGE_PATH + "AcuCobolParser.java");

// get the temporary directory - used by unit tests - to verify its content
File temporaryDirectory = new File(System.getProperty("java.io.tmpdir"));
assertThat(temporaryDirectory.exists()).isTrue();
assertThat(temporaryDirectory.isDirectory()).isTrue();
File[] temporaryFiles = temporaryDirectory.listFiles();

String sourceCodeRetention = "STORE_ALL_BUILD";
job.setDefinition(createPipelineWithSourceCode(sourceCodeRetention, sourceDirectory));

Run<?, ?> firstBuild = buildSuccessfully(job);

assertThat(getConsoleLog(firstBuild))
.contains("-> finished painting successfully");

verifySourceCodeInBuild(firstBuild, ACU_COBOL_PARSER);

Run<?, ?> secondBuild = buildSuccessfully(job);
Expand All @@ -142,6 +146,8 @@ private String createExternalSourceFolder() throws IOException {
verifySourceCodeInBuild(secondBuild, NO_SOURCE_CODE); // should be still available
verifySourceCodeInBuild(thirdBuild, NO_SOURCE_CODE); // should be still available

assertThat(temporaryDirectory.listFiles()).isEqualTo(temporaryFiles);

return firstBuild;
}

Expand Down