Skip to content

Commit

Permalink
[install] fix tests (oven-sh#2488)
Browse files Browse the repository at this point in the history
- avoid creating spurious directories next to test scripts
  • Loading branch information
alexlamsl authored Mar 26, 2023
1 parent 52d2765 commit 319efe9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
20 changes: 14 additions & 6 deletions test/cli/install/bad-workspace.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { spawnSync } from "bun";
import { test, describe, expect } from "bun:test";
import { afterEach, beforeEach, expect, test } from "bun:test";
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from "fs";
import { bunExe, bunEnv } from "harness";
import { mkdirSync, rmSync, writeFileSync } from "fs";
import { join } from "path";
import { tmpdir } from "os";

test("bad workspace path", () => {
const cwd = "/tmp/bun-bad-workspace";
let cwd: string;

beforeEach(() => {
cwd = mkdtempSync(join(realpathSync(tmpdir()), "bad-workspace.test"));
});

afterEach(() => {
rmSync(cwd, { recursive: true, force: true });
mkdirSync(cwd);
});

test("bad workspace path", () => {
writeFileSync(
`${cwd}/package.json`,
JSON.stringify(
Expand All @@ -32,5 +41,4 @@ test("bad workspace path", () => {
expect(text).toContain("glob star * in the middle of a path");

expect(exitCode).toBe(1);
rmSync(cwd, { recursive: true, force: true });
});
30 changes: 16 additions & 14 deletions test/cli/install/bun-install-pathname-trailing-slash.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { sleep } from "bun";
import { expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync } from "fs";
import { afterEach, beforeEach, expect, test } from "bun:test";
import { mkdtempSync, realpathSync, rmSync } from "fs";
import { bunEnv, bunExe } from "harness";
import { tmpdir } from "os";
import { join } from "path";

let package_dir: string;

beforeEach(() => {
package_dir = mkdtempSync(join(realpathSync(tmpdir()), "bun-install-path"));
});

afterEach(() => {
rmSync(package_dir, { recursive: true, force: true });
});

// https://github.com/oven-sh/bun/issues/2462
test("custom registry doesn't have multiple trailing slashes in pathname", async () => {
var urls: string[] = [];
const urls: string[] = [];

var server = Bun.serve({
const server = Bun.serve({
port: 0,
async fetch(req) {
urls.push(req.url);
return new Response("ok");
},
});
const { port, hostname } = server;
const package_dir = join(tmpdir(), mkdtempSync("bun-install-path"));
try {
mkdirSync(package_dir, { recursive: true });
} catch {}
await Bun.write(
join(package_dir, "bunfig.toml"),
`
Expand Down Expand Up @@ -49,10 +54,7 @@ registry = "http://${hostname}:${port}/prefixed-route/"
stdin: "ignore",
});

await sleep(10);

server.stop(true);
expect(urls.length).toBeGreaterThan(0);
expect(urls[0]).toBe(`http://${hostname}:${port}/prefixed-route/react`);
rmSync(package_dir, { recursive: true, force: true });
expect(urls.length).toBe(1);
expect(urls).toEqual([`http://${hostname}:${port}/prefixed-route/react`]);
});
6 changes: 3 additions & 3 deletions test/cli/install/dummy.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import { file, Server } from "bun";

var expect: typeof import("bun:test")["expect"];
let expect: typeof import("bun:test")["expect"];

import { mkdtemp, readdir, realpath, rm, writeFile } from "fs/promises";
import { tmpdir } from "os";
Expand Down Expand Up @@ -94,8 +94,8 @@ export function dummyAfterAll() {
server.stop();
}

var packageDirGetter: () => Promise<string> = async () => {
return await realpath(await mkdtemp(join(await realpath(tmpdir()), "bun-install-test-" + testCounter++ + "--")));
let packageDirGetter: () => Promise<string> = async () => {
return await mkdtemp(join(await realpath(tmpdir()), "bun-install-test-" + testCounter++ + "--"));
};
export async function dummyBeforeEach() {
resetHandler();
Expand Down

0 comments on commit 319efe9

Please sign in to comment.