Skip to content

Commit ea28b7e

Browse files
committed
Add basic driver tests
Driver tests test the compilation of an entire file or collection of files, from generating executables to linking libraries. These are particularly helpful for testing compatibility on different platforms.
1 parent 9ac66b0 commit ea28b7e

File tree

2 files changed

+98
-61
lines changed

2 files changed

+98
-61
lines changed

cobalt-cli/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::io::{prelude::*, BufReader};
1919
use std::path::{Path, PathBuf};
2020
use std::process::Command;
2121
use std::time::{Duration, Instant};
22+
#[cfg(test)]
2223
mod tests;
2324

2425
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]

cobalt-cli/src/tests/hello_world.rs

Lines changed: 97 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,99 +6,135 @@ fn test_hello_world_aot() {
66
let output_path_str = "src/tests/outputs/hello_world";
77

88
let input = clio::Input::new(input_path_str);
9-
assert!(input.is_ok(), "(clio) failed to load input: {:?}", &input.err());
9+
assert!(
10+
input.is_ok(),
11+
"(clio) failed to load input: {:?}",
12+
&input.err()
13+
);
1014
let output = clio::OutputPath::new(output_path_str);
11-
assert!(output.is_ok(), "(clio) failed to load output: {:?}", &output.err());
12-
13-
let cli = Cli::Aot {
14-
input: input.unwrap(),
15-
output: Some(output.unwrap()),
16-
linked: vec![],
17-
link_dirs: vec![],
18-
headers: vec![],
19-
triple: None,
20-
emit: OutputType::Executable,
21-
profile: None,
22-
continue_if_err: true,
23-
debug_mangle: false,
24-
no_default_link: false,
15+
assert!(
16+
output.is_ok(),
17+
"(clio) failed to load output: {:?}",
18+
&output.err()
19+
);
20+
21+
let cli = Cli::Aot {
22+
input: input.unwrap(),
23+
output: Some(output.unwrap()),
24+
linked: vec![],
25+
link_dirs: vec![],
26+
headers: vec![],
27+
triple: None,
28+
emit: OutputType::Executable,
29+
profile: None,
30+
continue_if_err: true,
31+
debug_mangle: false,
32+
no_default_link: false,
2533
timings: false,
2634
};
2735

2836
let result = driver(cli);
29-
assert!(result.is_ok(), "failure while compiling file: {:?}", &result.err());
30-
37+
assert!(
38+
result.is_ok(),
39+
"failure while compiling file: {:?}",
40+
&result.err()
41+
);
42+
3143
let command_output = Command::new(output_path_str)
3244
.output()
3345
.expect("Failed to execute command");
34-
let output_str = std::str::from_utf8(&command_output.stdout)
35-
.expect("Output is not valid UTF-8");
46+
let output_str =
47+
std::str::from_utf8(&command_output.stdout).expect("Output is not valid UTF-8");
3648
assert_eq!(output_str.trim(), "Hello, world!");
3749
}
3850

3951
#[test]
4052
fn test_hello_world_aot_linked() {
41-
let input_main_path_str = "src/tests/inputs/hello_world_linked_main.co";
53+
let input_main_path_str = "src/tests/inputs/hello_world_linked_main.co";
4254
let input_lib_path_str = "src/tests/inputs/hello_world_linked_lib.co";
4355
let output_lib_path_str = "src/tests/outputs/libhello_world_linked.o";
4456
let output_main_path_str = "src/tests/outputs/hello_world_linked";
45-
46-
// ---
47-
57+
58+
// ---
59+
4860
let input_lib = clio::Input::new(input_lib_path_str);
49-
assert!(input_lib.is_ok(), "(clio) failed to load input: {:?}", &input_lib.err());
61+
assert!(
62+
input_lib.is_ok(),
63+
"(clio) failed to load input: {:?}",
64+
&input_lib.err()
65+
);
5066
let output_lib = clio::OutputPath::new(output_lib_path_str);
51-
assert!(output_lib.is_ok(), "(clio) failed to load output: {:?}", &output_lib.err());
52-
53-
let cli_lib = Cli::Aot {
54-
input: input_lib.unwrap(),
55-
output: Some(output_lib.unwrap()),
56-
linked: vec![],
57-
link_dirs: vec![],
58-
headers: vec![],
59-
triple: None,
60-
emit: OutputType::Library,
61-
profile: None,
62-
continue_if_err: true,
63-
debug_mangle: false,
64-
no_default_link: false,
67+
assert!(
68+
output_lib.is_ok(),
69+
"(clio) failed to load output: {:?}",
70+
&output_lib.err()
71+
);
72+
73+
let cli_lib = Cli::Aot {
74+
input: input_lib.unwrap(),
75+
output: Some(output_lib.unwrap()),
76+
linked: vec![],
77+
link_dirs: vec![],
78+
headers: vec![],
79+
triple: None,
80+
emit: OutputType::Library,
81+
profile: None,
82+
continue_if_err: true,
83+
debug_mangle: false,
84+
no_default_link: false,
6585
timings: false,
6686
};
6787

6888
let result_lib = driver(cli_lib);
69-
assert!(result_lib.is_ok(), "failure while compiling library file: {:?}", &result_lib.err());
89+
assert!(
90+
result_lib.is_ok(),
91+
"failure while compiling library file: {:?}",
92+
&result_lib.err()
93+
);
7094

7195
// ---
7296

7397
let input_main = clio::Input::new(input_main_path_str);
74-
assert!(input_main.is_ok(), "(clio) failed to load input: {:?}", &input_main.err());
98+
assert!(
99+
input_main.is_ok(),
100+
"(clio) failed to load input: {:?}",
101+
&input_main.err()
102+
);
75103
let output_main = clio::OutputPath::new(output_main_path_str);
76-
assert!(output_main.is_ok(), "(clio) failed to load output: {:?}", &output_main.err());
77-
78-
let cli_main = Cli::Aot {
79-
input: input_main.unwrap(),
80-
output: Some(output_main.unwrap()),
81-
linked: vec!["hello_world_linked".to_string()],
82-
link_dirs: vec!["src/tests/outputs".into()],
83-
headers: vec![],
84-
triple: None,
85-
emit: OutputType::Executable,
86-
profile: None,
87-
continue_if_err: true,
88-
debug_mangle: false,
89-
no_default_link: false,
104+
assert!(
105+
output_main.is_ok(),
106+
"(clio) failed to load output: {:?}",
107+
&output_main.err()
108+
);
109+
110+
let cli_main = Cli::Aot {
111+
input: input_main.unwrap(),
112+
output: Some(output_main.unwrap()),
113+
linked: vec!["hello_world_linked".to_string()],
114+
link_dirs: vec!["src/tests/outputs".into()],
115+
headers: vec![],
116+
triple: None,
117+
emit: OutputType::Executable,
118+
profile: None,
119+
continue_if_err: true,
120+
debug_mangle: false,
121+
no_default_link: false,
90122
timings: false,
91123
};
92-
124+
93125
let result_main = driver(cli_main);
94-
assert!(result_main.is_ok(), "failure while compiling main file: {:?}", &result_main.err());
95-
126+
assert!(
127+
result_main.is_ok(),
128+
"failure while compiling main file: {:?}",
129+
&result_main.err()
130+
);
131+
96132
// ---
97-
133+
98134
let command_output = Command::new(output_main_path_str)
99135
.output()
100136
.expect("Failed to execute command");
101-
let output_str = std::str::from_utf8(&command_output.stdout)
102-
.expect("Output is not valid UTF-8");
137+
let output_str =
138+
std::str::from_utf8(&command_output.stdout).expect("Output is not valid UTF-8");
103139
assert_eq!(output_str.trim(), "Hello, world!");
104-
}
140+
}

0 commit comments

Comments
 (0)