Skip to content

Commit 15db037

Browse files
authored
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.
1 parent 9ccc6db commit 15db037

12 files changed

+80
-23
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/index.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { readdirSync } from "node:fs";
22

3-
import { execa } from "execa";
43
import test from "ava";
4+
import { execa } from "execa";
5+
6+
// Get all files in tests directory
7+
const files = readdirSync("tests");
8+
9+
// Files to ignore
10+
const ignore = ["index.js", "main.js", "README.md", "snapshots"];
511

6-
const tests = readdirSync("tests").filter((file) => file.endsWith(".test.js"));
12+
const testFiles = files.filter((file) => !ignore.includes(file));
713

8-
for (const file of tests) {
14+
// Throw an error if there is a file that does not end with test.js in the tests directory
15+
for (const file of testFiles) {
16+
if (!file.endsWith(".test.js")) {
17+
throw new Error(`File ${file} does not end with .test.js`);
18+
}
919
test(file, async (t) => {
1020
// Override Actions environment variables that change `core`’s behavior
1121
const env = {

tests/main-custom-github-api-url.test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { test, DEFAULT_ENV } from "./main.js";
1+
import { DEFAULT_ENV, test } from "./main.js";
22

33
// Verify that main works with a custom GitHub API URL passed as `github-api-url` input
44
await test(
55
() => {
66
process.env.INPUT_OWNER = process.env.GITHUB_REPOSITORY_OWNER;
7-
process.env.INPUT_REPOSITORIES = process.env.GITHUB_REPOSITORY;
7+
const currentRepoName = process.env.GITHUB_REPOSITORY.split("/")[1];
8+
process.env.INPUT_REPOSITORIES = currentRepoName;
89
},
910
{
1011
...DEFAULT_ENV,

tests/main-private-key-with-escaped-newlines.js

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { DEFAULT_ENV, test } from "./main.js";
2+
3+
// Verify `main` works correctly when `private-key` input has escaped newlines
4+
await test(() => {
5+
process.env["INPUT_PRIVATE-KEY"] = DEFAULT_ENV["INPUT_PRIVATE-KEY"].replace(
6+
/\n/g,
7+
"\\n"
8+
);
9+
});
File renamed without changes.

tests/main-token-get-owner-set-repo-set-to-many.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { test } from "./main.js";
33
// Verify `main` successfully obtains a token when the `owner` and `repositories` inputs are set (and the latter is a list of repos).
44
await test(() => {
55
process.env.INPUT_OWNER = process.env.GITHUB_REPOSITORY_OWNER;
6-
process.env.INPUT_REPOSITORIES = `${process.env.GITHUB_REPOSITORY},actions/toolkit`;
6+
const currentRepoName = process.env.GITHUB_REPOSITORY.split("/")[1];
7+
process.env.INPUT_REPOSITORIES = `${currentRepoName},toolkit`;
78
});

tests/main-token-get-owner-set-repo-set-to-one.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { test } from "./main.js";
33
// Verify `main` successfully obtains a token when the `owner` and `repositories` inputs are set (and the latter is a single repo).
44
await test(() => {
55
process.env.INPUT_OWNER = process.env.GITHUB_REPOSITORY_OWNER;
6-
process.env.INPUT_REPOSITORIES = process.env.GITHUB_REPOSITORY;
6+
const currentRepoName = process.env.GITHUB_REPOSITORY.split("/")[1];
7+
process.env.INPUT_REPOSITORIES = currentRepoName;
78
});

tests/main-token-get-owner-unset-repo-set.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { test } from "./main.js";
33
// Verify `main` successfully obtains a token when the `owner` input is not set, but the `repositories` input is set.
44
await test(() => {
55
delete process.env.INPUT_OWNER;
6-
process.env.INPUT_REPOSITORIES = process.env.GITHUB_REPOSITORY;
6+
const currentRepoName = process.env.GITHUB_REPOSITORY.split("/")[1];
7+
process.env.INPUT_REPOSITORIES = currentRepoName;
78
});

tests/main.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
4646

4747
// Set up mocking
4848
const baseUrl = new URL(env["INPUT_GITHUB-API-URL"]);
49-
const basePath = baseUrl.pathname === '/' ? '' : baseUrl.pathname;
49+
const basePath = baseUrl.pathname === "/" ? "" : baseUrl.pathname;
5050
const mockAgent = new MockAgent();
5151
mockAgent.disableNetConnect();
5252
setGlobalDispatcher(mockAgent);
@@ -58,8 +58,9 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
5858
const mockInstallationId = "123456";
5959
const mockAppSlug = "github-actions";
6060
const owner = env.INPUT_OWNER ?? env.GITHUB_REPOSITORY_OWNER;
61+
const currentRepoName = env.GITHUB_REPOSITORY.split("/")[1];
6162
const repo = encodeURIComponent(
62-
(env.INPUT_REPOSITORIES ?? env.GITHUB_REPOSITORY).split(",")[0]
63+
(env.INPUT_REPOSITORIES ?? currentRepoName).split(",")[0]
6364
);
6465
mockPool
6566
.intercept({
@@ -73,7 +74,7 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
7374
})
7475
.reply(
7576
200,
76-
{ id: mockInstallationId, "app_slug": mockAppSlug },
77+
{ id: mockInstallationId, app_slug: mockAppSlug },
7778
{ headers: { "content-type": "application/json" } }
7879
);
7980

tests/snapshots/index.js.md

+43-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Generated by [AVA](https://avajs.dev).
2424

2525
> stdout
2626
27-
`owner and repositories set, creating token for repositories "actions/create-github-app-token" owned by "actions"␊
27+
`owner and repositories set, creating token for repositories "create-github-app-token" owned by "actions"␊
2828
::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
2929
3030
::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
@@ -75,6 +75,45 @@ Generated by [AVA](https://avajs.dev).
7575
7676
''
7777

78+
## main-private-key-with-escaped-newlines.test.js
79+
80+
> stderr
81+
82+
''
83+
84+
> stdout
85+
86+
`owner and repositories not set, creating token for the current repository ("create-github-app-token")␊
87+
::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
88+
89+
::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
90+
91+
::set-output name=installation-id::123456␊
92+
93+
::set-output name=app-slug::github-actions␊
94+
::save-state name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
95+
::save-state name=expiresAt::2016-07-11T22:14:10Z`
96+
97+
## main-repo-skew.test.js
98+
99+
> stderr
100+
101+
`'Issued at' claim ('iat') must be an Integer representing the time that the assertion was issued.␊
102+
[@octokit/auth-app] GitHub API time and system time are different by 30 seconds. Retrying request with the difference accounted for.`
103+
104+
> stdout
105+
106+
`owner and repositories set, creating token for repositories "failed-repo" owned by "actions"␊
107+
::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
108+
109+
::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
110+
111+
::set-output name=installation-id::123456␊
112+
113+
::set-output name=app-slug::github-actions␊
114+
::save-state name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
115+
::save-state name=expiresAt::2016-07-11T22:14:10Z`
116+
78117
## main-token-get-owner-set-repo-fail-response.test.js
79118

80119
> stderr
@@ -103,7 +142,7 @@ Generated by [AVA](https://avajs.dev).
103142

104143
> stdout
105144
106-
`owner and repositories set, creating token for repositories "actions/create-github-app-token,actions/toolkit" owned by "actions"␊
145+
`owner and repositories set, creating token for repositories "create-github-app-token,toolkit" owned by "actions"␊
107146
::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
108147
109148
::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
@@ -122,7 +161,7 @@ Generated by [AVA](https://avajs.dev).
122161

123162
> stdout
124163
125-
`owner and repositories set, creating token for repositories "actions/create-github-app-token" owned by "actions"␊
164+
`owner and repositories set, creating token for repositories "create-github-app-token" owned by "actions"␊
126165
::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
127166
128167
::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
@@ -199,7 +238,7 @@ Generated by [AVA](https://avajs.dev).
199238

200239
> stdout
201240
202-
`owner not set, creating owner for given repositories "actions/create-github-app-token" in current owner ("actions")␊
241+
`owner not set, creating owner for given repositories "create-github-app-token" in current owner ("actions")␊
203242
::add-mask::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊
204243
205244
::set-output name=token::ghs_16C7e42F292c6912E7710c838347Ae178B4a␊

tests/snapshots/index.js.snap

198 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)