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-remove fix (todogroup#181)
- Loading branch information
1 parent
45ee596
commit ee913b3
Showing
6 changed files
with
181 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"$id": "https://raw.githubusercontent.com/todogroup/repolinter/master/fixes/file-remove-config.json", | ||
"type": "object", | ||
"properties": { | ||
"globsAll": { | ||
"type": "array", | ||
"items": { "type": "string" } | ||
}, | ||
"nocase": { "type": "boolean", "default": false } | ||
} | ||
} |
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,35 @@ | ||
// 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') | ||
|
||
/** | ||
* Removes a file or a list of files. | ||
* | ||
* @param {FileSystem} fs A filesystem object configured with filter paths and target directories | ||
* @param {object} options The rule configuration | ||
* @param {string[]} targets The files to modify (will be overridden by options if present) | ||
* @param {boolean} dryRun If true, repolinter will report suggested fixes, but will make no disk modifications. | ||
* @returns {Promise<Result>} The fix result | ||
*/ | ||
async function fileRemove (fs, options, targets, dryRun = false) { | ||
// overwrite the targets with the files specified by options | ||
if (options.globsAll && options.globsAll.length) { | ||
targets = await fs.findAllFiles(options.globsAll, !!options.nocase) | ||
} | ||
// check that any targets exist | ||
if (targets.length === 0) { | ||
return new Result('Found no files to remove', [], false) | ||
} | ||
// remove the files | ||
if (!dryRun) { | ||
await Promise.all(targets.map(async t => fs.removeFile(t))) | ||
} | ||
// create a result | ||
const removeTargets = targets.map(t => { return { passed: true, path: t, message: 'Remove file' } }) | ||
return new Result('', removeTargets, true) | ||
} | ||
|
||
module.exports = fileRemove |
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 |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
|
||
module.exports = [ | ||
'file-create', | ||
'file-modify' | ||
'file-modify', | ||
'file-remove' | ||
] |
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) | ||
}) | ||
}) | ||
}) |