Skip to content

Commit

Permalink
Merge pull request #1823 from openzim/sanitize-double-used-cli-parameter
Browse files Browse the repository at this point in the history
Stop script and throw error if used same cli parameter twice
  • Loading branch information
kelson42 authored Apr 10, 2023
2 parents 8674287 + 4ff19c6 commit 6b9260a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/sanitize-argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.')
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/bm.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/en10.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions test/unit/sanitize-argument.test.ts
Original file line number Diff line number Diff line change
@@ -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="[email protected]" --verbose=info
const twoVerboseParameters = {
_: [],
verbose: [true, 'info'],
mwUrl: 'https://en.wikipedia.org',
'mw-url': 'https://en.wikipedia.org',
adminEmail: '[email protected]',
'admin-email': '[email protected]',
$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="[email protected]" --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: '[email protected]',
'admin-email': '[email protected]',
$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="[email protected]" --articleList="User:Kelson/MWoffliner_CI_reference" --mwUrl="https://en.m.wikipedia.org/" --format=nopic --format=nopdf --format=novid
const threeFormatParameters = {
_: [],
verbose: 'info',
adminEmail: '[email protected]',
'admin-email': '[email protected]',
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()
})
})

0 comments on commit 6b9260a

Please sign in to comment.