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.
- Loading branch information
1 parent
fd62187
commit 184b787
Showing
9 changed files
with
276 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 fileExistence (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 = fileExistence |
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,113 @@ | ||
// Copyright 2017 TODO Group. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
const chai = require('chai') | ||
const expect = chai.expect | ||
|
||
describe('fixes', () => { | ||
describe('file-remove', () => { | ||
const fileRemove = require('../../fixes/file-remove') | ||
|
||
it('removes a file', async () => { | ||
const removePaths = [] | ||
/** @type {any} */ | ||
const mockFs = { | ||
removeFile (path) { | ||
removePaths.push(path) | ||
} | ||
} | ||
|
||
const res = await fileRemove(mockFs, {}, ['myfile'], false) | ||
expect(res.passed).to.equal(true) | ||
expect(res.targets).to.have.length(1) | ||
expect(res.targets[0].passed).to.equal(true) | ||
expect(res.targets[0].path).to.equal('myfile') | ||
expect(removePaths).to.deep.equal(['myfile']) | ||
}) | ||
|
||
it('does nothing if dryRun is true', async () => { | ||
const removePaths = [] | ||
/** @type {any} */ | ||
const mockFs = { | ||
removeFile (path) { | ||
removePaths.push(path) | ||
} | ||
} | ||
|
||
const res = await fileRemove(mockFs, {}, ['myfile'], true) | ||
expect(res.passed).to.equal(true) | ||
expect(res.targets).to.have.length(1) | ||
expect(res.targets[0].passed).to.equal(true) | ||
expect(res.targets[0].path).to.equal('myfile') | ||
expect(removePaths).to.deep.equal([]) | ||
}) | ||
|
||
it('removes multiple files', async () => { | ||
const removePaths = [] | ||
/** @type {any} */ | ||
const mockFs = { | ||
removeFile (path) { | ||
removePaths.push(path) | ||
} | ||
} | ||
|
||
const res = await fileRemove(mockFs, {}, ['myfile', 'otherfile'], false) | ||
expect(res.passed).to.equal(true) | ||
expect(res.targets).to.have.length(2) | ||
expect(res.targets[0].passed).to.equal(true) | ||
expect(res.targets[0].path).to.equal('myfile') | ||
expect(res.targets[1].passed).to.equal(true) | ||
expect(res.targets[1].path).to.equal('otherfile') | ||
expect(removePaths).to.deep.equal(['myfile', 'otherfile']) | ||
}) | ||
|
||
it('uses the glob option', async () => { | ||
const removePaths = [] | ||
/** @type {any} */ | ||
const mockFs = { | ||
removeFile (path) { | ||
removePaths.push(path) | ||
}, | ||
findAllFiles () { | ||
return ['myfile.txt'] | ||
} | ||
} | ||
|
||
const res = await fileRemove(mockFs, { globsAll: ['myfile'] }, [], false) | ||
expect(res.passed).to.equal(true) | ||
expect(res.targets).to.have.length(1) | ||
expect(res.targets[0].passed).to.equal(true) | ||
expect(res.targets[0].path).to.equal('myfile.txt') | ||
expect(removePaths).to.deep.equal(['myfile.txt']) | ||
}) | ||
|
||
it('overrides targets with the glob option', async () => { | ||
const removePaths = [] | ||
/** @type {any} */ | ||
const mockFs = { | ||
removeFile (path) { | ||
removePaths.push(path) | ||
}, | ||
findAllFiles () { | ||
return ['myfile.txt'] | ||
} | ||
} | ||
|
||
const res = await fileRemove(mockFs, { globsAll: ['myfile'] }, ['otherfile'], false) | ||
expect(res.passed).to.equal(true) | ||
expect(res.targets).to.have.length(1) | ||
expect(res.targets[0].passed).to.equal(true) | ||
expect(res.targets[0].path).to.equal('myfile.txt') | ||
expect(removePaths).to.deep.equal(['myfile.txt']) | ||
}) | ||
|
||
it('returns failure if no files are found', async () => { | ||
/** @type {any} */ | ||
const mockFs = {} | ||
|
||
const res = await fileRemove(mockFs, {}, [], false) | ||
expect(res.passed).to.equal(false) | ||
expect(res.targets).to.have.length(0) | ||
}) | ||
}) | ||
}) |
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']) | ||
}) | ||
}) | ||
}) |