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
[FIXED JENKINS-7478]
Create Line Ending Conversion utility to convert scripts to proper OS line ending type. Tests included.
- Loading branch information
David Ruhmann
committed
Aug 25, 2014
1 parent
3efe07f
commit 67ac47a
Showing
11 changed files
with
233 additions
and
53 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
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
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
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
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
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,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; | ||
} | ||
} | ||
|
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,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%"); | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
test/src/test/java/hudson/tools/BatchCommandInstallerTest.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,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.