summary refs log tree commit diff
path: root/tests/run-make/unstable-feature-usage-metrics/rmake.rs
blob: 1397548a6fcedb4c9ba80a935e4b7092a368534c (plain)
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
//! This test checks if unstable feature usage metric dump files `unstable-feature-usage*.json` work
//! as expected.
//!
//! - Basic sanity checks on a default ICE dump.
//!
//! See <https://github.com/rust-lang/rust/issues/129485>.
//!
//! # Test history
//!
//! - forked from dump-ice-to-disk test, which has flakeyness issues on i686-mingw, I'm assuming
//! those will be present in this test as well on the same platform

//@ ignore-windows
//FIXME(#128911): still flakey on i686-mingw.

use std::path::{Path, PathBuf};

use run_make_support::rfs::create_dir_all;
use run_make_support::{
    cwd, filename_contains, has_extension, rfs, run_in_tmpdir, rustc, serde_json,
    shallow_find_files,
};

fn find_feature_usage_metrics<P: AsRef<Path>>(dir: P) -> Vec<PathBuf> {
    shallow_find_files(dir, |path| {
        if filename_contains(path, "unstable_feature_usage") && has_extension(path, "json") {
            true
        } else {
            dbg!(path);
            false
        }
    })
}

fn main() {
    test_metrics_dump();
    test_metrics_errors();
}

#[track_caller]
fn test_metrics_dump() {
    run_in_tmpdir(|| {
        let metrics_dir = cwd().join("metrics");
        create_dir_all(&metrics_dir);
        rustc()
            .input("lib.rs")
            .env("RUST_BACKTRACE", "short")
            .arg(format!("-Zmetrics-dir={}", metrics_dir.display()))
            .run();
        let mut metrics = find_feature_usage_metrics(&metrics_dir);
        let json_path =
            metrics.pop().expect("there should be one metrics file in the output directory");

        // After the `pop` above, there should be no files left.
        assert!(
            metrics.is_empty(),
            "there should be no more than one metrics file in the output directory"
        );

        let message = rfs::read_to_string(json_path);
        let parsed: serde_json::Value =
            serde_json::from_str(&message).expect("metrics should be dumped as json");
        let expected = serde_json::json!(
            {
                "lib_features":[{"symbol":"ascii_char"}],
                "lang_features":[{"symbol":"box_patterns","since":null}]
            }
        );

        assert_eq!(expected, parsed);
    });
}

#[track_caller]
fn test_metrics_errors() {
    run_in_tmpdir(|| {
        rustc()
            .input("lib.rs")
            .env("RUST_BACKTRACE", "short")
            .arg("-Zmetrics-dir=invaliddirectorythatdefinitelydoesntexist")
            .run_fail()
            .assert_stderr_contains(
                "error: cannot dump feature usage metrics: No such file or directory",
            )
            .assert_stdout_not_contains("internal compiler error");
    });
}