From 6ea7d63380f2bcf210855b2b317cba09065cfcb1 Mon Sep 17 00:00:00 2001 From: ayazhafiz Date: Thu, 13 Jan 2022 20:56:55 -0500 Subject: [PATCH] Make sure line offsets are generated properly for external programs Closes #2329 --- cli/tests/cli_run.rs | 55 +++++++++++++++++++++++++++++-- cli/tests/known_bad/TypeError.roc | 11 +++++++ cli/tests/known_bad/platform | 1 + cli_utils/src/helpers.rs | 13 ++++++++ compiler/build/src/program.rs | 2 +- 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 cli/tests/known_bad/TypeError.roc create mode 120000 cli/tests/known_bad/platform diff --git a/cli/tests/cli_run.rs b/cli/tests/cli_run.rs index e190e33981..3cfdd7f0a1 100644 --- a/cli/tests/cli_run.rs +++ b/cli/tests/cli_run.rs @@ -6,12 +6,16 @@ extern crate roc_collections; extern crate roc_load; extern crate roc_module; +#[macro_use] +extern crate indoc; + #[cfg(test)] mod cli_run { use cli_utils::helpers::{ - example_file, examples_dir, extract_valgrind_errors, fixture_file, run_cmd, run_roc, - run_with_valgrind, ValgrindError, ValgrindErrorXWhat, + example_file, examples_dir, extract_valgrind_errors, fixture_file, known_bad_file, run_cmd, + run_roc, run_with_valgrind, ValgrindError, ValgrindErrorXWhat, }; + use roc_test_utils::assert_multiline_str_eq; use serial_test::serial; use std::path::{Path, PathBuf}; @@ -44,6 +48,27 @@ mod cli_run { use_valgrind: bool, } + fn strip_colors(str: &str) -> String { + use roc_reporting::report::*; + str.replace(RED_CODE, "") + .replace(WHITE_CODE, "") + .replace(BLUE_CODE, "") + .replace(YELLOW_CODE, "") + .replace(GREEN_CODE, "") + .replace(CYAN_CODE, "") + .replace(MAGENTA_CODE, "") + .replace(RESET_CODE, "") + .replace(BOLD_CODE, "") + .replace(UNDERLINE_CODE, "") + } + + fn check_compile_error(file: &Path, flags: &[&str], expected: &str) { + let compile_out = run_roc(&[&["check", file.to_str().unwrap()], &flags[..]].concat()); + let err = compile_out.stdout.trim(); + let err = strip_colors(&err); + assert_multiline_str_eq!(err, expected.into()); + } + fn check_output_with_stdin( file: &Path, stdin: &[&str], @@ -740,6 +765,32 @@ mod cli_run { true, ); } + + #[test] + fn known_type_error() { + check_compile_error( + &known_bad_file("TypeError.roc"), + &[], + indoc!( + r#" + ── UNRECOGNIZED NAME ─────────────────────────────────────────────────────────── + + I cannot find a `d` value + + 10│ _ <- await (line d) + ^ + + Did you mean one of these? + + U8 + Ok + I8 + F64 + + ────────────────────────────────────────────────────────────────────────────────"# + ), + ); + } } #[allow(dead_code)] diff --git a/cli/tests/known_bad/TypeError.roc b/cli/tests/known_bad/TypeError.roc new file mode 100644 index 0000000000..9a36916ff4 --- /dev/null +++ b/cli/tests/known_bad/TypeError.roc @@ -0,0 +1,11 @@ +app "type-error" + packages { pf: "platform" } + imports [ pf.Stdout.{ line }, pf.Task.{ await } ] + provides [ main ] to pf + +main = + _ <- await (line "a") + _ <- await (line "b") + _ <- await (line "c") + _ <- await (line d) + line "e" diff --git a/cli/tests/known_bad/platform b/cli/tests/known_bad/platform new file mode 120000 index 0000000000..79724c8631 --- /dev/null +++ b/cli/tests/known_bad/platform @@ -0,0 +1 @@ +../../../examples/cli/platform \ No newline at end of file diff --git a/cli_utils/src/helpers.rs b/cli_utils/src/helpers.rs index ffd0bcafb9..8167fe38eb 100644 --- a/cli_utils/src/helpers.rs +++ b/cli_utils/src/helpers.rs @@ -294,6 +294,19 @@ pub fn fixture_file(dir_name: &str, file_name: &str) -> PathBuf { path } +#[allow(dead_code)] +pub fn known_bad_file(file_name: &str) -> PathBuf { + let mut path = root_dir(); + + // Descend into cli/tests/known_bad/{file_name} + path.push("cli"); + path.push("tests"); + path.push("known_bad"); + path.push(file_name); + + path +} + #[allow(dead_code)] pub fn repl_eval(input: &str) -> Out { let mut cmd = Command::new(path_to_roc_binary()); diff --git a/compiler/build/src/program.rs b/compiler/build/src/program.rs index 98ea9de4ce..13c54c7005 100644 --- a/compiler/build/src/program.rs +++ b/compiler/build/src/program.rs @@ -82,7 +82,7 @@ fn report_problems_help( src_lines.extend(src.split('\n')); } - let lines = LineInfo::new(src); + let lines = LineInfo::new(&src_lines.join("\n")); // Report parsing and canonicalization problems let alloc = RocDocAllocator::new(&src_lines, *home, interns);