Skip to content

Commit 065188d

Browse files
Gozalarpl
authored andcommitted
feat: Allow negation in --ignore-files (#1348)
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
1 parent 1cd88a1 commit 065188d

File tree

7 files changed

+76
-9
lines changed

7 files changed

+76
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
"firefox-profile": "1.1.0",
6565
"fx-runner": "1.0.9",
6666
"git-rev-sync": "1.9.1",
67-
"minimatch": "3.0.4",
6867
"mkdirp": "0.5.1",
68+
"multimatch": "2.1.0",
6969
"mz": "2.7.0",
7070
"node-notifier": "5.2.1",
7171
"opn": "5.3.0",

src/util/file-filter.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22
import path from 'path';
33

4-
import minimatch from 'minimatch';
4+
import multimatch from 'multimatch';
55

66
import {createLogger} from './logger';
77

@@ -88,7 +88,12 @@ export class FileFilter {
8888
*/
8989
addToIgnoreList(files: Array<string>) {
9090
for (const file of files) {
91-
this.filesToIgnore.push(this.resolveWithSourceDir(file));
91+
if (file.charAt(0) === '!') {
92+
const resolvedFile = this.resolveWithSourceDir(file.substr(1));
93+
this.filesToIgnore.push(`!${resolvedFile}`);
94+
} else {
95+
this.filesToIgnore.push(this.resolveWithSourceDir(file));
96+
}
9297
}
9398
}
9499

@@ -104,12 +109,10 @@ export class FileFilter {
104109
*/
105110
wantFile(filePath: string): boolean {
106111
const resolvedPath = this.resolveWithSourceDir(filePath);
107-
for (const test of this.filesToIgnore) {
108-
if (minimatch(resolvedPath, test)) {
109-
log.debug(
110-
`FileFilter: ignoring file ${resolvedPath} (it matched ${test})`);
111-
return false;
112-
}
112+
const matches = multimatch(resolvedPath, this.filesToIgnore);
113+
if (matches.length > 0) {
114+
log.debug(`FileFilter: ignoring file ${resolvedPath}`);
115+
return false;
113116
}
114117
return true;
115118
}

tests/fixtures/minimal-web-ext/.private-file1.txt

Whitespace-only changes.

tests/fixtures/minimal-web-ext/node_modules/pkg1/file1.txt

Whitespace-only changes.

tests/fixtures/minimal-web-ext/node_modules/pkg2/file2.txt

Whitespace-only changes.

tests/unit/test-cmd/test.build.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,40 @@ describe('build', () => {
372372
});
373373
});
374374

375+
it('zips a package and includes a file from a negated filter', () => {
376+
const zipFile = new ZipFile();
377+
378+
return withTempDir(
379+
(tmpDir) =>
380+
build({
381+
sourceDir: fixturePath('minimal-web-ext'),
382+
artifactsDir: tmpDir.path(),
383+
ignoreFiles: [
384+
'!node_modules',
385+
'!node_modules/pkg1',
386+
'!node_modules/pkg1/**',
387+
],
388+
})
389+
.then((buildResult) => {
390+
assert.match(buildResult.extensionPath,
391+
/minimal_extension-1\.0\.zip$/);
392+
return buildResult.extensionPath;
393+
})
394+
.then((extensionPath) => zipFile.open(extensionPath))
395+
.then(() => zipFile.extractFilenames())
396+
.then((fileNames) => {
397+
fileNames.sort();
398+
assert.deepEqual(fileNames, [
399+
'background-script.js', 'manifest.json',
400+
'node_modules/',
401+
'node_modules/pkg1/',
402+
'node_modules/pkg1/file1.txt',
403+
]);
404+
return zipFile.close();
405+
})
406+
);
407+
});
408+
375409
describe('safeFileName', () => {
376410

377411
it('makes names safe for writing to a file system', () => {

tests/unit/test-util/test.file-filter.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,34 @@ describe('util/file-filter', () => {
131131
});
132132
});
133133

134+
describe('negation', () => {
135+
const filter = newFileFilter({
136+
sourceDir: '/src',
137+
ignoreFiles: [
138+
'!node_modules/libdweb/src/**',
139+
],
140+
});
141+
142+
it('ignore paths not captured by negation', () => {
143+
assert.equal(filter.wantFile('/src/node_modules/lib/foo.js'), false);
144+
assert.equal(filter.wantFile('/src/node_modules/lib'), false);
145+
assert.equal(filter.wantFile('/src/node_modules/what.js'), false);
146+
assert.equal(filter.wantFile('/src/node_modules/libdweb/what.js'), false);
147+
assert.equal(filter.wantFile('/src/node_modules/libdweb/src.js'), false);
148+
assert.equal(filter.wantFile('/src/node_modules/libdweb/src'), false);
149+
});
150+
151+
it('includes paths captured by negation', () => {
152+
assert.equal(
153+
filter.wantFile('/src/node_modules/libdweb/src/lib.js'),
154+
true);
155+
assert.equal(
156+
filter.wantFile('/src/node_modules/libdweb/src/sub/lib.js'),
157+
true);
158+
assert.equal(
159+
filter.wantFile('/src/node_modules/libdweb/src/node_modules/lib.js'),
160+
true);
161+
});
162+
});
163+
134164
});

0 commit comments

Comments
 (0)