diff --git a/compiler/reporting/src/report.rs b/compiler/reporting/src/report.rs index 484a5a6b3e..1dd09c7def 100644 --- a/compiler/reporting/src/report.rs +++ b/compiler/reporting/src/report.rs @@ -103,12 +103,15 @@ fn newline() -> ReportText { plain_text("\n") } +pub const RED_CODE: &str = "\u{001b}[31m"; +pub const WHITE_CODE: &str = "\u{001b}[31m"; + fn red(str: &str) -> String { let mut buf = String::new(); - buf.push_str("\u{001b}[31m"); + buf.push_str(RED_CODE); buf.push_str(str); - buf.push_str("\u{001b}[0m"); + buf.push_str(RESET_CODE); buf } @@ -116,14 +119,14 @@ fn red(str: &str) -> String { fn white(str: &str) -> String { let mut buf = String::new(); - buf.push_str("\u{001b}[31m"); + buf.push_str(WHITE_CODE); buf.push_str(str); - buf.push_str(RESET); + buf.push_str(RESET_CODE); buf } -const RESET: &str = "\u{001b}[0m"; +pub const RESET_CODE: &str = "\u{001b}[0m"; impl ReportText { /// Render to CI console output, where no colors are available. diff --git a/compiler/reporting/tests/test_reporting.rs b/compiler/reporting/tests/test_reporting.rs index 87f07a800f..7454d7812c 100644 --- a/compiler/reporting/tests/test_reporting.rs +++ b/compiler/reporting/tests/test_reporting.rs @@ -11,7 +11,9 @@ mod helpers; mod test_report { use crate::helpers::test_home; use roc_module::symbol::{Interns, ModuleId}; - use roc_reporting::report::{can_problem, plain_text, Report, ReportText, DEFAULT_PALETTE}; + use roc_reporting::report::{ + can_problem, plain_text, Report, ReportText, DEFAULT_PALETTE, RED_CODE, RESET_CODE, + }; use roc_types::pretty_print::name_all_type_vars; use roc_types::subs::Subs; use roc_types::types; @@ -82,6 +84,12 @@ mod test_report { assert_eq!(buf, expected_rendering); } + fn human_readable(str: &str) -> String { + return str + .replace(RED_CODE, "") + .replace(RESET_CODE, ""); + } + fn report_renders_in_color_from_src(src: &str, report: Report, expected_rendering: &str) { let (_type_problems, _can_problems, mut subs, home, interns) = infer_expr_help(src); let mut buf: String = String::new(); @@ -96,7 +104,7 @@ mod test_report { DEFAULT_PALETTE, ); - assert_eq!(buf, expected_rendering); + assert_eq!(human_readable(&buf), expected_rendering); } fn report_renders_in_color(report: Report, expected_rendering: &str) { @@ -245,10 +253,7 @@ mod test_report { #[test] fn report_in_color() { - report_renders_in_color( - to_simple_report(plain_text("y")), - "\u{001b}[31my\u{001b}[0m", - ); + report_renders_in_color(to_simple_report(plain_text("y")), "y"); } #[test]