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
use regex::bytes::Regex;
#[cfg(feature = "rustc")]
use spanned::Spanned;

#[cfg(feature = "rustc")]
use crate::{
    aux_builds::AuxBuilder, custom_flags::run::Run, custom_flags::rustfix::RustfixMode,
    custom_flags::Flag, filter::Match, rustc_stderr,
};
use crate::{
    diagnostics::Diagnostics,
    parser::CommandParserFunc,
    per_test_config::{Comments, Condition},
    CommandBuilder,
};
pub use color_eyre;
use color_eyre::eyre::Result;
use std::{
    collections::BTreeMap,
    num::NonZeroUsize,
    path::{Path, PathBuf},
    sync::{atomic::AtomicBool, Arc},
};

mod args;
pub use args::{Args, Format};

#[derive(Debug, Clone)]
/// Central datastructure containing all information to run the tests.
pub struct Config {
    /// Host triple; usually will be auto-detected.
    pub host: Option<String>,
    /// `None` to run on the host, otherwise a target triple
    pub target: Option<String>,
    /// The folder in which to start searching for .rs files
    pub root_dir: PathBuf,
    /// The binary to actually execute.
    pub program: CommandBuilder,
    /// What to do in case the stdout/stderr output differs from the expected one.
    pub output_conflict_handling: OutputConflictHandling,
    /// The recommended command to bless failing tests.
    pub bless_command: Option<String>,
    /// Where to dump files like the binaries compiled from tests.
    /// Defaults to `target/ui` in the current directory.
    pub out_dir: PathBuf,
    /// Skip test files whose names contain any of these entries.
    pub skip_files: Vec<String>,
    /// Only test files whose names contain any of these entries.
    pub filter_files: Vec<String>,
    /// Override the number of threads to use.
    pub threads: Option<NonZeroUsize>,
    /// Nextest emulation: only list the test itself, not its components.
    pub list: bool,
    /// Only run the tests that are ignored.
    pub run_only_ignored: bool,
    /// Filters must match exactly instead of just checking for substrings.
    pub filter_exact: bool,
    /// The default settings settable via `@` comments
    pub comment_defaults: Comments,
    /// The symbol(s) that signify the start of a comment.
    pub comment_start: &'static str,
    /// Custom comment parsers
    pub custom_comments: BTreeMap<&'static str, CommandParserFunc>,
    /// Custom diagnostic extractor (invoked on the output of tests)
    pub diagnostic_extractor: fn(&Path, &[u8]) -> Diagnostics,
    /// An atomic bool that can be set to `true` to abort all tests.
    /// Will not cancel child processes, but if set from a Ctrl+C handler,
    /// the pressing of Ctrl+C will already have cancelled child processes.
    pub abort_check: Arc<AtomicBool>,
}

impl Config {
    /// Create a configuration for testing the output of running
    /// `rustc` on the test files.
    #[cfg(feature = "rustc")]
    pub fn rustc(root_dir: impl Into<PathBuf>) -> Self {
        use crate::custom_flags::edition::Edition;

        let mut comment_defaults = Comments::default();

        #[derive(Debug)]
        struct NeedsAsmSupport;

        impl Flag for NeedsAsmSupport {
            fn must_be_unique(&self) -> bool {
                true
            }
            fn clone_inner(&self) -> Box<dyn Flag> {
                Box::new(NeedsAsmSupport)
            }
            fn test_condition(&self, config: &Config) -> bool {
                let target = config.target.as_ref().unwrap();
                static ASM_SUPPORTED_ARCHS: &[&str] = &[
                    "x86", "x86_64", "arm", "aarch64", "riscv32",
                    "riscv64",
                    // These targets require an additional asm_experimental_arch feature.
                    // "nvptx64", "hexagon", "mips", "mips64", "spirv", "wasm32",
                ];
                !ASM_SUPPORTED_ARCHS.iter().any(|arch| target.contains(arch))
            }
        }

        comment_defaults
            .base()
            .add_custom("edition", Edition("2021".into()));
        comment_defaults
            .base()
            .add_custom("rustfix", RustfixMode::MachineApplicable);
        let filters = vec![
            (Match::PathBackslash, b"/".to_vec()),
            #[cfg(windows)]
            (Match::Exact(vec![b'\r']), b"".to_vec()),
            #[cfg(windows)]
            (Match::Exact(br"\\?\".to_vec()), b"".to_vec()),
        ];
        comment_defaults
            .base()
            .normalize_stderr
            .clone_from(&filters);
        comment_defaults.base().normalize_stdout = filters;
        comment_defaults.base().exit_status = Spanned::dummy(1).into();
        comment_defaults.base().require_annotations = Spanned::dummy(true).into();
        let mut config = Self {
            host: None,
            target: None,
            root_dir: root_dir.into(),
            program: CommandBuilder::rustc(),
            output_conflict_handling: OutputConflictHandling::Error,
            bless_command: None,
            out_dir: std::env::var_os("CARGO_TARGET_DIR")
                .map(PathBuf::from)
                .unwrap_or_else(|| std::env::current_dir().unwrap().join("target"))
                .join("ui"),
            skip_files: Vec::new(),
            filter_files: Vec::new(),
            threads: None,
            list: false,
            run_only_ignored: false,
            filter_exact: false,
            comment_defaults,
            comment_start: "//",
            custom_comments: Default::default(),
            diagnostic_extractor: rustc_stderr::process,
            abort_check: Default::default(),
        };
        config
            .custom_comments
            .insert("no-rustfix", |parser, _args, span| {
                // args are ignored (can be used as comment)
                parser.set_custom_once("no-rustfix", (), span);
            });

        config
            .custom_comments
            .insert("edition", |parser, args, _span| {
                parser.set_custom_once("edition", Edition((*args).into()), args.span());
            });

        config
            .custom_comments
            .insert("needs-asm-support", |parser, _args, span| {
                // args are ignored (can be used as comment)
                parser.set_custom_once("needs-asm-support", NeedsAsmSupport, span);
            });

        config.custom_comments.insert("run", |parser, args, span| {
            let set = |exit_code| {
                parser.set_custom_once("run", Run { exit_code }, args.span());
                parser.exit_status = Spanned::new(0, span.clone()).into();
                parser.require_annotations = Spanned::new(false, span.clone()).into();

                let prev = parser
                    .custom
                    .insert("no-rustfix", Spanned::new(vec![Box::new(())], span.clone()));
                parser.check(span, prev.is_none(), "`run` implies `no-rustfix`");
            };
            if args.is_empty() {
                set(0);
            } else {
                match args.content.parse() {
                    Ok(exit_code) => {
                        set(exit_code);
                    }
                    Err(err) => parser.error(args.span(), err.to_string()),
                }
            }
        });
        config.custom_comments.insert("aux-build", |parser, args, span| {
            let name = match args.split_once(":") {
                Some((name, rest)) => {
                    parser.error(rest.span(), "proc macros are now auto-detected, you can remove the `:proc-macro` after the file name");
                    name
                },
                None => args,
            };

            parser
                .add_custom_spanned("aux-build", AuxBuilder { aux_file: name.map(|n| n.into())}, span);
        });
        config
    }

    /// Create a configuration for testing the output of running
    /// `cargo` on the test `Cargo.toml` files.
    #[cfg(feature = "rustc")]
    pub fn cargo(root_dir: impl Into<PathBuf>) -> Self {
        let mut this = Self {
            program: CommandBuilder::cargo(),
            custom_comments: Default::default(),
            diagnostic_extractor: rustc_stderr::process_cargo,
            comment_start: "#",
            ..Self::rustc(root_dir)
        };
        this.comment_defaults.base().custom.clear();
        this
    }

    /// Populate the config with the values from parsed command line arguments.
    pub fn with_args(&mut self, args: &Args) {
        let Args {
            ref filters,
            check,
            bless,
            list,
            exact,
            ignored,
            format: _,
            threads,
            ref skip,
        } = *args;

        self.threads = threads.or(self.threads);

        self.filter_files.extend_from_slice(filters);
        self.skip_files.extend_from_slice(skip);
        self.run_only_ignored = ignored;
        self.filter_exact = exact;

        self.list = list;

        if check {
            self.output_conflict_handling = OutputConflictHandling::Error;
        } else if bless {
            self.output_conflict_handling = OutputConflictHandling::Bless;
        }
    }

    /// Replace all occurrences of a path in stderr/stdout with a byte string.
    #[track_caller]
    pub fn path_filter(&mut self, path: &Path, replacement: &'static (impl AsRef<[u8]> + ?Sized)) {
        self.path_stderr_filter(path, replacement);
        self.path_stdout_filter(path, replacement);
    }

    /// Replace all occurrences of a path in stderr with a byte string.
    #[track_caller]
    pub fn path_stderr_filter(
        &mut self,
        path: &Path,
        replacement: &'static (impl AsRef<[u8]> + ?Sized),
    ) {
        let pattern = path.canonicalize().unwrap();
        self.comment_defaults.base().normalize_stderr.push((
            pattern.parent().unwrap().into(),
            replacement.as_ref().to_owned(),
        ));
    }

    /// Replace all occurrences of a path in stdout with a byte string.
    #[track_caller]
    pub fn path_stdout_filter(
        &mut self,
        path: &Path,
        replacement: &'static (impl AsRef<[u8]> + ?Sized),
    ) {
        let pattern = path.canonicalize().unwrap();
        self.comment_defaults.base().normalize_stdout.push((
            pattern.parent().unwrap().into(),
            replacement.as_ref().to_owned(),
        ));
    }

    /// Replace all occurrences of a regex pattern in stderr/stdout with a byte string.
    #[track_caller]
    pub fn filter(&mut self, pattern: &str, replacement: &'static (impl AsRef<[u8]> + ?Sized)) {
        self.stderr_filter(pattern, replacement);
        self.stdout_filter(pattern, replacement);
    }

    /// Replace all occurrences of a regex pattern in stderr with a byte string.
    #[track_caller]
    pub fn stderr_filter(
        &mut self,
        pattern: &str,
        replacement: &'static (impl AsRef<[u8]> + ?Sized),
    ) {
        self.comment_defaults.base().normalize_stderr.push((
            Regex::new(pattern).unwrap().into(),
            replacement.as_ref().to_owned(),
        ));
    }

    /// Replace all occurrences of a regex pattern in stdout with a byte string.
    #[track_caller]
    pub fn stdout_filter(
        &mut self,
        pattern: &str,
        replacement: &'static (impl AsRef<[u8]> + ?Sized),
    ) {
        self.comment_defaults.base().normalize_stdout.push((
            Regex::new(pattern).unwrap().into(),
            replacement.as_ref().to_owned(),
        ));
    }

    /// Make sure we have the host and target triples.
    pub fn fill_host_and_target(&mut self) -> Result<()> {
        if self.host.is_none() {
            self.host = Some(
                rustc_version::VersionMeta::for_command(std::process::Command::new(
                    &self.program.program,
                ))
                .map_err(|err| {
                    color_eyre::eyre::Report::new(err).wrap_err(format!(
                        "failed to parse rustc version info: {}",
                        self.program.display().to_string().replace('\\', "/")
                    ))
                })?
                .host,
            );
        }
        if self.target.is_none() {
            self.target = Some(self.host.clone().unwrap());
        }
        Ok(())
    }

    /// Check whether the host is the specified string
    pub fn host_matches_target(&self) -> bool {
        self.host.as_ref().expect("host should have been filled in")
            == self
                .target
                .as_ref()
                .expect("target should have been filled in")
    }

    pub(crate) fn get_pointer_width(&self) -> u8 {
        // Taken 1:1 from compiletest-rs
        fn get_pointer_width(triple: &str) -> u8 {
            if (triple.contains("64")
                && !triple.ends_with("gnux32")
                && !triple.ends_with("gnu_ilp32"))
                || triple.starts_with("s390x")
            {
                64
            } else if triple.starts_with("avr") {
                16
            } else {
                32
            }
        }
        get_pointer_width(self.target.as_ref().unwrap())
    }

    pub(crate) fn test_condition(&self, condition: &Condition) -> bool {
        let target = self.target.as_ref().unwrap();
        match condition {
            Condition::Bitwidth(bits) => bits.iter().any(|bits| self.get_pointer_width() == *bits),
            Condition::Target(t) => t.iter().any(|t| target.contains(t)),
            Condition::Host(t) => t.iter().any(|t| self.host.as_ref().unwrap().contains(t)),
            Condition::OnHost => self.host_matches_target(),
        }
    }

    /// Returns whether according to the in-file conditions, this file should be run.
    pub fn test_file_conditions(&self, comments: &Comments, revision: &str) -> bool {
        if comments
            .for_revision(revision)
            .flat_map(|r| r.ignore.iter())
            .any(|c| self.test_condition(c))
        {
            return self.run_only_ignored;
        }
        if comments.for_revision(revision).any(|r| {
            r.custom
                .values()
                .any(|flags| flags.content.iter().any(|flag| flag.test_condition(self)))
        }) {
            return self.run_only_ignored;
        }
        comments
            .for_revision(revision)
            .flat_map(|r| r.only.iter())
            .all(|c| self.test_condition(c))
            ^ self.run_only_ignored
    }

    pub(crate) fn aborted(&self) -> bool {
        self.abort_check.load(std::sync::atomic::Ordering::Relaxed)
    }
}

#[derive(Debug, Clone)]
/// The different options for what to do when stdout/stderr files differ from the actual output.
pub enum OutputConflictHandling {
    /// Fail the test when mismatches are found, if provided the command string
    /// in [`Config::bless_command`] will be suggested as a way to bless the
    /// test.
    Error,
    /// Ignore mismatches in the stderr/stdout files.
    Ignore,
    /// Instead of erroring if the stderr/stdout differs from the expected
    /// automatically replace it with the found output (after applying filters).
    Bless,
}