Skip to content

Commit

Permalink
cli tests refactoring progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-4 committed Oct 11, 2024
1 parent 3bad18d commit 0659abd
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 242 deletions.
362 changes: 172 additions & 190 deletions crates/cli/tests/cli_tests.rs

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions crates/cli/tests/snapshots/cli_run__cli_run__fibonacci.snap

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions crates/cli/tests/snapshots/cli_run__cli_run__quicksort.snap

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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`:

Expand Down
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
22 changes: 13 additions & 9 deletions crates/cli_test_utils/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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| {
Expand Down Expand Up @@ -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) {
Expand Down
40 changes: 26 additions & 14 deletions crates/cli_test_utils/src/exec_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/parse/benches/bench_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand Down

0 comments on commit 0659abd

Please sign in to comment.