Skip to content

Commit

Permalink
test: fix test file extensions and inputs for repositories (#161)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
parkerbxyz authored Sep 4, 2024
1 parent 9ccc6db commit 15db037
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
5 changes: 3 additions & 2 deletions tests/main-custom-github-api-url.test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 0 additions & 6 deletions tests/main-private-key-with-escaped-newlines.js

This file was deleted.

9 changes: 9 additions & 0 deletions tests/main-private-key-with-escaped-newlines.test.js
Original file line number Diff line number Diff line change
@@ -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"
);
});
File renamed without changes.
3 changes: 2 additions & 1 deletion tests/main-token-get-owner-set-repo-set-to-many.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
});
3 changes: 2 additions & 1 deletion tests/main-token-get-owner-set-repo-set-to-one.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
3 changes: 2 additions & 1 deletion tests/main-token-get-owner-unset-repo-set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
7 changes: 4 additions & 3 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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({
Expand All @@ -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" } }
);

Expand Down
47 changes: 43 additions & 4 deletions tests/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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␊
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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␊
Expand All @@ -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␊
Expand Down Expand Up @@ -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␊
Expand Down
Binary file modified tests/snapshots/index.js.snap
Binary file not shown.

0 comments on commit 15db037

Please sign in to comment.