From b77a8ef9782b682eddcd3abb2870d14e727d1d5f Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Sat, 9 Nov 2024 13:36:50 +0530 Subject: [PATCH] chore(util): validate all get mode optionsc --- src/util.js | 20 +++++++++++------ tests/specs/util.test.js | 46 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/util.js b/src/util.js index 0d636f1e..a7ee6e16 100644 --- a/src/util.js +++ b/src/util.js @@ -335,9 +335,16 @@ export const validate = async (options, releaseInfo) => { `Platform ${options.platform} and architecture ${options.arch} is not supported by this download server.`, ); } - if (typeof options.cacheDir !== "string") { + if (typeof options.downloadUrl === 'string' && !options.downloadUrl.startsWith('http') && !options.downloadUrl.startsWith('file')) { + throw new Error("Expected options.downloadUrl to be a string and starts with `http` or `file`."); + } + if (typeof options.manifestUrl === 'string' && !options.manifestUrl.startsWith('http') && !options.manifestUrl.startsWith('file')) { + throw new Error("Expected options.manifestUrl to be a string and starts with `http` or `file`."); + } + if (typeof options.cacheDir !== 'string') { throw new Error("Expected options.cacheDir to be a string. Got " + typeof options.cacheDir); } + if (typeof options.cache !== 'boolean') { throw new Error( 'Expected options.cache to be a boolean. Got ' + typeof options.cache, @@ -364,6 +371,9 @@ export const validate = async (options, releaseInfo) => { if (options.mode === 'get') { return undefined; } + if (typeof options.srcDir !== 'string') { + throw new Error("Expected options.srcDir to be a string. Got " + typeof options.srcDir); + } if (Array.isArray(options.argv)) { throw new Error( 'Expected options.argv to be an array. Got ' + typeof options.argv, @@ -375,16 +385,12 @@ export const validate = async (options, releaseInfo) => { ); } - if (options.srcDir) { - await fs.promises.readdir(options.srcDir); - } - if (options.mode === 'run') { return undefined; } - if (options.outDir) { - await fs.promises.readdir(options.outDir); + if (typeof options.outDir !== 'string') { + throw new Error("Expected options.outDir to be a string. Got " + typeof options.outDir); } if ( diff --git a/tests/specs/util.test.js b/tests/specs/util.test.js index 7c1185c7..c3225a04 100644 --- a/tests/specs/util.test.js +++ b/tests/specs/util.test.js @@ -19,5 +19,49 @@ describe('util/log', function () { it('throws error if user defined log level is invalid', async function () { expect(() => util.log('debug', 'errory', 'Lorem ipsum')).toThrow(); }); - + }); + +describe('util/validate', function () { + + it('throws error on invalid mode', async function () { + await expect(util.validate({ mode: 'gety' }, {})).rejects.toThrow(Error); + }); + + it('throws error if releases info is undefined', async function () { + await expect(util.validate({ mode: 'get' }, undefined)).rejects.toThrow(Error); + }); + + it('throws error on invalid flavor', async function () { + await expect(util.validate({ mode: 'get', flavor: 'notsdk' }, { flavours: ['normal'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid platform', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linox' }, { flavours: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid architecture', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64000' }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid download url', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64', downloadUrl: null }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid manifest url', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64', downloadUrl: 'file://path/to/fs', manifestUrl: null }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid cache directory', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64', downloadUrl: 'file://path/to/fs', manifestUrl: 'http://path/to/manifest', cacheDir: null }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid cache flag', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64', downloadUrl: 'file://path/to/fs', manifestUrl: 'http://path/to/manifest', cacheDir: './path/to/cache', cache: 'true' }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + + it('throws error on invalid ffmpeg flag', async function () { + await expect(util.validate({ mode: 'get', flavor: 'normal', platform: 'linux', arch: 'x64', downloadUrl: 'file://path/to/fs', manifestUrl: 'http://path/to/manifest', cacheDir: './path/to/cache', cache: true, ffmpeg: 'true' }, { flavors: ['normal'], files: ['linux-x64'] })).rejects.toThrow(Error); + }); + +}); \ No newline at end of file