term-transcript 0.5.0

Snapshotting and snapshot testing for CLI / REPL applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use std::io::{Cursor, Read};

use assert_matches::assert_matches;
use quick_xml::events::{BytesEnd, BytesStart, BytesText};
use test_casing::test_casing;

use super::*;
use crate::ExitStatus;

const SVG: &[u8] = br#"
    <svg viewBox="0 0 652 344" xmlns="http://www.w3.org/2000/svg">
      <foreignObject x="0" y="0" width="652" height="344">
        <div xmlns="http://www.w3.org/1999/xhtml" class="container">
          <div class="input"><pre><span class="prompt">$</span> ls -al --color=always</pre></div>
          <div class="output"><pre>total 28
drwxr-xr-x 1 alex alex 4096 Apr 18 12:54 <span class="fg4">.</span>
drwxrwxrwx 1 alex alex 4096 Apr 18 12:38 <span class="fg4 bg2">..</span>
-rw-r--r-- 1 alex alex 8199 Apr 18 12:48 Cargo.lock</pre>
          </div>
        </div>
      </foreignObject>
    </svg>
"#;

const LEGACY_SVG: &[u8] = br#"
    <svg viewBox="0 0 652 344" xmlns="http://www.w3.org/2000/svg">
      <foreignObject x="0" y="0" width="652" height="344">
        <div xmlns="http://www.w3.org/1999/xhtml" class="container">
          <div class="user-input"><pre><span class="prompt">$</span> ls -al --color=always</pre></div>
          <div class="term-output"><pre>total 28
drwxr-xr-x 1 alex alex 4096 Apr 18 12:54 <span class="fg4">.</span>
drwxrwxrwx 1 alex alex 4096 Apr 18 12:38 <span class="fg4 bg2">..</span>
-rw-r--r-- 1 alex alex 8199 Apr 18 12:48 Cargo.lock</pre>
          </div>
        </div>
      </foreignObject>
    </svg>
"#;

const PURE_SVG: &[u8] = br#"
<svg viewBox="0 0 720 138" width="720" height="138" xmlns="http://www.w3.org/2000/svg">
  <svg x="0" y="10" width="720" height="118" viewBox="0 0 720 118">
    <g class="input-bg"><rect x="0" y="0" width="100%" height="22"></rect></g>
    <g class="container fg7"><g class="input"><g><text class="prompt">$</text><text> ls -al --color&#x3D;always</text><text>
</text></g></g><g class="output"><g><text>total 28</text><text>
</text></g><g><text>drwxr-xr-x 1 alex alex 4096 Apr 18 12:54 </text><text class="fg4">.</text><text>
</text></g><text>drwxrwxrwx 1 alex alex 4096 Apr 18 12:38 </text><text class="fg4 bg2">..</text><text>
</text><g><text>-rw-r--r-- 1 alex alex 8199 Apr 18 12:48 Cargo.lock</text><text>
</text></g></g></g>
  </svg>
</svg>
"#;

#[test_casing(3, [SVG, LEGACY_SVG, PURE_SVG])]
fn reading_file(file_contents: &[u8]) {
    let transcript = Transcript::from_svg(file_contents).unwrap();
    assert_eq!(transcript.interactions().len(), 1);

    let interaction = &transcript.interactions()[0];
    assert_eq!(interaction.input().as_ref(), "ls -al --color=always");
    assert_eq!(interaction.input().prompt(), Some("$"));

    let plaintext = interaction.output().text();
    assert!(plaintext.starts_with("total 28\ndrwxr-xr-x"));
    assert!(plaintext.contains("4096 Apr 18 12:54 .\n"));
    assert!(!plaintext.contains(r#"<span class="fg4">.</span>"#));
    assert!(!plaintext.contains("__"), "{plaintext}");

    let color_spans = interaction.output().as_str().spans();
    assert_eq!(
        color_spans.len(),
        5,
        "{:#?}",
        color_spans.collect::<Vec<_>>()
    ); // 2 colored regions + 3 surrounding areas
}

#[test]
fn reading_file_with_extra_info() {
    let mut data = SVG.to_owned();
    data.extend_from_slice(b"<other>data</other>");
    let mut data = Cursor::new(data.as_slice());

    let transcript = Transcript::from_svg(&mut data).unwrap();
    assert_eq!(transcript.interactions().len(), 1);

    // Check that the parser stops after `</svg>`.
    let mut end = String::new();
    data.read_to_string(&mut end).unwrap();
    assert_eq!(end.trim_start(), "<other>data</other>");
}

#[test]
fn reading_file_with_no_output() {
    const SVG: &[u8] = br#"
        <svg viewBox="0 0 652 344" xmlns="http://www.w3.org/2000/svg">
          <foreignObject x="0" y="0" width="652" height="344">
            <div xmlns="http://www.w3.org/1999/xhtml" class="container">
              <div class="input"><pre><span class="prompt">$</span> ls &gt; /dev/null</pre></div>
              <div class="input"><pre><span class="prompt">$</span> ls</pre></div>
              <div class="output"><pre>total 28
drwxr-xr-x 1 alex alex 4096 Apr 18 12:54 <span class="fg-blue">.</span>
drwxrwxrwx 1 alex alex 4096 Apr 18 12:38 <span class="fg-blue bg-green">..</span>
-rw-r--r-- 1 alex alex 8199 Apr 18 12:48 Cargo.lock</pre>
              </div>
            </div>
          </foreignObject>
        </svg>
    "#;

    let transcript = Transcript::from_svg(SVG).unwrap();
    assert_eq!(transcript.interactions().len(), 2);

    assert_eq!(
        transcript.interactions()[0].input().as_ref(),
        "ls > /dev/null"
    );
    assert!(transcript.interactions()[0].output().text().is_empty());

    assert_eq!(transcript.interactions()[1].input().as_ref(), "ls");
    assert!(!transcript.interactions()[1].output().text().is_empty());
}

#[test]
fn reading_file_with_exit_code_info() {
    const SVG: &[u8] = br#"
        <svg viewBox="0 0 652 344" xmlns="http://www.w3.org/2000/svg">
          <foreignObject x="0" y="0" width="652" height="344">
            <div xmlns="http://www.w3.org/1999/xhtml" class="container">
              <div class="input input-failure" data-exit-status="127"><pre><span class="prompt">$</span> what</pre></div>
            </div>
          </foreignObject>
        </svg>
    "#;

    let transcript = Transcript::from_svg(SVG).unwrap();
    assert_eq!(transcript.interactions().len(), 1);

    let interaction = &transcript.interactions()[0];
    assert_eq!(interaction.input().as_ref(), "what");
    assert_eq!(interaction.exit_status(), Some(ExitStatus(127)));
}

#[test]
fn reading_file_with_hidden_input() {
    const SVG: &[u8] = br#"
        <svg viewBox="0 0 652 344" xmlns="http://www.w3.org/2000/svg">
          <foreignObject x="0" y="0" width="652" height="344">
            <div xmlns="http://www.w3.org/1999/xhtml" class="container">
              <div class="input input-hidden" data-exit-status="127"><pre><span class="prompt">$</span> what</pre></div>
            </div>
          </foreignObject>
        </svg>
    "#;

    let transcript = Transcript::from_svg(SVG).unwrap();
    assert_eq!(transcript.interactions().len(), 1);
    assert!(transcript.interactions()[0].input().is_hidden());
}

#[test]
fn invalid_exit_code_info() {
    const SVG: &[u8] = br#"
        <svg viewBox="0 0 652 344" xmlns="http://www.w3.org/2000/svg">
          <foreignObject x="0" y="0" width="652" height="344">
            <div xmlns="http://www.w3.org/1999/xhtml" class="container">
              <div class="input input-failure" data-exit-status="??"><pre><span class="prompt">$</span> what</pre></div>
            </div>
          </foreignObject>
        </svg>
    "#;

    let err = Transcript::from_svg(SVG).unwrap_err();
    assert_matches!(err.inner(), ParseError::InvalidExitStatus(_));
    assert!(err.location().start >= 200);
    assert!(!err.location().is_empty());
}

#[test]
fn reading_file_without_svg_tag() {
    let data: &[u8] = b"<div>Text</div>";
    let err = Transcript::from_svg(data).unwrap_err();

    assert_matches!(err.inner(), ParseError::UnexpectedRoot(tag) if tag == "div");
}

#[test]
fn reading_file_without_container() {
    let bogus_data: &[u8] = br#"
        <svg viewBox="0 0 652 344" xmlns="http://www.w3.org/2000/svg" version="1.1">
          <style>.container { color: #eee; }</style>
        </svg>"#;
    let err = Transcript::from_svg(bogus_data).unwrap_err();

    assert_matches!(err.inner(), ParseError::UnexpectedEof);
    assert_eq!(err.location().start, bogus_data.len());
    assert!(err.location().is_empty());
}

const INVALID_ATTRS: [&str; 5] = [
    "",
    // no class
    r#"xmlns="http://www.w3.org/1999/xhtml""#,
    // no namespace
    r#"class="container""#,
    // invalid namespace
    r#"xmlns="http://www.w3.org/2000/svg" class="container""#,
    // invalid class
    r#"xmlns="http://www.w3.org/1999/xhtml" class="cont""#,
];

#[test_casing(5, INVALID_ATTRS)]
fn reading_file_with_invalid_container(attrs: &str) {
    let bogus_data = format!(
        r#"
        <svg viewBox="0 0 652 344" xmlns="http://www.w3.org/2000/svg" version="1.1">
          <foreignObject x="0" y="0" width="652" height="344">
            <div {attrs}>Test</div>
          </foreignObject>
        </svg>
        "#
    );
    let err = Transcript::from_svg(bogus_data.as_bytes()).unwrap_err();

    assert_matches!(err.inner(), ParseError::InvalidContainer);
    assert_ne!(err.location().start, 0);
    assert!(!err.location().is_empty());
}

#[test]
fn reading_user_input_with_manual_events() {
    let mut state = UserInputState::new(None, false);
    {
        let event = Event::Start(BytesStart::new("pre"));
        assert!(state.process(event, 0..1).unwrap().is_none());
        assert_eq!(state.prompt_open_tags, None);
        assert!(state.text.plaintext_buffer().is_empty());
    }
    {
        let event = Event::Start(BytesStart::from_content(r#"span class="prompt""#, 4));
        assert!(state.process(event, 1..2).unwrap().is_none());
        assert_eq!(state.prompt_open_tags, Some(2));
        assert!(state.text.plaintext_buffer().is_empty());
    }
    {
        let event = Event::Text(BytesText::from_escaped("$"));
        assert!(state.process(event, 2..3).unwrap().is_none());
        assert_eq!(state.text.plaintext_buffer(), "$");
    }
    {
        let event = Event::End(BytesEnd::new("span"));
        assert!(state.process(event, 3..4).unwrap().is_none());
        assert_eq!(state.prompt.as_deref(), Some("$"));
        assert!(state.text.plaintext_buffer().is_empty());
    }
    {
        let event = Event::Text(BytesText::from_escaped(" rainbow"));
        assert!(state.process(event, 4..5).unwrap().is_none());
    }

    let event = Event::End(BytesEnd::new("pre"));
    assert!(state.process(event, 5..6).unwrap().is_none());

    let event = Event::End(BytesEnd::new("div"));
    let user_input = state.process(event, 6..7).unwrap().unwrap().input;
    assert_eq!(user_input.prompt(), Some("$"));
    assert_eq!(user_input.as_ref(), "rainbow");
}

fn read_user_input(input: &[u8]) -> UserInput {
    let mut wrapped_input = Vec::with_capacity(input.len() + 11);
    wrapped_input.extend_from_slice(b"<div>");
    wrapped_input.extend_from_slice(input);
    wrapped_input.extend_from_slice(b"</div>");

    let mut reader = XmlReader::from_reader(wrapped_input.as_slice());
    let mut state = UserInputState::new(None, false);

    // Skip the `<div>` start event.
    while !matches!(reader.read_event().unwrap(), Event::Start(_)) {
        // Drop the event.
    }

    loop {
        let event = reader.read_event().unwrap();
        if let Event::Eof = &event {
            panic!("Reached EOF without creating `UserInput`");
        }

        if let Some(interaction) = state.process(event, 0..1).unwrap() {
            break interaction.input;
        }
    }
}

#[test]
fn reading_user_input_base_case() {
    let user_input = read_user_input(br#"<pre><span class="prompt">&gt;</span> echo foo</pre>"#);

    assert_eq!(user_input.prompt(), Some(">"));
    assert_eq!(user_input.as_ref(), "echo foo");
}

#[test]
fn reading_user_input_without_wrapper() {
    let user_input = read_user_input(br#"<span class="prompt">&gt;</span> echo foo"#);

    assert_eq!(user_input.prompt(), Some(">"));
    assert_eq!(user_input.as_ref(), "echo foo");
}

#[test]
fn reading_user_input_without_prompt() {
    let user_input = read_user_input(br#"<pre>echo <span class="bold">foo</span></pre>"#);

    assert_eq!(user_input.prompt(), None);
    assert_eq!(user_input.as_ref(), "echo foo");
}

#[test]
fn reading_user_input_with_prompt_only() {
    let user_input = read_user_input(br#"<pre><span class="prompt">$</span></pre>"#);

    assert_eq!(user_input.prompt(), Some("$"));
    assert_eq!(user_input.as_ref(), "");
}

#[test]
fn reading_user_input_with_bogus_prompt_location() {
    let user_input =
        read_user_input(br#"<pre>echo foo <span class="prompt">&gt;</span> output.log</pre>"#);

    assert_eq!(user_input.prompt(), None);
    assert_eq!(user_input.as_ref(), "echo foo > output.log");
}

#[test]
fn reading_user_input_with_multiple_prompts() {
    let user_input = read_user_input(
        b"<pre><span class=\"prompt\">&gt;&gt;&gt;</span> \
          echo foo <span class=\"prompt\">&gt;</span> output.log</pre>",
    );

    assert_eq!(user_input.prompt(), Some(">>>"));
    assert_eq!(user_input.as_ref(), "echo foo > output.log");
}

#[test]
fn reading_user_input_with_leading_spaces() {
    let user_input =
        read_user_input(b"<pre><span class=\"prompt\">&gt;</span>   ls &gt; /dev/null</pre>");
    assert_eq!(user_input.as_ref(), "  ls > /dev/null");
}

#[test]
fn newline_breaks_are_normalized() {
    let mut state = TextReadingState::default();
    let text = BytesText::from_escaped("some\ntext\r\nand more text");
    state.process(Event::Text(text), 0..1).unwrap();
    assert_eq!(state.plaintext_buffer(), "some\ntext\nand more text");
}

#[test]
fn parser_errors_on_unknown_entity() {
    const SVG: &[u8] = br#"
        <svg viewBox="0 0 652 344" xmlns="http://www.w3.org/2000/svg">
          <foreignObject x="0" y="0" width="652" height="344">
            <div xmlns="http://www.w3.org/1999/xhtml" class="container">
              <div class="input"><pre>&what;</pre></div>
            </div>
          </foreignObject>
        </svg>
    "#;

    let err = Transcript::from_svg(SVG).unwrap_err();

    assert_matches!(
        err.inner(),
        ParseError::Xml(quick_xml::Error::Escape(
            quick_xml::escape::EscapeError::UnrecognizedEntity(pos, entity),
        )) if entity == "what" && *pos == err.location()
    );
}

#[test]
fn reading_legacy_pure_svg_with_hard_breaks() {
    const SVG: &str = r#"
<svg x="0" y="10" width="720" height="342" viewBox="0 0 720 342">
  <g class="container fg7"><g xml:space="preserve" class="input"><text x="10" y="16"><tspan class="prompt">$</tspan> font-subset info RobotoMono.ttf
</text></g><g xml:space="preserve" class="output"><text x="42" y="42"><tspan class="bold">Roboto Mono</tspan> <tspan class="dimmed">Regular</tspan>
</text><text x="42" y="60"><tspan class="bold">License:</tspan> This Font Software is licensed under the SIL Open Font License, Version<tspan class="hard-br" dx="5">&gt;</tspan>
</text><text x="42" y="78"> 1.1. This license is available with a FAQ at: https://openfontlicense.org
</text></g>
  </g>
</svg>"#;

    let transcript = Transcript::from_svg(SVG.as_bytes()).unwrap();
    assert_eq!(transcript.interactions().len(), 1);
    let output = transcript.interactions()[0].output();
    assert!(
        output.text().starts_with("Roboto Mono Regular\nLicense:"),
        "{output:#?}"
    );
    assert_eq!(output.text().lines().count(), 2, "{output:#?}");
    assert!(!output.text().contains('>'), "{output:#?}");
}

#[test]
fn reading_pure_svg_with_hard_breaks() {
    const SVG: &str = r#"
<svg x="0" y="10" width="720" height="342" viewBox="0 0 720 342">
  <g class="container fg7"><g class="input"><text x="10" y="16">$</text> <text>font-subset info RobotoMono.ttf
</text></g><g class="output"><text x="42" y="42" class="bold">Roboto Mono </text><text class="dimmed">Regular</text><text>
</text><text x="42" y="60" class="bold">License:</text><text> This Font Software is licensed under the SIL Open Font License, Version</text><text class="hard-br">
</text><text x="42" y="78"> 1.1. This license is available with a FAQ at: https://openfontlicense.org
</text></g>
  </g>
</svg>"#;

    let transcript = Transcript::from_svg(SVG.as_bytes()).unwrap();
    assert_eq!(transcript.interactions().len(), 1);
    let output = transcript.interactions()[0].output();
    assert!(
        output.text().starts_with("Roboto Mono Regular\nLicense:"),
        "{output:#?}"
    );
    assert_eq!(output.text().lines().count(), 2, "{output:#?}");
}

#[test]
fn reading_pure_svg_with_styled_hard_breaks() {
    const SVG: &str = r#"
<svg x="0" y="10" width="720" height="342" viewBox="0 0 720 342">
  <g class="container fg7"><g xml:space="preserve" class="input"><text x="10" y="16"><tspan class="prompt">$</tspan> font-subset info RobotoMono.ttf
</text></g><g xml:space="preserve" class="output"><text x="42" y="60"><tspan class="bold">License:</tspan> <tspan class="fg3">don't know</tspan><tspan class="hard-br" dx="5">&gt;</tspan>
</text><text x="42" y="78"><tspan class="fg3"> lol</tspan>
</text></g>
  </g>
</svg>"#;

    let transcript = Transcript::from_svg(SVG.as_bytes()).unwrap();
    assert_eq!(transcript.interactions().len(), 1);
    let output = transcript.interactions()[0].output();
    assert_eq!(output.text(), "License: don't know lol");
}