forked from todogroup/repolinter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add file-not-exists rule (todogroup#183)
* feat: add file-not-exists rule
- Loading branch information
1 parent
ee913b3
commit ebca0b1
Showing
11 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"$id": "https://raw.githubusercontent.com/todogroup/repolinter/master/rules/file-not-exists-config.json", | ||
"type": "object", | ||
"properties": { | ||
"nocase": { | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"globsAll": { | ||
"type": "array", | ||
"items": { "type": "string" } | ||
}, | ||
"pass-message": { "type": "string" } | ||
}, | ||
"required": ["globsAll"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2017 TODO Group. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
const Result = require('../lib/result') | ||
// eslint-disable-next-line no-unused-vars | ||
const FileSystem = require('../lib/file_system') | ||
|
||
/** | ||
* Check if a file is not present in the repository. Fails on the first file | ||
* matching the glob pattern, succeeds if no file matching any of the patterns | ||
* is found. | ||
* | ||
* @param {FileSystem} fs A filesystem object configured with filter paths and target directories | ||
* @param {object} options The rule configuration | ||
* @returns {Promise<Result>} The lint rule result | ||
*/ | ||
async function fileNotExistence (fs, options) { | ||
const fileList = options.globsAll | ||
const file = options.dirs ? await fs.findAll(fileList, options.nocase) : await fs.findAllFiles(fileList, options.nocase) | ||
|
||
return file.length !== 0 | ||
? new Result('Found files', file.map(f => { return { passed: false, path: f } }), false) | ||
: new Result( | ||
`${options['pass-message'] !== undefined ? options['pass-message'] + '. ' : ''}Did not find a file matching the specified patterns`, | ||
fileList.map(f => { return { pattern: f, passed: true } }), | ||
true) | ||
} | ||
|
||
module.exports = fileNotExistence |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2017 TODO Group. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
const chai = require('chai') | ||
const expect = chai.expect | ||
|
||
describe('rule', () => { | ||
describe('files_not_exists', () => { | ||
const fileNotExists = require('../../rules/file-not-exists') | ||
|
||
it('returns a passed result if no files exist', async () => { | ||
/** @type {any} */ | ||
const mockfs = { | ||
findAllFiles () { | ||
return [] | ||
}, | ||
targetDir: '.' | ||
} | ||
|
||
const ruleopts = { | ||
globsAll: ['LICENSE*'] | ||
} | ||
|
||
const actual = await fileNotExists(mockfs, ruleopts) | ||
|
||
expect(actual.passed).to.equal(true) | ||
expect(actual.targets).to.have.length(1) | ||
expect(actual.targets[0].pattern).to.equal(ruleopts.globsAll[0]) | ||
}) | ||
|
||
it('returns a passed result if no directories or files exist', async () => { | ||
/** @type {any} */ | ||
const mockfs = { | ||
findAll () { | ||
return [] | ||
}, | ||
targetDir: '.' | ||
} | ||
|
||
const ruleopts = { | ||
globsAll: ['LICENSE*'], | ||
dirs: true | ||
} | ||
|
||
const actual = await fileNotExists(mockfs, ruleopts) | ||
|
||
expect(actual.passed).to.equal(true) | ||
expect(actual.targets).to.have.length(1) | ||
expect(actual.targets[0].pattern).to.equal(ruleopts.globsAll[0]) | ||
}) | ||
|
||
it('returns a failure result if requested file exists', async () => { | ||
/** @type {any} */ | ||
const mockfs = { | ||
findAllFiles () { | ||
return ['somefile'] | ||
}, | ||
targetDir: '.' | ||
} | ||
|
||
const ruleopts = { | ||
globsAll: ['LICENSE*'] | ||
} | ||
|
||
const actual = await fileNotExists(mockfs, ruleopts) | ||
|
||
expect(actual.passed).to.equal(false) | ||
expect(actual.targets).to.have.length(1) | ||
expect(actual.targets[0]).to.deep.include({ passed: false, path: 'somefile' }) | ||
}) | ||
|
||
it('returns a pass result if requested file doesn\'t exist with a pass message', async () => { | ||
/** @type {any} */ | ||
const mockfs = { | ||
findAllFiles () { | ||
return [] | ||
}, | ||
targetDir: '.' | ||
} | ||
|
||
const ruleopts = { | ||
globsAll: ['LICENSE*'], | ||
'pass-message': 'The license file should exist.' | ||
} | ||
|
||
const actual = await fileNotExists(mockfs, ruleopts) | ||
|
||
expect(actual.passed).to.equal(true) | ||
expect(actual.targets).to.have.length(1) | ||
expect(actual.targets[0].pattern).to.equal(ruleopts.globsAll[0]) | ||
expect(actual.message).to.contain(ruleopts['pass-message']) | ||
}) | ||
}) | ||
}) |