Skip to content

Migrate patterns.js tests#20

Merged
fabiospampinato merged 15 commits intoprettier:mainfrom
pralkarz:patterns-tests
Feb 9, 2025
Merged

Migrate patterns.js tests#20
fabiospampinato merged 15 commits intoprettier:mainfrom
pralkarz:patterns-tests

Conversation

@pralkarz
Copy link
Contributor

@pralkarz pralkarz commented Jan 27, 2025

Please see the // MIGRATION comments on what changes were needed and questions regarding the implementation. Naturally I'm going to remove those when we finalize this PR.

The commented test cases are:

  1. Ones with negated patterns ! that don't seem supported any more? Maybe there's some flag that I missed?
  2. The one that expects to return 0 when there are no patterns passed. I expect the changed behavior (exit code 1 and an error message) is what we want. Should I adjust the test case?

The migrated test: https://github.com/prettier/prettier/blob/main/tests/integration/__tests__/patterns.js.
The migrated snapshots: https://github.com/prettier/prettier/blob/main/tests/integration/__tests__/__snapshots__/patterns.js.snap.
The migrated fixtures: https://github.com/prettier/prettier/tree/main/tests/integration/cli/patterns and https://github.com/prettier/prettier/tree/main/tests/integration/cli/patterns-special-characters.

Comment on lines 10 to 13
// MIGRATION: Error is no longer reported when there are other matches, even with
// `--error-on-unmatched-pattern`. Adjusted the stderr snapshot for now.
// Should we change the logic to report the invalid patterns regardless?
// Changed tha status to 1.
Copy link
Collaborator

@fabiospampinato fabiospampinato Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code in the rewrite we report an error only if nothing is actually matched, not if something is matched but it's subsequently ignored.

There may also be a difference here depending on how many patterns we are trying to match, like does the old CLI throw if any pattern is unmatched, or if every pattern in the list of patterns is unmatched? Worth checking which one it is before deciding what to do here.

Either case worth investigating aligning with the original implementation unless we have a reasonable choice not to, maybe this one is fine, but if we can align it's better to be aligned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case in the old CLI returns 2, the matches for directory/**/*.js in stdout, and [error] No files matching the pattern were found: "non-existent.js". in stderr. The new version is kinda counter-intuitive, as it returns 1, but doesn't write anything to stderr, because we got some matches from the valid pattern. The code responsible for this is on lines 200-204 in index.ts.

Copy link
Collaborator

@fabiospampinato fabiospampinato Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we do want to return an error when a single pattern isn't finding any non ignored files within the list of patterns, right? 🤔 Or is whether the found files are ignored or not not a factor in the old CLI?

Copy link
Contributor Author

@pralkarz pralkarz Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's consider a case where there are two patterns, one that generates some matches, and one that doesn't. In my opinion, the latter isn't indicative of a failure – there simply aren't any files that match the pattern, and that's fine. So I think the best course of action would be to return 0 (indicating success), write the matches to stdout, and write nothing to stderr.

In this paradigm (not sure whether it's the right word here), even having a single pattern that doesn't match anything wouldn't be a failure – it would simply write nothing to stdout and stderr, and return 0. We should only write to stderr (in both one pattern and two patterns cases) when we set the --error-on-unmatched-pattern flag, also including which pattern(s) didn't match in the error message.

What do you think? It might not be as simple as I wrote down here as from what I see the CLI is very strict when it comes to returning 0, as in there are many edge cases that need to be met for that to happen. Not sure how significant of a refactor would be needed to loosen it up. This is naturally assuming that you agree with my proposal here, which you don't have to. I spent a lot less time in this project than you, so I'm definitely missing some key ideas and details.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have as much context as I had when I wrote this code, but it looks like erroring on unmatched patters is the default behavior in v3 and v4.

I think what you are proposing makes sense to me, but since this aims to be a largely backwards compatible rewrite (though not fully) I think if we can align with v3 it seems better for our goals with this 🤔

I don't remember what the difference is exactly here, we only error if the sum of all globs didn't find any matches, while v3 errors if any globs didn't find any matches? If so that's a bit problematic to handle as we would probably want some extra metadata for this from tiny-readdir-glob, to avoid searching with each glob individually and then merging their results, but it should be doable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a comment here and investigate v3 vs v4 (and decide whether to add extra metadata to tiny-readdir-glob) in a separate PR so we could land this one? From when I first worked on this, I seem to remember there were some problems with --error-on-unmatched-pattern too, so it would make sense to research within a smaller scope.

@fabiospampinato
Copy link
Collaborator

Probably worth linking to the source files for reference in the future.

@fabiospampinato
Copy link
Collaborator

fabiospampinato commented Jan 27, 2025

Ones with negated patterns ! that don't seem supported any more? Maybe there's some flag that I missed?

It sounds like there may be a bug in tiny-readdir-glob around that, worth looking closer into this, we probably shouldn't break support for that.

The one that expects to return 0 when there are no patterns passed. I expect the changed behavior (exit code 1 and an error message) is what we want. Should I adjust the test case?

Yeah seems fine, no patterns no success.

@pralkarz
Copy link
Contributor Author

pralkarz commented Jan 27, 2025

It sounds like there may be a bug in tiny-readdir-glob around that, worth looking closer into this, we probably shouldn't break support for that.

I'm down to look into that throughout the week, unless you'd like to handle it? I see there are no tests for ! patterns in tiny-readdir-glob, so maybe it's not implemented as it wasn't needed previously.

@fabiospampinato
Copy link
Collaborator

fabiospampinato commented Jan 27, 2025

It sounds like there may be a bug in tiny-readdir-glob around that, worth looking closer into this, we probably shouldn't break support for that.

I'm down to look into that throughout the week, unless you'd like to handle it? I see there are no tests for ! patterns in tiny-readdir-glob, so maybe it's not implemented as it wasn't needed previously.

I'll take any help available, but even just making a failing test that reproduces the problem is 80% toward solving it. But also feel free to leave it to me if you think your time is better spent on another area.

@pralkarz
Copy link
Contributor Author

It sounds like there may be a bug in tiny-readdir-glob around that, worth looking closer into this, we probably shouldn't break support for that.

I'm down to look into that throughout the week, unless you'd like to handle it? I see there are no tests for ! patterns in tiny-readdir-glob, so maybe it's not implemented as it wasn't needed previously.

I'll take any help available, but even just making a failing test that reproduces the problem is 80% toward solving it. But also feel free to leave it to me if you think your time is better spent on another area.

That's what I wanted to start with – a failing test case. I'm gonna go ahead and tentatively put this on my plate. In case something more urgent pops out throughout the week and I won't be able to look into it, I'll let you know either here or in the Discord server.

@fabiospampinato fabiospampinato force-pushed the main branch 4 times, most recently from 393ff5f to 11afbb2 Compare February 9, 2025 17:45
@pralkarz
Copy link
Contributor Author

pralkarz commented Feb 9, 2025

After updating tiny-readdir-glob to the latest version, everything passes except for one accordingly commented test. Currently discussing the solution in Discord.

Notes:

  1. The exit code 1 matches v3's behavior, but I'm not a fan of it being returned when we checked some files, but nothing was written. In my opinion, most test cases in this file should return 0.
  2. The test case with a non-existent pattern still needs some discussions and decisions on how to handle it.

@pralkarz pralkarz marked this pull request as ready for review February 9, 2025 19:10
@fabiospampinato fabiospampinato merged commit 3f76bda into prettier:main Feb 9, 2025
1 of 2 checks passed
@fabiospampinato
Copy link
Collaborator

Thanks!

@pralkarz pralkarz deleted the patterns-tests branch February 9, 2025 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants