-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathself_checks_test.rs
More file actions
134 lines (114 loc) · 4.41 KB
/
Copy pathself_checks_test.rs
File metadata and controls
134 lines (114 loc) · 4.41 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use homeboy::commands::lint::{run as run_lint, LintArgs};
use homeboy::commands::test::{run as run_test, TestArgs};
use std::fs;
use std::path::Path;
fn write_component(root: &Path, scripts: &str) {
fs::write(
root.join("homeboy.json"),
format!(
r#"{{
"id": "fixture",
"scripts": {}
}}"#,
scripts
),
)
.expect("homeboy.json should be written");
}
fn write_script(root: &Path, name: &str, body: &str) {
let script_dir = root.join("scripts");
fs::create_dir_all(&script_dir).expect("script dir should be created");
fs::write(script_dir.join(name), body).expect("script should be written");
}
fn lint_args(root: &Path) -> LintArgs {
LintArgs::for_test("fixture", root)
}
fn test_args(root: &Path) -> TestArgs {
TestArgs::for_test("fixture", root)
}
fn json_test_args(root: &Path) -> TestArgs {
let mut args = test_args(root);
args.json_summary = true;
args
}
#[test]
fn lint_runs_declared_self_check_without_extensions() {
let dir = tempfile::tempdir().expect("temp dir");
write_component(dir.path(), r#"{ "lint": ["sh scripts/lint.sh"] }"#);
write_script(dir.path(), "lint.sh", "printf 'lint self-check ran\\n'\n");
let (output, exit_code) = run_lint(lint_args(dir.path())).expect("lint self-check should run");
assert_eq!(exit_code, 0);
assert!(output.passed);
assert_eq!(output.component, "fixture");
}
#[test]
fn test_runs_declared_self_check_without_extensions() {
let dir = tempfile::tempdir().expect("temp dir");
write_component(dir.path(), r#"{ "test": ["sh scripts/test.sh"] }"#);
write_script(dir.path(), "test.sh", "printf 'test self-check ran\\n'\n");
let (output, exit_code) = run_test(test_args(dir.path())).expect("test self-check should run");
assert_eq!(exit_code, 0);
assert!(output.passed);
assert_eq!(output.component, "fixture");
}
#[test]
fn non_zero_self_check_fails_command_and_surfaces_output() {
let dir = tempfile::tempdir().expect("temp dir");
write_component(dir.path(), r#"{ "test": ["sh scripts/fail.sh"] }"#);
write_script(
dir.path(),
"fail.sh",
"printf 'visible failure stdout\\n'\nprintf 'visible failure stderr\\n' >&2\nexit 7\n",
);
let (output, exit_code) = run_test(test_args(dir.path()))
.expect("test self-check failure should return structured output");
assert_eq!(exit_code, 7);
assert!(!output.passed);
assert_eq!(output.status, "failed");
let raw = output
.raw_output
.expect("failure should include raw output");
assert!(raw.stdout_tail.contains("visible failure stdout"));
assert!(raw.stderr_tail.contains("visible failure stderr"));
}
#[test]
fn json_self_check_failure_reports_bounded_large_output_metadata() {
let dir = tempfile::tempdir().expect("temp dir");
write_component(dir.path(), r#"{ "test": ["sh scripts/large-fail.sh"] }"#);
write_script(
dir.path(),
"large-fail.sh",
"perl -e 'print \"stdout-line-\" . (\"x\" x 80) . \"\\n\" for 1..800'\nperl -e 'print STDERR \"stderr-line-\" . (\"x\" x 80) . \"\\n\" for 1..800'\nexit 7\n",
);
let (output, exit_code) = run_test(json_test_args(dir.path()))
.expect("test self-check failure should return structured output");
assert_eq!(exit_code, 7);
assert!(!output.passed);
let raw = output
.raw_output
.expect("failure should include bounded raw output metadata");
assert!(raw.truncated);
assert!(raw.stdout_truncated);
assert!(raw.stderr_truncated);
assert!(raw.stdout_seen_bytes > raw.stdout_limit_bytes);
assert!(raw.stderr_seen_bytes > raw.stderr_limit_bytes);
assert!(raw.stdout_retained_bytes <= raw.stdout_limit_bytes);
assert!(raw.stderr_retained_bytes <= raw.stderr_limit_bytes);
assert!(raw.stdout_tail.contains("stdout-line"));
assert!(raw.stderr_tail.contains("stderr-line"));
}
#[test]
fn missing_extension_and_self_check_keeps_existing_error() {
let dir = tempfile::tempdir().expect("temp dir");
write_component(dir.path(), r#"{}"#);
let err = match run_lint(lint_args(dir.path())) {
Ok(_) => panic!("lint without extension or self-check should fail"),
Err(err) => err,
};
assert_eq!(err.code.as_str(), "extension.unsupported");
assert!(
err.to_string()
.contains("No extension provider configured for component 'fixture'"),
"unexpected error: {err}"
);
}