mv_command_line_common/
testing.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::env::read_bool_env_var;
5
6/// Extension for raw output files
7pub const OUT_EXT: &str = "out";
8/// Extension for expected output files
9pub const EXP_EXT: &str = "exp";
10
11/// If any of these env vars is set, the test harness should overwrite
12/// the existing .exp files with the output instead of checking
13/// them against the output.
14pub const UPDATE_BASELINE: &str = "UPDATE_BASELINE";
15pub const UPBL: &str = "UPBL";
16pub const UB: &str = "UB";
17
18pub const PRETTY: &str = "PRETTY";
19pub const FILTER: &str = "FILTER";
20
21pub fn read_env_update_baseline() -> bool {
22    read_bool_env_var(UPDATE_BASELINE) || read_bool_env_var(UPBL) || read_bool_env_var(UB)
23}
24
25pub fn format_diff(expected: impl AsRef<str>, actual: impl AsRef<str>) -> String {
26    use difference::*;
27
28    let changeset = Changeset::new(expected.as_ref(), actual.as_ref(), "\n");
29
30    let mut ret = String::new();
31
32    for seq in changeset.diffs {
33        match &seq {
34            Difference::Same(x) => {
35                ret.push_str(x);
36                ret.push('\n');
37            }
38            Difference::Add(x) => {
39                ret.push_str("\x1B[92m");
40                ret.push_str(x);
41                ret.push_str("\x1B[0m");
42                ret.push('\n');
43            }
44            Difference::Rem(x) => {
45                ret.push_str("\x1B[91m");
46                ret.push_str(x);
47                ret.push_str("\x1B[0m");
48                ret.push('\n');
49            }
50        }
51    }
52    ret
53}