Skip to content

Commit

Permalink
feat: Allow negation in --ignore-files (#1348)
Browse files Browse the repository at this point in the history
Fix #1347

* test: Add test for negated ignore path
* feat: Allow negation in --ignore-files
* swaps minimatch for multimatch to allow opt-in inclusion of node_modules subset
* test: exercise the build command with negated ignore
  • Loading branch information
Gozala authored and rpl committed Jul 24, 2018
1 parent 1cd88a1 commit 065188d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
"firefox-profile": "1.1.0",
"fx-runner": "1.0.9",
"git-rev-sync": "1.9.1",
"minimatch": "3.0.4",
"mkdirp": "0.5.1",
"multimatch": "2.1.0",
"mz": "2.7.0",
"node-notifier": "5.2.1",
"opn": "5.3.0",
Expand Down
19 changes: 11 additions & 8 deletions src/util/file-filter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */
import path from 'path';

import minimatch from 'minimatch';
import multimatch from 'multimatch';

import {createLogger} from './logger';

Expand Down Expand Up @@ -88,7 +88,12 @@ export class FileFilter {
*/
addToIgnoreList(files: Array<string>) {
for (const file of files) {
this.filesToIgnore.push(this.resolveWithSourceDir(file));
if (file.charAt(0) === '!') {
const resolvedFile = this.resolveWithSourceDir(file.substr(1));
this.filesToIgnore.push(`!${resolvedFile}`);
} else {
this.filesToIgnore.push(this.resolveWithSourceDir(file));
}
}
}

Expand All @@ -104,12 +109,10 @@ export class FileFilter {
*/
wantFile(filePath: string): boolean {
const resolvedPath = this.resolveWithSourceDir(filePath);
for (const test of this.filesToIgnore) {
if (minimatch(resolvedPath, test)) {
log.debug(
`FileFilter: ignoring file ${resolvedPath} (it matched ${test})`);
return false;
}
const matches = multimatch(resolvedPath, this.filesToIgnore);
if (matches.length > 0) {
log.debug(`FileFilter: ignoring file ${resolvedPath}`);
return false;
}
return true;
}
Expand Down
Empty file.
Empty file.
Empty file.
34 changes: 34 additions & 0 deletions tests/unit/test-cmd/test.build.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,40 @@ describe('build', () => {
});
});

it('zips a package and includes a file from a negated filter', () => {
const zipFile = new ZipFile();

return withTempDir(
(tmpDir) =>
build({
sourceDir: fixturePath('minimal-web-ext'),
artifactsDir: tmpDir.path(),
ignoreFiles: [
'!node_modules',
'!node_modules/pkg1',
'!node_modules/pkg1/**',
],
})
.then((buildResult) => {
assert.match(buildResult.extensionPath,
/minimal_extension-1\.0\.zip$/);
return buildResult.extensionPath;
})
.then((extensionPath) => zipFile.open(extensionPath))
.then(() => zipFile.extractFilenames())
.then((fileNames) => {
fileNames.sort();
assert.deepEqual(fileNames, [
'background-script.js', 'manifest.json',
'node_modules/',
'node_modules/pkg1/',
'node_modules/pkg1/file1.txt',
]);
return zipFile.close();
})
);
});

describe('safeFileName', () => {

it('makes names safe for writing to a file system', () => {
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test-util/test.file-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,34 @@ describe('util/file-filter', () => {
});
});

describe('negation', () => {
const filter = newFileFilter({
sourceDir: '/src',
ignoreFiles: [
'!node_modules/libdweb/src/**',
],
});

it('ignore paths not captured by negation', () => {
assert.equal(filter.wantFile('/src/node_modules/lib/foo.js'), false);
assert.equal(filter.wantFile('/src/node_modules/lib'), false);
assert.equal(filter.wantFile('/src/node_modules/what.js'), false);
assert.equal(filter.wantFile('/src/node_modules/libdweb/what.js'), false);
assert.equal(filter.wantFile('/src/node_modules/libdweb/src.js'), false);
assert.equal(filter.wantFile('/src/node_modules/libdweb/src'), false);
});

it('includes paths captured by negation', () => {
assert.equal(
filter.wantFile('/src/node_modules/libdweb/src/lib.js'),
true);
assert.equal(
filter.wantFile('/src/node_modules/libdweb/src/sub/lib.js'),
true);
assert.equal(
filter.wantFile('/src/node_modules/libdweb/src/node_modules/lib.js'),
true);
});
});

});

0 comments on commit 065188d

Please sign in to comment.