From 7b567e0d18e44d0f214e591b8eb39f7892c3f29b Mon Sep 17 00:00:00 2001 From: Alex Ley Date: Thu, 24 Oct 2024 23:45:55 +0100 Subject: [PATCH] test(apply): add `apply_to_path_with_invalid_lang()` see #485 --- crates/cli_bin/tests/apply.rs | 28 +++++++++++++++++++ ...pply__apply_to_path_with_invalid_lang.snap | 14 ++++++++++ 2 files changed, 42 insertions(+) create mode 100644 crates/cli_bin/tests/snapshots/apply__apply_to_path_with_invalid_lang.snap diff --git a/crates/cli_bin/tests/apply.rs b/crates/cli_bin/tests/apply.rs index 663528d00..6a27c8ca2 100644 --- a/crates/cli_bin/tests/apply.rs +++ b/crates/cli_bin/tests/apply.rs @@ -2969,6 +2969,34 @@ fn apply_to_path_with_invalid_javascript_extension() -> Result<()> { Ok(()) } +/// test that we show an 'Error parsing source code' when we try to apply +/// to a path which contains the wrong language as specified in the lang flag +/// see https://github.com/getgrit/gritql/issues/485 +#[test] +fn apply_to_path_with_invalid_lang() -> Result<()> { + let (_temp_dir, fixture_dir) = get_fixture("invalid_extensions", false)?; + + let mut cmd = get_test_cmd()?; + cmd.arg("apply") + .arg("`object` => ``") + .arg("file4.js.py") + .arg("--lang=py") + .arg("--force") + .current_dir(&fixture_dir); + + let result = cmd.output()?; + + let stderr = String::from_utf8(result.stderr)?; + println!("stderr: {:?}", stderr); + let stdout = String::from_utf8(result.stdout)?; + println!("stdout: {:?}", stdout); + + assert!(result.status.success(), "Command failed"); + assert_snapshot!(stdout); + + Ok(()) +} + /// Ban multiple stdin paths #[test] fn apply_stdin_two_paths() -> Result<()> { diff --git a/crates/cli_bin/tests/snapshots/apply__apply_to_path_with_invalid_lang.snap b/crates/cli_bin/tests/snapshots/apply__apply_to_path_with_invalid_lang.snap new file mode 100644 index 000000000..3bfd1a400 --- /dev/null +++ b/crates/cli_bin/tests/snapshots/apply__apply_to_path_with_invalid_lang.snap @@ -0,0 +1,14 @@ +--- +source: crates/cli_bin/tests/apply.rs +expression: stdout +--- +file4.js.py: ERROR (code: 300) - Error parsing source code at 1:7 in file4.js.py. This may cause otherwise applicable queries to not match. +file4.js.py + -const mine = (object) => { + - return String(object) + 'is mine'; + +const mine = () => { + + return String() + 'is mine'; + }; + + +Processed 1 files and found 2 matches