Compare commits

...

2 Commits

Author SHA1 Message Date
Don Isaac
d20463954a test(cli/init): refactor init.test.ts 2025-01-10 20:58:43 -08:00
Don Isaac
254133c258 feat(cli/init): new repos use text-based lockfile 2025-01-10 20:58:43 -08:00
3 changed files with 109 additions and 65 deletions

View File

@@ -0,0 +1,6 @@
# Bun-specific configuration. This file is optional.
# Docs: https://bun.sh/docs/runtime/bunfig
[install]
# Use a text-based lockfile instead of a binary one. This will become the default in v1.2
saveTextLockfile = true

View File

@@ -65,6 +65,7 @@ pub const InitCommand = struct {
const @".gitignore" = @embedFile("init/gitignore.default");
const @"tsconfig.json" = @embedFile("init/tsconfig.default.json");
const @"README.md" = @embedFile("init/README.default.md");
const @"bunfig.toml" = @embedFile("init/bunfig.default.toml");
/// Create a new asset file, overriding anything that already exists. Known
/// assets will have their contents pre-populated; otherwise the file will be empty.
@@ -321,6 +322,7 @@ pub const InitCommand = struct {
write_gitignore: bool = true,
write_package_json: bool = true,
write_tsconfig: bool = true,
write_bunfig: bool = true,
write_readme: bool = true,
};
@@ -342,6 +344,8 @@ pub const InitCommand = struct {
break :brk true;
};
steps.write_bunfig = !existsZ("bunfig.toml");
{
try fields.object.putString(alloc, "name", fields.name);
if (fields.entry_point.len > 0) {
@@ -440,6 +444,12 @@ pub const InitCommand = struct {
};
}
if (steps.write_bunfig) {
Assets.create("bunfig.toml", .{}) catch {
// suppressed
};
}
if (steps.write_tsconfig) {
brk: {
const extname = std.fs.path.extension(fields.entry_point);

View File

@@ -1,76 +1,104 @@
import { expect, test } from "bun:test";
import type { SyncSubprocess } from "bun";
import { describe, beforeAll, afterAll, expect, test, it, jest } from "bun:test";
import fs from "fs";
import { bunEnv, bunExe, tmpdirSync } from "harness";
import path from "path";
test("bun init works", () => {
const temp = tmpdirSync();
jest.setTimeout(30_000);
const out = Bun.spawnSync({
cmd: [bunExe(), "init", "-y"],
cwd: temp,
stdio: ["ignore", "inherit", "inherit"],
env: bunEnv,
/** package.json, README.md not included */
const filesCreated = ["index.ts", ".gitignore", "node_modules", "tsconfig.json", "bunfig.toml"];
const defaultPackageJson = ({ name }) => ({
"name": name,
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest",
},
"peerDependencies": {
"typescript": "^5.0.0",
},
});
describe("`bun init -y`", () => {
let temp: string;
let out: SyncSubprocess<"ignore", "inherit">;
beforeAll(() => {
temp = tmpdirSync();
out = Bun.spawnSync({
cmd: [bunExe(), "init", "-y"],
cwd: temp,
stdio: ["ignore", "ignore", "inherit"],
env: bunEnv,
});
});
expect(out.signal).toBe(undefined);
expect(out.exitCode).toBe(0);
const pkg = JSON.parse(fs.readFileSync(path.join(temp, "package.json"), "utf8"));
expect(pkg).toEqual({
"name": path.basename(temp).toLowerCase(),
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest",
},
"peerDependencies": {
"typescript": "^5.0.0",
},
});
const readme = fs.readFileSync(path.join(temp, "README.md"), "utf8");
expect(readme).toStartWith("# " + path.basename(temp).toLowerCase() + "\n");
expect(readme).toInclude("v" + Bun.version.replaceAll("-debug", ""));
expect(readme).toInclude("index.ts");
expect(fs.existsSync(path.join(temp, "index.ts"))).toBe(true);
expect(fs.existsSync(path.join(temp, ".gitignore"))).toBe(true);
expect(fs.existsSync(path.join(temp, "node_modules"))).toBe(true);
expect(fs.existsSync(path.join(temp, "tsconfig.json"))).toBe(true);
}, 30_000);
test("bun init with piped cli", () => {
const temp = tmpdirSync();
const out = Bun.spawnSync({
cmd: [bunExe(), "init"],
cwd: temp,
stdio: [new Blob(["\n\n\n\n\n\n\n\n\n\n\n\n"]), "inherit", "inherit"],
env: bunEnv,
afterAll(() => {
Bun.$`rm -rf ${temp}`.nothrow();
});
expect(out.signal).toBe(undefined);
expect(out.exitCode).toBe(0);
const pkg = JSON.parse(fs.readFileSync(path.join(temp, "package.json"), "utf8"));
expect(pkg).toEqual({
"name": path.basename(temp).toLowerCase(),
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest",
},
"peerDependencies": {
"typescript": "^5.0.0",
},
it("exits successfully", () => {
expect(out).not.toHaveProperty("signal");
expect(out.exitCode).toBe(0);
});
const readme = fs.readFileSync(path.join(temp, "README.md"), "utf8");
expect(readme).toStartWith("# " + path.basename(temp).toLowerCase() + "\n");
expect(readme).toInclude("v" + Bun.version.replaceAll("-debug", ""));
expect(readme).toInclude("index.ts");
expect(fs.existsSync(path.join(temp, "index.ts"))).toBe(true);
expect(fs.existsSync(path.join(temp, ".gitignore"))).toBe(true);
expect(fs.existsSync(path.join(temp, "node_modules"))).toBe(true);
expect(fs.existsSync(path.join(temp, "tsconfig.json"))).toBe(true);
}, 30_000);
it("creates the expected package.json", () => {
const pkg = JSON.parse(fs.readFileSync(path.join(temp, "package.json"), "utf8"));
const expected = defaultPackageJson({ name: path.basename(temp).toLowerCase() });
expect(pkg).toEqual(expected);
});
it("populates the README.md template", () => {
const readme = fs.readFileSync(path.join(temp, "README.md"), "utf8");
expect(readme).toStartWith("# " + path.basename(temp).toLowerCase() + "\n");
expect(readme).toInclude("v" + Bun.version.replaceAll("-debug", ""));
expect(readme).toInclude("index.ts");
});
it.each(filesCreated)("creates %s", file => {
expect(fs.existsSync(path.join(temp, file))).toBeTrue();
});
});
describe("`bun init` wth piped cli", () => {
let temp: string;
let out: SyncSubprocess;
beforeAll(() => {
temp = tmpdirSync();
out = Bun.spawnSync({
cmd: [bunExe(), "init"],
cwd: temp,
stdio: [new Blob(["\n".repeat(12)]), "ignore", "inherit"],
env: bunEnv,
});
});
afterAll(() => {
Bun.$`rm -rf ${temp}`.nothrow();
});
it("exits successfully", () => {
expect(out).not.toHaveProperty("signal");
expect(out.exitCode).toBe(0);
});
it("creates the expected package.json", () => {
const pkg = JSON.parse(fs.readFileSync(path.join(temp, "package.json"), "utf8"));
const expected = defaultPackageJson({ name: path.basename(temp).toLowerCase() });
expect(pkg).toEqual(expected);
});
it("populates the README.md template", () => {
const readme = fs.readFileSync(path.join(temp, "README.md"), "utf8");
expect(readme).toStartWith("# " + path.basename(temp).toLowerCase() + "\n");
expect(readme).toInclude("v" + Bun.version.replaceAll("-debug", ""));
expect(readme).toInclude("index.ts");
});
it.each(filesCreated)("creates %s", file => {
expect(fs.existsSync(path.join(temp, file))).toBeTrue();
});
});