Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[JENKINS-49898] maven-invoker-plugin add support for relative path in…
… "reportsDirectory", "projectsDirectory" and "cloneProjectsTo"
- Loading branch information
Cyrille Le Clerc
committed
Mar 6, 2018
1 parent
1cfc35a
commit 90d7474
Showing
6 changed files
with
833 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/util/FileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.jenkinsci.plugins.pipeline.maven.util; | ||
|
||
import hudson.FilePath; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a> | ||
*/ | ||
public class FileUtils { | ||
|
||
public static boolean isAbsolutePath(@Nonnull String path) { | ||
if (isWindows(path)) { | ||
if (path.length() > 3 && path.charAt(1) == ':' && path.charAt(2) == '\\') { | ||
// windows path such as "C:\path\to\..." | ||
return true; | ||
} else if (path.length() > 3 && path.charAt(1) == ':' && path.charAt(2) == '/') { | ||
// nasty windows path such as "C:/path/to/...". See JENKINS-44088 | ||
return true; | ||
} else if (path.length() > 2 && path.charAt(0) == '\\' && path.charAt(1) == '\\') { | ||
// Microsoft Windows UNC mount ("\\myserver\myfolder") | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
// see java.io.UnixFileSystem.prefixLength() | ||
return path.charAt(0) == '/'; | ||
} | ||
|
||
} | ||
|
||
public static boolean isWindows(@Nonnull FilePath path) { | ||
return isWindows(path.getRemote()); | ||
} | ||
|
||
public static boolean isWindows(@Nonnull String path) { | ||
if (path.length() > 3 && path.charAt(1) == ':' && path.charAt(2) == '\\') { | ||
// windows path such as "C:\path\to\..." | ||
return true; | ||
} else if (path.length() > 3 && path.charAt(1) == ':' && path.charAt(2) == '/') { | ||
// nasty windows path such as "C:/path/to/...". See JENKINS-44088 | ||
return true; | ||
} else if (path.length() > 2 && path.charAt(0) == '\\' && path.charAt(1) == '\\') { | ||
// Microsoft Windows UNC mount ("\\myserver\myfolder") | ||
return true; | ||
} | ||
int indexOfSlash = path.indexOf('/'); | ||
int indexOfBackSlash = path.indexOf('\\'); | ||
if (indexOfSlash == -1) { | ||
return true; | ||
} else if (indexOfBackSlash == -1) { | ||
return false; | ||
} else if (indexOfSlash < indexOfBackSlash) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...c/test/java/org/jenkinsci/plugins/pipeline/maven/publishers/InvokerRunsPublisherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.jenkinsci.plugins.pipeline.maven.publishers; | ||
|
||
import hudson.FilePath; | ||
import hudson.model.TaskListener; | ||
import hudson.util.StreamTaskListener; | ||
import org.hamcrest.Matchers; | ||
import org.jenkinsci.plugins.pipeline.maven.util.XmlUtils; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
|
||
import java.io.File; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
|
||
/** | ||
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a> | ||
*/ | ||
public class InvokerRunsPublisherTest { | ||
Document doc; | ||
|
||
@Before | ||
public void before() throws Exception { | ||
String mavenSpyLogs = "org/jenkinsci/plugins/pipeline/maven/maven-spy-maven-invoker-plugin-integration-tests.xml"; | ||
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(mavenSpyLogs); | ||
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); | ||
} | ||
|
||
/* | ||
<ExecutionEvent type="MojoSucceeded" class="org.apache.maven.lifecycle.internal.DefaultExecutionEvent" _time="2018-03-06 23:49:30.662"> | ||
<project baseDir="/path/to/khmarbaise/maui/src/main/resources/mp-it-1" file="/path/to/khmarbaise/maui/src/main/resources/mp-it-1/pom.xml" groupId="com.soebes.maven.guide.mp.it" name="Maven Plugin Integration Test" artifactId="mp-it-1" version="0.1-SNAPSHOT"> | ||
<build sourceDirectory="/path/to/khmarbaise/maui/src/main/resources/mp-it-1/src/main/java" directory="/path/to/khmarbaise/maui/src/main/resources/mp-it-1/target"/> | ||
</project> | ||
<plugin executionId="integration-test" goal="run" lifecyclePhase="integration-test" groupId="org.apache.maven.plugins" artifactId="maven-invoker-plugin" version="3.0.1"> | ||
<projectsDirectory>src/it</projectsDirectory> | ||
<cloneProjectsTo>/path/to/khmarbaise/maui/src/main/resources/mp-it-1/target/it</cloneProjectsTo> | ||
<reportsDirectory>${invoker.reportsDirectory}</reportsDirectory> | ||
</plugin> | ||
</ExecutionEvent> | ||
*/ | ||
|
||
/** | ||
* projectsDirectory = src/it -> relative path | ||
* cloneProjectsTo = /path/to/khmarbaise/maui/src/main/resources/mp-it-1/target/it -> absolute path | ||
* reportsDirectory = ${invoker.reportsDirectory} -> variabilized path | ||
*/ | ||
@Test | ||
public void test_relative_path_and_absolute_path_and_variabilized_path() { | ||
InvokerRunsPublisher invokerRunsPublisher = new InvokerRunsPublisher(); | ||
List<Element> invokerRunEvents = XmlUtils.getExecutionEvents(doc.getDocumentElement(), InvokerRunsPublisher.GROUP_ID, InvokerRunsPublisher.ARTIFACT_ID, InvokerRunsPublisher.RUN_GOAL); | ||
|
||
FilePath workspace = new FilePath(new File("/path/to/khmarbaise/maui/src/main/resources/mp-it-1")); | ||
TaskListener listener = new StreamTaskListener(System.out, StandardCharsets.UTF_8); | ||
|
||
System.out.println(invokerRunEvents.size()); | ||
List<Element> invokerRunSucceededEvents = new ArrayList<>(); | ||
for (Element invokerRunEvent : invokerRunEvents) { | ||
String eventType = invokerRunEvent.getAttribute("type"); | ||
if (eventType.equals("MojoSucceeded")) { | ||
invokerRunSucceededEvents.add(invokerRunEvent); | ||
} | ||
} | ||
Assert.assertThat(invokerRunSucceededEvents.size(), Matchers.is(1)); | ||
Element invokerRunSucceedEvent = invokerRunSucceededEvents.get(0); | ||
|
||
Element projectElt = XmlUtils.getUniqueChildElement(invokerRunSucceedEvent, "project"); | ||
Element pluginElt = XmlUtils.getUniqueChildElement(invokerRunSucceedEvent, "plugin"); | ||
Element reportsDirectoryElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "reportsDirectory"); | ||
Element cloneProjectsToElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "cloneProjectsTo"); | ||
Element projectsDirectoryElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "projectsDirectory"); | ||
|
||
String reportsDirectory = invokerRunsPublisher.expandAndRelativize(reportsDirectoryElt, "reportsDirectory", invokerRunSucceedEvent, projectElt, workspace, listener); | ||
Assert.assertThat(reportsDirectory, Matchers.is("target/invoker-reports")); | ||
String projectsDirectory = invokerRunsPublisher.expandAndRelativize(projectsDirectoryElt, "projectsDirectory", invokerRunSucceedEvent, projectElt, workspace, listener); | ||
Assert.assertThat(projectsDirectory, Matchers.is("src/it")); | ||
String cloneProjectsTo = invokerRunsPublisher.expandAndRelativize(cloneProjectsToElt, "cloneProjectsTo", invokerRunSucceedEvent, projectElt, workspace, listener); | ||
Assert.assertThat(cloneProjectsTo, Matchers.is("target/it")); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/util/FileUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.jenkinsci.plugins.pipeline.maven.util; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a> | ||
*/ | ||
public class FileUtilsTest { | ||
|
||
@Test | ||
public void test_isAbsolutePath_with_windows_absolute_path() { | ||
Assert.assertThat(FileUtils.isAbsolutePath("c:\\jenkins\\workspace\\"), Matchers.is(true)); | ||
} | ||
|
||
@Test | ||
public void test_isAbsolutePath_with_windows_relative_path() { | ||
Assert.assertThat(FileUtils.isAbsolutePath("jenkins\\workspace\\"), Matchers.is(false)); | ||
} | ||
|
||
@Test | ||
public void test_isAbsolutePath_with_linux_absolute_path() { | ||
Assert.assertThat(FileUtils.isAbsolutePath("/var/lib/jenkins/workspace"), Matchers.is(true)); | ||
} | ||
|
||
@Test | ||
public void test_isAbsolutePath_with_linux_relative_path() { | ||
Assert.assertThat(FileUtils.isAbsolutePath("jenkins/workspace"), Matchers.is(false)); | ||
} | ||
|
||
@Test | ||
public void test_isAbsolutePath_with_windows_unc_absolute_path() { | ||
Assert.assertThat(FileUtils.isAbsolutePath("\\\\myserver\\jenkins\\workspace\\"), Matchers.is(true)); | ||
} | ||
} |
Oops, something went wrong.