Skip to main content

spawn_db/commands/test/
compare.rs

1use crate::commands::{Command, Outcome, TelemetryDescribe, TelemetryInfo};
2use crate::config::Config;
3use crate::sqltest::Tester;
4use anyhow::Result;
5use futures::TryStreamExt;
6
7const RESET: &str = "\x1b[0m";
8const BOLD: &str = "\x1b[1m";
9const RED: &str = "\x1b[31m";
10const GREEN: &str = "\x1b[32m";
11
12pub struct CompareTests {
13    pub name: Option<String>,
14}
15
16impl TelemetryDescribe for CompareTests {
17    fn telemetry(&self) -> TelemetryInfo {
18        TelemetryInfo::new("test compare")
19            .with_properties(vec![("is_comparing_all", self.name.is_none().to_string())])
20    }
21}
22
23impl Command for CompareTests {
24    async fn execute(&self, config: &Config) -> Result<Outcome> {
25        let test_files: Vec<String> = match &self.name {
26            Some(name) => vec![name.clone()],
27            None => {
28                let mut tests: Vec<String> = Vec::new();
29                let mut fs_lister = config
30                    .operator()
31                    .lister(&config.pather().tests_folder())
32                    .await?;
33                while let Some(entry) = fs_lister.try_next().await? {
34                    let path = entry.path().to_string();
35                    if path.ends_with("/") {
36                        tests.push(path)
37                    }
38                }
39                tests
40            }
41        };
42
43        let mut failed = false;
44
45        for test_file in test_files {
46            let tester = Tester::new(config, &test_file);
47
48            match tester.run_compare(None).await {
49                Ok(result) => match result.diff {
50                    None => {
51                        println!("{}[PASS]{} {}", GREEN, RESET, test_file);
52                    }
53                    Some(diff) => {
54                        failed = true;
55                        println!("\n{}[FAIL]{} {}{}{}", RED, RESET, BOLD, test_file, RESET);
56                        println!("{}--- Diff ---{}", BOLD, RESET);
57                        println!("{}", diff);
58                        println!("{}-------------{}\n", BOLD, RESET);
59                    }
60                },
61                Err(e) => return Err(e),
62            };
63        }
64
65        if failed {
66            return Err(anyhow::anyhow!(
67                "{}!{} Differences found in one or more tests",
68                RED,
69                RESET
70            ));
71        }
72
73        Ok(Outcome::Success)
74    }
75}