|
26 | 26 | import static org.junit.Assert.assertEquals;
|
27 | 27 | import static org.junit.Assert.assertTrue;
|
28 | 28 |
|
| 29 | +import com.cloudbees.jenkins.support.filter.ContentFilter; |
29 | 30 | import java.io.ByteArrayOutputStream;
|
30 | 31 | import java.io.File;
|
| 32 | +import java.io.IOException; |
31 | 33 | import java.nio.charset.Charset;
|
| 34 | +import java.nio.charset.StandardCharsets; |
32 | 35 | import org.apache.commons.io.FileUtils;
|
33 | 36 | import org.junit.Rule;
|
34 | 37 | import org.junit.Test;
|
@@ -69,4 +72,24 @@ public void encoding() throws Exception {
|
69 | 72 | new FileContent("-", f).writeTo(baos, input -> input);
|
70 | 73 | assertTrue(baos.toString().contains("Checking folder"));
|
71 | 74 | }
|
| 75 | + |
| 76 | + @Test |
| 77 | + public void normalizesLineEndingsWhenFiltering() throws IOException { |
| 78 | + File f = tmp.newFile(); |
| 79 | + FileUtils.writeStringToFile(f, "Before\r\nlines\n", StandardCharsets.UTF_8); |
| 80 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 81 | + new FileContent("-", f).writeTo(baos, s -> s.replace("Before", "After")); |
| 82 | + // Note that besides the filtering, \r has been dropped! |
| 83 | + assertEquals("After\nlines\n", baos.toString()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void preservesLineEndingsWithContentFilterNone() throws IOException { |
| 88 | + File f = tmp.newFile(); |
| 89 | + FileUtils.writeStringToFile(f, "Cool\r\nlines\n", StandardCharsets.UTF_8); |
| 90 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 91 | + new FileContent("-", f).writeTo(baos, ContentFilter.NONE); |
| 92 | + String x = baos.toString(); |
| 93 | + assertEquals("Cool\r\nlines\n", baos.toString()); |
| 94 | + } |
72 | 95 | }
|
0 commit comments