standout-test 10.0.0

In-process test harness for applications built with the standout CLI framework
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
use clap::Command;
use serde_json::json;
use serial_test::serial;
use standout::cli::FnHandler;
use standout::cli::{App, ExitStatus, HelpResult, Output, RunErrorKind, SuccessKind};
use standout::EmbeddedTemplates;
use standout::Theme;
use standout_fixtures::downstream;
use standout_test::TestHarness;

const TEMPLATES: &[(&str, &str)] = &[("review", "listed")];
fn configured_help(app: &App, cmd: Command, args: &[&str]) -> String {
    match app.get_matches_from(cmd, args, &standout::InputSources::from_process()) {
        HelpResult::Help(h) | HelpResult::PagedHelp(h) => h,
        other => panic!("expected rendered help, got {other:?}"),
    }
}
#[test]
#[serial]
fn the_help_word_renders_themed_help_through_run() {
    let fixture = downstream().flat().build();
    let result =
        TestHarness::new()
            .text_output()
            .run(fixture.app(), fixture.command(), ["lookma", "help"]);
    result.assert_success();
    assert_eq!(result.success_kind(), Some(SuccessKind::ClapHelp));
    result.assert_stdout_contains("Diff a git range");
    result.assert_stdout_contains("USAGE");
}
#[test]
#[serial]
fn downstream_theme_help_preserves_option_cues_through_run() {
    let fixture = downstream().build();
    let result =
        TestHarness::new()
            .with_color()
            .run(fixture.app(), fixture.command(), ["lookma", "help"]);
    result.assert_success();
    assert_eq!(result.success_kind(), Some(SuccessKind::ClapHelp));
    let output = result.stdout();
    let plain = result.stdout_plain();
    assert!(
        !output.contains("[header?]") && !output.contains("[metavar?]"),
        "app output themes must not leak unresolved help tags:\n{output}"
    );
    assert!(plain.contains("RANGE"), "{plain}");
    assert!(plain.contains("--threshold <RATIO>"), "{plain}");
    assert!(plain.contains("-c, --color <BOOL>"), "{plain}");
    assert!(
        plain.contains("--pattern <pattern>"),
        "fallback metavars should survive the App run path:\n{plain}"
    );
    assert!(
        plain.contains("default: brief") && plain.contains("possible values: brief, full, none"),
        "defaults and enumerated values should survive the App run path:\n{plain}"
    );
    let staged = plain
        .find("--staged")
        .expect("rendered help should include --staged");
    let threshold = plain
        .find("--threshold")
        .expect("rendered help should include --threshold");
    let staged_block = &plain[staged..threshold];
    assert!(
        !staged_block.contains('<') && !staged_block.contains("possible values:"),
        "presence flags should not advertise bool values:\n{plain}"
    );
}
#[test]
#[serial]
fn the_help_flags_render_themed_help_through_run() {
    let fixture = downstream().flat().without_help_word().build();
    for flag in ["--help", "-h"] {
        let result = TestHarness::new().text_output().run(
            fixture.app(),
            fixture.command(),
            ["lookma", flag],
        );
        result.assert_success();
        assert_eq!(result.success_kind(), Some(SuccessKind::ClapHelp));
        result.assert_stdout_contains("USAGE");
    }
}
#[test]
#[serial]
fn the_help_word_renders_a_topic_through_run() {
    let fixture = downstream().flat().build();
    let result = TestHarness::new().text_output().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "help", "ranges"],
    );
    result.assert_success();
    result.assert_stdout_contains("two revisions separated by two dots");
}
#[test]
#[serial]
fn the_help_word_renders_a_subcommands_help_through_run() {
    let fixture = downstream().build();
    let word = TestHarness::new().text_output().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "help", "review"],
    );
    word.assert_success();
    word.assert_stdout_contains("Review a range hunk by hunk");
    drop(word);
    let flag = TestHarness::new().text_output().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "review", "--help"],
    );
    flag.assert_success();
    flag.assert_stdout_contains("Review a range hunk by hunk");
}
#[test]
#[serial]
fn the_help_word_honours_the_output_flag_through_run() {
    let fixture = downstream().flat().build();
    let tagged = TestHarness::new().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "help", "--output", "term-debug"],
    );
    tagged.assert_success();
    tagged.assert_stdout_contains("[header]USAGE[/header]");
    drop(tagged);
    for mode in ["json", "yaml"] {
        let result = TestHarness::new().run(
            fixture.app(),
            fixture.command(),
            ["lookma", "help", "--output", mode],
        );
        result.assert_success();
        assert_eq!(result.success_kind(), Some(SuccessKind::ClapHelp));
        assert!(
            !result.stdout().contains("USAGE"),
            "{mode}: help is the versioned document, not the page:\n{}",
            result.stdout()
        );
        result.assert_stdout_contains("schema_version");
    }
    let csv = TestHarness::new().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "help", "--output", "csv"],
    );
    csv.assert_error_kind(RunErrorKind::Render);
    let diagnostic = csv.expect_diagnostic();
    assert_eq!(diagnostic.kind, standout::cli::DiagnosticKind::Render);
    assert!(
        diagnostic.summary.contains("no Csv projection"),
        "{diagnostic:?}"
    );
}
#[test]
#[serial]
fn the_help_document_on_a_color_tty_carries_no_ansi() {
    let fixture = downstream().flat().build();
    let result = TestHarness::new().with_color().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "help", "--output", "json"],
    );
    result.assert_success();
    assert!(
        !result.stdout().contains('\u{1b}'),
        "the help document is data, never styled:\n{:?}",
        result.stdout()
    );
    let document: serde_json::Value = serde_json::from_str(result.stdout()).unwrap();
    assert_eq!(document["schema_version"], 1);
    assert_eq!(document["name"], "lookma");
}
#[test]
#[serial]
fn structured_topics_still_print_human_text() {
    let fixture = downstream().flat().build();
    let list = TestHarness::new().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "help", "topics", "--output", "json"],
    );
    list.assert_success();
    list.assert_stdout_contains("Available Topics");
    assert!(
        !list.stdout().trim_start().starts_with('{'),
        "topics --output=json must not emit a JSON document:\n{}",
        list.stdout()
    );
    drop(list);
    let topic = TestHarness::new().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "help", "ranges", "--output", "yaml"],
    );
    topic.assert_success();
    topic.assert_stdout_contains("two revisions separated by two dots");
    assert!(
        !topic.stdout().trim_start().starts_with('{'),
        "topic --output=yaml must not emit a structured document:\n{}",
        topic.stdout()
    );
}
#[test]
#[serial]
fn the_output_flag_reaches_the_word_and_the_flags_alike() {
    let fixture = downstream().flat().build();
    for args in [
        &["lookma", "help", "--output", "term-debug"][..],
        &["lookma", "--help", "--output", "term-debug"][..],
        &["lookma", "--output", "term-debug", "--help"][..],
        &["lookma", "-h", "--output=term-debug"][..],
        &[
            "lookma",
            "--output",
            "text",
            "--help",
            "--output",
            "term-debug",
        ][..],
    ] {
        let result = TestHarness::new()
            .no_color()
            .run(fixture.app(), fixture.command(), args);
        result.assert_success();
        assert!(
            result.stdout().contains("[header]USAGE[/header]"),
            "{args:?} must render in the typed mode:\n{}",
            result.stdout()
        );
    }
    let text = TestHarness::new().no_color().run(
        fixture.app(),
        fixture.command(),
        [
            "lookma",
            "--output",
            "term-debug",
            "--help",
            "--output",
            "text",
        ],
    );
    text.assert_success();
    text.assert_stdout_contains("USAGE");
    assert!(
        !text.stdout().contains("[header]"),
        "the last `--output` wins:\n{}",
        text.stdout()
    );
}
#[test]
#[serial]
fn a_pager_request_rides_back_as_a_typed_success() {
    let fixture = downstream().flat().build();
    let result = TestHarness::new().text_output().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "help", "--page"],
    );
    result.assert_success();
    assert_eq!(result.success_kind(), Some(SuccessKind::PagedHelp));
    result.assert_stdout_contains("USAGE");
}
fn assert_is_root_help(rendered: &str) {
    assert!(
        rendered.contains("Git range to diff"),
        "expected the root's help, got:\n{rendered}"
    );
}
#[test]
#[serial]
fn an_option_value_is_not_read_as_the_targeted_command() {
    let fixture = downstream().build();
    let args = ["lookma", "--output-file-path", "review", "--help"];
    let dispatched = TestHarness::new().run(fixture.app(), fixture.command(), args);
    dispatched.assert_success();
    assert_is_root_help(dispatched.stdout());
    drop(dispatched);
    assert_is_root_help(&configured_help(fixture.app(), fixture.command(), &args));
}
#[test]
#[serial]
fn a_short_option_value_is_not_read_as_the_targeted_command() {
    let fixture = downstream().build();
    let result = TestHarness::new().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "-p", "review", "--help"],
    );
    result.assert_success();
    assert_is_root_help(result.stdout());
}
#[test]
#[serial]
fn the_walk_stops_where_the_help_request_is() {
    let fixture = downstream().build();
    for flag in ["--help", "-h"] {
        let result =
            TestHarness::new().run(fixture.app(), fixture.command(), ["lookma", flag, "review"]);
        result.assert_success();
        assert_is_root_help(result.stdout());
    }
}
fn broken_theme() -> Theme {
    Theme::new().add("header", "no-such-style")
}
fn app_with_a_broken_theme() -> App {
    App::builder()
        .templates(EmbeddedTemplates::new(TEMPLATES, ""))
        .help_handling(true)
        .include_framework_templates(false)
        .theme(broken_theme())
        .command_with(
            "review",
            FnHandler::new(|_m, _ctx| Ok(Output::Render(json!({})))),
            |cfg| cfg,
        )
        .unwrap()
        .build()
        .unwrap()
}
#[test]
#[serial]
fn a_help_that_cannot_be_rendered_is_not_a_usage_error() {
    let fixture = downstream().build();
    for args in [
        &["lookma", "help"][..],
        &["lookma", "--help"][..],
        &["lookma", "-h"][..],
    ] {
        let result = TestHarness::new().run(&app_with_a_broken_theme(), fixture.command(), args);
        result.assert_error();
        result.assert_error_kind(RunErrorKind::Render);
        result.assert_exit_status(ExitStatus::FAILURE);
        result.assert_error_contains("failed to render help");
    }
}
#[test]
#[serial]
fn a_render_failure_is_not_disguised_as_an_unrecognized_topic() {
    let fixture = downstream().build();
    let result = TestHarness::new().run(
        &app_with_a_broken_theme(),
        fixture.command(),
        ["lookma", "help", "review"],
    );
    result.assert_error();
    result.assert_error_kind(RunErrorKind::Render);
    result.assert_error_contains("failed to render help");
    assert!(
        !result
            .error()
            .unwrap_or_default()
            .contains("wasn't recognized"),
        "a broken theme is not an unknown topic: {:?}",
        result.error()
    );
}
#[test]
#[serial]
fn both_entry_points_render_the_same_help() {
    let fixture = downstream().flat().build();
    for args in [
        ["lookma", "help", "--output", "text"],
        ["lookma", "--help", "--output", "text"],
    ] {
        let dispatched = TestHarness::new().run(fixture.app(), fixture.command(), args);
        dispatched.assert_success();
        let configured = configured_help(fixture.app(), fixture.command(), &args);
        assert_eq!(
            dispatched.stdout(),
            configured,
            "entry points disagree for {args:?}"
        );
    }
}
#[test]
#[serial]
fn a_flat_command_keeps_the_word_as_data_through_run_without_the_opt_in() {
    let fixture = downstream().flat().without_help_word().build();
    let result =
        TestHarness::new()
            .text_output()
            .run(fixture.app(), fixture.command(), ["lookma", "help"]);
    result.assert_success();
    result.assert_stdout_eq("range=help");
}
#[test]
#[serial]
fn the_escape_delivers_the_literal_word_through_run() {
    let fixture = downstream().flat().build();
    let result = TestHarness::new().run(fixture.app(), fixture.command(), ["lookma", "--", "help"]);
    result.assert_success();
    result.assert_stdout_eq("range=help");
}
#[test]
#[serial]
fn a_normal_invocation_is_untouched_through_run() {
    let fixture = downstream().flat().build();
    let result = TestHarness::new().text_output().run(
        fixture.app(),
        fixture.command(),
        ["lookma", "main..HEAD"],
    );
    result.assert_success();
    result.assert_stdout_eq("range=main..HEAD");
}