Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/cli/shell-files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { Bash } from "../Bash.js";
import { OverlayFs } from "../fs/overlay-fs/overlay-fs.js";
import { seedOverlayFiles } from "./shell-files.js";

describe("seedOverlayFiles", () => {
let tempDir: string;

beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "just-bash-shell-files-"));
fs.writeFileSync(path.join(tempDir, "real.txt"), "from-disk");
});

afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});

it("seeds overlay-only files for shell startup", async () => {
const overlayFs = new OverlayFs({ root: tempDir, mountPoint: "/" });
seedOverlayFiles(overlayFs, {
"/seeded.txt": "from-seed",
"nested/seeded2.txt": "from-seed-2",
});

const env = new Bash({ fs: overlayFs, cwd: "/" });
const one = await env.exec("cat /seeded.txt");
const two = await env.exec("cat /nested/seeded2.txt");

expect(one.exitCode).toBe(0);
expect(one.stdout).toBe("from-seed");
expect(two.exitCode).toBe(0);
expect(two.stdout).toBe("from-seed-2");
});

it("does nothing when files are omitted", async () => {
const overlayFs = new OverlayFs({ root: tempDir, mountPoint: "/" });
seedOverlayFiles(overlayFs, undefined);

const env = new Bash({ fs: overlayFs, cwd: "/" });
const result = await env.exec("cat /seeded.txt");

expect(result.exitCode).toBe(1);
expect(result.stderr).toContain("No such file or directory");
});
});
15 changes: 15 additions & 0 deletions src/cli/shell-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { OverlayFs } from "../fs/overlay-fs/overlay-fs.js";

/**
* Seed in-memory overlay files for shell startup fixtures.
* Paths are virtual shell paths (e.g. "/tmp/data.txt").
*/
export function seedOverlayFiles(
overlayFs: OverlayFs,
files?: Record<string, string>,
): void {
if (!files) return;
for (const [path, content] of Object.entries(files)) {
overlayFs.writeFileSync(path, content);
}
}
2 changes: 2 additions & 0 deletions src/cli/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as readline from "node:readline";
import { Bash } from "../Bash.js";
import { OverlayFs } from "../fs/overlay-fs/overlay-fs.js";
import { getErrorMessage } from "../interpreter/helpers/errors.js";
import { seedOverlayFiles } from "./shell-files.js";

// ANSI colors
const colors = {
Expand Down Expand Up @@ -47,6 +48,7 @@ class VirtualShell {
root,
mountPoint: "/",
});
seedOverlayFiles(overlayFs, options.files);

this.env = new Bash({
fs: overlayFs,
Expand Down