Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: test match only with filenames #165

Merged
merged 6 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/cli_bin/fixtures/match_filename/.grit/grit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: 0.0.1
patterns:
- name: rewrite_filename
body: |
engine marzano(0.1)
language markdown

file($name, $body) where {
$name <: `index.js`
}
samples:
- input: |
// @filename: index.js

console.log('hello')
output: |
// @filename: index.js

console.log('hello')
- input: |
// @filename: main.js

// This should be unmatched
33 changes: 33 additions & 0 deletions crates/cli_bin/tests/patterns_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,36 @@ fn tests_patterns_with_foreign_function_call_from_dot_grit_lib() -> Result<()> {
assert!(output.status.success());
Ok(())
}

#[test]
fn checks_non_matching_yaml_sample() -> Result<()> {
let (_temp_dir, dir) = get_fixture("yaml_unmatched", false)?;

let mut test = get_test_cmd()?;
test.arg("patterns")
.arg("test")
.current_dir(dir);

let output = test.output()?;

assert!(output.status.success());

Ok(())
}


#[test]
fn tests_match_only_with_file_name() -> Result<()> {
let (_temp_dir, dir) = get_fixture("match_filename", false)?;

let mut test = get_test_cmd()?;
test.arg("patterns")
.arg("test")
.current_dir(dir);

let output = test.output()?;

assert!(output.status.success());

Ok(())
}
16 changes: 0 additions & 16 deletions crates/cli_bin/tests/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,3 @@ fn lists_imported_patterns() -> Result<()> {

Ok(())
}

#[test]
fn checks_non_matching_yaml_sample() -> Result<()> {
let (_temp_dir, dir) = get_fixture("yaml_unmatched", false)?;

let mut test = get_test_cmd()?;
test.arg("patterns")
.arg("test")
.current_dir(dir);

let output = test.output()?;

assert!(output.status.success());

Ok(())
}
12 changes: 9 additions & 3 deletions crates/gritmodule/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub fn test_pattern_sample(
});
}
MatchResult::Match(r) => {
if sample.input.contains("// @filename:") {
if is_multifile_sample(&sample.input) {
continue;
}
raw_actual_outputs.push(RichFile {
Expand All @@ -239,7 +239,7 @@ pub fn test_pattern_sample(
if raw_actual_outputs.is_empty() {
if sample.output.is_none() {
return SampleTestResult::new_passing(matches, false);
} else {
} else if !is_multifile_sample(&sample.input) {
return SampleTestResult {
matches,
state: GritTestResultState::FailedMatch,
Expand Down Expand Up @@ -268,7 +268,9 @@ pub fn test_pattern_sample(

let mut raw_expected_outputs = infer_rich_files_from_content(&compiled.language, sample_output);

if raw_actual_outputs.len() < raw_expected_outputs.len() && compiled.is_multifile {
if raw_actual_outputs.len() < raw_expected_outputs.len()
&& is_multifile_sample(&sample.input)
{
for file in rich_files.iter() {
if raw_actual_outputs.iter().any(|f| f.path == file.path) {
continue;
Expand Down Expand Up @@ -327,6 +329,10 @@ pub fn test_pattern_sample(
}
}

fn is_multifile_sample(input: &str) -> bool {
input.contains("// @filename:")
}

#[derive(Debug)]
pub enum MismatchInfo {
Path(OutputInfo),
Expand Down
Loading