Skip to content

Commit

Permalink
test: Add tests to verify 'main' overrides 'request'’s default 'baseU…
Browse files Browse the repository at this point in the history
…rl' only when 'GITHUB_API_URL' is set
  • Loading branch information
smockle committed Oct 3, 2023
1 parent b872bf9 commit 9d87259
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 5 deletions.
14 changes: 12 additions & 2 deletions package-lock.json

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

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@
"ava": "^5.3.1",
"dotenv": "^16.3.1",
"esbuild": "^0.19.4",
"esmock": "^2.5.1",
"execa": "^8.0.1",
"undici": "^5.25.2"
},
"ava": {
"nodeArguments": [
"--no-warnings=ExperimentalWarning",
"--loader=esmock"
]
},
"release": {
"branches": [
"+([0-9]).x",
Expand All @@ -44,4 +51,4 @@
]
]
}
}
}
15 changes: 13 additions & 2 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { readdirSync } from "node:fs";
import { readFileSync, readdirSync } from "node:fs";
import * as url from "node:url";

import { execa } from "execa";
import test from "ava";

// If ava is configured to use node arguments, use them when this script runs node
const nodeArguments =
readFileSync(
url.fileURLToPath(new URL("../package.json", import.meta.url)),
"utf8"
)?.ava?.nodeArguments ?? [];

const tests = readdirSync("tests").filter((file) => file.endsWith(".test.js"));

for (const file of tests) {
test(file, async (t) => {
const { stderr, stdout } = await execa("node", [`tests/${file}`]);
const { stderr, stdout } = await execa("node", [
...nodeArguments,
`tests/${file}`,
]);
t.snapshot(stderr, "stderr");
t.snapshot(stdout, "stdout");
});
Expand Down
30 changes: 30 additions & 0 deletions tests/main-request-with-baseurl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Verify `main` overrides `request`’s default `baseUrl` when `GITHUB_API_URL` is set.
// @ts-check

import esmock from "esmock";

process.env.GITHUB_REPOSITORY = "actions/create-github-app-token";
process.env.GITHUB_API_URL = "https://github.acme-inc.com/api/v3";

// inputs are set as environment variables with the prefix INPUT_
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
process.env.INPUT_APP_ID = "123456";
process.env.INPUT_PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
ABC/def/123==
-----END RSA PRIVATE KEY-----`;

await esmock("../main.js", {
"../lib/main.js": {
main: async (
_appId,
_privateKey,
_repository,
_core,
_createAppAuth,
request
) => {
console.log(request.endpoint.DEFAULTS.baseUrl);
console.log(request.endpoint.DEFAULTS.headers["user-agent"]);
},
},
});
30 changes: 30 additions & 0 deletions tests/main-request-without-baseurl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Verify `main` doesn’t override `request`’s default `baseUrl` when `GITHUB_API_URL` isn’t set.
// @ts-check

import esmock from "esmock";

process.env.GITHUB_REPOSITORY = "actions/create-github-app-token";
delete process.env.GITHUB_API_URL;

// inputs are set as environment variables with the prefix INPUT_
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
process.env.INPUT_APP_ID = "123456";
process.env.INPUT_PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
ABC/def/123==
-----END RSA PRIVATE KEY-----`;

await esmock("../main.js", {
"../lib/main.js": {
main: async (
_appId,
_privateKey,
_repository,
_core,
_createAppAuth,
request
) => {
console.log(request.endpoint.DEFAULTS.baseUrl);
console.log(request.endpoint.DEFAULTS.headers["user-agent"]);
},
},
});
22 changes: 22 additions & 0 deletions tests/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ Generated by [AVA](https://avajs.dev).
''

## main-request-with-baseurl.test.js

> stderr
''

> stdout
`https://github.acme-inc.com/api/v3␊
actions/create-github-app-token`

## main-request-without-baseurl.test.js

> stderr
''

> stdout
`https://api.github.com␊
actions/create-github-app-token`

## post-token-set.test.js

> stderr
Expand Down
Binary file modified tests/snapshots/index.js.snap
Binary file not shown.

0 comments on commit 9d87259

Please sign in to comment.