Skip to content

Commit

Permalink
strip-ansi-escapes
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Apr 20, 2024
1 parent 7945e69 commit 34e8cb2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
24 changes: 12 additions & 12 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ library FooLib {

cmd.arg("build");

assert!(cmd.stdout_lossy().ends_with(
assert!(cmd.stdout_lossy_strip_ansi().ends_with(
"
Compiler run successful!
"
Expand Down Expand Up @@ -697,7 +697,7 @@ contract A {
.unwrap();

cmd.args(["build", "--force"]);
let out = cmd.stdout_lossy();
let out = cmd.stdout_lossy_strip_ansi();
// expect no warning as path is ignored
assert!(out.contains("Compiler run successful!"));
assert!(!out.contains("Compiler run successful with warnings:"));
Expand All @@ -706,7 +706,7 @@ contract A {
// need to reset empty error codes as default would set some error codes
prj.write_config(Config { ignored_error_codes: vec![], ..Default::default() });

let out = cmd.stdout_lossy();
let out = cmd.stdout_lossy_strip_ansi();
// expect warnings as path is not ignored
assert!(out.contains("Compiler run successful with warnings:"), "{out}");
assert!(out.contains("Warning") && out.contains("SPDX-License-Identifier"), "{out}");
Expand All @@ -731,15 +731,15 @@ contract A {
.unwrap();

cmd.args(["build", "--force"]);
let out = cmd.stdout_lossy();
let out = cmd.stdout_lossy_strip_ansi();
// no warnings
assert!(out.contains("Compiler run successful!"));
assert!(!out.contains("Compiler run successful with warnings:"));

// don't ignore errors
let config = Config { ignored_error_codes: vec![], ..Default::default() };
prj.write_config(config);
let out = cmd.stdout_lossy();
let out = cmd.stdout_lossy_strip_ansi();

assert!(out.contains("Compiler run successful with warnings:"), "{out}");
assert!(out.contains("Warning") && out.contains("SPDX-License-Identifier"), "{out}");
Expand All @@ -763,7 +763,7 @@ contract A {

// there are no errors
cmd.args(["build", "--force"]);
let out = cmd.stdout_lossy();
let out = cmd.stdout_lossy_strip_ansi();
assert!(out.contains("Compiler run successful with warnings:"), "{out}");

// warning fails to compile
Expand All @@ -778,7 +778,7 @@ contract A {
..Default::default()
};
prj.write_config(config);
let out = cmd.stdout_lossy();
let out = cmd.stdout_lossy_strip_ansi();

assert!(out.contains("Compiler run successful!"));
assert!(!out.contains("Compiler run successful with warnings:"));
Expand Down Expand Up @@ -1077,7 +1077,7 @@ contract CounterCopy is Counter {

// build and check output
cmd.forge_fuse().arg("build");
let output = cmd.stdout_lossy();
let output = cmd.stdout_lossy_strip_ansi();
assert!(output.contains("Compiler run successful",));
}
);
Expand Down Expand Up @@ -1501,7 +1501,7 @@ forgetest_init!(can_use_absolute_imports, |prj, cmd| {
.unwrap();

cmd.arg("build");
let stdout = cmd.stdout_lossy();
let stdout = cmd.stdout_lossy_strip_ansi();
assert!(stdout.contains("Compiler run successful"));
});

Expand Down Expand Up @@ -1544,7 +1544,7 @@ contract MyTest is IMyTest {}
.unwrap();

cmd.arg("build");
let stdout = cmd.stdout_lossy();
let stdout = cmd.stdout_lossy_strip_ansi();
assert!(stdout.contains("Compiler run successful"), "{stdout}");
});

Expand Down Expand Up @@ -1631,7 +1631,7 @@ forgetest_init!(can_build_sizes_repeatedly, |prj, cmd| {
prj.clear_cache();

cmd.args(["build", "--sizes"]);
let out = cmd.stdout_lossy();
let out = cmd.stdout_lossy_strip_ansi();

// contains: Counter ┆ 0.247 ┆ 24.329
assert!(out.contains(TEMPLATE_CONTRACT));
Expand All @@ -1648,7 +1648,7 @@ forgetest_init!(can_build_names_repeatedly, |prj, cmd| {
prj.clear_cache();

cmd.args(["build", "--names"]);
let out = cmd.stdout_lossy();
let out = cmd.stdout_lossy_strip_ansi();

assert!(out.contains(TEMPLATE_CONTRACT));

Expand Down
8 changes: 4 additions & 4 deletions crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ contract Greeter {}

cmd.arg("build");

assert!(cmd.stdout_lossy().contains("Compiler run successful!"));
assert!(cmd.stdout_lossy_strip_ansi().contains("Compiler run successful!"));
});

// tests that `--use <solc>` works
Expand All @@ -343,13 +343,13 @@ contract Foo {}
.unwrap();

cmd.args(["build", "--use", OTHER_SOLC_VERSION]);
let stdout = cmd.stdout_lossy();
let stdout = cmd.stdout_lossy_strip_ansi();
assert!(stdout.contains("Compiler run successful"));

cmd.forge_fuse()
.args(["build", "--force", "--use", &format!("solc:{OTHER_SOLC_VERSION}")])
.root_arg();
let stdout = cmd.stdout_lossy();
let stdout = cmd.stdout_lossy_strip_ansi();
assert!(stdout.contains("Compiler run successful"));

// fails to use solc that does not exist
Expand All @@ -361,7 +361,7 @@ contract Foo {}
.unwrap()
.expect("solc is installed");
cmd.forge_fuse().args(["build", "--force", "--use"]).arg(local_solc.solc).root_arg();
let stdout = cmd.stdout_lossy();
let stdout = cmd.stdout_lossy_strip_ansi();
assert!(stdout.contains("Compiler run successful"));
});

Expand Down
10 changes: 9 additions & 1 deletion crates/test-utils/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,14 @@ impl TestCommand {
lossy_string(&self.output().stdout)
}

/// Executes the command and returns the stderr as lossy `String`.
///
/// **Note**: This function checks whether the command was successful.
#[track_caller]
pub fn stdout_lossy_strip_ansi(&mut self) -> String {
strip_ansi_escapes::strip_str(lossy_string(&self.output().stdout))
}

/// Executes the command and returns the stderr as lossy `String`.
///
/// **Note**: This function does **not** check whether the command was successful.
Expand Down Expand Up @@ -1131,7 +1139,7 @@ pub fn dir_list<P: AsRef<Path>>(dir: P) -> Vec<String> {
}

fn lossy_string(bytes: &[u8]) -> String {
strip_ansi_escapes::strip_str(String::from_utf8_lossy(bytes).replace("\r\n", "\n"))
String::from_utf8_lossy(bytes).replace("\r\n", "\n")
}

#[cfg(test)]
Expand Down

0 comments on commit 34e8cb2

Please sign in to comment.