This commit is contained in:
William Durand 2024-06-18 22:26:23 +02:00 коммит произвёл GitHub
Родитель 47cf339657
Коммит d27ac18bf6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
8 изменённых файлов: 1 добавлений и 238 удалений

Просмотреть файл

@ -69,9 +69,6 @@ const linter = linter.createInstance({
output: 'none',
boring: false,
selfHosted: false,
// Lint only the selected files
// scanFile: ['path/...', ...]
//
// Exclude files:
shouldScanFile: (fileName) => true,
},
@ -92,7 +89,6 @@ linter.run()
summary: {
error, notice, warning,
},
scanFile,
count,
error: [{
type: "error",

Просмотреть файл

@ -27,14 +27,6 @@ export default class Collector {
}
_addMessage(type, opts, _Message = Message) {
// Filter the messages reported by file when the Linter has been configured
// with a custom scanFile array using --scan-file CLI option.
if (this.config.scanFile && opts.file) {
if (!this.config.scanFile.some((v) => v === opts.file)) {
return;
}
}
// Message will throw for incorrect types.
// we have a test to ensure that is the case.
const message = new _Message(type, opts);

Просмотреть файл

@ -46,18 +46,6 @@ export default class Linter {
set config(cfg) {
this._config = cfg;
// normalize the scanFile option:
// convert into an array if needed and filter out any undefined
// or empty strings.
if (this._config.scanFile) {
let scanFile = Array.isArray(this._config.scanFile)
? this._config.scanFile
: [this._config.scanFile];
scanFile = scanFile.filter((el) => el && el.length > 0);
this._config.scanFile = scanFile;
}
}
get config() {
@ -267,11 +255,6 @@ export default class Linter {
}
});
if (this.output.scanFile) {
out.push(`Selected files: ${this.output.scanFile.join(', ')}`);
out.push('');
}
return out.join('\n');
}
@ -282,15 +265,12 @@ export default class Linter {
metadata: this.addonMetadata,
};
if (this.config.scanFile) {
output.scanFile = this.config.scanFile;
}
constants.MESSAGE_TYPES.forEach((type) => {
const messageType = `${type}s`;
output[messageType] = this.collector[messageType];
output.summary[messageType] = this.collector[messageType].length;
});
return output;
}
@ -540,18 +520,6 @@ export default class Linter {
return this.config.shouldScanFile(fileOrDirName, isDir);
}
if (this.config.scanFile) {
const manifestFileNames = ['manifest.json', 'package.json'];
// Always scan sub directories and the manifest files,
// or the linter will not be able to detect the addon type.
if (isDir || manifestFileNames.includes(fileOrDirName)) {
return true;
}
return this.config.scanFile.some((v) => v === fileOrDirName);
}
// Defaults to true.
return true;
}
@ -561,14 +529,6 @@ export default class Linter {
await this.extractMetadata(deps);
const files = await this.io.getFiles();
if (
this.config.scanFile &&
!this.config.scanFile.some((f) => Object.keys(files).includes(f))
) {
const _files = this.config.scanFile.join(', ');
throw new Error(`Selected file(s) not found: ${_files}`);
}
// Known libraries do not need to be scanned
const filesWithoutJSLibraries = Object.keys(files).filter((file) => {
return !Object.prototype.hasOwnProperty.call(

Просмотреть файл

@ -72,12 +72,6 @@ const options = {
default: 3,
requiresArg: true,
},
'scan-file': {
alias: ['f'],
describe: 'Scan a selected file',
type: 'string',
requiresArg: true,
},
'disable-linter-rules': {
describe: 'Disable list of comma separated eslint rules',
type: 'string',

Просмотреть файл

@ -20,7 +20,6 @@ describe('getDefaultConfigValue()', () => {
});
it('should return undefined', () => {
expect(getDefaultConfigValue('scan-file')).toBeUndefined();
expect(getDefaultConfigValue('disable-linter-rules')).toBeUndefined();
});

Просмотреть файл

@ -54,23 +54,6 @@ describe('Basic CLI tests', function cliCallback() {
expect(args.disableXpiAutoclose).toEqual(false);
});
it('should support a --scan-file option', () => {
let args = cli.parse(['foo/', '--scan-file', 'dir/test1.txt']);
expect(args.scanFile).toEqual('dir/test1.txt');
args = cli.parse([
'foo/',
'--scan-file',
'dir/test1.txt',
'--scan-file',
'dir/test2.txt',
]);
expect(args.scanFile).toEqual(['dir/test1.txt', 'dir/test2.txt']);
args = cli.parse(['foo/']);
expect(args.scanFile).toEqual(undefined);
});
it('should default warnings-as-errors to false', () => {
const args = cli.parse(['foo/bar.zip']);
expect(args.warningsAsErrors).toEqual(false);

Просмотреть файл

@ -139,64 +139,6 @@ describe('Collector', () => {
expect(collection.notices.length).toEqual(0);
});
it('should filter message by filename if config.scanFile is defined', () => {
const collection = new Collector({
scanFile: ['test.js', 'no-match-file.js'],
});
expect(collection.length).toEqual(0);
// Test linting error without a file.
collection.addError({
...fakeMessageData,
});
expect(collection.length).toEqual(1);
expect(collection.errors.length).toEqual(1);
expect(collection.warnings.length).toEqual(0);
expect(collection.notices.length).toEqual(0);
expect(collection.errors[0].code).toEqual(fakeMessageData.code);
// Test linting error with an excluded file.
collection.addError({
...fakeMessageData,
file: 'non-test.js',
});
expect(collection.length).toEqual(1);
// Test linting error with an included file.
collection.addError({
...fakeMessageData,
file: 'test.js',
});
expect(collection.length).toEqual(2);
// Test filtered warnings.
collection.addWarning({
...fakeMessageData,
file: 'test.js',
});
expect(collection.length).toEqual(3);
// Test filtered notices.
collection.addNotice({
...fakeMessageData,
file: 'test.js',
});
expect(collection.length).toEqual(4);
expect(collection.errors.length).toEqual(2);
expect(collection.warnings.length).toEqual(1);
expect(collection.notices.length).toEqual(1);
expect(collection.errors[1].code).toEqual(fakeMessageData.code);
expect(collection.errors[1].file).toEqual('test.js');
expect(collection.warnings[0].code).toEqual(fakeMessageData.code);
expect(collection.warnings[0].file).toEqual('test.js');
expect(collection.notices[0].code).toEqual(fakeMessageData.code);
expect(collection.notices[0].file).toEqual('test.js');
});
it('should throw when getting messages for an undefined instancePath', () => {
const collection = new Collector();
expect(() => {

Просмотреть файл

@ -184,28 +184,10 @@ describe('Linter', () => {
);
});
it('should optionally scan a single file', async () => {
const addonLinter = new Linter({
_: ['tests/fixtures/webextension_scan_file'],
scanFile: ['subdir/test.js'],
});
// Stub print to prevent output.
addonLinter.print = sinon.stub();
const getFileSpy = sinon.spy(addonLinter, 'scanFile');
await addonLinter.scan();
sinon.assert.callOrder(
getFileSpy.withArgs('manifest.json'),
getFileSpy.withArgs('subdir/test.js')
);
});
it.each(['mjs', 'jsm'])('should scan %s files', async (fileExtension) => {
const filename = `file.${fileExtension}`;
const addonLinter = new Linter({
_: ['tests/fixtures/webextension_scan_file'],
scanFile: [filename],
});
// Stub print to prevent output.
addonLinter.print = sinon.stub();
@ -249,43 +231,6 @@ describe('Linter', () => {
});
});
it('should optionally scan selected files', async () => {
const addonLinter = new Linter({
_: ['tests/fixtures/webextension_scan_file'],
scanFile: ['subdir/test.js', 'subdir/test2.js'],
});
// Stub print to prevent output.
addonLinter.print = sinon.stub();
const getFileSpy = sinon.spy(addonLinter, 'scanFile');
await addonLinter.scan();
// There is no guarantee that test.js and test2.js will always
// be scanned in the same order (the order will depend from the
// order of the files returned by the getFiles method from the
// IOBase subclass, part of the addons-scanner-utils) and asserting
// a specific calls order here was making this test to fail
// intermittently.
sinon.assert.calledWith(getFileSpy, 'manifest.json');
sinon.assert.calledWith(getFileSpy, 'subdir/test.js');
sinon.assert.calledWith(getFileSpy, 'subdir/test2.js');
});
it('should raise an error if selected file are not found', async () => {
const files = ['subdir/test3.js', 'subdir/test4.js'];
const addonLinter = new Linter({
_: ['tests/fixtures/webextension_scan_file'],
scanFile: files,
});
// Stub print to prevent output.
addonLinter.print = sinon.stub();
await expect(addonLinter.scan()).rejects.toThrow(
`Selected file(s) not found: ${files.join(', ')}`
);
});
it('should throw when message.type is undefined', async () => {
const addonLinter = new Linter({ _: ['tests/fixtures/webextension.zip'] });
addonLinter.io = { files: { whatever: {} } };
@ -498,26 +443,6 @@ describe('Linter.print()', () => {
sinon.assert.notCalled(addonLinter.toJSON);
sinon.assert.notCalled(fakeConsole.log);
});
it('should print scanFile if any', () => {
const addonLinter = new Linter({
_: ['foo'],
scanFile: ['testfile.js'],
});
const textOutputSpy = sinon.spy(addonLinter, 'textOutput');
addonLinter.config.output = 'text';
let logData = '';
const fakeConsole = {
// eslint-disable-next-line no-return-assign
log: sinon.spy((...args) => (logData += `${args.join(' ')}\n`)),
};
addonLinter.print(fakeConsole);
sinon.assert.calledOnce(textOutputSpy);
sinon.assert.calledOnce(fakeConsole.log);
expect(logData).toContain('Selected files: testfile.js');
});
});
describe('Linter.toJSON()', () => {
@ -1740,34 +1665,6 @@ describe('Linter.run()', () => {
expect(FakeXpi.closeWasCalled).toEqual(true);
});
it('should not close the IO object when auto-close feature is enabled (default) and selected files are missing', async () => {
const addonLinter = new Linter({
_: ['tests/fixtures/webextension.zip'],
scanFile: ['subdir/test.js'],
disableXpiAutoclose: false,
});
await expect(addonLinter.run({ _Xpi: FakeXpi })).rejects.toThrow(
/Selected file\(s\) not found/
);
expect(FakeXpi.closeWasCalled).toEqual(false);
});
it('should close the IO object when auto-close feature is disabled and selected files are missing', async () => {
const addonLinter = new Linter({
_: ['tests/fixtures/webextension.zip'],
scanFile: ['subdir/test.js'],
disableXpiAutoclose: true,
});
await expect(addonLinter.run({ _Xpi: FakeXpi })).rejects.toThrow(
/Selected file\(s\) not found/
);
expect(FakeXpi.closeWasCalled).toEqual(true);
});
it('should work with empty ZIP files', async () => {
const addonLinter = new Linter({
_: ['tests/fixtures/empty.zip'],