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: add any-file-contents rule #290

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Brend-Smits
Copy link
Collaborator

Motivation

The file-contents rule passes if all the files discovered include the content specified via a regular expression. However, for our use case we need to check whether the content is included in at least one of the discovered files.

Proposed Changes

Add any-file-contents rule, which mimics the functionality of the file-contents rule. However, the any-file-contents rule features a globsAny option, instead of globsAll.

To reduce code duplication, the fileContents method (which implements the logic of the file-contents rule) is extended with an optional boolean parameter (called any). If this parameter has the value true, then the rule passes if and only if there is at least one file with content matching the regex. Otherwise, the rule passes if and only if all files include content matching the regex.

Test Plan

  • it returns passes if requested file contents exists in exactly one file
  • it returns passes if requested file contents exists in two files
  • it returns fails if the requested file contents does not exist in any file
  • it returns failure if no file exists with failure flag enabled
  • it should handle broken symlinks

Original Pull Request on: philips-forks#18

// support legacy configuration keys
const fileList = options.globsAll || options.files
const fileList = (any ? options.globsAny : options.globsAll) || options.files
Copy link
Member

Choose a reason for hiding this comment

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

Let's not change the file-contents function parameter, keep the function always take globsAll and it matches the intention of if all of the files returned by the globs match the supplied string, or if no files are returned..

You can move this part to any-file-contents.js instead.


| Input | Required | Type | Default | Description |
| ------------------------ | -------- | ---------- | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `globsAny` | **Yes** | `string[]` | | A list of globs to get files for. This rule passes if at least one of the files returned by the globs match the supplied string, or if no files are returned. |
Copy link
Member

Choose a reason for hiding this comment

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

Quote:

However, for our use case we need to check whether the content is included in at least one of the discovered files.

I think it doesn't make sense for the rule to pass if no files are returned. It would make sense if the rule fails if no files are returned., since we are looking for at least there is one file has this thing there.

That means the fail-on-non-existent would be success-on-non-existent.

@hinricht
Copy link

hinricht commented Feb 5, 2024

We need a similar but different use case :)
We want to ensure that all files in a glob don't include a certain content.
Right now I didn't find a way to accomplish this.

@hinricht
Copy link

@Brend-Smits Ping, do you mind taking care of the feedback of @zhaoyuheng200 ? It would be nice to merge this !

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.

3 participants