1
use std::env;
2

            
3
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
4
pub enum AcceptMode {
5
    /// Normal test mode: compare generated files with expected files and do not rewrite fixtures.
6
    Disabled,
7
    /// Rewrite expected output fixtures, but leave expected runtime budgets untouched.
8
    Accept,
9
    /// Rewrite output fixtures and runtime budgets exactly as observed.
10
    AcceptWithTimes,
11
    /// Rewrite output fixtures, but only raise runtime budgets.
12
    ///
13
    /// Catching slowdowns is more important than automatically accepting speedups. Runtimes
14
    /// are non-deterministic and machine/load dependent, so a significant slowdown may be
15
    /// worth noticing while a one-off faster run should not lower the recorded budget.
16
    AcceptWithSlowerTimes,
17
}
18

            
19
impl AcceptMode {
20
2298
    pub fn from_env() -> Self {
21
2298
        match env::var("ACCEPT").as_deref() {
22
            Ok("false") => Self::Disabled,
23
            Ok("true") => Self::Accept,
24
            Ok("with-times") => Self::AcceptWithTimes,
25
            Ok("with-exact-times") => Self::AcceptWithTimes,
26
            Ok("with-slower-times") => Self::AcceptWithSlowerTimes,
27
2298
            _ => Self::Disabled,
28
        }
29
2298
    }
30

            
31
2298
    pub fn accepts_outputs(self) -> bool {
32
2298
        !matches!(self, Self::Disabled)
33
2298
    }
34

            
35
1137
    pub fn records_expected_time(self) -> bool {
36
1137
        matches!(self, Self::AcceptWithTimes | Self::AcceptWithSlowerTimes)
37
1137
    }
38

            
39
    pub fn expected_time_to_record(self, current: Option<u64>, observed: u64) -> Option<u64> {
40
        match self {
41
            Self::AcceptWithTimes => Some(observed),
42
            Self::AcceptWithSlowerTimes if current.is_none_or(|current| observed > current) => {
43
                Some(observed)
44
            }
45
            _ => None,
46
        }
47
    }
48

            
49
    pub fn refresh_hint() -> &'static str {
50
        "Run with ACCEPT=true, ACCEPT=with-slower-times, or ACCEPT=with-exact-times"
51
    }
52
}