Skip to content

Commit

Permalink
chore(util): validate all get mode optionsc
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushmanchhabra committed Nov 9, 2024
1 parent d9f4807 commit b77a8ef
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.");

Check failure on line 339 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (macos-15)

Strings must use singlequote

Check failure on line 339 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (ubuntu-24.04)

Strings must use singlequote

Check failure on line 339 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (windows-2022)

Strings must use singlequote
}
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`.");

Check failure on line 342 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (macos-15)

Strings must use singlequote

Check failure on line 342 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (ubuntu-24.04)

Strings must use singlequote

Check failure on line 342 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (windows-2022)

Strings must use singlequote
}
if (typeof options.cacheDir !== 'string') {
throw new Error("Expected options.cacheDir to be a string. Got " + typeof options.cacheDir);

Check failure on line 345 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (macos-15)

Strings must use singlequote

Check failure on line 345 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (ubuntu-24.04)

Strings must use singlequote

Check failure on line 345 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (windows-2022)

Strings must use singlequote
}

if (typeof options.cache !== 'boolean') {
throw new Error(
'Expected options.cache to be a boolean. Got ' + typeof options.cache,
Expand All @@ -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);

Check failure on line 375 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (macos-15)

Strings must use singlequote

Check failure on line 375 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (ubuntu-24.04)

Strings must use singlequote

Check failure on line 375 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (windows-2022)

Strings must use singlequote
}
if (Array.isArray(options.argv)) {
throw new Error(
'Expected options.argv to be an array. Got ' + typeof options.argv,
Expand All @@ -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);

Check failure on line 393 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (macos-15)

Strings must use singlequote

Check failure on line 393 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (ubuntu-24.04)

Strings must use singlequote

Check failure on line 393 in src/util.js

View workflow job for this annotation

GitHub Actions / tests (windows-2022)

Strings must use singlequote
}

if (
Expand Down
46 changes: 45 additions & 1 deletion tests/specs/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});

0 comments on commit b77a8ef

Please sign in to comment.