diff --git a/src/sanitize-argument.ts b/src/sanitize-argument.ts index 97236b626..0bc3a0b8f 100644 --- a/src/sanitize-argument.ts +++ b/src/sanitize-argument.ts @@ -10,14 +10,18 @@ import * as QueryStringParser from 'querystring' import { isValidEmail } from './util/index.js' import * as path from 'path' import { fileURLToPath } from 'url' +import { parameterDescriptions } from './parameterList.js' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) +const parametersWithArrayType = ['format'] export async function sanitize_all(argv: any) { // extracting all arguments const { articleList, addNamespaces, speed: _speed, adminEmail, mwUrl, customZimFavicon, optimisationCacheUrl, verbose, customZimLongDescription, customZimDescription } = argv + sanitizeDoubleUsedParameters(argv) + sanitize_articlesList_addNamespaces(articleList, addNamespaces) // sanitizing verbose @@ -99,6 +103,15 @@ export function sanitize_articlesList_addNamespaces(articlesList: string, addNam } } +export function sanitizeDoubleUsedParameters(options: object) { + const parameterKeys = Object.keys(parameterDescriptions) + for (const [optionKey, optionValue] of Object.entries(options)) { + if (parameterKeys.includes(optionKey) && !parametersWithArrayType.includes(optionKey) && Array.isArray(optionValue)) { + throw new Error(`Parameter '--${optionKey}' can only be used once`) + } + } +} + export function sanitize_speed(_speed: any) { if (_speed && isNaN(_speed)) { throw new Error('speed is not a number, please give a number value to --speed.') diff --git a/test/e2e/bm.e2e.test.ts b/test/e2e/bm.e2e.test.ts index fc4f693ea..a18870808 100644 --- a/test/e2e/bm.e2e.test.ts +++ b/test/e2e/bm.e2e.test.ts @@ -31,7 +31,7 @@ describe('bm', () => { for (const dump of outFiles) { if (dump.nopic) { // nopic has enough files - expect(dump.status.files.success).toBeGreaterThan(16) + expect(dump.status.files.success).toBeGreaterThan(15) // nopic has enough redirects expect(dump.status.redirects.written).toBeGreaterThan(170) // nopic has enough articles diff --git a/test/e2e/en10.e2e.test.ts b/test/e2e/en10.e2e.test.ts index f7eef572e..d28dbbcd9 100644 --- a/test/e2e/en10.e2e.test.ts +++ b/test/e2e/en10.e2e.test.ts @@ -34,7 +34,7 @@ describe('en10', () => { for (const dump of outFiles) { if (dump.nopic) { // nopic has enough files - expect(dump.status.files.success).toBeGreaterThan(18) + expect(dump.status.files.success).toBeGreaterThan(17) expect(dump.status.files.success).toBeLessThan(25) // nopic has enough redirects expect(dump.status.redirects.written).toBeGreaterThan(480) diff --git a/test/unit/sanitize-argument.test.ts b/test/unit/sanitize-argument.test.ts new file mode 100644 index 000000000..0b9c69122 --- /dev/null +++ b/test/unit/sanitize-argument.test.ts @@ -0,0 +1,47 @@ +import { sanitize_all } from '../../src/sanitize-argument.js' + +describe('Sanitize parameters', () => { + test('sanitizing usage of the same parameter more than one time', async () => { + // equivalent to command: node lib/cli.js --verbose --mwUrl="https://en.wikipedia.org" --adminEmail="test@test.test" --verbose=info + const twoVerboseParameters = { + _: [], + verbose: [true, 'info'], + mwUrl: 'https://en.wikipedia.org', + 'mw-url': 'https://en.wikipedia.org', + adminEmail: 'test@test.test', + 'admin-email': 'test@test.test', + $0: 'node_modules/ts-node/dist/child/child-entrypoint.js', + } + + await expect(sanitize_all(twoVerboseParameters)).rejects.toThrow(/Parameter '--verbose' can only be used once/) + + // equivalent to command: node lib/cli.js --verbose --mwUrl="https://en.wikipedia.org" --adminEmail="test@test.test" --mwUrl="https://en.wikipedia.org" + const twoUrlParameters = { + _: [], + verbose: true, + mwUrl: ['https://en.wikipedia.org', 'https://en.wikipedia.org'], + 'mw-url': ['https://en.wikipedia.org', 'https://en.wikipedia.org'], + adminEmail: 'test@test.test', + 'admin-email': 'test@test.test', + $0: 'node_modules/ts-node/dist/child/child-entrypoint.js', + } + + await expect(sanitize_all(twoUrlParameters)).rejects.toThrow(/Parameter '--mwUrl' can only be used once/) + + // equivalent to command: node lib/cli.js --verbose=info --adminEmail="est@test.test" --articleList="User:Kelson/MWoffliner_CI_reference" --mwUrl="https://en.m.wikipedia.org/" --format=nopic --format=nopdf --format=novid + const threeFormatParameters = { + _: [], + verbose: 'info', + adminEmail: 'test@test.test', + 'admin-email': 'test@test.test', + articleList: 'User:Kelson/MWoffliner_CI_reference', + 'article-list': 'User:Kelson/MWoffliner_CI_reference', + mwUrl: 'https://en.m.wikipedia.org/', + 'mw-url': 'https://en.m.wikipedia.org/', + format: ['nopic', 'nopdf', 'novid'], + $0: 'node_modules/ts-node/dist/child/child-entrypoint.js', + } + + expect(await sanitize_all(threeFormatParameters)).toBeUndefined() + }) +})