Skip to content

Commit

Permalink
test(publish): ci names in user-agent (#14328)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway authored Oct 4, 2024
1 parent adc86c7 commit f307d2a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
62 changes: 54 additions & 8 deletions test/cli/install/registry/bun-install-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
toHaveBins,
toMatchNodeModulesAt,
writeShebangScript,
stderrForInstall,
} from "harness";
import { join, resolve, sep } from "path";
import { readdirSorted } from "../dummy.registry";
Expand Down Expand Up @@ -548,10 +549,14 @@ describe("publish", async () => {
setAuthHeader?: boolean;
otpFail?: boolean;
npmNotice?: boolean;
expectedCI?: string;
}) {
return async function (req: Request) {
const { token, setAuthHeader = true, otpFail = false, npmNotice = false } = opts;
if (req.url.includes("otp-pkg")) {
if (opts.expectedCI) {
expect(req.headers.get("user-agent")).toContain("ci/" + opts.expectedCI);
}
if (req.headers.get("npm-otp") === token) {
if (otpFail) {
return new Response(
Expand Down Expand Up @@ -698,6 +703,47 @@ describe("publish", async () => {
expect(exitCode).toBe(0);
expect(err).toContain(`note: visit http://localhost:${mockRegistry.port}/auth to login`);
});

const fakeCIEnvs = [
{ ci: "expo-application-services", envs: { EAS_BUILD: "hi" } },
{ ci: "codemagic", envs: { CM_BUILD_ID: "hi" } },
{ ci: "vercel", envs: { "NOW_BUILDER": "hi" } },
];
for (const envInfo of fakeCIEnvs) {
test(`CI user agent name: ${envInfo.ci}`, async () => {
const token = await generateRegistryUser(`otp-${envInfo.ci}`, "otp");
using mockRegistry = Bun.serve({
port: 0,
fetch: mockRegistryFetch({ token, expectedCI: envInfo.ci }),
});

const bunfig = `
[install]
cache = false
registry = { url = "http://localhost:${mockRegistry.port}", token = "${token}" }`;

await Promise.all([
rm(join(import.meta.dir, "packages", "otp-pkg-4"), { recursive: true, force: true }),
write(join(packageDir, "bunfig.toml"), bunfig),
write(
join(packageDir, "package.json"),
JSON.stringify({
name: "otp-pkg-4",
version: "4.4.4",
dependencies: {
"otp-pkg-4": "4.4.4",
},
}),
),
]);

const { out, err, exitCode } = await publish(
{ ...env, ...envInfo.envs, ...{ BUILDKITE: undefined, GITHUB_ACTIONS: undefined } },
packageDir,
);
expect(exitCode).toBe(0);
});
}
});

test("can publish a package then install it", async () => {
Expand Down Expand Up @@ -7873,7 +7919,7 @@ for (const forceWaiterThread of isLinux ? [false, true] : [false]) {
env: testEnv,
});

let err = await Bun.readableStreamToText(stderr);
let err = stderrForInstall(await Bun.readableStreamToText(stderr));
expect(err).toContain("Saved lockfile");
expect(err).not.toContain("not found");
expect(err).not.toContain("error:");
Expand Down Expand Up @@ -7906,7 +7952,7 @@ for (const forceWaiterThread of isLinux ? [false, true] : [false]) {
env: testEnv,
}));

err = await Bun.readableStreamToText(stderr);
err = stderrForInstall(await Bun.readableStreamToText(stderr));
expect(err).not.toContain("Saved lockfile");
expect(err).not.toContain("not found");
expect(err).not.toContain("error:");
Expand Down Expand Up @@ -7940,7 +7986,7 @@ for (const forceWaiterThread of isLinux ? [false, true] : [false]) {
env: testEnv,
});

const err = await Bun.readableStreamToText(stderr);
const err = stderrForInstall(await Bun.readableStreamToText(stderr));
expect(err).toContain("Saved lockfile");
expect(err).not.toContain("not found");
expect(err).not.toContain("error:");
Expand Down Expand Up @@ -7980,7 +8026,7 @@ for (const forceWaiterThread of isLinux ? [false, true] : [false]) {
env: testEnv,
});

let err = await Bun.readableStreamToText(stderr);
let err = stderrForInstall(await Bun.readableStreamToText(stderr));
expect(err).toContain("Saved lockfile");
expect(err).not.toContain("not found");
expect(err).not.toContain("error:");
Expand Down Expand Up @@ -8067,7 +8113,7 @@ for (const forceWaiterThread of isLinux ? [false, true] : [false]) {
env: testEnv,
});

let err = await Bun.readableStreamToText(stderr);
let err = stderrForInstall(await Bun.readableStreamToText(stderr));
expect(err).toContain("Saved lockfile");
expect(err).not.toContain("not found");
expect(err).not.toContain("error:");
Expand Down Expand Up @@ -8102,7 +8148,7 @@ for (const forceWaiterThread of isLinux ? [false, true] : [false]) {
env: testEnv,
}));

err = await Bun.readableStreamToText(stderr);
err = stderrForInstall(await Bun.readableStreamToText(stderr));
expect(err).not.toContain("Saved lockfile");
expect(err).not.toContain("not found");
expect(err).not.toContain("error:");
Expand Down Expand Up @@ -8140,7 +8186,7 @@ for (const forceWaiterThread of isLinux ? [false, true] : [false]) {
env: testEnv,
});

let err = await Bun.readableStreamToText(stderr);
let err = stderrForInstall(await Bun.readableStreamToText(stderr));
expect(err).toContain("Saved lockfile");
expect(err).not.toContain("not found");
expect(err).not.toContain("error:");
Expand Down Expand Up @@ -8187,7 +8233,7 @@ for (const forceWaiterThread of isLinux ? [false, true] : [false]) {
env: testEnv,
}));

err = await Bun.readableStreamToText(stderr);
err = stderrForInstall(await Bun.readableStreamToText(stderr));
expect(err).toContain("Saved lockfile");
expect(err).not.toContain("not found");
expect(err).not.toContain("error:");
Expand Down
7 changes: 6 additions & 1 deletion test/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ export async function runBunInstall(
});
expect(stdout).toBeDefined();
expect(stderr).toBeDefined();
let err = (await new Response(stderr).text()).replace(/warn: Slow filesystem.*/g, "");
let err = stderrForInstall(await new Response(stderr).text());
expect(err).not.toContain("panic:");
if (!options?.allowErrors) {
expect(err).not.toContain("error:");
Expand All @@ -1022,6 +1022,11 @@ export async function runBunInstall(
return { out, err, exited };
}

// stderr with `slow filesystem` warning removed
export function stderrForInstall(err: string) {
return err.replace(/warn: Slow filesystem.*/g, "");
}

export async function runBunUpdate(
env: NodeJS.ProcessEnv,
cwd: string,
Expand Down

0 comments on commit f307d2a

Please sign in to comment.