Skip to content

Commit 1a9685d

Browse files
sarahdeitketimja
andauthored
Change default behavior to truncate Checks text from start when maxSize present (#270)
* change default behavior to truncate from start * retrigger ci * Update src/test/java/io/jenkins/plugins/checks/api/ChecksOutputTest.java --------- Co-authored-by: Tim Jacomb <[email protected]>
1 parent 1ad165e commit 1a9685d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/main/java/io/jenkins/plugins/checks/api/ChecksOutput.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ public Optional<String> getText() {
7676
* @return Text, truncated to maxSize with truncation message if appropriate.
7777
*/
7878
public Optional<String> getText(final int maxSize) {
79-
return Optional.ofNullable(text).map(s -> s.build(maxSize));
79+
return Optional.ofNullable(text)
80+
.map(s -> new TruncatedString.Builder()
81+
.setChunkOnNewlines()
82+
.setTruncateStart()
83+
.addText(s.toString())
84+
.build()
85+
.buildByChars(maxSize));
8086
}
8187

8288
public List<ChecksAnnotation> getChecksAnnotations() {

src/test/java/io/jenkins/plugins/checks/api/ChecksOutputTest.java

+29
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,35 @@ void shouldCopyConstructCorrectly() {
9797
.containsExactlyInAnyOrderElementsOf(images);
9898
}
9999

100+
@Test
101+
void shouldTruncateTextFromStart() {
102+
String longText = "This is the beginning.\n" + "Middle part.\n".repeat(10) + "This is the end.\n";
103+
ChecksOutput checksOutput = new ChecksOutputBuilder()
104+
.withText(longText)
105+
.build();
106+
107+
String truncated = checksOutput.getText(75).orElse("");
108+
109+
assertThat(truncated)
110+
.startsWith("Output truncated.")
111+
.endsWith("This is the end.\n");
112+
assertThat(truncated.length()).isLessThanOrEqualTo(75);
113+
}
114+
115+
@Test
116+
void shouldNotTruncateShortText() {
117+
String shortText = "This is a short text that should not be truncated.";
118+
ChecksOutput checksOutput = new ChecksOutputBuilder()
119+
.withText(shortText)
120+
.build();
121+
122+
String result = checksOutput.getText(100).orElse("");
123+
124+
assertThat(result)
125+
.isEqualTo(shortText)
126+
.doesNotContain("Output truncated.");
127+
}
128+
100129
private List<ChecksAnnotation> createAnnotations() {
101130
final ChecksAnnotationBuilder builder = new ChecksAnnotationBuilder()
102131
.withPath("src/main/java/1.java")

0 commit comments

Comments
 (0)