Skip to content

Commit

Permalink
fix: 423 missing file message (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-ley-scrub authored Oct 24, 2024
1 parent e5d3023 commit 4642258
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crates/cli_bin/fixtures/pattern_non_existent/.grit/grit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: 0.0.1
patterns:
- file: patterns/non_existent.md
21 changes: 21 additions & 0 deletions crates/cli_bin/tests/patterns_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,27 @@ fn fails_recursion() -> Result<()> {
Ok(())
}

#[test]
fn fails_to_find_non_existent_file() -> Result<()> {
let (_temp_dir, temp_fixture_path) = get_fixture("pattern_non_existent", false)?;

let mut cmd = get_test_cmd()?;
cmd.arg("patterns")
.arg("test")
.current_dir(&temp_fixture_path);
let output = cmd.output()?;
let stdout = String::from_utf8(output.stdout)?;
let stderr = String::from_utf8(output.stderr)?;
println!("stdout: {}", stdout);
println!("stderr: {}", stderr);

assert!(
stderr.contains("Failed to find pattern at .grit/patterns/non_existent.md. Does it exist?")
);

Ok(())
}

#[test]
fn test_multifile_fails_if_unchanged_file_has_incorrect_expected() -> Result<()> {
let (_temp_dir, fixture_dir) = get_fixture("test_multifile_fail", true)?;
Expand Down
13 changes: 10 additions & 3 deletions crates/gritmodule/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ pub async fn get_patterns_from_file(
ext: PatternFileExt,
overrides: GritDefinitionOverrides,
) -> Result<Vec<ModuleGritPattern>> {
let repo_root = find_repo_root_from(path.clone()).await?;
let content = fs::read_to_string(&path).await?;
let path_str = path.to_string_lossy();
let repo_root = find_repo_root_from(path.clone()).await.context(format!(
"Failed to find repository root from path {}",
path_str
))?;
let content = fs::read_to_string(&path).await.context(format!(
"Failed to find pattern at {}. Does it exist?",
extract_relative_path(&path_str, &repo_root)
))?;
let mut file = RichFile {
path: path.to_string_lossy().to_string(),
path: path_str.to_string(),
content,
};
ext.get_patterns(&mut file, &source_module, &repo_root, overrides)
Expand Down

0 comments on commit 4642258

Please sign in to comment.