about summary refs log tree commit diff
path: root/src/tools/clippy/tests/config-metadata.rs
blob: af9fe064dc708ae8351f6f01f7c84fbbd7b86578 (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
#![feature(rustc_private)]

use clippy_config::{ClippyConfiguration, get_configuration_metadata};
use itertools::Itertools;
use regex::Regex;
use std::borrow::Cow;
use std::{env, fs};

fn metadata() -> impl Iterator<Item = ClippyConfiguration> {
    get_configuration_metadata()
        .into_iter()
        .filter(|config| config.deprecation_reason.is_none())
        .filter(|config| !config.lints.is_empty())
}

#[test]
fn book() {
    let path = "book/src/lint_configuration.md";
    let current = fs::read_to_string(path).unwrap();

    let configs = metadata().map(|conf| conf.to_markdown_paragraph()).join("\n");
    let expected = format!(
        r"<!--
This file is generated by `cargo bless --test config-metadata`.
Please use that command to update the file and do not edit it by hand.
-->

# Lint Configuration Options

The following list shows each configuration option, along with a description, its default value, an example
and lints affected.

---

{}
",
        configs.trim(),
    );

    if current != expected {
        if env::var_os("RUSTC_BLESS").is_some_and(|v| v != "0") {
            fs::write(path, expected).unwrap();
        } else {
            panic!("`{path}` is out of date, run `cargo bless --test config-metadata` to update it");
        }
    }
}

#[test]
fn changelog() {
    let path = "CHANGELOG.md";
    let current = fs::read_to_string(path).unwrap();

    let configs = metadata().map(|conf| conf.to_markdown_link()).join("\n");

    let re = Regex::new(
        "(?s)\
        (<!-- begin autogenerated links to configuration documentation -->)\
        .*\
        (<!-- end autogenerated links to configuration documentation -->)\
        ",
    )
    .unwrap();
    let expected = re.replace(&current, format!("$1\n{configs}\n$2"));

    assert!(
        matches!(expected, Cow::Owned(_)),
        "failed to find configuration section in `{path}`"
    );

    if current != expected {
        if env::var_os("RUSTC_BLESS").is_some_and(|v| v != "0") {
            fs::write(path, expected.as_bytes()).unwrap();
        } else {
            panic!("`{path}` is out of date, run `cargo bless --test config-metadata` to update it");
        }
    }
}