summaryrefslogtreecommitdiffstats
path: root/src/tools/cargo/crates/rustfix/tests/parse_and_replace.rs
blob: 902275b64e5663d3d109d7f88a89d3db4e750837 (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
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
#![allow(clippy::disallowed_methods, clippy::print_stdout, clippy::print_stderr)]

use anyhow::{anyhow, ensure, Context, Error};
use rustfix::apply_suggestions;
use std::collections::HashSet;
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use tempfile::tempdir;
use tracing::{debug, info, warn};

mod fixmode {
    pub const EVERYTHING: &str = "yolo";
}

mod settings {
    // can be set as env var to debug
    pub const CHECK_JSON: &str = "RUSTFIX_TEST_CHECK_JSON";
    pub const RECORD_JSON: &str = "RUSTFIX_TEST_RECORD_JSON";
    pub const RECORD_FIXED_RUST: &str = "RUSTFIX_TEST_RECORD_FIXED_RUST";
}

fn compile(file: &Path) -> Result<Output, Error> {
    let tmp = tempdir()?;

    let args: Vec<OsString> = vec![
        file.into(),
        "--error-format=json".into(),
        "--emit=metadata".into(),
        "--crate-name=rustfix_test".into(),
        "--out-dir".into(),
        tmp.path().into(),
    ];

    let res = Command::new(env::var_os("RUSTC").unwrap_or("rustc".into()))
        .args(&args)
        .env("CLIPPY_DISABLE_DOCS_LINKS", "true")
        .env_remove("RUST_LOG")
        .output()?;

    Ok(res)
}

fn compile_and_get_json_errors(file: &Path) -> Result<String, Error> {
    let res = compile(file)?;
    let stderr = String::from_utf8(res.stderr)?;
    if stderr.contains("is only accepted on the nightly compiler") {
        panic!("rustfix tests require a nightly compiler");
    }

    match res.status.code() {
        Some(0) | Some(1) | Some(101) => Ok(stderr),
        _ => Err(anyhow!(
            "failed with status {:?}: {}",
            res.status.code(),
            stderr
        )),
    }
}

fn compiles_without_errors(file: &Path) -> Result<(), Error> {
    let res = compile(file)?;

    match res.status.code() {
        Some(0) => Ok(()),
        _ => {
            info!(
                "file {:?} failed to compile:\n{}",
                file,
                String::from_utf8(res.stderr)?
            );
            Err(anyhow!(
                "failed with status {:?} (`env RUST_LOG=parse_and_replace=info` for more info)",
                res.status.code(),
            ))
        }
    }
}

fn read_file(path: &Path) -> Result<String, Error> {
    use std::io::Read;

    let mut buffer = String::new();
    let mut file = fs::File::open(path)?;
    file.read_to_string(&mut buffer)?;
    Ok(buffer)
}

fn diff(expected: &str, actual: &str) -> String {
    use similar::{ChangeTag, TextDiff};
    use std::fmt::Write;

    let mut res = String::new();
    let diff = TextDiff::from_lines(expected.trim(), actual.trim());

    let mut different = false;
    for op in diff.ops() {
        for change in diff.iter_changes(op) {
            let prefix = match change.tag() {
                ChangeTag::Equal => continue,
                ChangeTag::Insert => "+",
                ChangeTag::Delete => "-",
            };
            if !different {
                write!(
                    &mut res,
                    "differences found (+ == actual, - == expected):\n"
                )
                .unwrap();
                different = true;
            }
            write!(&mut res, "{} {}", prefix, change.value()).unwrap();
        }
    }
    if different {
        write!(&mut res, "").unwrap();
    }

    res
}

fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Error> {
    let file: &Path = file.as_ref();
    let json_file = file.with_extension("json");
    let fixed_file = file.with_extension("fixed.rs");

    let filter_suggestions = if mode == fixmode::EVERYTHING {
        rustfix::Filter::Everything
    } else {
        rustfix::Filter::MachineApplicableOnly
    };

    debug!("next up: {:?}", file);
    let code = read_file(file).context(format!("could not read {}", file.display()))?;
    let errors =
        compile_and_get_json_errors(file).context(format!("could compile {}", file.display()))?;
    let suggestions =
        rustfix::get_suggestions_from_json(&errors, &HashSet::new(), filter_suggestions)
            .context("could not load suggestions")?;

    if std::env::var(settings::RECORD_JSON).is_ok() {
        use std::io::Write;
        let mut recorded_json = fs::File::create(&file.with_extension("recorded.json")).context(
            format!("could not create recorded.json for {}", file.display()),
        )?;
        recorded_json.write_all(errors.as_bytes())?;
    }

    if std::env::var(settings::CHECK_JSON).is_ok() {
        let expected_json = read_file(&json_file).context(format!(
            "could not load json fixtures for {}",
            file.display()
        ))?;
        let expected_suggestions =
            rustfix::get_suggestions_from_json(&expected_json, &HashSet::new(), filter_suggestions)
                .context("could not load expected suggestions")?;

        ensure!(
            expected_suggestions == suggestions,
            "got unexpected suggestions from clippy:\n{}",
            diff(
                &format!("{:?}", expected_suggestions),
                &format!("{:?}", suggestions)
            )
        );
    }

    let fixed = apply_suggestions(&code, &suggestions)
        .context(format!("could not apply suggestions to {}", file.display()))?;

    if std::env::var(settings::RECORD_FIXED_RUST).is_ok() {
        use std::io::Write;
        let mut recorded_rust = fs::File::create(&file.with_extension("recorded.rs"))?;
        recorded_rust.write_all(fixed.as_bytes())?;
    }

    let expected_fixed =
        read_file(&fixed_file).context(format!("could read fixed file for {}", file.display()))?;
    ensure!(
        fixed.trim() == expected_fixed.trim(),
        "file {} doesn't look fixed:\n{}",
        file.display(),
        diff(fixed.trim(), expected_fixed.trim())
    );

    compiles_without_errors(&fixed_file)?;

    Ok(())
}

fn get_fixture_files(p: &str) -> Result<Vec<PathBuf>, Error> {
    Ok(fs::read_dir(&p)?
        .into_iter()
        .map(|e| e.unwrap().path())
        .filter(|p| p.is_file())
        .filter(|p| {
            let x = p.to_string_lossy();
            x.ends_with(".rs") && !x.ends_with(".fixed.rs") && !x.ends_with(".recorded.rs")
        })
        .collect())
}

fn assert_fixtures(dir: &str, mode: &str) {
    let files = get_fixture_files(&dir)
        .context(format!("couldn't load dir `{}`", dir))
        .unwrap();
    let mut failures = 0;

    for file in &files {
        if let Err(err) = test_rustfix_with_file(file, mode) {
            println!("failed: {}", file.display());
            warn!("{:?}", err);
            failures += 1;
        }
        info!("passed: {:?}", file);
    }

    if failures > 0 {
        panic!(
            "{} out of {} fixture asserts failed\n\
             (run with `env RUST_LOG=parse_and_replace=info` to get more details)",
            failures,
            files.len(),
        );
    }
}

#[test]
fn everything() {
    tracing_subscriber::fmt::init();
    assert_fixtures("./tests/everything", fixmode::EVERYTHING);
}