diff --git a/package-lock.json b/package-lock.json index a318de79..262dd0f7 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 5d481644..f300270c 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"; + +// Get all files in tests directory +const files = readdirSync("tests"); + +// Files to ignore +const ignore = ["index.js", "main.js", "README.md", "snapshots"]; -const tests = readdirSync("tests").filter((file) => file.endsWith(".test.js")); +const testFiles = files.filter((file) => !ignore.includes(file)); -for (const file of tests) { +// 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 eb2cffa0..0579fafa 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 a19ada7d..00000000 --- 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 00000000..baa7f07c --- /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 fa18b1a4..173fcf45 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 3e0f7335..78bd93eb 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 89d6a850..99638639 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 3e52f69d..245b6e6e 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 c458d391..023d104b 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 5a065370..89369aac 100644 Binary files a/tests/snapshots/index.js.snap and b/tests/snapshots/index.js.snap differ