From 15db0371dad2d605879bcac268eeaeafcf23a859 Mon Sep 17 00:00:00 2001 From: Parker Brown <17183625+parkerbxyz@users.noreply.github.com> Date: Tue, 3 Sep 2024 20:04:47 -0700 Subject: [PATCH] test: fix test file extensions and inputs for repositories (#161) This pull request fixes the file extension for two test files that were incorrectly named. This caused them not to be tested. A new test has been added to ensure all test files have the correct extension. This also fixes a bug in some tests where `repositories` inputs included the repository owner. The owner has been removed from these inputs and the snapshots have been updated. --- package-lock.json | 4 +- tests/index.js | 16 ++++-- tests/main-custom-github-api-url.test.js | 5 +- .../main-private-key-with-escaped-newlines.js | 6 --- ...-private-key-with-escaped-newlines.test.js | 9 ++++ ...in-repo-skew.js => main-repo-skew.test.js} | 0 ...ken-get-owner-set-repo-set-to-many.test.js | 3 +- ...oken-get-owner-set-repo-set-to-one.test.js | 3 +- ...ain-token-get-owner-unset-repo-set.test.js | 3 +- tests/main.js | 7 +-- tests/snapshots/index.js.md | 47 ++++++++++++++++-- tests/snapshots/index.js.snap | Bin 1128 -> 1326 bytes 12 files changed, 80 insertions(+), 23 deletions(-) delete mode 100644 tests/main-private-key-with-escaped-newlines.js create mode 100644 tests/main-private-key-with-escaped-newlines.test.js rename tests/{main-repo-skew.js => main-repo-skew.test.js} (100%) diff --git a/package-lock.json b/package-lock.json index a318de7..262dd0f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "create-github-app-token", - "version": "1.10.2", + "version": "1.10.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "create-github-app-token", - "version": "1.10.2", + "version": "1.10.3", "license": "MIT", "dependencies": { "@actions/core": "^1.10.1", diff --git a/tests/index.js b/tests/index.js index 5d48164..f300270 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,11 +1,21 @@ import { readdirSync } from "node:fs"; -import { execa } from "execa"; import test from "ava"; +import { execa } from "execa"; -const tests = readdirSync("tests").filter((file) => file.endsWith(".test.js")); +// Get all files in tests directory +const files = readdirSync("tests"); -for (const file of tests) { +// Files to ignore +const ignore = ["index.js", "main.js", "README.md", "snapshots"]; + +const testFiles = files.filter((file) => !ignore.includes(file)); + +// Throw an error if there is a file that does not end with test.js in the tests directory +for (const file of testFiles) { + if (!file.endsWith(".test.js")) { + throw new Error(`File ${file} does not end with .test.js`); + } test(file, async (t) => { // Override Actions environment variables that change `core`’s behavior const env = { diff --git a/tests/main-custom-github-api-url.test.js b/tests/main-custom-github-api-url.test.js index eb2cffa..0579faf 100644 --- a/tests/main-custom-github-api-url.test.js +++ b/tests/main-custom-github-api-url.test.js @@ -1,10 +1,11 @@ -import { test, DEFAULT_ENV } from "./main.js"; +import { DEFAULT_ENV, test } from "./main.js"; // Verify that main works with a custom GitHub API URL passed as `github-api-url` input await test( () => { process.env.INPUT_OWNER = process.env.GITHUB_REPOSITORY_OWNER; - process.env.INPUT_REPOSITORIES = process.env.GITHUB_REPOSITORY; + const currentRepoName = process.env.GITHUB_REPOSITORY.split("/")[1]; + process.env.INPUT_REPOSITORIES = currentRepoName; }, { ...DEFAULT_ENV, diff --git a/tests/main-private-key-with-escaped-newlines.js b/tests/main-private-key-with-escaped-newlines.js deleted file mode 100644 index a19ada7..0000000 --- a/tests/main-private-key-with-escaped-newlines.js +++ /dev/null @@ -1,6 +0,0 @@ -import { test, DEFAULT_ENV } from "./main.js"; - -// Verify `main` works correctly when `private-key` input has escaped newlines -await test(() => { - process.env['INPUT_PRIVATE-KEY'] = DEFAULT_ENV.PRIVATE_KEY.replace(/\n/g, '\\n') -}); diff --git a/tests/main-private-key-with-escaped-newlines.test.js b/tests/main-private-key-with-escaped-newlines.test.js new file mode 100644 index 0000000..baa7f07 --- /dev/null +++ b/tests/main-private-key-with-escaped-newlines.test.js @@ -0,0 +1,9 @@ +import { DEFAULT_ENV, test } from "./main.js"; + +// Verify `main` works correctly when `private-key` input has escaped newlines +await test(() => { + process.env["INPUT_PRIVATE-KEY"] = DEFAULT_ENV["INPUT_PRIVATE-KEY"].replace( + /\n/g, + "\\n" + ); +}); diff --git a/tests/main-repo-skew.js b/tests/main-repo-skew.test.js similarity index 100% rename from tests/main-repo-skew.js rename to tests/main-repo-skew.test.js diff --git a/tests/main-token-get-owner-set-repo-set-to-many.test.js b/tests/main-token-get-owner-set-repo-set-to-many.test.js index fa18b1a..173fcf4 100644 --- a/tests/main-token-get-owner-set-repo-set-to-many.test.js +++ b/tests/main-token-get-owner-set-repo-set-to-many.test.js @@ -3,5 +3,6 @@ import { test } from "./main.js"; // Verify `main` successfully obtains a token when the `owner` and `repositories` inputs are set (and the latter is a list of repos). await test(() => { process.env.INPUT_OWNER = process.env.GITHUB_REPOSITORY_OWNER; - process.env.INPUT_REPOSITORIES = `${process.env.GITHUB_REPOSITORY},actions/toolkit`; + const currentRepoName = process.env.GITHUB_REPOSITORY.split("/")[1]; + process.env.INPUT_REPOSITORIES = `${currentRepoName},toolkit`; }); diff --git a/tests/main-token-get-owner-set-repo-set-to-one.test.js b/tests/main-token-get-owner-set-repo-set-to-one.test.js index 3e0f733..78bd93e 100644 --- a/tests/main-token-get-owner-set-repo-set-to-one.test.js +++ b/tests/main-token-get-owner-set-repo-set-to-one.test.js @@ -3,5 +3,6 @@ import { test } from "./main.js"; // Verify `main` successfully obtains a token when the `owner` and `repositories` inputs are set (and the latter is a single repo). await test(() => { process.env.INPUT_OWNER = process.env.GITHUB_REPOSITORY_OWNER; - process.env.INPUT_REPOSITORIES = process.env.GITHUB_REPOSITORY; + const currentRepoName = process.env.GITHUB_REPOSITORY.split("/")[1]; + process.env.INPUT_REPOSITORIES = currentRepoName; }); diff --git a/tests/main-token-get-owner-unset-repo-set.test.js b/tests/main-token-get-owner-unset-repo-set.test.js index 89d6a85..9963863 100644 --- a/tests/main-token-get-owner-unset-repo-set.test.js +++ b/tests/main-token-get-owner-unset-repo-set.test.js @@ -3,5 +3,6 @@ import { test } from "./main.js"; // Verify `main` successfully obtains a token when the `owner` input is not set, but the `repositories` input is set. await test(() => { delete process.env.INPUT_OWNER; - process.env.INPUT_REPOSITORIES = process.env.GITHUB_REPOSITORY; + const currentRepoName = process.env.GITHUB_REPOSITORY.split("/")[1]; + process.env.INPUT_REPOSITORIES = currentRepoName; }); diff --git a/tests/main.js b/tests/main.js index 3e52f69..245b6e6 100644 --- a/tests/main.js +++ b/tests/main.js @@ -46,7 +46,7 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) { // Set up mocking const baseUrl = new URL(env["INPUT_GITHUB-API-URL"]); - const basePath = baseUrl.pathname === '/' ? '' : baseUrl.pathname; + const basePath = baseUrl.pathname === "/" ? "" : baseUrl.pathname; const mockAgent = new MockAgent(); mockAgent.disableNetConnect(); setGlobalDispatcher(mockAgent); @@ -58,8 +58,9 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) { const mockInstallationId = "123456"; const mockAppSlug = "github-actions"; const owner = env.INPUT_OWNER ?? env.GITHUB_REPOSITORY_OWNER; + const currentRepoName = env.GITHUB_REPOSITORY.split("/")[1]; const repo = encodeURIComponent( - (env.INPUT_REPOSITORIES ?? env.GITHUB_REPOSITORY).split(",")[0] + (env.INPUT_REPOSITORIES ?? currentRepoName).split(",")[0] ); mockPool .intercept({ @@ -73,7 +74,7 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) { }) .reply( 200, - { id: mockInstallationId, "app_slug": mockAppSlug }, + { id: mockInstallationId, app_slug: mockAppSlug }, { headers: { "content-type": "application/json" } } ); diff --git a/tests/snapshots/index.js.md b/tests/snapshots/index.js.md index c458d39..023d104 100644 --- a/tests/snapshots/index.js.md +++ b/tests/snapshots/index.js.md @@ -24,7 +24,7 @@ Generated by [AVA](https://avajs.dev). > stdout - `owner and repositories set, creating token for repositories "actions/create-github-app-token" owned by "actions"␊ + `owner and repositories set, creating token for repositories "create-github-app-token" owned by "actions"␊ ::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ ␊ ::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ @@ -75,6 +75,45 @@ Generated by [AVA](https://avajs.dev). '' +## main-private-key-with-escaped-newlines.test.js + +> stderr + + '' + +> stdout + + `owner and repositories not set, creating token for the current repository ("create-github-app-token")␊ + ::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ + ␊ + ::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ + ␊ + ::set-output name=installation-id::123456␊ + ␊ + ::set-output name=app-slug::github-actions␊ + ::save-state name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ + ::save-state name=expiresAt::2016-07-11T22:14:10Z` + +## main-repo-skew.test.js + +> stderr + + `'Issued at' claim ('iat') must be an Integer representing the time that the assertion was issued.␊ + [@octokit/auth-app] GitHub API time and system time are different by 30 seconds. Retrying request with the difference accounted for.` + +> stdout + + `owner and repositories set, creating token for repositories "failed-repo" owned by "actions"␊ + ::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ + ␊ + ::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ + ␊ + ::set-output name=installation-id::123456␊ + ␊ + ::set-output name=app-slug::github-actions␊ + ::save-state name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ + ::save-state name=expiresAt::2016-07-11T22:14:10Z` + ## main-token-get-owner-set-repo-fail-response.test.js > stderr @@ -103,7 +142,7 @@ Generated by [AVA](https://avajs.dev). > stdout - `owner and repositories set, creating token for repositories "actions/create-github-app-token,actions/toolkit" owned by "actions"␊ + `owner and repositories set, creating token for repositories "create-github-app-token,toolkit" owned by "actions"␊ ::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ ␊ ::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ @@ -122,7 +161,7 @@ Generated by [AVA](https://avajs.dev). > stdout - `owner and repositories set, creating token for repositories "actions/create-github-app-token" owned by "actions"␊ + `owner and repositories set, creating token for repositories "create-github-app-token" owned by "actions"␊ ::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ ␊ ::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ @@ -199,7 +238,7 @@ Generated by [AVA](https://avajs.dev). > stdout - `owner not set, creating owner for given repositories "actions/create-github-app-token" in current owner ("actions")␊ + `owner not set, creating owner for given repositories "create-github-app-token" in current owner ("actions")␊ ::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ ␊ ::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊ diff --git a/tests/snapshots/index.js.snap b/tests/snapshots/index.js.snap index 5a0653706ded1917da4cb52e95edaeab23821f70..89369aac6f4c1bc3492e578620e484db70cc9830 100644 GIT binary patch literal 1326 zcmV+}1=0FJRzVQng$u(pyYZ}#h2Z|m_Fk8 zBIfw&v>%HI00000000B+nL%&kL=?vx5JIYiwA>JjU{tM?7V2!Ac9Ygx5M9)5bAYZ& zS9DpSXkx#lL+r8fjFasR@fm88P1{T_7<8;SkR;PAKUmGKqlgJM(zvHHY{y}3klS#Ek-dLqMgnF zj#DqW6XjfGhOE@ki9a!5QAdUY;GGzT_&@b}>U25Z%ABRmSz~jVf=SrJM4sewDWHs7 zZX#qDSbZjslCDJ~W+ky-CY!$++5D*zjyS}6DltT%U^0vu2qHkP5;q1a8SfJyBOjbSfs-H6{TQ z0uyDWwN%K*;jMi3E7i7Y4w%y~67-t_%|l5<5=A}(&mpP3DIgV*mQ(h+p-gjDENsUR zwz<8y_{5HNX>PL zV!@6fc=g;!=8RrW<6DNtaUGr^ao3ROoQmnW(Y$$Exw&jmYg%w_-qr|9$O7Tg2p1*} zBcE|7wKV-=G_xOznwic-K|_;AK-?sbfy>E7$7H#%x2?=O*-SdwP~)OfV2J^Yin0FJ zNcvw3)wU3csySIDu1{G&ma7cEtc{NPb3knA}aeWb0l#HKCvr$GDyc*>~Ikc&yh0O%|?% zYjtGM4Y?<3WFKUFtQ4q*f%7D)GPHn`OI)6*|Kt1|E|`Ow2kHrnb$5; zi6s+BVy`&)no=#B4sj&y`Y=atHt5-Bqo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