Skip to content

Commit cbef171

Browse files
parkerbxyzlepadatu
authored andcommitted
test: fix test file extensions and inputs for repositories (actions#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 1e425f8 commit cbef171

11 files changed

+35
-326
lines changed

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
@@ -20,7 +20,7 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
2020

2121
// Set up mocking
2222
const baseUrl = new URL(env["INPUT_GITHUB-API-URL"]);
23-
const basePath = baseUrl.pathname === '/' ? '' : baseUrl.pathname;
23+
const basePath = baseUrl.pathname === "/" ? "" : baseUrl.pathname;
2424
const mockAgent = new MockAgent();
2525
mockAgent.disableNetConnect();
2626
setGlobalDispatcher(mockAgent);
@@ -32,8 +32,9 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
3232
const mockInstallationId = "123456";
3333
const mockAppSlug = "github-actions";
3434
const owner = env.INPUT_OWNER ?? env.GITHUB_REPOSITORY_OWNER;
35+
const currentRepoName = env.GITHUB_REPOSITORY.split("/")[1];
3536
const repo = encodeURIComponent(
36-
(env.INPUT_REPOSITORIES ?? env.GITHUB_REPOSITORY).split(",")[0]
37+
(env.INPUT_REPOSITORIES ?? currentRepoName).split(",")[0]
3738
);
3839
mockPool
3940
.intercept({
@@ -47,7 +48,7 @@ export async function test(cb = (_mockPool) => {}, env = DEFAULT_ENV) {
4748
})
4849
.reply(
4950
200,
50-
{ id: mockInstallationId, "app_slug": mockAppSlug },
51+
{ id: mockInstallationId, app_slug: mockAppSlug },
5152
{ headers: { "content-type": "application/json" } }
5253
);
5354

0 commit comments

Comments
 (0)