Skip to content

Commit

Permalink
[JENKINS-43624] always use the file separator of the build agent, don…
Browse files Browse the repository at this point in the history
…’t always use “/“. Particularly important for `jenkins.model.ArtifactManager#archive()` and Windows agents.
  • Loading branch information
Cyrille Le Clerc committed May 7, 2017
1 parent 436dba5 commit 0181a5b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
Expand Up @@ -68,7 +68,7 @@ public void processMavenSpyLogs(StepContext context, FilePath mavenSpyLogFolder)
LOGGER.warning("TaskListener is NULL, default to stderr");
listener = new StreamBuildListener((OutputStream) System.err);
}
FilePath workspace = context.get(FilePath.class); // TODO check that it's the good workspace
FilePath workspace = context.get(FilePath.class);

DocumentBuilder documentBuilder;
try {
Expand Down
Expand Up @@ -159,7 +159,7 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
LOGGER.warning("TaskListener is NULL, default to stderr");
listener = new StreamBuildListener((OutputStream) System.err);
}
FilePath workspace = context.get(FilePath.class); // TODO check that it's the good workspace
FilePath workspace = context.get(FilePath.class);
Run run = context.get(Run.class);
Launcher launcher = context.get(Launcher.class);

Expand Down
Expand Up @@ -45,7 +45,8 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
LOGGER.warning("TaskListener is NULL, default to stderr");
listener = new StreamBuildListener((OutputStream) System.err);
}
FilePath workspace = context.get(FilePath.class); // TODO check that it's the good workspace
FilePath workspace = context.get(FilePath.class);
final String fileSeparatorOnAgent = XmlUtils.getFileSeparatorOnRemote(workspace);

List<MavenSpyLogProcessor.MavenArtifact> mavenArtifacts = listArtifacts(mavenSpyLogsElt);
List<MavenSpyLogProcessor.MavenArtifact> attachedMavenArtifacts = listAttachedArtifacts(mavenSpyLogsElt);
Expand Down Expand Up @@ -74,9 +75,9 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
}

String artifactPathInArchiveZone =
mavenArtifact.groupId.replace('.', '/') + "/" +
mavenArtifact.artifactId + "/" +
mavenArtifact.version + "/" +
mavenArtifact.groupId.replace(".", fileSeparatorOnAgent) + fileSeparatorOnAgent +
mavenArtifact.artifactId + fileSeparatorOnAgent +
mavenArtifact.version + fileSeparatorOnAgent +
mavenArtifact.getFileName();

String artifactPathInWorkspace = XmlUtils.getPathInWorkspace(mavenArtifact.file, workspace);
Expand All @@ -101,7 +102,9 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
listener.getLogger().flush();
}
}
LOGGER.log(Level.FINE, "Archive and fingerprint {0}", artifactsToArchive);
if (LOGGER.isLoggable(Level.FINE)) {
listener.getLogger().println("Archive and fingerprint " + artifactsToArchive);
}

// ARCHIVE GENERATED MAVEN ARTIFACT
// see org.jenkinsci.plugins.workflow.steps.ArtifactArchiverStepExecution#run
Expand Down
Expand Up @@ -164,7 +164,9 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
}

private void executeReporter(StepContext context, TaskListener listener, List<Element> testEvents, String goal) throws IOException, InterruptedException {
FilePath workspace = context.get(FilePath.class); // TODO check that it's the good workspace
FilePath workspace = context.get(FilePath.class);
final String fileSeparatorOnAgent = XmlUtils.getFileSeparatorOnRemote(workspace);

Run run = context.get(Run.class);
Launcher launcher = context.get(Launcher.class);

Expand Down Expand Up @@ -210,7 +212,7 @@ private void executeReporter(StepContext context, TaskListener listener, List<El

reportsDirectory = XmlUtils.getPathInWorkspace(reportsDirectory, workspace);

String testResults = reportsDirectory + "/*.xml";
String testResults = reportsDirectory + fileSeparatorOnAgent + "*.xml";
listener.getLogger().println("[withMaven] Archive test results for Maven artifact " + mavenArtifact.toString() + " generated by " +
pluginInvocation + ": " + testResults);
JUnitResultArchiver archiver = new JUnitResultArchiver(testResults);
Expand Down
Expand Up @@ -44,7 +44,9 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
LOGGER.warning("TaskListener is NULL, default to stderr");
listener = new StreamBuildListener((OutputStream) System.err);
}
FilePath workspace = context.get(FilePath.class); // TODO check that it's the good workspace
FilePath workspace = context.get(FilePath.class);
final String fileSeparatorOnAgent = XmlUtils.getFileSeparatorOnRemote(workspace);

Run run = context.get(Run.class);
Launcher launcher = context.get(Launcher.class);

Expand Down Expand Up @@ -83,7 +85,7 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
String sourceDirectoryRelativePath = XmlUtils.getPathInWorkspace(sourceDirectory, workspace);

if (workspace.child(sourceDirectoryRelativePath).exists()) {
sourceDirectoriesPatterns.add(sourceDirectoryRelativePath + "/**/*");
sourceDirectoriesPatterns.add(sourceDirectoryRelativePath + fileSeparatorOnAgent + "**" + fileSeparatorOnAgent + "*");
listener.getLogger().println("[withMaven] Scan Tasks for Maven artifact " + mavenArtifact.toString() + " in source directory " + sourceDirectoryRelativePath);
} else {
LOGGER.log(Level.FINE, "Skip task scanning for {0}, folder {1} does not exist", new Object[]{mavenArtifact, sourceDirectoryRelativePath});
Expand Down
Expand Up @@ -140,7 +140,7 @@ public static List<Element> getExecutionEvents(@Nonnull Element mavenSpyLogs, St
Set<String> expectedTypes = new HashSet<>(Arrays.asList(expectedType));
List<Element> result = new ArrayList<>();
for (Element element : getChildrenElements(mavenSpyLogs, "ExecutionEvent")) {
if (expectedTypes.contains(element.getAttribute("type"))){
if (expectedTypes.contains(element.getAttribute("type"))) {
result.add(element);
}
}
Expand Down Expand Up @@ -181,16 +181,9 @@ public static List<Element> getExecutionEvents(@Nonnull Element mavenSpyLogs, St
*/
@Nonnull
public static String getPathInWorkspace(@Nonnull String absoluteFilePath, @Nonnull FilePath workspace) {
String workspaceRemote = workspace.getRemote();

String fileSeparator;
if (absoluteFilePath.contains("\\")) {
// '\' character found in the absoluteFilePath, this is windows
fileSeparator = "\\";
} else {
fileSeparator = "/";
}
String fileSeparator = getFileSeparatorOnRemote(workspace);

String workspaceRemote = workspace.getRemote();
if (!workspaceRemote.endsWith(fileSeparator)) {
workspaceRemote = workspaceRemote + fileSeparator;
}
Expand All @@ -201,6 +194,28 @@ public static String getPathInWorkspace(@Nonnull String absoluteFilePath, @Nonnu
}
}

/**
* Return the File separator "/" or "\" that is effective on the remote agent.
*
* @param filePath
* @return "/" or "\"
*/
@Nonnull
public static String getFileSeparatorOnRemote(@Nonnull FilePath filePath) {
int indexOfSlash = filePath.getRemote().indexOf('/');
int indexOfBackSlash = filePath.getRemote().indexOf('\\');
if (indexOfSlash == -1) {
return "\\";
} else if (indexOfBackSlash == -1) {
return "/";
} else if (indexOfSlash < indexOfBackSlash) {
return "/";
} else {
return "\\";
}

}

/**
* @param projectElt
* @return {@code project/build/@directory"}
Expand Down

0 comments on commit 0181a5b

Please sign in to comment.