forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunit_test.rs
More file actions
116 lines (104 loc) · 3.68 KB
/
unit_test.rs
File metadata and controls
116 lines (104 loc) · 3.68 KB
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
use crate::config;
use colored::*;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(rename_all = "kebab-case")]
pub struct Opts {
/// Vector config files in TOML format to test.
#[structopt(name = "config-toml", long, use_delimiter(true))]
paths_toml: Vec<PathBuf>,
/// Vector config files in JSON format to test.
#[structopt(name = "config-json", long, use_delimiter(true))]
paths_json: Vec<PathBuf>,
/// Vector config files in YAML format to test.
#[structopt(name = "config-yaml", long, use_delimiter(true))]
paths_yaml: Vec<PathBuf>,
/// Any number of Vector config files to test. If none are specified the
/// default config path `/etc/vector/vector.toml` will be targeted.
#[structopt(use_delimiter(true))]
paths: Vec<PathBuf>,
/// Read configuration from files in one or more directories.
/// File format is detected from the file name.
///
/// Files not ending in .toml, .json, .yaml, or .yml will be ignored.
#[structopt(
name = "config-dir",
short = "C",
long,
env = "VECTOR_CONFIG_DIR",
use_delimiter(true)
)]
pub config_dirs: Vec<PathBuf>,
}
impl Opts {
fn paths_with_formats(&self) -> Vec<config::ConfigPath> {
config::merge_path_lists(vec![
(&self.paths, None),
(&self.paths_toml, Some(config::Format::Toml)),
(&self.paths_json, Some(config::Format::Json)),
(&self.paths_yaml, Some(config::Format::Yaml)),
])
.map(|(path, hint)| config::ConfigPath::File(path, hint))
.chain(
self.config_dirs
.iter()
.map(|dir| config::ConfigPath::Dir(dir.to_path_buf())),
)
.collect()
}
}
pub async fn cmd(opts: &Opts) -> exitcode::ExitCode {
let mut aggregated_test_inspections = Vec::new();
let mut aggregated_test_errors = Vec::new();
let paths = opts.paths_with_formats();
let paths = match config::process_paths(&paths) {
Some(paths) => paths,
None => return exitcode::CONFIG,
};
println!("Running tests");
match config::build_unit_tests(&paths).await {
Ok(mut tests) => {
tests.iter_mut().for_each(|t| {
let (test_inspections, test_errors) = t.run();
if !test_inspections.is_empty() {
aggregated_test_inspections.push((t.name.clone(), test_inspections));
}
if !test_errors.is_empty() {
println!("test {} ... {}", t.name, "failed".red());
aggregated_test_errors.push((t.name.clone(), test_errors));
} else {
println!("test {} ... {}", t.name, "passed".green());
}
});
if tests.is_empty() {
println!("{}", "No tests found.".yellow());
}
}
Err(errs) => {
error!("Failed to execute tests:\n{}.", errs.join("\n"));
return exitcode::CONFIG;
}
}
if !aggregated_test_inspections.is_empty() {
println!("\ninspections:");
for (test_name, inspection) in aggregated_test_inspections {
println!("\ntest {}:\n", test_name);
for inspect in inspection {
println!("{}\n", inspect);
}
}
}
if !aggregated_test_errors.is_empty() {
println!("\nfailures:");
for (test_name, fails) in aggregated_test_errors {
println!("\ntest {}:\n", test_name);
for fail in fails {
println!("{}\n", fail);
}
}
exitcode::CONFIG
} else {
exitcode::OK
}
}