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
//! [`Harness`] for discovering test inputs and asserting against snapshot files
//!
//! # Examples
//!
//! ```rust,no_run
//! snapbox::harness::Harness::new(
//!     "tests/fixtures/invalid",
//!     setup,
//!     test,
//! )
//! .select(["tests/cases/*.in"])
//! .action_env("SNAPSHOTS")
//! .test();
//!
//! fn setup(input_path: std::path::PathBuf) -> snapbox::harness::Case {
//!     let name = input_path.file_name().unwrap().to_str().unwrap().to_owned();
//!     let expected = input_path.with_extension("out");
//!     snapbox::harness::Case {
//!         name,
//!         fixture: input_path,
//!         expected,
//!     }
//! }
//!
//! fn test(input_path: &std::path::Path) -> Result<usize, Box<dyn std::error::Error>> {
//!     let raw = std::fs::read_to_string(input_path)?;
//!     let num = raw.parse::<usize>()?;
//!
//!     let actual = num + 10;
//!
//!     Ok(actual)
//! }
//! ```

use crate::data::DataFormat;
use crate::Action;

pub struct Harness<S, T> {
    root: std::path::PathBuf,
    overrides: Option<ignore::overrides::Override>,
    setup: S,
    test: T,
    action: Action,
}

impl<S, T, I, E> Harness<S, T>
where
    I: std::fmt::Display,
    E: std::fmt::Display,
    S: Fn(std::path::PathBuf) -> Case + Send + Sync + 'static,
    T: Fn(&std::path::Path) -> Result<I, E> + Send + Sync + 'static,
{
    pub fn new(root: impl Into<std::path::PathBuf>, setup: S, test: T) -> Self {
        Self {
            root: root.into(),
            overrides: None,
            setup,
            test,
            action: Action::Verify,
        }
    }

    /// Path patterns for selecting input files
    ///
    /// This used gitignore syntax
    pub fn select<'p>(mut self, patterns: impl IntoIterator<Item = &'p str>) -> Self {
        let mut overrides = ignore::overrides::OverrideBuilder::new(&self.root);
        for line in patterns {
            overrides.add(line).unwrap();
        }
        self.overrides = Some(overrides.build().unwrap());
        self
    }

    /// Read the failure action from an environment variable
    pub fn action_env(mut self, var_name: &str) -> Self {
        let action = Action::with_env_var(var_name);
        self.action = action.unwrap_or(self.action);
        self
    }

    /// Override the failure action
    pub fn action(mut self, action: Action) -> Self {
        self.action = action;
        self
    }

    /// Run tests
    pub fn test(self) -> ! {
        let mut walk = ignore::WalkBuilder::new(&self.root);
        walk.standard_filters(false);
        let tests = walk.build().filter_map(|entry| {
            let entry = entry.unwrap();
            let is_dir = entry.file_type().map(|f| f.is_dir()).unwrap_or(false);
            let path = entry.into_path();
            if let Some(overrides) = &self.overrides {
                overrides
                    .matched(&path, is_dir)
                    .is_whitelist()
                    .then(|| path)
            } else {
                Some(path)
            }
        });

        let tests: Vec<_> = tests
            .into_iter()
            .map(|path| {
                let case = (self.setup)(path);
                Test {
                    name: case.name.clone(),
                    kind: "".into(),
                    is_ignored: false,
                    is_bench: false,
                    data: case,
                }
            })
            .collect();

        let args = libtest_mimic::Arguments::from_args();
        libtest_mimic::run_tests(&args, tests, move |test| {
            match (self.test)(&test.data.fixture) {
                Ok(actual) => {
                    let actual = actual.to_string();
                    let actual = crate::Data::text(actual).map_text(crate::utils::normalize_lines);
                    let verify = Verifier::new()
                        .palette(crate::report::Palette::auto())
                        .action(self.action);
                    verify.verify(&test.data.expected, actual)
                }
                Err(err) => libtest_mimic::Outcome::Failed {
                    msg: Some(err.to_string()),
                },
            }
        })
        .exit()
    }
}

struct Verifier {
    palette: crate::report::Palette,
    action: Action,
}

impl Verifier {
    fn new() -> Self {
        Default::default()
    }

    fn palette(mut self, palette: crate::report::Palette) -> Self {
        self.palette = palette;
        self
    }

    fn action(mut self, action: Action) -> Self {
        self.action = action;
        self
    }

    fn verify(
        &self,
        expected_path: &std::path::Path,
        actual: crate::Data,
    ) -> libtest_mimic::Outcome {
        match self.action {
            Action::Skip => libtest_mimic::Outcome::Ignored,
            Action::Ignore => {
                let _ = self.do_verify(expected_path, actual);
                libtest_mimic::Outcome::Ignored
            }
            Action::Verify => self.do_verify(expected_path, actual),
            Action::Overwrite => self.do_overwrite(expected_path, actual),
        }
    }

    fn do_overwrite(
        &self,
        expected_path: &std::path::Path,
        actual: crate::Data,
    ) -> libtest_mimic::Outcome {
        match self.try_overwrite(expected_path, actual) {
            Ok(()) => libtest_mimic::Outcome::Passed,
            Err(err) => libtest_mimic::Outcome::Failed {
                msg: Some(err.to_string()),
            },
        }
    }

    fn try_overwrite(
        &self,
        expected_path: &std::path::Path,
        actual: crate::Data,
    ) -> crate::Result<()> {
        actual.write_to(expected_path)?;
        Ok(())
    }

    fn do_verify(
        &self,
        expected_path: &std::path::Path,
        actual: crate::Data,
    ) -> libtest_mimic::Outcome {
        match self.try_verify(expected_path, actual) {
            Ok(()) => libtest_mimic::Outcome::Passed,
            Err(err) => libtest_mimic::Outcome::Failed {
                msg: Some(err.to_string()),
            },
        }
    }

    fn try_verify(
        &self,
        expected_path: &std::path::Path,
        actual: crate::Data,
    ) -> crate::Result<()> {
        let expected = crate::Data::read_from(expected_path, Some(DataFormat::Text))?
            .map_text(crate::utils::normalize_lines);

        if expected != actual {
            let mut buf = String::new();
            crate::report::write_diff(
                &mut buf,
                &expected,
                &actual,
                Some(&expected_path.display()),
                None,
                self.palette,
            )
            .map_err(|e| e.to_string())?;
            Err(buf.into())
        } else {
            Ok(())
        }
    }
}

impl Default for Verifier {
    fn default() -> Self {
        Self {
            palette: crate::report::Palette::auto(),
            action: Action::Verify,
        }
    }
}

pub struct Case {
    pub name: String,
    pub fixture: std::path::PathBuf,
    pub expected: std::path::PathBuf,
}

type Test = libtest_mimic::Test<Case>;