Skip to content

Commit ef46066

Browse files
committed
compiletest: only truncate at the end, to make it more clearly visible
1 parent 38bbc2c commit ef46066

File tree

2 files changed

+31
-63
lines changed

2 files changed

+31
-63
lines changed

src/tools/compiletest/src/read2.rs

+17-27
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ pub fn read2_abbreviated(mut child: Child, filter_paths_from_len: &[String]) ->
2727
Ok(Output { status, stdout: stdout.into_bytes(), stderr: stderr.into_bytes() })
2828
}
2929

30-
const HEAD_LEN: usize = 160 * 1024;
31-
const TAIL_LEN: usize = 256 * 1024;
30+
const MAX_OUT_LEN: usize = 512 * 1024;
3231

3332
// Whenever a path is filtered when counting the length of the output, we need to add some
3433
// placeholder length to ensure a compiler emitting only filtered paths doesn't cause a OOM.
@@ -39,7 +38,7 @@ const FILTERED_PATHS_PLACEHOLDER_LEN: usize = 32;
3938

4039
enum ProcOutput {
4140
Full { bytes: Vec<u8>, filtered_len: usize },
42-
Abbreviated { head: Vec<u8>, skipped: usize, tail: Box<[u8]> },
41+
Abbreviated { head: Vec<u8>, skipped: usize },
4342
}
4443

4544
impl ProcOutput {
@@ -83,24 +82,21 @@ impl ProcOutput {
8382
}
8483

8584
let new_len = bytes.len();
86-
if (*filtered_len).min(new_len) <= HEAD_LEN + TAIL_LEN {
85+
if (*filtered_len).min(new_len) <= MAX_OUT_LEN {
8786
return;
8887
}
8988

9089
let mut head = replace(bytes, Vec::new());
91-
let mut middle = head.split_off(HEAD_LEN);
92-
let tail = middle.split_off(middle.len() - TAIL_LEN).into_boxed_slice();
93-
let skipped = new_len - HEAD_LEN - TAIL_LEN;
94-
ProcOutput::Abbreviated { head, skipped, tail }
90+
// Don't truncate if this as a whole line.
91+
// That should make it less likely that we cut a JSON line in half.
92+
if head.last() != Some(&('\n' as u8)) {
93+
head.truncate(MAX_OUT_LEN);
94+
}
95+
let skipped = new_len - head.len();
96+
ProcOutput::Abbreviated { head, skipped }
9597
}
96-
ProcOutput::Abbreviated { ref mut skipped, ref mut tail, .. } => {
98+
ProcOutput::Abbreviated { ref mut skipped, .. } => {
9799
*skipped += data.len();
98-
if data.len() <= TAIL_LEN {
99-
tail[..data.len()].copy_from_slice(data);
100-
tail.rotate_left(data.len());
101-
} else {
102-
tail.copy_from_slice(&data[(data.len() - TAIL_LEN)..]);
103-
}
104100
return;
105101
}
106102
};
@@ -110,18 +106,12 @@ impl ProcOutput {
110106
fn into_bytes(self) -> Vec<u8> {
111107
match self {
112108
ProcOutput::Full { bytes, .. } => bytes,
113-
ProcOutput::Abbreviated { mut head, mut skipped, tail } => {
114-
let mut tail = &*tail;
115-
116-
// Skip over '{' at the start of the tail, so we don't later wrongfully consider this as json.
117-
// See <https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Weird.20CI.20failure/near/321797811>
118-
while tail.get(0) == Some(&b'{') {
119-
tail = &tail[1..];
120-
skipped += 1;
121-
}
122-
123-
write!(&mut head, "\n\n<<<<<< SKIPPED {} BYTES >>>>>>\n\n", skipped).unwrap();
124-
head.extend_from_slice(tail);
109+
ProcOutput::Abbreviated { mut head, skipped } => {
110+
let head_note =
111+
format!("<<<<<< TRUNCATED, SHOWING THE FIRST {} BYTES >>>>>>\n\n", head.len());
112+
head.splice(0..0, head_note.into_bytes());
113+
write!(&mut head, "\n\n<<<<<< TRUNCATED, DROPPED {} BYTES >>>>>>", skipped)
114+
.unwrap();
125115
head
126116
}
127117
}

src/tools/compiletest/src/read2/tests.rs

+14-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::read2::{ProcOutput, FILTERED_PATHS_PLACEHOLDER_LEN, HEAD_LEN, TAIL_LEN};
1+
use std::io::Write;
2+
3+
use crate::read2::{ProcOutput, FILTERED_PATHS_PLACEHOLDER_LEN, MAX_OUT_LEN};
24

35
#[test]
46
fn test_abbreviate_short_string() {
@@ -21,35 +23,13 @@ fn test_abbreviate_short_string_multiple_steps() {
2123
fn test_abbreviate_long_string() {
2224
let mut out = ProcOutput::new();
2325

24-
let data = vec![b'.'; HEAD_LEN + TAIL_LEN + 16];
26+
let data = vec![b'.'; MAX_OUT_LEN + 16];
2527
out.extend(&data, &[]);
2628

27-
let mut expected = vec![b'.'; HEAD_LEN];
28-
expected.extend_from_slice(b"\n\n<<<<<< SKIPPED 16 BYTES >>>>>>\n\n");
29-
expected.extend_from_slice(&vec![b'.'; TAIL_LEN]);
30-
31-
// We first check the length to avoid endless terminal output if the length differs, since
32-
// `out` is hundreds of KBs in size.
33-
let out = out.into_bytes();
34-
assert_eq!(expected.len(), out.len());
35-
assert_eq!(expected, out);
36-
}
37-
38-
#[test]
39-
fn test_abbreviate_long_string_multiple_steps() {
40-
let mut out = ProcOutput::new();
41-
42-
out.extend(&vec![b'.'; HEAD_LEN], &[]);
43-
out.extend(&vec![b'.'; TAIL_LEN], &[]);
44-
// Also test whether the rotation works
45-
out.extend(&vec![b'!'; 16], &[]);
46-
out.extend(&vec![b'?'; 16], &[]);
47-
48-
let mut expected = vec![b'.'; HEAD_LEN];
49-
expected.extend_from_slice(b"\n\n<<<<<< SKIPPED 32 BYTES >>>>>>\n\n");
50-
expected.extend_from_slice(&vec![b'.'; TAIL_LEN - 32]);
51-
expected.extend_from_slice(&vec![b'!'; 16]);
52-
expected.extend_from_slice(&vec![b'?'; 16]);
29+
let mut expected = Vec::new();
30+
write!(expected, "<<<<<< TRUNCATED, SHOWING THE FIRST {MAX_OUT_LEN} BYTES >>>>>>\n\n").unwrap();
31+
expected.extend_from_slice(&[b'.'; MAX_OUT_LEN]);
32+
expected.extend_from_slice(b"\n\n<<<<<< TRUNCATED, DROPPED 16 BYTES >>>>>>");
5333

5434
// We first check the length to avoid endless terminal output if the length differs, since
5535
// `out` is hundreds of KBs in size.
@@ -86,9 +66,8 @@ fn test_abbreviate_filters_avoid_abbreviations() {
8666
let mut out = ProcOutput::new();
8767
let filters = &[std::iter::repeat('a').take(64).collect::<String>()];
8868

89-
let mut expected = vec![b'.'; HEAD_LEN - FILTERED_PATHS_PLACEHOLDER_LEN as usize];
69+
let mut expected = vec![b'.'; MAX_OUT_LEN - FILTERED_PATHS_PLACEHOLDER_LEN as usize];
9070
expected.extend_from_slice(filters[0].as_bytes());
91-
expected.extend_from_slice(&vec![b'.'; TAIL_LEN]);
9271

9372
out.extend(&expected, filters);
9473

@@ -104,14 +83,13 @@ fn test_abbreviate_filters_can_still_cause_abbreviations() {
10483
let mut out = ProcOutput::new();
10584
let filters = &[std::iter::repeat('a').take(64).collect::<String>()];
10685

107-
let mut input = vec![b'.'; HEAD_LEN];
108-
input.extend_from_slice(&vec![b'.'; TAIL_LEN]);
86+
let mut input = vec![b'.'; MAX_OUT_LEN];
10987
input.extend_from_slice(filters[0].as_bytes());
11088

111-
let mut expected = vec![b'.'; HEAD_LEN];
112-
expected.extend_from_slice(b"\n\n<<<<<< SKIPPED 64 BYTES >>>>>>\n\n");
113-
expected.extend_from_slice(&vec![b'.'; TAIL_LEN - 64]);
114-
expected.extend_from_slice(&vec![b'a'; 64]);
89+
let mut expected = Vec::new();
90+
write!(expected, "<<<<<< TRUNCATED, SHOWING THE FIRST {MAX_OUT_LEN} BYTES >>>>>>\n\n").unwrap();
91+
expected.extend_from_slice(&[b'.'; MAX_OUT_LEN]);
92+
expected.extend_from_slice(b"\n\n<<<<<< TRUNCATED, DROPPED 64 BYTES >>>>>>");
11593

11694
out.extend(&input, filters);
11795

0 commit comments

Comments
 (0)