Permalink
Please
sign in to comment.
Browse files
[FIXED JENKINS-7478]
Create Line Ending Conversion utility to convert scripts to proper OS line ending type. Tests included.
- Loading branch information
Showing
with
233 additions
and 53 deletions.
- +8 −2 core/src/main/java/hudson/tasks/BatchFile.java
- +9 −25 core/src/main/java/hudson/tasks/Shell.java
- +1 −14 core/src/main/java/hudson/tools/AbstractCommandInstaller.java
- +7 −1 core/src/main/java/hudson/tools/BatchCommandInstaller.java
- +7 −1 core/src/main/java/hudson/tools/CommandInstaller.java
- +66 −0 core/src/main/java/hudson/util/LineEndingConversion.java
- +30 −0 test/src/test/java/hudson/tasks/BatchFileTest.java
- +33 −10 test/src/test/java/hudson/tasks/ShellTest.java
- +22 −0 test/src/test/java/hudson/tools/BatchCommandInstallerTest.java
- +22 −0 test/src/test/java/hudson/tools/CommandInstallerTest.java
- +28 −0 test/src/test/java/hudson/util/LineEndingConversionTest.java
@@ -0,0 +1,66 @@ | ||
package hudson.util; | ||
|
||
/** | ||
* Converts line endings of a string. | ||
* | ||
* @since TODO | ||
* @author David Ruhmann | ||
*/ | ||
public class LineEndingConversion { | ||
|
||
/** | ||
* Supported line ending types for conversion | ||
*/ | ||
public enum EOLType { | ||
CR, | ||
CRLF, | ||
LF, | ||
LFCR, | ||
Mac, | ||
Unix, | ||
Windows | ||
} | ||
|
||
/** | ||
* Convert line endings of a string to the given type. Default to Unix type. | ||
* | ||
* @param input | ||
* The string containing line endings to be converted. | ||
* @param type | ||
* Type of line endings to convert the string into. | ||
* @return | ||
* String updated with the new line endings or null if given null. | ||
*/ | ||
public static String convertEOL(String input, EOLType type) { | ||
if (null == input || 0 == input.length()) { | ||
return input; | ||
} | ||
// Convert line endings to Unix LF, | ||
// which also sets up the string for other conversions | ||
input = input.replace("\r\n","\n"); | ||
input = input.replace("\r","\n"); | ||
switch (type) { | ||
case CR: | ||
case Mac: | ||
// Convert line endings to CR | ||
input = input.replace("\n", "\r"); | ||
break; | ||
case CRLF: | ||
case Windows: | ||
// Convert line endings to Windows CR/LF | ||
input = input.replace("\n", "\r\n"); | ||
break; | ||
default: | ||
case LF: | ||
case Unix: | ||
// Conversion already completed | ||
return input; | ||
case LFCR: | ||
// Convert line endings to LF/CR | ||
input = input.replace("\n", "\n\r"); | ||
break; | ||
} | ||
return input; | ||
} | ||
} | ||
|
@@ -0,0 +1,30 @@ | ||
package hudson.tasks; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
/** | ||
* Tests for the BatchFile tasks class. | ||
* | ||
* @author David Ruhmann | ||
*/ | ||
public class BatchFileTest { | ||
|
||
@Rule | ||
public JenkinsRule rule = new JenkinsRule(); | ||
|
||
@Issue("JENKINS-7478") | ||
@Test | ||
public void validateBatchFileCommandEOL() throws Exception { | ||
BatchFile obj = new BatchFile("echo A\necho B\recho C"); | ||
rule.assertStringContains(obj.getCommand(), "echo A\r\necho B\r\necho C"); | ||
} | ||
|
||
@Test | ||
public void validateBatchFileContents() throws Exception { | ||
BatchFile obj = new BatchFile("echo A\necho B\recho C"); | ||
rule.assertStringContains(obj.getContents(), "echo A\r\necho B\r\necho C\r\nexit %ERRORLEVEL%"); | ||
} | ||
} |
@@ -0,0 +1,22 @@ | ||
package hudson.tools; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
/** | ||
* Tests for the BatchCommandInstaller tools class. | ||
* | ||
* @author David Ruhmann | ||
*/ | ||
public class BatchCommandInstallerTest { | ||
|
||
@Rule | ||
public JenkinsRule rule = new JenkinsRule(); | ||
|
||
@Test | ||
public void validateBatchCommandInstallerCommandEOL() throws Exception { | ||
BatchCommandInstaller obj = new BatchCommandInstaller("", "echo A\necho B\recho C", ""); | ||
rule.assertStringContains(obj.getCommand(), "echo A\r\necho B\r\necho C"); | ||
} | ||
} |

Oops, something went wrong.
0 comments on commit
67ac47a