Skip to content

Commit

Permalink
Merge pull request #13 from olivierdagenais/JENKINS-18822
Browse files Browse the repository at this point in the history
Fix JENKINS-18822
  • Loading branch information
olivierdagenais committed Aug 6, 2013
2 parents da1deaf + b0b8b3e commit 7dc5389
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.398</version>
<version>1.447</version>
</parent>

<artifactId>rake</artifactId>
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/hudson/plugins/rake/Rake.java
@@ -1,5 +1,6 @@
package hudson.plugins.rake;

import static hudson.plugins.rake.Util.findInPath;
import static hudson.plugins.rake.Util.getCanonicalRubies;
import static hudson.plugins.rake.Util.getGemsDir;
import static hudson.plugins.rake.Util.hasGemsInstalled;
Expand Down Expand Up @@ -87,6 +88,7 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene

Launcher lastBuiltLauncher = getLastBuiltLauncher(build, launcher, listener);

final String pathSeparator = lastBuiltLauncher.isUnix()? ":" : ";";
RubyInstallation rake = getRake();
if (rake != null) {
File exec = rake.getExecutable();
Expand All @@ -96,7 +98,22 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
}
args.add(exec.getPath());
} else {
args.add(lastBuiltLauncher.isUnix()?"rake":"rake.bat");
String executable = lastBuiltLauncher.isUnix()?"rake":"rake.bat";
// search PATH to build an absolute path to the executable,
// to work around a bug in Java 7u21 - 7u25
// "JDK-8016721 : (process) Behavior of %~dp0 in .cmd and .bat scripts has changed"
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=8016721
String path = null;
try {
final EnvVars env = build.getEnvironment(listener);
path = env.get("PATH");
} catch (IOException e) {
// no big deal; ignore and we'll skip the PATH scan below
}
if (path != null) {
executable = findInPath(executable, path, pathSeparator);
}
args.add(executable);
}

if (rakeFile != null && rakeFile.length() > 0) {
Expand Down Expand Up @@ -130,7 +147,7 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
StringBuilder builder = new StringBuilder();
String path = env.get("PATH");
if (path != null) {
builder.append(path).append(File.pathSeparator);
builder.append(path).append(pathSeparator);
}

builder.append(rake.getBinPath());
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/hudson/plugins/rake/Util.java
Expand Up @@ -121,6 +121,27 @@ public static Collection<File> getRubyInstallations() throws IOException {

return getRubyInstallations(systemPath);
}

/**
* Works kind of like the "which" program; the method won't try to apply
* PATHEXT to {@link executable} when searching the PATH.
* @param executable The full name of the program to find in the {@link path}.
* @param path The value of the PATH environment variable.
* @param pathSeparator The path separation character for the agent's OS.
* @return The absolute path to the {@link executable} if it was found in the path;
* the original value of {@link executable} otherwise.
*/
public static String findInPath(String executable, String path, String pathSeparator) {
final String[] parts = path.split(pathSeparator);
for (String part : parts) {
final File potentialExecutable = new File(part, executable);
if (potentialExecutable.exists()) {
executable = potentialExecutable.getAbsolutePath();
break;
}
}
return executable;
}

protected static Collection<File> getRubyInstallations(String systemPath) throws IOException {
Collection<File> rubyVersions = new LinkedHashSet<File>();
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/hudson/plugins/rake/TestUtil.java
Expand Up @@ -41,6 +41,37 @@ public void testDetectRubyInstallations() throws Exception {
}
}

public void testFindInPath() throws IOException {
String temp = System.getenv("TEMP");
if (temp == null || temp.length() == 0) {
temp = "/tmp";
}
final File folderOne = new File(temp, "testFindInPath-folderOne");
final File folderTwo = new File(temp, "testFindInPath-folderTwo");
final File emptyFile = new File(folderTwo, "testFindInPath-emptyFile.txt");
try {
folderOne.mkdir();
folderTwo.mkdir();
emptyFile.createNewFile();
final String path = folderOne.getAbsolutePath() + File.pathSeparator + folderTwo.getAbsolutePath();

final String actual = Util.findInPath(emptyFile.getName(), path, File.pathSeparator);

assertEquals(emptyFile.getAbsolutePath(), actual);
}
finally {
if (emptyFile.exists()) {
emptyFile.delete();
}
if (folderTwo.exists()) {
folderTwo.delete();
}
if (folderOne.exists()) {
folderOne.delete();
}
}
}

public void testGetCanonicalRubies() throws IOException {
if (execTest() && System.getenv("JRUBY_HOME") != null) {
File file = new File(System.getenv("JRUBY_HOME") + "/bin/jruby");
Expand Down

0 comments on commit 7dc5389

Please sign in to comment.