Skip to content

Commit 6b83dc3

Browse files
committed
Check for null in addition to undefined; extend tests accordingly
1 parent ca0540d commit 6b83dc3

File tree

6 files changed

+54
-27
lines changed

6 files changed

+54
-27
lines changed

lib/start-proxy.js

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

lib/start-proxy.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/start-proxy.test.js

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

lib/start-proxy.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/start-proxy.test.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,26 @@ test("getCredentials throws error when credential is not an object", async (t) =
6969
});
7070

7171
test("getCredentials throws error when credential missing host and url", async (t) => {
72-
const registryCredentials = Buffer.from(
73-
JSON.stringify([{ type: "npm_registry", token: "abc" }]),
74-
).toString("base64");
72+
const testCredentials = [
73+
[{ type: "npm_registry", token: "abc" }],
74+
[{ type: "npm_registry", token: "abc", host: null }],
75+
[{ type: "npm_registry", token: "abc", url: null }],
76+
].map(toEncodedJSON);
7577

76-
t.throws(
77-
() =>
78-
startProxyExports.getCredentials(
79-
getRunnerLogger(true),
80-
undefined,
81-
registryCredentials,
82-
undefined,
83-
),
84-
{
85-
message: "Invalid credentials - must specify host or url",
86-
},
87-
);
78+
for (const testCredential of testCredentials) {
79+
t.throws(
80+
() =>
81+
startProxyExports.getCredentials(
82+
getRunnerLogger(true),
83+
undefined,
84+
testCredential,
85+
undefined,
86+
),
87+
{
88+
message: "Invalid credentials - must specify host or url",
89+
},
90+
);
91+
}
8892
});
8993

9094
test("getCredentials filters by language when specified", async (t) => {

src/start-proxy.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ const LANGUAGE_TO_REGISTRY_TYPE: Record<Language, string> = {
2727
swift: "",
2828
} as const;
2929

30+
/**
31+
* Checks that `value` is neither `undefined` nor `null`.
32+
* @param value The value to test.
33+
* @returns Narrows the type of `value` to exclude `undefined` and `null`.
34+
*/
35+
function isDefined<T>(value: T | null | undefined): value is T {
36+
return value !== undefined && value !== null;
37+
}
38+
3039
// getCredentials returns registry credentials from action inputs.
3140
// It prefers `registries_credentials` over `registry_secrets`.
3241
// If neither is set, it returns an empty array.
@@ -77,14 +86,14 @@ export function getCredentials(
7786
}
7887

7988
// Mask credentials to reduce chance of accidental leakage in logs.
80-
if (e.password !== undefined) {
89+
if (isDefined(e.password)) {
8190
core.setSecret(e.password);
8291
}
83-
if (e.token !== undefined) {
92+
if (isDefined(e.token)) {
8493
core.setSecret(e.token);
8594
}
8695

87-
if (e.url === undefined && e.host === undefined) {
96+
if (!isDefined(e.url) && !isDefined(e.host)) {
8897
// The proxy needs one of these to work. If both are defined, the url has the precedence.
8998
throw new ConfigurationError(
9099
"Invalid credentials - must specify host or url",

0 commit comments

Comments
 (0)