Skip to content

Commit fb4ef30

Browse files
authored
Merge pull request #509 from dwnusbaum/skip-filter-when-anonymization-disabled
Preserve line endings in `FileContent` when anonymization is disabled and avoid duplicating binary content
2 parents e7c02e6 + 4638e99 commit fb4ef30

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/main/java/com/cloudbees/jenkins/support/api/BaseFileContent.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ protected void writeTo(OutputStream os) throws IOException {
124124
}
125125

126126
protected void writeTo(OutputStream os, @NonNull ContentFilter filter) throws IOException {
127-
if (isBinary) {
127+
if (isBinary || filter == ContentFilter.NONE) {
128128
writeTo(os);
129+
return;
129130
}
130131

131132
try {

src/test/java/com/cloudbees/jenkins/support/api/FileContentTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
import static org.junit.Assert.assertEquals;
2727
import static org.junit.Assert.assertTrue;
2828

29+
import com.cloudbees.jenkins.support.filter.ContentFilter;
2930
import java.io.ByteArrayOutputStream;
3031
import java.io.File;
32+
import java.io.IOException;
3133
import java.nio.charset.Charset;
34+
import java.nio.charset.StandardCharsets;
3235
import org.apache.commons.io.FileUtils;
3336
import org.junit.Rule;
3437
import org.junit.Test;
@@ -69,4 +72,24 @@ public void encoding() throws Exception {
6972
new FileContent("-", f).writeTo(baos, input -> input);
7073
assertTrue(baos.toString().contains("Checking folder"));
7174
}
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+
}
7295
}

0 commit comments

Comments
 (0)