Skip to content

Commit

Permalink
Fix poorly written test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed May 10, 2024
1 parent f52c177 commit 2b68b72
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"lint": "eslint './**/*.d.ts' --cache",
"lint:fix": "eslint './**/*.d.ts' --cache --fix",
"test": "node packages/bun-internal-test/src/runner.node.mjs ./build/bun-debug",
"test:release": "node packages/bun-internal-test/src/runner.node.mjs ./build-release/bun"
"test:release": "node packages/bun-internal-test/src/runner.node.mjs ./build-release/bun",
"install": "echo \"Ran bun install in the wrong directory. This is a bug in your test. Please fix your test.\" && exit 42",
"postinstall": "echo \"Ran bun install in the wrong directory. This is a bug in your test. Please fix your test.\" && exit 42",
"preinstall": "echo \"Ran bun install in the wrong directory. This is a bug in your test. Please fix your test.\" && exit 42",
"prepublish": "echo \"Ran bun install in the wrong directory. This is a bug in your test. Please fix your test.\" && exit 42"
}
}
}
17 changes: 17 additions & 0 deletions packages/bun-internal-test/src/runner.node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ uncygwinTempDir();
const cwd = resolve(fileURLToPath(import.meta.url), "../../../../");
process.chdir(cwd);

function makeRunningBunInstallInWrongDirectoryFailInCI() {
const paths = [join(cwd, "package.json"), join(cwd, "test", "package.json")];

for (const current of paths) {
const inputPackageJSON = JSON.parse(readFileSync(current, "utf-8"));
inputPackageJSON.scripts ??= {};
inputPackageJSON.scripts.prepublish =
inputPackageJSON.scripts.preinstall =
inputPackageJSON.scripts.postinstall =
inputPackageJSON.scripts.install =
`echo "Ran bun install in the wrong directory. This is a bug in your test. Please fix your test." && exit 42`;
writeFileSync(current, JSON.stringify(inputPackageJSON, null, 2));
}
}

makeRunningBunInstallInWrongDirectoryFailInCI();

const ci = !!process.env["GITHUB_ACTIONS"];
const enableProgressBar = false;

Expand Down
1 change: 1 addition & 0 deletions test/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const isLinux = process.platform === "linux";
export const isPosix = isMacOS || isLinux;
export const isWindows = process.platform === "win32";
export const isIntelMacOS = isMacOS && process.arch === "x64";
export const isBunCI = !!process.env.BUN_FEATURE_FLAG_INTERNAL_FOR_TESTING;

export const bunEnv: NodeJS.ProcessEnv = {
...process.env,
Expand Down
32 changes: 16 additions & 16 deletions test/js/third_party/stripe.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { bunExe } from "bun:harness";
import { bunEnv, tmpdirSync } from "harness";
import { expect, it } from "bun:test";
import { bunEnv, isBunCI, tmpdirSync } from "harness";
import * as path from "node:path";
import { createTest } from "node-harness";
const { describe, expect, it, beforeAll, afterAll, createDoneDotAll } = createTest(import.meta.path);

it.skipIf(!process.env.TEST_INFO_STRIPE)("should be able to query a charge", async () => {
it.skipIf(!isBunCI && !process.env.TEST_INFO_STRIPE)("should be able to query a charge", async () => {
const package_dir = tmpdirSync("bun-test-");

await Bun.write(
Expand All @@ -16,26 +15,23 @@ it.skipIf(!process.env.TEST_INFO_STRIPE)("should be able to query a charge", asy
}),
);

let { stdout, stderr } = Bun.spawn({
let { exited } = Bun.spawn({
cmd: [bunExe(), "install"],
stdout: "pipe",
stdout: "inherit",
cwd: package_dir,
stdin: "ignore",
stderr: "pipe",
stderr: "inherit",
env: bunEnv,
});
let err = await new Response(stderr).text();
expect(err).not.toContain("panic:");
expect(err).not.toContain("error:");
expect(err).not.toContain("warn:");
let out = await new Response(stdout).text();
expect(await exited).toBe(0);

// prettier-ignore
const [access_token, charge_id, account_id] = process.env.TEST_INFO_STRIPE?.split(",");

const fixture_path = path.join(package_dir, "index.js");
await Bun.write(
fixture_path,
String.raw`
`
const Stripe = require("stripe");
const stripe = Stripe("${access_token}");
Expand All @@ -48,16 +44,20 @@ it.skipIf(!process.env.TEST_INFO_STRIPE)("should be able to query a charge", asy
});
`,
);
let stdout, stderr;

({ stdout, stderr } = Bun.spawn({
({ stdout, stderr, exited } = Bun.spawn({
cmd: [bunExe(), "run", fixture_path],
stdout: "pipe",
stdin: "ignore",
stderr: "pipe",
cwd: package_dir,
env: bunEnv,
}));
out = await new Response(stdout).text();
let out = await new Response(stdout).text();
expect(out).toBeEmpty();
err = await new Response(stderr).text();
let err = await new Response(stderr).text();
expect(err).toContain(`error: No such charge: '${charge_id}'\n`);

expect(await exited).toBe(1);
});
8 changes: 6 additions & 2 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
},
"private": true,
"scripts": {
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"install": "echo \"Ran bun install in the wrong directory. This is a bug in your test. Please fix your test.\" && exit 42",
"postinstall": "echo \"Ran bun install in the wrong directory. This is a bug in your test. Please fix your test.\" && exit 42",
"preinstall": "echo \"Ran bun install in the wrong directory. This is a bug in your test. Please fix your test.\" && exit 42",
"prepublish": "echo \"Ran bun install in the wrong directory. This is a bug in your test. Please fix your test.\" && exit 42"
}
}
}

0 comments on commit 2b68b72

Please sign in to comment.