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.
Merge pull request #19 from philips-forks/feature/add-file-or-directo…
…ry-exists-rule Add file-or-directory-existence rule
- Loading branch information
Showing
6 changed files
with
191 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"$id": "https://raw.githubusercontent.com/todogroup/repolinter/master/rules/file-or-directory-existence-config.json", | ||
"type": "object", | ||
"properties": { | ||
"nocase": { | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"globsAny": { | ||
"type": "array", | ||
"items": { "type": "string" } | ||
}, | ||
"fail-message": { "type": "string" } | ||
}, | ||
"oneOf": [ | ||
{ "required": ["globsAny"] } | ||
] | ||
} | ||
|
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,39 @@ | ||
// Copyright 2017 TODO Group. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
const Result = require('../lib/result') | ||
|
||
async function fileOrDirectoryExistence(fs, options) { | ||
const fileOrDirectoryExists = await fs.findFirst( | ||
options.globsAny, | ||
options.nocase | ||
) | ||
|
||
const passed = !!fileOrDirectoryExists | ||
|
||
return passed | ||
? new Result( | ||
'', | ||
[ | ||
{ | ||
passed: true, | ||
path: fileOrDirectoryExists, | ||
message: 'Found file or directory matching the specified patterns' | ||
} | ||
], | ||
true | ||
) | ||
: new Result( | ||
`${ | ||
options['fail-message'] !== undefined | ||
? options['fail-message'] + '. ' | ||
: '' | ||
}Did not find a file or directory matching the specified patterns`, | ||
options.globsAny.map(f => { | ||
return { passed: false, pattern: f } | ||
}), | ||
false | ||
) | ||
} | ||
|
||
module.exports = fileOrDirectoryExistence |
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,107 @@ | ||
// Copyright 2017 TODO Group. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
const chai = require('chai') | ||
const path = require('path') | ||
const FileSystem = require('../../lib/file_system') | ||
|
||
const expect = chai.expect | ||
|
||
describe('rule', () => { | ||
describe('file_or_directory_existence', () => { | ||
const fileOrDirectoryExistence = require('../../rules/file-existence') | ||
const fs = new FileSystem(path.resolve('./tests/rules'), []) | ||
|
||
it('returns a passed result if both files and directories exist matching the given pattern', async () => { | ||
/** @type {any} */ | ||
const ruleoptsImageFirst = { | ||
globsAny: ['image_for_test.png', 'markup_test_files/'] | ||
} | ||
|
||
const ruleoptsDirFirst = { | ||
globsAny: ['markup_test_files/', 'image_for_test.png'] | ||
} | ||
|
||
const actualImageFirst = await fileOrDirectoryExistence( | ||
fs, | ||
ruleoptsImageFirst | ||
) | ||
|
||
expect(actualImageFirst.passed).to.equal(true) | ||
expect(actualImageFirst.targets).to.have.length(1) | ||
expect(actualImageFirst.targets[0]).to.deep.include({ | ||
passed: true, | ||
path: 'image_for_test.png' | ||
}) | ||
|
||
const actualDirFirst = await fileOrDirectoryExistence( | ||
fs, | ||
ruleoptsDirFirst | ||
) | ||
|
||
expect(actualDirFirst.passed).to.equal(true) | ||
expect(actualDirFirst.targets).to.have.length(1) | ||
expect(actualDirFirst.targets[0]).to.deep.include({ | ||
passed: true, | ||
path: 'markup_test_files/' | ||
}) | ||
}) | ||
|
||
it('returns a passed result if only files exist matching the given pattern', async () => { | ||
/** @type {any} */ | ||
const ruleopts = { | ||
globsAny: ['markup_test_files_nonexistent/', 'image_for_test.png'] | ||
} | ||
|
||
const actual = await fileOrDirectoryExistence(fs, ruleopts) | ||
|
||
expect(actual.passed).to.equal(true) | ||
expect(actual.targets).to.have.length(1) | ||
expect(actual.targets[0]).to.deep.include({ | ||
passed: true, | ||
path: 'image_for_test.png' | ||
}) | ||
}) | ||
|
||
it('returns a passed result if only directories exist matching the given pattern', async () => { | ||
/** @type {any} */ | ||
const ruleopts = { | ||
globsAny: [ | ||
'image_for_test_nonexistent.png', | ||
'another_nonexistent_image.jpg', | ||
'markup_test_files/' | ||
] | ||
} | ||
|
||
const actual = await fileOrDirectoryExistence(fs, ruleopts) | ||
|
||
expect(actual.passed).to.equal(true) | ||
expect(actual.targets).to.have.length(1) | ||
expect(actual.targets[0]).to.deep.include({ | ||
passed: true, | ||
path: 'markup_test_files/' | ||
}) | ||
}) | ||
|
||
it('returns a failed result if neither files or directories exist matching the given pattern', async () => { | ||
/** @type {any} */ | ||
const ruleopts = { | ||
globsAny: [ | ||
'image_for_test_nonexistent.png', | ||
'a_nonexistent_directory/', | ||
'another_nonexistent_image.jpg', | ||
'markup_test_files_nonexistent/' | ||
] | ||
} | ||
|
||
const actual = await fileOrDirectoryExistence(fs, ruleopts) | ||
|
||
expect(actual.passed).to.equal(false) | ||
expect(actual.targets).to.have.length(4) | ||
expect(actual.targets[0]).to.deep.include({ | ||
passed: false, | ||
pattern: 'image_for_test_nonexistent.png' | ||
}) | ||
}) | ||
}) | ||
}) |