From 8f1198afd05140c4ea6bfed43f0a6dc3c00a6671 Mon Sep 17 00:00:00 2001 From: Jack Chan Date: Mon, 30 May 2022 01:03:54 +0800 Subject: [PATCH 1/5] test: add testcase of validating url asset without path and missing url and path --- test/verify.test.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/verify.test.js b/test/verify.test.js index d3d737d2..c0dbe30c 100644 --- a/test/verify.test.js +++ b/test/verify.test.js @@ -795,3 +795,47 @@ test.serial('Does not throw an error for option without validator', async (t) => ); t.true(gitlab.isDone()); }); + +test.serial( + 'Won\'t throw SemanticReleaseError if "assets" option is an Array of objects with url field but missing the "path" property', + async (t) => { + const owner = 'test_user'; + const repo = 'test_repo'; + const env = {GITLAB_TOKEN: 'gitlab_token'}; + const assets = [{url: 'https://gitlab.com/gitlab-org/gitlab/-/blob/master/README.md'}]; + const gitlab = authenticate(env) + .get(`/projects/${owner}%2F${repo}`) + .reply(200, {permissions: {project_access: {access_level: 40}}}); + + await t.notThrowsAsync( + verify( + {assets}, + {env, options: {repositoryUrl: `https://gitlab.com/${owner}/${repo}.git`}, logger: t.context.logger} + ) + ); + t.true(gitlab.isDone()); + } +); + +test.serial( + 'Throw SemanticReleaseError if "assets" option is an Array of objects without url nor path property', + async (t) => { + const owner = 'test_user'; + const repo = 'test_repo'; + const env = {GITLAB_TOKEN: 'gitlab_token'}; + const assets = [{name: 'README.md'}]; + const gitlab = authenticate(env) + .get(`/projects/${owner}%2F${repo}`) + .reply(200, {permissions: {project_access: {access_level: 40}}}); + + const [error, ...errors] = await t.throwsAsync( + verify( + {assets}, + {env, options: {repositoryUrl: `https://gitlab.com/${owner}/${repo}.git`}, logger: t.context.logger} + ) + ); + t.is(error.name, 'SemanticReleaseError'); + t.is(error.code, 'EINVALIDASSETS'); + t.true(gitlab.isDone()); + } +); \ No newline at end of file From cb15dbb57c54c0cdc9722f1c11c020c5964c2824 Mon Sep 17 00:00:00 2001 From: Jack Chan Date: Mon, 30 May 2022 01:04:26 +0800 Subject: [PATCH 2/5] fix: url or path is required in assets --- lib/verify.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/verify.js b/lib/verify.js index c68ee754..fb9b3496 100644 --- a/lib/verify.js +++ b/lib/verify.js @@ -15,7 +15,7 @@ const canBeDisabled = (validator) => (value) => value === false || validator(val const VALIDATORS = { assets: isArrayOf( - (asset) => isStringOrStringArray(asset) || (isPlainObject(asset) && isStringOrStringArray(asset.path)) + (asset) => isStringOrStringArray(asset) || (isPlainObject(asset) && isNonEmptyString(asset.url)) || (isPlainObject(asset) && isStringOrStringArray(asset.path)) ), failTitle: canBeDisabled(isNonEmptyString), failComment: canBeDisabled(isNonEmptyString), From 84dc39ea512c870fb191ad9e350db36b893607b0 Mon Sep 17 00:00:00 2001 From: Jack Chan Date: Mon, 30 May 2022 01:05:53 +0800 Subject: [PATCH 3/5] test: testcase for validating asset with path without url --- test/verify.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/verify.test.js b/test/verify.test.js index c0dbe30c..005e7c3e 100644 --- a/test/verify.test.js +++ b/test/verify.test.js @@ -817,6 +817,27 @@ test.serial( } ); +test.serial( + 'Won\'t throw SemanticReleaseError if "assets" option is an Array of objects with path field but missing the "url" property', + async (t) => { + const owner = 'test_user'; + const repo = 'test_repo'; + const env = {GITLAB_TOKEN: 'gitlab_token'}; + const assets = [{path: 'README.md'}]; + const gitlab = authenticate(env) + .get(`/projects/${owner}%2F${repo}`) + .reply(200, {permissions: {project_access: {access_level: 40}}}); + + await t.notThrowsAsync( + verify( + {assets}, + {env, options: {repositoryUrl: `https://gitlab.com/${owner}/${repo}.git`}, logger: t.context.logger} + ) + ); + t.true(gitlab.isDone()); + } +); + test.serial( 'Throw SemanticReleaseError if "assets" option is an Array of objects without url nor path property', async (t) => { From f1db0252cd2fdb474908783a3f98f41de1e50641 Mon Sep 17 00:00:00 2001 From: Jack Chan Date: Tue, 31 May 2022 01:57:52 +0800 Subject: [PATCH 4/5] fix: readability & reduce same call again --- lib/verify.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/verify.js b/lib/verify.js index fb9b3496..177b119c 100644 --- a/lib/verify.js +++ b/lib/verify.js @@ -15,7 +15,7 @@ const canBeDisabled = (validator) => (value) => value === false || validator(val const VALIDATORS = { assets: isArrayOf( - (asset) => isStringOrStringArray(asset) || (isPlainObject(asset) && isNonEmptyString(asset.url)) || (isPlainObject(asset) && isStringOrStringArray(asset.path)) + (asset) => isStringOrStringArray(asset) || (isPlainObject(asset) && (isNonEmptyString(asset.url) || isStringOrStringArray(asset.path))) ), failTitle: canBeDisabled(isNonEmptyString), failComment: canBeDisabled(isNonEmptyString), From 0d66e02236b72761db6722ee0467670d98f6ee29 Mon Sep 17 00:00:00 2001 From: Jack Chan Date: Tue, 31 May 2022 02:32:06 +0800 Subject: [PATCH 5/5] chore: lint code --- lib/verify.js | 4 +++- test/verify.test.js | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/verify.js b/lib/verify.js index 177b119c..5fb3628e 100644 --- a/lib/verify.js +++ b/lib/verify.js @@ -15,7 +15,9 @@ const canBeDisabled = (validator) => (value) => value === false || validator(val const VALIDATORS = { assets: isArrayOf( - (asset) => isStringOrStringArray(asset) || (isPlainObject(asset) && (isNonEmptyString(asset.url) || isStringOrStringArray(asset.path))) + (asset) => + isStringOrStringArray(asset) || + (isPlainObject(asset) && (isNonEmptyString(asset.url) || isStringOrStringArray(asset.path))) ), failTitle: canBeDisabled(isNonEmptyString), failComment: canBeDisabled(isNonEmptyString), diff --git a/test/verify.test.js b/test/verify.test.js index 005e7c3e..5129fe01 100644 --- a/test/verify.test.js +++ b/test/verify.test.js @@ -849,7 +849,7 @@ test.serial( .get(`/projects/${owner}%2F${repo}`) .reply(200, {permissions: {project_access: {access_level: 40}}}); - const [error, ...errors] = await t.throwsAsync( + const [error] = await t.throwsAsync( verify( {assets}, {env, options: {repositoryUrl: `https://gitlab.com/${owner}/${repo}.git`}, logger: t.context.logger} @@ -859,4 +859,4 @@ test.serial( t.is(error.code, 'EINVALIDASSETS'); t.true(gitlab.isDone()); } -); \ No newline at end of file +);