panicgraph 0.2.1

Reports which functions can panic, why, and through what call path.
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! The standalone flame graph.

#![cfg(feature = "svg")]

mod support;

use panicgraph::{
    Body, Category, CategorySet, FuncKey,
    args::{Closures, Generics},
    palette::{Palette, Theme},
    select::Selection,
    solve::Edges,
    svg::{self, View},
    verify::Verdicts,
};

use crate::support::{BodyBuilder, graph};

/// A function that panics once, under the given name.
fn body(name: &str, category: Category) -> Body {
    BodyBuilder::new(name).panics(category).build()
}

/// A view of everything, folded, in the light theme.
fn view() -> View {
    View {
        suppressed: CategorySet::EMPTY,
        selection: Selection::default(),
        edges: Edges::default(),
        fold: true,
        theme: Theme::Light,
    }
}

/// Renders a graph of the given functions.
fn render(bodies: Vec<Body>) -> String {
    render_view(bodies, view())
}

/// Renders a graph of the given functions, seen the given way.
fn render_view(bodies: Vec<Body>, view: View) -> String {
    render_checked(bodies, view, None)
}

/// Renders a graph of the given functions, seen the given way, with the
/// artifact's verdicts when there are any.
fn render_checked(
    bodies: Vec<Body>,
    view: View,
    verdicts: Option<&Verdicts>,
) -> String {
    let mut out = String::new();
    svg::render(&graph(bodies), view, verdicts, &mut out)
        .expect("the graph should render");
    out
}

/// A view that shows the given selection.
fn selecting(selection: Selection) -> View {
    View {
        selection,
        fold: false,
        ..view()
    }
}

/// Removes the embedded styling and script, whose contents are not markup
/// and would otherwise be read as tags.
fn markup_only(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut rest = text;
    while let Some(start) =
        rest.find("<style>").or_else(|| rest.find("<script"))
    {
        out.push_str(&rest[..start]);
        let after = &rest[start..];
        let end = after
            .find("</style>")
            .map(|i| i + "</style>".len())
            .or_else(|| after.find("</script>").map(|i| i + "</script>".len()));
        match end {
            Some(i) => rest = &after[i..],
            None => return out,
        }
    }
    out.push_str(rest);
    out
}

/// Rejects anything that is not well formed, so a broken document fails here
/// rather than in whatever opens it.
fn parse(text: &str) {
    let markup = markup_only(text);
    let mut depth = 0i32;
    let mut chars = markup.chars().peekable();
    let mut in_tag = false;
    while let Some(c) = chars.next() {
        match c {
            '<' => match chars.peek() {
                Some('/') => {
                    in_tag = true;
                    depth -= 1;
                }
                Some('?' | '!') => in_tag = false,
                _ => {
                    in_tag = true;
                    depth += 1;
                }
            },
            '/' if in_tag && chars.peek() == Some(&'>') => depth -= 1,
            '>' => in_tag = false,
            _ => {}
        }
    }
    assert_eq!(depth, 0, "every element should be closed");
}

/// Every value the named attribute takes anywhere in the text.
fn values_of<'a>(text: &'a str, name: &str) -> Vec<&'a str> {
    let key = format!(" {name}=\"");
    let mut values = Vec::new();
    let mut rest = text;
    while let Some(start) = rest.find(&key) {
        let value = &rest[start + key.len()..];
        let end = value.find('"').expect("an attribute value ends");
        values.push(&value[..end]);
        rest = &value[end..];
    }
    values
}

/// The lines of the frame with the given name: its group, its title, its
/// rect and its label.
fn frame<'a>(out: &'a str, name: &str) -> Vec<&'a str> {
    let lines: Vec<&str> = out.lines().collect();
    let key = format!("data-name=\"{name}\"");
    let at = lines
        .iter()
        .position(|line| {
            line.starts_with("<g class=\"f\"") && line.contains(&key)
        })
        .unwrap_or_else(|| panic!("no frame is named {name}"));
    lines[at..at + 4].to_vec()
}

/// Where the named frame starts, in the units the file is laid out in.
fn frame_x(out: &str, name: &str) -> f64 {
    values_of(frame(out, name)[0], "data-x")[0]
        .parse()
        .expect("data-x is a number")
}

/// The fill of the named frame.
fn frame_fill<'a>(out: &'a str, name: &str) -> &'a str {
    values_of(frame(out, name)[2], "fill")[0]
}

#[test]
fn the_document_stands_alone() {
    let out = render(vec![body("parse", Category::Unwrap)]);
    assert!(out.starts_with("<?xml"));
    assert!(out.trim_end().ends_with("</svg>"));
    assert!(out.contains("<style>"), "styling travels with the file");
    assert!(
        out.contains("<script"),
        "so does the script that explores it"
    );
    assert!(
        !out.contains("http://127.0.0.1"),
        "nothing may point at a server that will not be running"
    );
    parse(&out);
}

#[test]
fn a_rust_path_does_not_break_the_document() {
    // Generic arguments and trait qualification put angle brackets and
    // ampersands straight into a name.
    let name = "<Vec<T> as Index<&'a str>>::index & \"more\"";
    let out = render(vec![body(name, Category::Index)]);
    assert!(
        !out.contains("<Vec<T>"),
        "the raw brackets would have closed the tag"
    );
    assert!(out.contains("&lt;Vec&lt;T&gt;"));
    assert!(out.contains("&amp;"));
    assert!(out.contains("&quot;"));
    parse(&out);
}

#[test]
fn every_frame_carries_a_title_so_it_reads_without_script() {
    let out = render(vec![
        body("parse", Category::Unwrap),
        body("read", Category::Index),
    ]);
    let frames = out.matches("<g class=\"f\"").count();
    let titles = out.matches("<title>").count();
    assert!(frames > 0);
    // The mark carries the one title that is not a frame's.
    assert_eq!(
        frames + 1,
        titles,
        "a frame with no title is mute when hovered"
    );
}

#[test]
fn a_panic_is_named_not_only_coloured() {
    let out = render(vec![body("parse", Category::Unwrap)]);
    assert!(
        out.contains("unwrap panic"),
        "the category belongs in the text, not in the fill alone"
    );
}

#[test]
fn an_empty_graph_still_renders() {
    let out = render(Vec::new());
    assert!(out.contains("</svg>"));
    parse(&out);
}

#[test]
fn the_picture_carries_the_controls_it_describes() {
    let out = render(vec![body("parse", Category::Unwrap)]);
    for id in [
        "title", "subtitle", "unzoom", "search", "matched", "detail", "note",
    ] {
        assert!(
            out.contains(&format!("id=\"{id}\"")),
            "the {id} element is what the script reads and writes"
        );
    }
    assert!(
        out.contains("id=\"frames\""),
        "the frames travel in one group, which is what the script walks"
    );
    for rule in [".hide", ".parent rect", ".match rect", ".ctl"] {
        assert!(
            out.contains(rule),
            "the {rule} rule is what a zoom or a search turns on"
        );
    }
    parse(&out);
}

#[test]
fn the_assumptions_are_written_on_the_picture() {
    let bare = render(vec![body("parse", Category::Unwrap)]);
    assert!(
        bare.contains("assuming nothing impossible"),
        "a graph drawn under no assumption has to say so"
    );

    let assumed = render_view(
        vec![body("parse", Category::Unwrap)],
        View {
            suppressed: CategorySet::oom(),
            ..view()
        },
    );
    assert!(
        assumed.contains("assuming impossible:"),
        "a graph is only readable beside the policy it was drawn under"
    );
    assert!(assumed.contains("alloc-failure"));
    parse(&assumed);
}

#[test]
fn a_selection_narrows_what_is_drawn_and_is_written_on_the_picture() {
    let bodies = || {
        vec![
            body("parse", Category::Unwrap),
            body("read", Category::Index),
        ]
    };
    let all = render(bodies());
    assert!(all.contains("unwrap panic") && all.contains("index panic"));

    let some = render_view(
        bodies(),
        selecting(Selection {
            only: Some(CategorySet::single(Category::Unwrap)),
            ..Selection::default()
        }),
    );
    assert!(some.contains("unwrap panic"), "the selection is drawn");
    assert!(!some.contains("index panic"), "the rest is not");
    assert!(
        some.contains("showing only: unwrap"),
        "the picture says what it was narrowed to"
    );
    parse(&some);
}

#[test]
fn a_frame_carries_what_the_script_zooms_and_searches_with() {
    let out = render(vec![
        body("parse", Category::Unwrap),
        body("read", Category::Index),
    ]);
    let frames = out.matches("<g class=\"f\"").count();
    assert!(frames > 0);
    for attribute in ["data-x=", "data-w=", "data-y=", "data-name="] {
        assert_eq!(
            out.matches(attribute).count(),
            frames,
            "every frame needs {attribute} for the script to place it again"
        );
    }
}

#[test]
fn the_picture_is_as_wide_as_the_window() {
    let out = render(vec![
        body("parse", Category::Unwrap),
        body("read", Category::Index),
    ]);
    assert!(
        out.contains("<svg version=\"1.1\" width=\"100%\""),
        "the drawing takes the width of whatever holds it"
    );
    assert!(
        out.contains("viewBox=\"0 0 1200 "),
        "and names the width it was fitted for, which a viewer without \
         scripting scales as a whole"
    );
    assert!(
        out.contains("<svg id=\"frames\" x=\"10\" width=\"1180\">"),
        "the frames keep the margin flame graphs keep at either side"
    );

    // Every frame is placed as a share of the width, never at a pixel, so
    // it stretches with the window, and every frame has a label element to
    // write its name into once it has the room.
    let frames = out.matches("<g class=\"f\"").count();
    assert!(frames > 0);
    let rects: Vec<&str> = out
        .lines()
        .filter(|line| line.starts_with("<rect x="))
        .collect();
    let labels: Vec<&str> = out
        .lines()
        .filter(|line| {
            line.starts_with("<text x=") && line.contains("class=\"l\"")
        })
        .collect();
    assert_eq!(rects.len(), frames, "one rect per frame");
    assert_eq!(labels.len(), frames, "one label per frame");
    for tag in rects.iter().chain(&labels) {
        for value in values_of(tag, "x") {
            assert!(value.ends_with('%'), "{tag} is placed at a pixel");
        }
    }
    for tag in &rects {
        for value in values_of(tag, "width") {
            assert!(value.ends_with('%'), "{tag} is sized in pixels");
        }
    }
}

#[test]
fn the_picture_is_signed_with_the_project_mark() {
    let out = render(vec![body("parse", Category::Unwrap)]);
    let lockup = include_str!("../assets/panicgraph-lockup.svg");
    // The mark is the one the interactive view shows, stroke for stroke.
    let strokes = values_of(lockup, "d");
    assert!(!strokes.is_empty(), "the lockup draws its mark with paths");
    for stroke in strokes {
        assert!(
            out.contains(&format!("d=\"{stroke}\"")),
            "the stroke {stroke} is missing from the mark"
        );
    }
    assert!(out.contains(">PanicGraph<"), "the name goes with the mark");
    assert!(
        out.contains(&format!("href=\"{}\"", env!("CARGO_PKG_REPOSITORY"))),
        "the mark links to the project"
    );
    assert!(
        out.contains(&format!("PanicGraph {}", env!("CARGO_PKG_VERSION"))),
        "the tooltip names the version that drew the file"
    );
    parse(&out);
}

#[test]
fn the_dark_theme_draws_on_a_dark_page_in_its_own_ink() {
    let out = render_view(
        vec![body("parse", Category::Unwrap)],
        View {
            theme: Theme::Dark,
            ..view()
        },
    );
    let colours = Palette::load(Theme::Dark).expect("the palette loads");
    let colours = colours.colours();
    assert!(
        out.contains(&format!(
            "<rect width=\"100%\" height=\"100%\" fill=\"{}\"/>",
            colours.page
        )),
        "the page takes the theme's colour"
    );
    assert!(
        out.contains(&format!("stroke=\"{}\"", colours.brand)),
        "the mark takes the theme's ink"
    );
    assert!(
        out.contains(&format!("fill: {};", colours.ink)),
        "so does the title"
    );
    parse(&out);
}

#[test]
fn each_category_takes_a_step_of_its_own() {
    let out = render_view(
        vec![
            body("parse", Category::Unwrap),
            body("read", Category::Index),
        ],
        View {
            fold: false,
            ..view()
        },
    );
    let palette = Palette::load(Theme::Light).expect("the palette loads");
    assert_eq!(frame_fill(&out, "unwrap"), palette.panic(Category::Unwrap));
    assert_eq!(frame_fill(&out, "index"), palette.panic(Category::Index));
    assert_ne!(
        frame_fill(&out, "unwrap"),
        frame_fill(&out, "index"),
        "two panics of one family are still told apart"
    );
}

#[test]
fn siblings_band_by_family_and_a_call_takes_the_tint_of_what_is_under_it() {
    // Named so that the alphabet would order them differently from the
    // families, which is what decides.
    let out = render_view(
        vec![
            body("a_alloc", Category::CapacityOverflow),
            body("b_logic", Category::Unwrap),
            body("c_unsure", Category::Unknown),
        ],
        View {
            fold: false,
            ..view()
        },
    );
    assert!(
        frame_x(&out, "b_logic") < frame_x(&out, "a_alloc"),
        "logic leads"
    );
    assert!(
        frame_x(&out, "a_alloc") < frame_x(&out, "c_unsure"),
        "allocation comes before what is unverified"
    );

    let palette = Palette::load(Theme::Light).expect("the palette loads");
    let calls = &palette.colours().call;
    assert_eq!(frame_fill(&out, "b_logic"), calls.logic);
    assert_eq!(frame_fill(&out, "a_alloc"), calls.alloc);
    assert_eq!(frame_fill(&out, "c_unsure"), calls.unsure);
    assert_eq!(
        frame_fill(&out, "crate"),
        calls.logic,
        "a tie between families goes to the one listed first"
    );
}

#[test]
fn dependencies_are_drawn_only_when_asked_for() {
    let bodies = || {
        let mut dependency = body("serde::parse", Category::Unwrap);
        dependency.local = false;
        vec![body("parse", Category::Index), dependency]
    };
    let local = render_view(bodies(), selecting(Selection::default()));
    assert!(
        !local.contains("data-name=\"serde\""),
        "a dependency stays out by default"
    );
    let all = render_view(
        bodies(),
        selecting(Selection {
            all_crates: true,
            ..Selection::default()
        }),
    );
    assert!(
        all.contains("data-name=\"serde\""),
        "and comes in when asked"
    );
    assert!(
        all.contains("dependencies included"),
        "which the picture says under its title"
    );
    parse(&all);
}

#[test]
fn a_closure_folds_into_its_parent_when_asked() {
    let bodies = || {
        vec![
            BodyBuilder::new("run").calls("run::{closure#0}").build(),
            body("run::{closure#0}", Category::Unwrap),
        ]
    };
    let separate = render_view(bodies(), selecting(Selection::default()));
    assert!(
        separate.contains("data-name=\"run::{closure#0}\""),
        "a closure is a function of its own by default"
    );
    let folded = render_view(
        bodies(),
        selecting(Selection {
            closures: Closures::Parent,
            ..Selection::default()
        }),
    );
    assert!(
        !folded.contains("{closure"),
        "folded, the closure's name is gone"
    );
    assert_eq!(
        folded.matches("data-name=\"run\"").count(),
        1,
        "the closure's panic joins the one frame its parent has"
    );
    assert!(
        folded.contains("unwrap panic"),
        "and the panic is still drawn"
    );
    assert!(folded.contains("closures folded into their parents"));
    parse(&folded);
}

#[test]
fn a_generic_function_shows_its_instantiations_when_asked() {
    let bodies = || {
        let mut written = body("gen", Category::Unwrap);
        written.key = FuncKey("generic:gen".to_owned());
        let mut instance = body("gen", Category::Index);
        instance.key = FuncKey("gen<u8>".to_owned());
        vec![written, instance]
    };
    let written = render_view(bodies(), selecting(Selection::default()));
    assert!(
        written.contains("unwrap panic") && written.contains("index panic"),
        "as written, the body and its instantiation both show"
    );
    let instantiated = render_view(
        bodies(),
        selecting(Selection {
            generics: Generics::Instantiated,
            ..Selection::default()
        }),
    );
    assert!(instantiated.contains("index panic"));
    assert!(
        !instantiated.contains("unwrap panic"),
        "instantiated, the written body yields"
    );
    assert!(instantiated.contains("generic functions as instantiated"));
    parse(&instantiated);
}

#[test]
fn a_test_target_adds_instantiations_but_not_itself() {
    let mut test = body("tests::it_works", Category::Unwrap);
    test.from_tests = true;
    let mut written = body("gen", Category::Unwrap);
    written.key = FuncKey("generic:gen".to_owned());
    let mut instance = body("gen", Category::Index);
    instance.key = FuncKey("gen<u8>".to_owned());
    instance.from_tests = true;
    let out = render_view(
        vec![test, written, instance],
        selecting(Selection::default()),
    );
    assert!(!out.contains("it_works"), "a test is not drawn");
    assert!(
        out.contains("index panic"),
        "an instantiation a test makes of the crate's own function is"
    );
}

#[test]
fn verdicts_are_written_on_the_panics_when_the_artifact_was_checked() {
    let bodies = || vec![body("parse", Category::Unwrap)];
    let unchecked = render_view(bodies(), view());
    assert!(!unchecked.contains("compiled artifact"));
    // An empty sweep settles nothing, which is a verdict of its own.
    let checked = render_checked(bodies(), view(), Some(&Verdicts::default()));
    assert!(
        checked.contains(
            "unwrap panic, 1 reachable, 100.0%, unverified in \
                          the compiled artifact"
        ),
        "the verdict goes on the panic, in the report's words"
    );
    parse(&checked);
}

#[test]
fn the_calls_followed_are_written_under_the_title() {
    let plain = render(vec![body("f", Category::Index)]);
    assert!(
        !plain.contains("left out") && !plain.contains("candidate targets"),
        "the default edges say nothing"
    );
    let narrowed = render_view(
        vec![body("f", Category::Index)],
        View {
            edges: Edges {
                follow_inexact: false,
                candidates: false,
            },
            ..view()
        },
    );
    assert!(narrowed.contains("calls through objects and pointers left out"));
    let widened = render_view(
        vec![body("f", Category::Index)],
        View {
            edges: Edges {
                follow_inexact: true,
                candidates: true,
            },
            ..view()
        },
    );
    assert!(widened.contains("candidate targets of those calls followed"));
    parse(&widened);
}