Skip to content

Commit 9d87259

Browse files
committed
test: Add tests to verify 'main' overrides 'request'’s default 'baseUrl' only when 'GITHUB_API_URL' is set
1 parent b872bf9 commit 9d87259

File tree

7 files changed

+115
-5
lines changed

7 files changed

+115
-5
lines changed

package-lock.json

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@
1818
"ava": "^5.3.1",
1919
"dotenv": "^16.3.1",
2020
"esbuild": "^0.19.4",
21+
"esmock": "^2.5.1",
2122
"execa": "^8.0.1",
2223
"undici": "^5.25.2"
2324
},
25+
"ava": {
26+
"nodeArguments": [
27+
"--no-warnings=ExperimentalWarning",
28+
"--loader=esmock"
29+
]
30+
},
2431
"release": {
2532
"branches": [
2633
"+([0-9]).x",
@@ -44,4 +51,4 @@
4451
]
4552
]
4653
}
47-
}
54+
}

tests/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
import { readdirSync } from "node:fs";
1+
import { readFileSync, readdirSync } from "node:fs";
2+
import * as url from "node:url";
23

34
import { execa } from "execa";
45
import test from "ava";
56

7+
// If ava is configured to use node arguments, use them when this script runs node
8+
const nodeArguments =
9+
readFileSync(
10+
url.fileURLToPath(new URL("../package.json", import.meta.url)),
11+
"utf8"
12+
)?.ava?.nodeArguments ?? [];
13+
614
const tests = readdirSync("tests").filter((file) => file.endsWith(".test.js"));
715

816
for (const file of tests) {
917
test(file, async (t) => {
10-
const { stderr, stdout } = await execa("node", [`tests/${file}`]);
18+
const { stderr, stdout } = await execa("node", [
19+
...nodeArguments,
20+
`tests/${file}`,
21+
]);
1122
t.snapshot(stderr, "stderr");
1223
t.snapshot(stdout, "stdout");
1324
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Verify `main` overrides `request`’s default `baseUrl` when `GITHUB_API_URL` is set.
2+
// @ts-check
3+
4+
import esmock from "esmock";
5+
6+
process.env.GITHUB_REPOSITORY = "actions/create-github-app-token";
7+
process.env.GITHUB_API_URL = "https://github.acme-inc.com/api/v3";
8+
9+
// inputs are set as environment variables with the prefix INPUT_
10+
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
11+
process.env.INPUT_APP_ID = "123456";
12+
process.env.INPUT_PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
13+
ABC/def/123==
14+
-----END RSA PRIVATE KEY-----`;
15+
16+
await esmock("../main.js", {
17+
"../lib/main.js": {
18+
main: async (
19+
_appId,
20+
_privateKey,
21+
_repository,
22+
_core,
23+
_createAppAuth,
24+
request
25+
) => {
26+
console.log(request.endpoint.DEFAULTS.baseUrl);
27+
console.log(request.endpoint.DEFAULTS.headers["user-agent"]);
28+
},
29+
},
30+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Verify `main` doesn’t override `request`’s default `baseUrl` when `GITHUB_API_URL` isn’t set.
2+
// @ts-check
3+
4+
import esmock from "esmock";
5+
6+
process.env.GITHUB_REPOSITORY = "actions/create-github-app-token";
7+
delete process.env.GITHUB_API_URL;
8+
9+
// inputs are set as environment variables with the prefix INPUT_
10+
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs
11+
process.env.INPUT_APP_ID = "123456";
12+
process.env.INPUT_PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
13+
ABC/def/123==
14+
-----END RSA PRIVATE KEY-----`;
15+
16+
await esmock("../main.js", {
17+
"../lib/main.js": {
18+
main: async (
19+
_appId,
20+
_privateKey,
21+
_repository,
22+
_core,
23+
_createAppAuth,
24+
request
25+
) => {
26+
console.log(request.endpoint.DEFAULTS.baseUrl);
27+
console.log(request.endpoint.DEFAULTS.headers["user-agent"]);
28+
},
29+
},
30+
});

tests/snapshots/index.js.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,28 @@ Generated by [AVA](https://avajs.dev).
1414
1515
''
1616

17+
## main-request-with-baseurl.test.js
18+
19+
> stderr
20+
21+
''
22+
23+
> stdout
24+
25+
`https://github.acme-inc.com/api/v3␊
26+
actions/create-github-app-token`
27+
28+
## main-request-without-baseurl.test.js
29+
30+
> stderr
31+
32+
''
33+
34+
> stdout
35+
36+
`https://api.github.com␊
37+
actions/create-github-app-token`
38+
1739
## post-token-set.test.js
1840

1941
> stderr

tests/snapshots/index.js.snap

96 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)