cli tests refactoring progress

This commit is contained in:
Anton-4 2024-10-11 20:54:31 +02:00
parent 3bad18dc92
commit 0659abdc40
No known key found for this signature in database
GPG key ID: 0971D718C0A9B937
12 changed files with 221 additions and 248 deletions

View file

@ -51,8 +51,7 @@ mod cli_tests {
#[test]
#[cfg_attr(windows, ignore)]
fn platform_switching_rust() {
let cli_build_cmd = ExecCli::new(
let cli_build = ExecCli::new(
CMD_BUILD,
file_from_root("examples/platform-switching", "rocLovesRust.roc")
)
@ -61,62 +60,206 @@ mod cli_tests {
let expected_output = "Roc <3 Rust!\n";
cli_build_cmd.full_check(expected_output, TEST_LEGACY_LINKER, ALLOW_VALGRIND);
cli_build.full_check_build_and_run(expected_output, TEST_LEGACY_LINKER, ALLOW_VALGRIND, None, None);
}
/*
#[test]
#[cfg_attr(windows, ignore)]
fn platform_switching_zig() {
let runner = ExecCli::new_roc()
.arg(CMD_RUN)
let cli_build = ExecCli::new(
CMD_BUILD,
file_from_root("examples/platform-switching", "rocLovesZig.roc")
)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG)
.add_arg_if(LINKER_FLAG, TEST_LEGACY_LINKER)
.with_valgrind(ALLOW_VALGRIND)
.arg(file_from_root("examples/platform-switching", "rocLovesZig.roc").as_path());
let out = runner.run();
out.assert_clean_success();
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG);
let expected_output = "Roc <3 Zig!\n";
cli_build.full_check_build_and_run(expected_output, TEST_LEGACY_LINKER, ALLOW_VALGRIND, None, None);
}
#[test]
fn platform_switching_wasm() {
// this is a web assembly example, but we don't test with JS at the moment
// so let's just check it for now
let runner = ExecCli::new_roc().arg(CMD_CHECK).arg(
file_from_root("examples/platform-switching", "rocLovesWebAssembly.roc").as_path(),
);
let cli_check = ExecCli::new(
CMD_CHECK,
file_from_root("examples/platform-switching", "rocLovesWebAssembly.roc")
);
let out = runner.run();
out.assert_clean_success();
let cli_check_out = cli_check.run();
cli_check_out.assert_clean_success();
}
#[test]
#[cfg_attr(
windows,
ignore = "Flaky failure: Roc command failed with status ExitStatus(ExitStatus(3221225477))"
)]
fn fibonacci() {
let cli_build = ExecCli::new(
CMD_BUILD,
file_from_root("crates/cli/tests/test-projects/algorithms", "fibonacci.roc")
)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG);
let expected_output = "55\n";
cli_build.full_check_build_and_run(expected_output, TEST_LEGACY_LINKER, ALLOW_VALGRIND, None, None);
}
#[test]
#[cfg_attr(windows, ignore)]
fn quicksort() {
let cli_build = ExecCli::new(
CMD_BUILD,
file_from_root("crates/cli/tests/test-projects/algorithms", "quicksort.roc")
)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG);
let expected_output = "[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2]\n";
cli_build.full_check_build_and_run(expected_output, TEST_LEGACY_LINKER, ALLOW_VALGRIND, None, None);
}
// TODO: write a new test once mono bugs are resolved in investigation
// Encountering this TODO years later, I presume the new test should test the execution, not just roc check.
#[test]
#[cfg(not(debug_assertions))] // https://github.com/roc-lang/roc/issues/4806 - later observation: this issue is closed but the tests still hangs in debug mode
fn check_virtual_dom_server() {
let cli_check = ExecCli::new(
CMD_CHECK,
file_from_root("examples/virtual-dom-wip", "example-server.roc")
);
let cli_check_out = cli_check.run();
cli_check_out.assert_clean_success();
}
// TODO: write a new test once mono bugs are resolved in investigation
// Encountering this TODO years later, I presume the new test should test the execution, not just roc check.
#[test]
#[cfg(not(debug_assertions))] // https://github.com/roc-lang/roc/issues/4806 - later observation: this issue is closed but the tests still hangs in debug mode
fn check_virtual_dom_client() {
let cli_check = ExecCli::new(
CMD_CHECK,
file_from_root("examples/virtual-dom-wip", "example-client.roc")
);
let cli_check_out = cli_check.run();
cli_check_out.assert_clean_success();
}
#[test]
#[cfg_attr(windows, ignore)]
// tea = The Elm Architecture
fn terminal_ui_tea() {
let cli_build = ExecCli::new(
CMD_BUILD,
file_from_root("crates/cli/tests/test-projects/tui", "main.roc")
)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG);
let expected_output = "Hello World!\nHello Worldfoo!\n";
cli_build.full_check_build_and_run(expected_output, TEST_LEGACY_LINKER, ALLOW_VALGRIND, Some("foo\n"), None);
}
// TODO check this out, there's more that's going wrong than a segfault
#[test]
/*#[cfg_attr(
any(target_os = "windows", target_os = "linux", target_os = "macos"),
ignore = "Segfault, likely broken because of alias analysis: https://github.com/roc-lang/roc/issues/6544"
)]*/
fn false_interpreter() {
let cli_build = ExecCli::new(
CMD_BUILD,
file_from_root("crates/cli/tests/test-projects/false-interpreter", "main.roc")
)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG);
let sqrt_false_path_buf = file_from_root("crates/cli/tests/test-projects/false-interpreter/examples", "sqrt.false");
let app_args = ["--",
sqrt_false_path_buf
.as_path()
.to_str()
.unwrap()];
cli_build.full_check_build_and_run("1414", TEST_LEGACY_LINKER, ALLOW_VALGRIND, None, Some(&app_args));
}
#[test]
#[cfg_attr(windows, ignore)]
fn transitive_expects() {
let cli_test = ExecCli::new(
CMD_TEST,
file_from_root("crates/cli/tests/test-projects/expects_transitive", "main.roc")
);
let cli_test_out = cli_test.run();
cli_test_out.assert_clean_success();
cli_test_out.assert_stdout_and_stderr_ends_with("0 failed and 3 passed in <ignored for test> ms.\n");
}
#[test]
#[cfg_attr(windows, ignore)]
fn transitive_expects_verbose() {
let cli_test = ExecCli::new(
CMD_TEST,
file_from_root("crates/cli/tests/test-projects/expects_transitive", "main.roc")
).arg("--verbose");
let cli_test_out = cli_test.run();
cli_test_out.assert_clean_success();
insta::assert_snapshot!(cli_test_out.normalize_stdout_and_stderr());
}
#[test]
#[cfg_attr(windows, ignore)]
fn multiple_exposed() {
let cli_build = ExecCli::new(
CMD_BUILD,
file_from_root("crates/cli/tests/test-projects/multiple_exposed", "main.roc")
)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG);
let expected_output = "55\n3628800\n";
cli_build.full_check_build_and_run(expected_output, TEST_LEGACY_LINKER, ALLOW_VALGRIND, None, None);
}
#[test]
#[cfg_attr(windows, ignore)]
fn test_module_imports_pkg_w_flag() {
let runner = ExecCli::new_roc()
.arg(CMD_TEST)
.with_valgrind(ALLOW_VALGRIND)
.add_args(["--main", "tests/module_imports_pkg/app.roc"])
.arg(file_from_root("crates/cli/tests/module_imports_pkg", "Module.roc").as_path());
let cli_test = ExecCli::new(
CMD_TEST,
file_from_root("crates/cli/tests/test-projects/module_imports_pkg", "Module.roc")
).add_args(["--main", "tests/test-projects/module_imports_pkg/app.roc"]);
let out = runner.run();
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
let cli_test_out = cli_test.run();
cli_test_out.assert_clean_success();
cli_test_out.assert_stdout_and_stderr_ends_with("0 failed and 1 passed in <ignored for test> ms.\n");
}
#[test]
#[cfg_attr(windows, ignore)]
fn test_module_imports_pkg_no_flag() {
let runner = ExecCli::new_roc()
.arg(CMD_TEST)
.with_valgrind(ALLOW_VALGRIND)
.arg(file_from_root("crates/cli/tests/module_imports_pkg", "Module.roc").as_path());
let cli_test = ExecCli::new(
CMD_TEST,
file_from_root("crates/cli/tests/test-projects/module_imports_pkg", "Module.roc")
);
let out = runner.run();
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
let cli_test_out = cli_test.run();
insta::assert_snapshot!(cli_test_out.normalize_stdout_and_stderr());
}
/*
#[test]
#[cfg_attr(windows, ignore)]
fn test_module_imports_unknown_pkg() {
@ -152,167 +295,6 @@ mod cli_tests {
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
}
#[test]
#[cfg_attr(windows, ignore)]
fn transitive_expects() {
let runner = ExecCli::new_roc()
.arg(CMD_TEST)
.with_valgrind(ALLOW_VALGRIND)
.arg(file_from_root("crates/cli/tests/expects_transitive", "main.roc").as_path());
let out = runner.run();
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
}
#[test]
#[cfg_attr(windows, ignore)]
fn transitive_expects_verbose() {
let runner = ExecCli::new_roc()
.arg(CMD_TEST)
.with_valgrind(ALLOW_VALGRIND)
.arg("--verbose")
.arg(file_from_root("crates/cli/tests/expects_transitive", "main.roc").as_path());
let out = runner.run();
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
}
#[test]
#[cfg_attr(
windows,
ignore = "Flaky failure: Roc command failed with status ExitStatus(ExitStatus(3221225477))"
)]
fn fibonacci() {
let runner = ExecCli::new_roc()
.arg(CMD_RUN)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG)
.add_arg_if(LINKER_FLAG, TEST_LEGACY_LINKER)
.with_valgrind(ALLOW_VALGRIND)
.arg(file_from_root("crates/cli/tests/algorithms", "fibonacci.roc").as_path());
let out = runner.run();
out.assert_clean_success();
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
}
#[test]
#[cfg_attr(windows, ignore)]
fn quicksort() {
let runner = ExecCli::new_roc()
.arg(CMD_RUN)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG)
.add_arg_if(LINKER_FLAG, TEST_LEGACY_LINKER)
.with_valgrind(ALLOW_VALGRIND)
.arg(file_from_root("crates/cli/tests/algorithms", "quicksort.roc").as_path());
let out = runner.run();
out.assert_clean_success();
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
}
#[test]
#[cfg_attr(windows, ignore)]
fn multiple_exposed() {
let runner = ExecCli::new_roc()
.arg(CMD_RUN)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG)
.add_arg_if(LINKER_FLAG, TEST_LEGACY_LINKER)
.with_valgrind(ALLOW_VALGRIND)
.arg(file_from_root("crates/cli/tests/multiple_exposed", "main.roc").as_path())
.with_stdin_vals(vec!["foo\n"]);
let out = runner.run();
out.assert_clean_success();
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
}
// TODO: write a new test once mono bugs are resolved in investigation
#[test]
#[cfg(not(debug_assertions))] // https://github.com/roc-lang/roc/issues/4806
fn check_virtual_dom_server() {
ExecCli::new_roc()
.add_args([
CMD_CHECK,
file_from_root("examples/virtual-dom-wip", "example-server.roc")
.to_str()
.unwrap(),
])
.run()
.assert_clean_success();
}
// TODO: write a new test once mono bugs are resolved in investigation
#[test]
#[cfg(not(debug_assertions))] // https://github.com/roc-lang/roc/issues/4806
fn check_virtual_dom_client() {
ExecCli::new_roc()
.add_args([
CMD_CHECK,
file_from_root("examples/virtual-dom-wip", "example-client.roc")
.to_str()
.unwrap(),
])
.run()
.assert_clean_success();
}
#[test]
#[cfg_attr(windows, ignore)]
// tea = The Elm Architecture
fn terminal_ui_tea() {
let runner = ExecCli::new_roc()
.arg(CMD_RUN)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG)
.add_arg_if(LINKER_FLAG, TEST_LEGACY_LINKER)
.with_valgrind(ALLOW_VALGRIND)
.arg(file_from_root("crates/cli/tests/tui", "main.roc").as_path())
.with_stdin_vals(vec!["foo\n"]);
let out = runner.run();
out.assert_clean_success();
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
}
#[test]
#[cfg_attr(
any(target_os = "windows", target_os = "linux", target_os = "macos"),
ignore = "Segfault, likely broken because of alias analysis: https://github.com/roc-lang/roc/issues/6544"
)]
fn false_interpreter() {
// Test building
let build_runner = ExecCli::new_roc()
.arg(CMD_BUILD)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG)
.add_arg_if(LINKER_FLAG, TEST_LEGACY_LINKER)
.arg(file_from_root("crates/cli/tests/false-interpreter", "False.roc").as_path())
.run();
build_runner.assert_clean_success();
// Test running
let runner = ExecCli::new_roc()
.arg(CMD_RUN)
.add_arg_if(LINKER_FLAG, TEST_LEGACY_LINKER)
.with_valgrind(ALLOW_VALGRIND)
.arg(file_from_root("crates/cli/tests/false-interpreter", "False.roc").as_path())
.add_args([
"--",
file_from_root("crates/cli/tests/false-interpreter/examples", "sqrt.false")
.as_path()
.to_str()
.unwrap(),
]);
let out = runner.run();
out.assert_clean_success();
insta::assert_snapshot!(out.normalize_stdout_and_stderr());
}
mod test_platform_effects_zig {
use super::*;
use cli_test_utils::helpers::{file_from_root, ExecCli};

View file

@ -1,5 +0,0 @@
---
source: crates/cli/tests/cli_tests.rs
expression: out.normalize_stdout_and_stderr()
---
55🔨 Building host ...

View file

@ -1,5 +0,0 @@
---
source: crates/cli/tests/cli_tests.rs
expression: out.normalize_stdout_and_stderr()
---
0 failed and 1 passed in <ignored for test> ms.

View file

@ -1,5 +0,0 @@
---
source: crates/cli/tests/cli_tests.rs
expression: out.normalize_stdout_and_stderr()
---
Roc <3 Rust!🔨 Building host ...

View file

@ -1,5 +0,0 @@
---
source: crates/cli/tests/cli_tests.rs
expression: out.normalize_stdout_and_stderr()
---
Roc <3 Zig!🔨 Building host ...

View file

@ -1,5 +0,0 @@
---
source: crates/cli/tests/cli_tests.rs
expression: out.normalize_stdout_and_stderr()
---
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2]🔨 Building host ...

View file

@ -1,8 +1,8 @@
---
source: crates/cli/tests/cli_tests.rs
expression: out.normalize_stdout_and_stderr()
expression: cli_test_out.normalize_stdout_and_stderr()
---
── UNRECOGNIZED PACKAGE in tests/module_imports_pkg/Module.roc ─────────────────
── UNRECOGNIZED PACKAGE in tests/test-projects/module_imports_pkg/Module.roc ───
This module is trying to import from `pkg`:

View file

@ -1,6 +1,6 @@
---
source: crates/cli/tests/cli_tests.rs
expression: out.normalize_stdout_and_stderr()
expression: cli_test_out.normalize_stdout_and_stderr()
---
Compiled in <ignored for test> ms.

View file

@ -6,7 +6,7 @@ use regex::Regex;
use roc_command_utils::pretty_command_string;
use roc_reporting::report::ANSI_STYLE_CODES;
pub fn run_command(mut cmd: Command, stdin_vals:&[&str]) -> CmdOut {
pub fn run_command(mut cmd: Command, stdin_opt: Option<&str>) -> CmdOut {
let cmd_str = pretty_command_string(&cmd);
let command = cmd
@ -20,7 +20,7 @@ pub fn run_command(mut cmd: Command, stdin_vals:&[&str]) -> CmdOut {
let stdin = roc_cmd_child.stdin.as_mut().expect("Failed to open stdin");
for stdin_str in stdin_vals.iter() {
if let Some(stdin_str) = stdin_opt {
stdin
.write_all(stdin_str.as_bytes())
.unwrap_or_else(|err| {
@ -89,25 +89,29 @@ impl CmdOut {
/// This normalises the output for comparison in tests such as replacing timings
/// with a placeholder, or stripping ANSI colors
pub fn assert_stdout_and_stderr_ends_with(&self, expected: &str) {
let normalised_output = format!(
"{}{}",
normalize_for_tests(&self.stdout),
normalize_for_tests(&self.stderr)
);
let normalised_stdout_stderr = self.normalize_stdout_and_stderr();
assert!(
normalised_output.ends_with(expected),
normalised_stdout_stderr.ends_with(expected),
"\n{}EXPECTED stdout and stderr after normalizing:\n----------------\n{}{}\n{}ACTUAL stdout and stderr after normalizing:\n----------------\n{}{}{}\n----------------\n{}",
ANSI_STYLE_CODES.cyan,
ANSI_STYLE_CODES.reset,
expected,
ANSI_STYLE_CODES.cyan,
ANSI_STYLE_CODES.reset,
normalised_output,
normalised_stdout_stderr,
ANSI_STYLE_CODES.cyan,
ANSI_STYLE_CODES.reset,
);
}
pub fn normalize_stdout_and_stderr(&self) -> String {
format!(
"{}{}",
normalize_for_tests(&self.stdout),
normalize_for_tests(&self.stderr)
)
}
}
pub fn assert_no_unexpected_error(stderr: &str) {

View file

@ -54,45 +54,57 @@ impl ExecCli {
roc_cli_command.arg(self.roc_file_path.clone());
roc_cli_command.args(&self.args);
run_command(roc_cli_command, &[])
let app_stdin_opt = None;
run_command(roc_cli_command, app_stdin_opt)
}
pub fn full_check(mut self, expected_output: &'static str, both_linkers: bool, with_valgrind: bool) {
self.check_build_and_run(expected_output, with_valgrind);
pub fn full_check_build_and_run(mut self, expected_output: &'static str, both_linkers: bool, with_valgrind: bool, app_stdin_opt: Option<&str>, app_args_opt: Option<&[&str]>) {
self.check_build_and_run(expected_output, with_valgrind, app_stdin_opt, app_args_opt);
if both_linkers {
self = self.arg(LINKER_FLAG);
self.check_build_and_run(expected_output, with_valgrind);
self.check_build_and_run(expected_output, with_valgrind, app_stdin_opt, app_args_opt);
}
}
fn check_build_and_run(&self, expected_output: &'static str, with_valgrind: bool) {
fn check_build_and_run(&self, expected_output: &'static str, with_valgrind: bool, app_stdin_opt: Option<&str>, app_args_opt: Option<&[&str]>) {
let build_cmd_out = self.run();
build_cmd_out.assert_clean_success();
let executable_output = self.run_executable(with_valgrind);
let executable_output = self.run_executable(false, app_stdin_opt, app_args_opt);
executable_output.assert_clean_success();
assert_eq!(executable_output.stdout, expected_output);
if with_valgrind {
let executable_output_w_valgrind = self.run_executable(true, app_stdin_opt, app_args_opt);
assert!(executable_output_w_valgrind.status.success(), "Valgrind found issue(s):\n{}\nCommand used for building:\n\t{:?}", executable_output_w_valgrind, build_cmd_out.cmd_str);
}
}
// run executable produced by e.g. `roc build`
fn run_executable(&self, with_valgrind: bool) -> CmdOut {
fn run_executable(&self, with_valgrind: bool, app_stdin_opt: Option<&str>, app_args_opt: Option<&[&str]>) -> CmdOut {
let executable = self.get_executable();
if with_valgrind {
let mut command = Command::new("valgrind");
command.args(&[
"--leak-check=full",
"--error-exitcode=1",
"--errors-for-leak-kinds=all",
"--errors-for-leak-kinds=definite,possible",
executable.to_str().unwrap(),
]);
run_command(command, &[])
if let Some(args) = app_args_opt {
command.args(args);
}
run_command(command, app_stdin_opt)
} else {
let command = Command::new(executable);
run_command(command, &[])
let mut command = Command::new(executable);
if let Some(args) = app_args_opt {
command.args(args);
}
run_command(command, app_stdin_opt)
}
}

View file

@ -13,7 +13,7 @@ pub fn parse_benchmark(c: &mut Criterion) {
path.push("examples");
path.push("cli");
path.push("false-interpreter");
path.push("False.roc");
path.push("main.roc");
let src = std::fs::read_to_string(&path).unwrap();
b.iter(|| {