Skip to content

Commit

Permalink
test: unify how all files create a temp directory (#11057)
Browse files Browse the repository at this point in the history
Co-authored-by: nektro <nektro@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
  • Loading branch information
3 people committed May 15, 2024
1 parent 19f210b commit 4efe026
Show file tree
Hide file tree
Showing 62 changed files with 200 additions and 274 deletions.
4 changes: 2 additions & 2 deletions test/bundler/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bunEnv, bunExe } from "harness";
import { bunEnv, bunExe, tmpdirSync } from "harness";
import { describe, expect, test } from "bun:test";
import fs from "node:fs";
import { tmpdir } from "node:os";
Expand Down Expand Up @@ -54,7 +54,7 @@ describe("bun build", () => {
});

test("works with utf8 bom", () => {
const tmp = fs.mkdtempSync(path.join(tmpdir(), "bun-build-utf8-bom-"));
const tmp = tmpdirSync();
const src = path.join(tmp, "index.js");
fs.writeFileSync(src, '\ufeffconsole.log("hello world");', { encoding: "utf8" });
const { exitCode } = Bun.spawnSync({
Expand Down
5 changes: 2 additions & 3 deletions test/cli/hot/hot.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { spawn } from "bun";
import { beforeAll, beforeEach, expect, it } from "bun:test";
import { bunExe, bunEnv, tempDirWithFiles, bunRun, bunRunAsScript } from "harness";
import { bunExe, bunEnv, tmpdirSync } from "harness";
import { cpSync, readFileSync, renameSync, rmSync, unlinkSync, writeFileSync, copyFileSync } from "fs";
import { join } from "path";
import { tmpdir } from "os";

let hotRunnerRoot: string = "",
cwd = "";
beforeEach(() => {
const hotPath = join(tmpdir(), "bun-hot-test-" + (Date.now() | 0) + "_" + Math.random().toString(36).slice(2));
const hotPath = tmpdirSync();
hotRunnerRoot = join(hotPath, "hot-runner-root.js");
rmSync(hotPath, { recursive: true, force: true });
cpSync(import.meta.dir, hotPath, { recursive: true, force: true });
Expand Down
8 changes: 4 additions & 4 deletions test/cli/init/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from "fs";
import path from "path";
import os from "os";
import { bunExe, bunEnv } from "harness";
import { bunExe, bunEnv, tmpdirSync } from "harness";
import { test, expect } from "bun:test";

test("bun init works", () => {
const temp = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bun-init-X")));
const temp = tmpdirSync();

const out = Bun.spawnSync({
cmd: [bunExe(), "init", "-y"],
Expand Down Expand Up @@ -40,7 +40,7 @@ test("bun init works", () => {
}, 30_000);

test("bun init with piped cli", () => {
const temp = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bun-init-X")));
const temp = tmpdirSync();

const out = Bun.spawnSync({
cmd: [bunExe(), "init"],
Expand Down
10 changes: 4 additions & 6 deletions test/cli/install/bad-workspace.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { spawnSync } from "bun";
import { afterEach, beforeEach, expect, test } from "bun:test";
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from "fs";
import { bunExe, bunEnv } from "harness";
import { join } from "path";
import { tmpdir } from "os";
import { beforeEach, expect, test } from "bun:test";
import { writeFileSync } from "fs";
import { bunExe, bunEnv, tmpdirSync } from "harness";

let cwd: string;

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

test("bad workspace path", () => {
Expand Down
9 changes: 4 additions & 5 deletions test/cli/install/bun-add.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { file, spawn } from "bun";
import { afterAll, afterEach, beforeAll, beforeEach, expect, it } from "bun:test";
import { bunExe, bunEnv as env, toHaveBins, toBeValidBin, toBeWorkspaceLink, ospath } from "harness";
import { access, mkdir, mkdtemp, readlink, realpath, rm, writeFile, copyFile, appendFile } from "fs/promises";
import { bunExe, bunEnv as env, toHaveBins, toBeValidBin, toBeWorkspaceLink, tmpdirSync } from "harness";
import { access, mkdir, readlink, rm, writeFile, copyFile, appendFile } from "fs/promises";
import { join, relative } from "path";
import { tmpdir } from "os";
import {
dummyAfterAll,
dummyAfterEach,
Expand Down Expand Up @@ -33,7 +32,7 @@ beforeAll(() => {
});

beforeEach(async () => {
add_dir = await mkdtemp(join(await realpath(tmpdir()), "bun-add.test"));
add_dir = tmpdirSync();
await dummyBeforeEach();
});
afterEach(async () => {
Expand Down Expand Up @@ -1763,7 +1762,7 @@ async function installRedirectsToAdd(saveFlagFirst: boolean) {
" 1 package installed",
]);
expect(await exited).toBe(0);
expect(await file(join(package_dir, "package.json")).text()).toInclude("bun-add.test");
expect(await file(join(package_dir, "package.json")).text()).toInclude("bun.test.");
}

it("should add dependency alongside peerDependencies", async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/cli/install/bun-create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { join } from "path";
let x_dir: string;

beforeEach(async () => {
x_dir = tmpdirSync("bun-create.test");
x_dir = tmpdirSync();
});

describe("should not crash", async () => {
Expand Down
4 changes: 1 addition & 3 deletions test/cli/install/bun-install-pathname-trailing-slash.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { afterEach, beforeEach, expect, test } from "bun:test";
import { mkdtempSync, realpathSync, rmSync } from "fs";
import { bunEnv, bunExe, tmpdirSync } from "harness";
import { tmpdir } from "os";
import { join } from "path";

let package_dir: string;

beforeEach(() => {
package_dir = tmpdirSync("bun-install-path");
package_dir = tmpdirSync();
});

// https://github.com/oven-sh/bun/issues/2462
Expand Down
9 changes: 4 additions & 5 deletions test/cli/install/bun-link.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { spawn, file } from "bun";
import { afterAll, afterEach, beforeAll, beforeEach, expect, it } from "bun:test";
import { bunExe, bunEnv as env, toBeValidBin, toHaveBins } from "harness";
import { access, mkdtemp, readlink, realpath, rm, writeFile, mkdir } from "fs/promises";
import { basename, join, sep, dirname } from "path";
import { tmpdir } from "os";
import { bunExe, bunEnv as env, tmpdirSync, toBeValidBin, toHaveBins } from "harness";
import { access, writeFile, mkdir } from "fs/promises";
import { basename, join } from "path";
import {
dummyAfterAll,
dummyAfterEach,
Expand All @@ -24,7 +23,7 @@ expect.extend({
});

beforeEach(async () => {
link_dir = await mkdtemp(join(await realpath(tmpdir()), "bun-link.test"));
link_dir = tmpdirSync();
await dummyBeforeEach();
});
afterEach(async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/cli/install/bun-pm.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hash, spawn } from "bun";
import { afterAll, afterEach, beforeAll, beforeEach, expect, it } from "bun:test";
import { bunEnv, bunExe, bunEnv as env } from "harness";
import { bunEnv, bunExe, bunEnv as env, tmpdirSync } from "harness";
import { mkdir, writeFile, exists } from "fs/promises";
import { join } from "path";
import {
Expand Down Expand Up @@ -371,7 +371,7 @@ it("should remove all cache", async () => {

import { tmpdir } from "os";
it("bun pm migrate", async () => {
const test_dir = join(tmpdir(), "contoso-test" + Math.random().toString(36).slice(2));
const test_dir = tmpdirSync();

cpSync(join(import.meta.dir, "migration/contoso-test"), test_dir, { recursive: true });

Expand Down
7 changes: 3 additions & 4 deletions test/cli/install/bun-remove.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { bunExe, bunEnv as env } from "harness";
import { mkdir, mkdtemp, realpath, rm, writeFile } from "fs/promises";
import { bunExe, bunEnv as env, tmpdirSync } from "harness";
import { mkdir, writeFile } from "fs/promises";
import { join, relative } from "path";
import { tmpdir } from "os";
import { afterAll, afterEach, beforeAll, beforeEach, expect, it } from "bun:test";
import { dummyAfterAll, dummyAfterEach, dummyBeforeAll, dummyBeforeEach, package_dir } from "./dummy.registry";
import { spawn } from "bun";
Expand All @@ -13,7 +12,7 @@ afterAll(dummyAfterAll);
let remove_dir: string;

beforeEach(async () => {
remove_dir = await mkdtemp(join(await realpath(tmpdir()), "bun-remove.test"));
remove_dir = tmpdirSync();
await dummyBeforeEach();
});

Expand Down
9 changes: 3 additions & 6 deletions test/cli/install/bun-run.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { file, spawn, spawnSync } from "bun";
import { afterEach, beforeEach, expect, it, describe } from "bun:test";
import { bunEnv, bunExe, bunEnv as env, isWindows } from "harness";
import { mkdtemp, realpath, rm, writeFile, exists, mkdir } from "fs/promises";
import { tmpdir } from "os";
import { bunEnv, bunExe, bunEnv as env, isWindows, tmpdirSync } from "harness";
import { rm, writeFile, exists, mkdir } from "fs/promises";
import { join } from "path";
import { readdirSorted } from "./dummy.registry";

let run_dir: string;

beforeEach(async () => {
run_dir = await realpath(
await mkdtemp(join(tmpdir(), "bun-run.test." + Math.trunc(Math.random() * 9999999).toString(32))),
);
run_dir = tmpdirSync();
});

for (let withRun of [false, true]) {
Expand Down
8 changes: 2 additions & 6 deletions test/cli/install/bun-upgrade.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { spawn, spawnSync } from "bun";
import { beforeEach, expect, it } from "bun:test";
import { bunExe, bunEnv as env } from "harness";
import { mkdtemp, realpath, readFile } from "fs/promises";
import { tmpdir } from "os";
import { bunExe, bunEnv as env, tmpdirSync } from "harness";
import { join } from "path";
import { copyFileSync } from "js/node/fs/export-star-from";
import { upgrade_test_helpers } from "bun:internal-for-testing";
Expand All @@ -12,9 +10,7 @@ let run_dir: string;
let exe_name: string = "bun-debug" + (process.platform === "win32" ? ".exe" : "");

beforeEach(async () => {
run_dir = await realpath(
await mkdtemp(join(tmpdir(), "bun-upgrade.test." + Math.trunc(Math.random() * 9999999).toString(32))),
);
run_dir = tmpdirSync();
copyFileSync(bunExe(), join(run_dir, exe_name));
});

Expand Down
2 changes: 1 addition & 1 deletion test/cli/install/bun-workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var port: number = 4873;
var packageDir: string;

beforeEach(() => {
packageDir = tmpdirSync("bun-workspaces-" + testCounter++ + "-");
packageDir = tmpdirSync();
env.BUN_INSTALL_CACHE_DIR = join(packageDir, ".bun-cache");
env.BUN_TMPDIR = env.TMPDIR = env.TEMP = join(packageDir, ".bun-tmp");
writeFileSync(
Expand Down
10 changes: 5 additions & 5 deletions test/cli/install/bunx.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawn } from "bun";
import { afterEach, beforeEach, expect, it } from "bun:test";
import { bunExe, bunEnv, isWindows } from "harness";
import { mkdtemp, realpath, writeFile, rm } from "fs/promises";
import { bunExe, bunEnv, isWindows, tmpdirSync } from "harness";
import { writeFile, rm } from "fs/promises";
import { tmpdir } from "os";
import { join } from "path";
import { readdirSorted } from "./dummy.registry";
Expand Down Expand Up @@ -29,9 +29,9 @@ beforeEach(async () => {
}
});

install_cache_dir = await mkdtemp(join(tmpdir(), "bun-install-cache-" + Math.random().toString(36).slice(2)));
current_tmpdir = await realpath(await mkdtemp(join(tmpdir(), "bun-x-tmpdir" + Math.random().toString(36).slice(2))));
x_dir = await realpath(await mkdtemp(join(tmpdir(), "bun-x.test" + Math.random().toString(36).slice(2))));
install_cache_dir = tmpdirSync();
current_tmpdir = tmpdirSync();
x_dir = tmpdirSync();

env.TEMP = current_tmpdir;
env.BUN_TMPDIR = env.TMPDIR = current_tmpdir;
Expand Down
3 changes: 1 addition & 2 deletions test/cli/install/dummy.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type Pkg = {
};
let handler: Handler;
let server: Server;
let testCounter = 0;
export let package_dir: string;
export let requested: number;
export let root_url: string;
Expand Down Expand Up @@ -98,7 +97,7 @@ export function dummyAfterAll() {
}

let packageDirGetter: () => string = () => {
return tmpdirSync("bun-install-test-" + testCounter++ + "--");
return tmpdirSync();
};
export async function dummyBeforeEach() {
resetHandler();
Expand Down
6 changes: 2 additions & 4 deletions test/cli/install/migration/complex-workspace.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import fs from "fs";
import path from "path";
import { test, expect, describe, beforeAll } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { tmpdir } from "os";
import { join } from "path";
import { bunEnv, bunExe, tmpdirSync } from "harness";

let cwd = join(tmpdir(), "complex-workspace-test" + Math.random().toString(36).slice(2, 8));
let cwd = tmpdirSync();

function validate(packageName: string, version: string, realPackageName?: string) {
test(`${packageName} is ${realPackageName ? `${realPackageName}@${version}` : version}`, () => {
Expand Down
29 changes: 6 additions & 23 deletions test/cli/install/migration/migrate.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import fs from "fs";
import { test, expect, beforeAll, afterAll } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { join, sep } from "path";
import { mkdtempSync } from "js/node/fs/export-star-from";
import { tmpdir } from "os";

const ROOT_TEMP_DIR = join(tmpdir(), "migrate", sep);

beforeAll(() => {
// if the test was stopped early
fs.rmSync(ROOT_TEMP_DIR, { recursive: true, force: true });
fs.mkdirSync(ROOT_TEMP_DIR);
});

afterAll(() => {
fs.rmSync(ROOT_TEMP_DIR, {
recursive: true,
force: true,
});
});
import { test, expect } from "bun:test";
import { bunEnv, bunExe, tmpdirSync } from "harness";
import { join } from "path";

function testMigration(lockfile: string) {
const testDir = mkdtempSync(ROOT_TEMP_DIR);
const testDir = tmpdirSync();

fs.writeFileSync(
join(testDir, "package.json"),
Expand Down Expand Up @@ -58,7 +41,7 @@ test("migrate from npm lockfile v2 during `bun add`", () => {

// Currently this upgrades svelte :(
test.todo("migrate workspace from npm during `bun add`", async () => {
const testDir = mkdtempSync(ROOT_TEMP_DIR);
const testDir = tmpdirSync();

fs.cpSync(join(import.meta.dir, "add-while-migrate-workspace"), testDir, { recursive: true });

Expand All @@ -77,7 +60,7 @@ test.todo("migrate workspace from npm during `bun add`", async () => {
});

test("migrate from npm lockfile that is missing `resolved` properties", async () => {
const testDir = mkdtempSync(ROOT_TEMP_DIR);
const testDir = tmpdirSync();

fs.cpSync(join(import.meta.dir, "missing-resolved-properties"), testDir, { recursive: true });

Expand Down

0 comments on commit 4efe026

Please sign in to comment.