From 35057538dc23fd59d15829cbe22552483726b485 Mon Sep 17 00:00:00 2001 From: Brend Smits Date: Fri, 31 Mar 2023 10:57:44 +0200 Subject: [PATCH 1/4] feat: add any-file-contents rule Signed-off-by: Brend Smits --- rules/any-file-contents-config.json | 30 ++++++ rules/any-file-contents.js | 21 ++++ rules/file-contents.js | 8 +- tests/rules/any_file_contents_tests.js | 137 +++++++++++++++++++++++++ 4 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 rules/any-file-contents-config.json create mode 100644 rules/any-file-contents.js create mode 100644 tests/rules/any_file_contents_tests.js diff --git a/rules/any-file-contents-config.json b/rules/any-file-contents-config.json new file mode 100644 index 00000000..786b5d7b --- /dev/null +++ b/rules/any-file-contents-config.json @@ -0,0 +1,30 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema", + "$id": "https://raw.githubusercontent.com/todogroup/repolinter/master/rules/any-file-contents-config.json", + "type": "object", + "properties": { + "nocase": { + "type": "boolean", + "default": false + }, + "globsAny": { + "type": "array", + "items": { "type": "string" } + }, + "branches": { + "type": "array", + "items": { "type": "string" }, + "default": [] + }, + "skipDefaultBranch": { "type": "boolean", "default": false }, + "content": { "type": "string" }, + "flags": { "type": "string" }, + "human-readable-content": { "type": "string" }, + "fail-on-non-existent": { + "type": "boolean", + "default": false + } + }, + "required": ["content"], + "oneOf": [{ "required": ["globsAny"] }, { "required": ["files"] }] +} diff --git a/rules/any-file-contents.js b/rules/any-file-contents.js new file mode 100644 index 00000000..87e79373 --- /dev/null +++ b/rules/any-file-contents.js @@ -0,0 +1,21 @@ +// Copyright 2017 TODO Group. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +// eslint-disable-next-line no-unused-vars +const Result = require('../lib/result') +// eslint-disable-next-line no-unused-vars +const FileSystem = require('../lib/file_system') +const fileContents = require('./file-contents') + +/** + * Check that at least one file within a list contains a regular expression. + * + * @param {FileSystem} fs A filesystem object configured with filter paths and target directories + * @param {object} options The rule configuration + * @returns {Promise} The lint rule result + */ +function anyFileContents(fs, options) { + return fileContents(fs, options, false, true) +} + +module.exports = anyFileContents diff --git a/rules/file-contents.js b/rules/file-contents.js index 89ed1a0c..1445260f 100644 --- a/rules/file-contents.js +++ b/rules/file-contents.js @@ -32,9 +32,9 @@ function getContext(matchedLine, regexMatch, contextLength) { * @returns {Promise} The lint rule result * @ignore */ -async function fileContents(fs, options, not = false) { +async function fileContents(fs, options, not = false, any = false) { // support legacy configuration keys - const fileList = options.globsAll || options.files + const fileList = (any ? options.globsAny : options.globsAll) || options.files const files = await fs.findAllFiles(fileList, !!options.nocase) const regexFlags = options.flags || '' @@ -272,7 +272,9 @@ async function fileContents(fs, options, not = false) { } const filteredResults = results.filter(r => r !== null) - const passed = !filteredResults.find(r => !r.passed) + const passed = any + ? filteredResults.some(r => r.passed) + : !filteredResults.find(r => !r.passed) return new Result('', filteredResults, passed) } diff --git a/tests/rules/any_file_contents_tests.js b/tests/rules/any_file_contents_tests.js new file mode 100644 index 00000000..0fb27cc6 --- /dev/null +++ b/tests/rules/any_file_contents_tests.js @@ -0,0 +1,137 @@ +// Copyright 2017 TODO Group. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +const chai = require('chai') +const expect = chai.expect +const FileSystem = require('../../lib/file_system') + +describe('rule', () => { + describe('any_file_contents', () => { + const anyFileContents = require('../../rules/any-file-contents') + + it('returns passes if requested file contents exists in exactly one file', async () => { + /** @type {any} */ + const mockfs = { + findAllFiles() { + return ['x.md', 'CONTRIBUTING.md', 'README.md'] + }, + getFileContents(file) { + return file === 'README.md' ? 'foo' : 'bar' + }, + targetDir: '.' + } + const ruleopts = { + globsAny: ['README*', 'x.md', 'CONTRIBUTING.md'], + content: '[abcdef][oO0][^q]' + } + + const actual = await anyFileContents(mockfs, ruleopts) + expect(actual.passed).to.equal(true) + expect(actual.targets).to.have.length(3) + expect(actual.targets[2]).to.deep.include({ + passed: true, + path: 'README.md' + }) + }) + + it('returns passes if requested file contents exists in two files', async () => { + /** @type {any} */ + const mockfs = { + findAllFiles() { + return ['x.md', 'CONTRIBUTING.md', 'README.md'] + }, + getFileContents(file) { + return file === 'README.md' ? 'bar' : 'foo' + }, + targetDir: '.' + } + const ruleopts = { + globsAny: ['README*', 'x.md', 'CONTRIBUTING.md'], + content: '[abcdef][oO0][^q]' + } + + const actual = await anyFileContents(mockfs, ruleopts) + + expect(actual.passed).to.equal(true) + expect(actual.targets).to.have.length(3) + expect(actual.targets[0]).to.deep.include({ + passed: true, + path: 'x.md' + }) + expect(actual.targets[1]).to.deep.include({ + passed: true, + path: 'CONTRIBUTING.md' + }) + expect(actual.targets[2]).to.deep.include({ + passed: false, + path: 'README.md' + }) + }) + + it('returns fails if the requested file contents does not exist in any file', async () => { + /** @type {any} */ + const mockfs = { + findAllFiles() { + return ['x.md', 'CONTRIBUTING.md', 'README.md'] + }, + getFileContents() { + return 'bar' + }, + targetDir: '.' + } + const ruleopts = { + globsAny: ['README*', 'x.md', 'CONTRIBUTING.md'], + content: '[abcdef][oO0][^q]' + } + + const actual = await anyFileContents(mockfs, ruleopts) + expect(actual.passed).to.equal(false) + expect(actual.targets).to.have.length(3) + expect(actual.targets[0]).to.deep.include({ + passed: false, + path: 'x.md' + }) + expect(actual.targets[1]).to.deep.include({ + passed: false, + path: 'CONTRIBUTING.md' + }) + }) + + it('returns failure if no file exists with failure flag enabled', async () => { + /** @type {any} */ + const mockfs = { + findAllFiles() { + return [] + }, + getFileContents() {}, + targetDir: '.' + } + + const ruleopts = { + globsAny: ['README.md', 'READMOI.md'], + content: 'foo', + 'fail-on-non-existent': true + } + + const actual = await anyFileContents(mockfs, ruleopts) + + expect(actual.passed).to.equal(false) + }) + + it('should handle broken symlinks', async () => { + const brokenSymlink = './tests/rules/broken_symlink_for_test' + const stat = require('fs').lstatSync(brokenSymlink) + expect(stat.isSymbolicLink()).to.equal(true) + const fs = new FileSystem(require('path').resolve('.')) + + const ruleopts = { + globsAny: [brokenSymlink], + lineCount: 1, + patterns: ['something'], + 'fail-on-non-existent': true + } + const actual = await anyFileContents(fs, ruleopts) + expect(actual.passed).to.equal(false) + }) + }) +}) From 100942384e0eb2fc257bd48a939f9ec1d9b73194 Mon Sep 17 00:00:00 2001 From: Brend Smits Date: Fri, 31 Mar 2023 11:01:50 +0200 Subject: [PATCH 2/4] remove branches config option as this is not implemented in this PR yet Signed-off-by: Brend Smits --- rules/any-file-contents-config.json | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/rules/any-file-contents-config.json b/rules/any-file-contents-config.json index 786b5d7b..8df22d95 100644 --- a/rules/any-file-contents-config.json +++ b/rules/any-file-contents-config.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema", - "$id": "https://raw.githubusercontent.com/todogroup/repolinter/master/rules/any-file-contents-config.json", + "$id": "https://raw.githubusercontent.com/todogroup/repolinter/main/rules/any-file-contents-config.json", "type": "object", "properties": { "nocase": { @@ -11,12 +11,6 @@ "type": "array", "items": { "type": "string" } }, - "branches": { - "type": "array", - "items": { "type": "string" }, - "default": [] - }, - "skipDefaultBranch": { "type": "boolean", "default": false }, "content": { "type": "string" }, "flags": { "type": "string" }, "human-readable-content": { "type": "string" }, From 913ce14880c22dc4b88d109cfade1b325109c116 Mon Sep 17 00:00:00 2001 From: Brend Smits Date: Fri, 31 Mar 2023 11:20:32 +0200 Subject: [PATCH 3/4] add rule to schema and rules file Co-authored-by: Dragos Serban Signed-off-by: Brend Smits --- docs/rules.md | 13 +++++++++++++ rules/any-file-contents-config.json | 2 +- rules/rules.js | 3 ++- rulesets/schema.json | 12 ++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/rules.md b/docs/rules.md index 7f040d38..3d3ce60f 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -25,6 +25,7 @@ Below is a complete list of rules that Repolinter can run, along with their conf - [`large-file`](#large-file) - [`license-detectable-by-licensee`](#license-detectable-by-licensee) - [`best-practices-badge-present`](#best-practices-badge-present) + - [`any-file-contents`](#any-file-contents) ## Reference @@ -229,3 +230,15 @@ Check Best Practices Badge is present in README. Optionally check a certain badg | Input | Required | Type | Default | Description | | ------------ | -------- | ---------- | ------- | ------------------------------------------------------------------ | | `minPercentage` | No | `integer` | `null` | Minimum [Tiered Percentage](https://github.com/coreinfrastructure/best-practices-badge/blob/main/doc/api.md#tiered-percentage-in-openssf-best-practices-badge) accomplished by project. `passing=100`, `silver=200`, `gold=300`, set to `0` or `null` to disable check. | + +### `any-file-contents` +Checks if the contents of at least one file in a given list match a given regular expression. + +| 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. | +| `content` | **Yes** | `string` | | The regular expression to check using `String#search`. This expression should not include the enclosing slashes and may need to be escaped to properly integrate with the JSON config (ex. `".+@.+\\..+"` for an email address). | +| `nocase` | No | `boolean` | `false` | Set to `true` to make the globs case insensitive. This does not effect the case sensitivity of the regular expression. | +| `flags` | No | `string` | `""` | The flags to use for the regular expression in `content` (ex. `"i"` for case insensitivity). | +| `human-readable-content` | No | `string` | The regular expression in `content` | The string to print instead of the regular expression when generating human-readable output. | +| `fail-on-non-existent` | No | `boolean` | `false` | Set to `true` to disable passing if no files are found from `globsAll`. | diff --git a/rules/any-file-contents-config.json b/rules/any-file-contents-config.json index 8df22d95..8248528f 100644 --- a/rules/any-file-contents-config.json +++ b/rules/any-file-contents-config.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema", - "$id": "https://raw.githubusercontent.com/todogroup/repolinter/main/rules/any-file-contents-config.json", + "$id": "https://raw.githubusercontent.com/todogroup/repolinter/master/rules/any-file-contents-config.json", "type": "object", "properties": { "nocase": { diff --git a/rules/rules.js b/rules/rules.js index 0acb0134..006e46e8 100644 --- a/rules/rules.js +++ b/rules/rules.js @@ -20,5 +20,6 @@ module.exports = { 'git-working-tree': require('./git-working-tree'), 'large-file': require('./large-file'), 'license-detectable-by-licensee': require('./license-detectable-by-licensee'), - 'json-schema-passes': require('./json-schema-passes') + 'json-schema-passes': require('./json-schema-passes'), + 'any-file-contents': require('./any-file-contents') } diff --git a/rulesets/schema.json b/rulesets/schema.json index 8c469454..3760e989 100644 --- a/rulesets/schema.json +++ b/rulesets/schema.json @@ -253,6 +253,18 @@ } } } + }, + { + "if": { + "properties": { "type": { "const": "any-file-contents" } } + }, + "then": { + "properties": { + "options": { + "$ref": "../rules/any-file-contents-config.json" + } + } + } } ] }, From 0c197ece65352075ab37f29cb0d8831b7446d6cb Mon Sep 17 00:00:00 2001 From: Brend Smits Date: Fri, 31 Mar 2023 15:15:01 +0200 Subject: [PATCH 4/4] add branches and skipDefaultBranch option Signed-off-by: Brend Smits --- rules/file-not-contents-config.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rules/file-not-contents-config.json b/rules/file-not-contents-config.json index 24f5fd1c..b0e8115a 100644 --- a/rules/file-not-contents-config.json +++ b/rules/file-not-contents-config.json @@ -18,6 +18,12 @@ "type": "string" } }, + "branches": { + "type": "array", + "items": { "type": "string" }, + "default": [] + }, + "skipDefaultBranch": { "type": "boolean", "default": false }, "flags": { "type": "string" }, "human-readable-content": { "type": "string" }, "fail-on-non-existent": {