Skip to content

Commit

Permalink
test: fix test file extensions and inputs for repositories (actions#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 and lepadatu committed Sep 4, 2024
1 parent 1e425f8 commit cbef171
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 326 deletions.
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 @@ -20,7 +20,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 @@ -32,8 +32,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 @@ -47,7 +48,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
Loading

0 comments on commit cbef171

Please sign in to comment.