Remove use of Bun.file (#14215)

This commit is contained in:
Dax
2026-02-19 11:32:32 -05:00
committed by GitHub
parent 0fcba68d4c
commit 02a9495063
44 changed files with 634 additions and 473 deletions

View File

@@ -7,6 +7,7 @@ import path from "path"
import fs from "fs/promises"
import { pathToFileURL } from "url"
import { Global } from "../../src/global"
import { Filesystem } from "../../src/util/filesystem"
// Get managed config directory from environment (set in preload.ts)
const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR!
@@ -17,11 +18,11 @@ afterEach(async () => {
async function writeManagedSettings(settings: object, filename = "opencode.json") {
await fs.mkdir(managedConfigDir, { recursive: true })
await Bun.write(path.join(managedConfigDir, filename), JSON.stringify(settings))
await Filesystem.write(path.join(managedConfigDir, filename), JSON.stringify(settings))
}
async function writeConfig(dir: string, config: object, name = "opencode.json") {
await Bun.write(path.join(dir, name), JSON.stringify(config))
await Filesystem.write(path.join(dir, name), JSON.stringify(config))
}
test("loads config with defaults when no files exist", async () => {
@@ -58,7 +59,7 @@ test("loads JSON config file", async () => {
test("loads JSONC config file", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.jsonc"),
`{
// This is a comment
@@ -144,7 +145,7 @@ test("preserves env variables when adding $schema to config", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
// Config without $schema - should trigger auto-add
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
theme: "{env:PRESERVE_VAR}",
@@ -159,7 +160,7 @@ test("preserves env variables when adding $schema to config", async () => {
expect(config.theme).toBe("secret_value")
// Read the file to verify the env variable was preserved
const content = await Bun.file(path.join(tmp.path, "opencode.json")).text()
const content = await Filesystem.readText(path.join(tmp.path, "opencode.json"))
expect(content).toContain("{env:PRESERVE_VAR}")
expect(content).not.toContain("secret_value")
expect(content).toContain("$schema")
@@ -177,7 +178,7 @@ test("preserves env variables when adding $schema to config", async () => {
test("handles file inclusion substitution", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(path.join(dir, "included.txt"), "test_theme")
await Filesystem.write(path.join(dir, "included.txt"), "test_theme")
await writeConfig(dir, {
$schema: "https://opencode.ai/config.json",
theme: "{file:included.txt}",
@@ -196,7 +197,7 @@ test("handles file inclusion substitution", async () => {
test("handles file inclusion with replacement tokens", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(path.join(dir, "included.md"), "const out = await Bun.$`echo hi`")
await Filesystem.write(path.join(dir, "included.md"), "const out = await Bun.$`echo hi`")
await writeConfig(dir, {
$schema: "https://opencode.ai/config.json",
theme: "{file:included.md}",
@@ -233,7 +234,7 @@ test("validates config schema and throws on invalid fields", async () => {
test("throws error for invalid JSON", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(path.join(dir, "opencode.json"), "{ invalid json }")
await Filesystem.write(path.join(dir, "opencode.json"), "{ invalid json }")
},
})
await Instance.provide({
@@ -336,7 +337,7 @@ test("handles command configuration", async () => {
test("migrates autoshare to share field", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -358,7 +359,7 @@ test("migrates autoshare to share field", async () => {
test("migrates mode field to agent field", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -395,7 +396,7 @@ test("loads config from .opencode directory", async () => {
const agentDir = path.join(opencodeDir, "agent")
await fs.mkdir(agentDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(agentDir, "test.md"),
`---
model: test/model
@@ -428,7 +429,7 @@ test("loads agents from .opencode/agents (plural)", async () => {
const agentsDir = path.join(opencodeDir, "agents")
await fs.mkdir(path.join(agentsDir, "nested"), { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(agentsDir, "helper.md"),
`---
model: test/model
@@ -437,7 +438,7 @@ mode: subagent
Helper agent prompt`,
)
await Bun.write(
await Filesystem.write(
path.join(agentsDir, "nested", "child.md"),
`---
model: test/model
@@ -479,7 +480,7 @@ test("loads commands from .opencode/command (singular)", async () => {
const commandDir = path.join(opencodeDir, "command")
await fs.mkdir(path.join(commandDir, "nested"), { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(commandDir, "hello.md"),
`---
description: Test command
@@ -487,7 +488,7 @@ description: Test command
Hello from singular command`,
)
await Bun.write(
await Filesystem.write(
path.join(commandDir, "nested", "child.md"),
`---
description: Nested command
@@ -524,7 +525,7 @@ test("loads commands from .opencode/commands (plural)", async () => {
const commandsDir = path.join(opencodeDir, "commands")
await fs.mkdir(path.join(commandsDir, "nested"), { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(commandsDir, "hello.md"),
`---
description: Test command
@@ -532,7 +533,7 @@ description: Test command
Hello from plural commands`,
)
await Bun.write(
await Filesystem.write(
path.join(commandsDir, "nested", "child.md"),
`---
description: Nested command
@@ -568,7 +569,7 @@ test("updates config and writes to file", async () => {
const newConfig = { model: "updated/model" }
await Config.update(newConfig as any)
const writtenConfig = JSON.parse(await Bun.file(path.join(tmp.path, "config.json")).text())
const writtenConfig = await Filesystem.readJson(path.join(tmp.path, "config.json"))
expect(writtenConfig.model).toBe("updated/model")
},
})
@@ -639,8 +640,8 @@ test("installs dependencies in writable OPENCODE_CONFIG_DIR", async () => {
},
})
expect(await Bun.file(path.join(tmp.extra, "package.json")).exists()).toBe(true)
expect(await Bun.file(path.join(tmp.extra, ".gitignore")).exists()).toBe(true)
expect(await Filesystem.exists(path.join(tmp.extra, "package.json"))).toBe(true)
expect(await Filesystem.exists(path.join(tmp.extra, ".gitignore"))).toBe(true)
} finally {
if (prev === undefined) delete process.env.OPENCODE_CONFIG_DIR
else process.env.OPENCODE_CONFIG_DIR = prev
@@ -653,12 +654,12 @@ test("resolves scoped npm plugins in config", async () => {
const pluginDir = path.join(dir, "node_modules", "@scope", "plugin")
await fs.mkdir(pluginDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(dir, "package.json"),
JSON.stringify({ name: "config-fixture", version: "1.0.0", type: "module" }, null, 2),
)
await Bun.write(
await Filesystem.write(
path.join(pluginDir, "package.json"),
JSON.stringify(
{
@@ -672,9 +673,9 @@ test("resolves scoped npm plugins in config", async () => {
),
)
await Bun.write(path.join(pluginDir, "index.js"), "export default {}\n")
await Filesystem.write(path.join(pluginDir, "index.js"), "export default {}\n")
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: ["@scope/plugin"] }, null, 2),
)
@@ -708,7 +709,7 @@ test("merges plugin arrays from global and local configs", async () => {
await fs.mkdir(opencodeDir, { recursive: true })
// Global config with plugins
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -717,7 +718,7 @@ test("merges plugin arrays from global and local configs", async () => {
)
// Local .opencode config with different plugins
await Bun.write(
await Filesystem.write(
path.join(opencodeDir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -753,7 +754,7 @@ test("does not error when only custom agent is a subagent", async () => {
const agentDir = path.join(opencodeDir, "agent")
await fs.mkdir(agentDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(agentDir, "helper.md"),
`---
model: test/model
@@ -784,7 +785,7 @@ test("merges instructions arrays from global and local configs", async () => {
const opencodeDir = path.join(projectDir, ".opencode")
await fs.mkdir(opencodeDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -792,7 +793,7 @@ test("merges instructions arrays from global and local configs", async () => {
}),
)
await Bun.write(
await Filesystem.write(
path.join(opencodeDir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -823,7 +824,7 @@ test("deduplicates duplicate instructions from global and local configs", async
const opencodeDir = path.join(projectDir, ".opencode")
await fs.mkdir(opencodeDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -831,7 +832,7 @@ test("deduplicates duplicate instructions from global and local configs", async
}),
)
await Bun.write(
await Filesystem.write(
path.join(opencodeDir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -867,7 +868,7 @@ test("deduplicates duplicate plugins from global and local configs", async () =>
await fs.mkdir(opencodeDir, { recursive: true })
// Global config with plugins
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -876,7 +877,7 @@ test("deduplicates duplicate plugins from global and local configs", async () =>
)
// Local .opencode config with some overlapping plugins
await Bun.write(
await Filesystem.write(
path.join(opencodeDir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -915,7 +916,7 @@ test("deduplicates duplicate plugins from global and local configs", async () =>
test("migrates legacy tools config to permissions - allow", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -946,7 +947,7 @@ test("migrates legacy tools config to permissions - allow", async () => {
test("migrates legacy tools config to permissions - deny", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -977,7 +978,7 @@ test("migrates legacy tools config to permissions - deny", async () => {
test("migrates legacy write tool to edit permission", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1086,7 +1087,7 @@ test("missing managed settings file is not an error", async () => {
test("migrates legacy edit tool to edit permission", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1115,7 +1116,7 @@ test("migrates legacy edit tool to edit permission", async () => {
test("migrates legacy patch tool to edit permission", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1144,7 +1145,7 @@ test("migrates legacy patch tool to edit permission", async () => {
test("migrates legacy multiedit tool to edit permission", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1173,7 +1174,7 @@ test("migrates legacy multiedit tool to edit permission", async () => {
test("migrates mixed legacy tools config", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1208,7 +1209,7 @@ test("migrates mixed legacy tools config", async () => {
test("merges legacy tools with existing permission config", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1241,7 +1242,7 @@ test("merges legacy tools with existing permission config", async () => {
test("permission config preserves key order", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1289,7 +1290,7 @@ test("project config can override MCP server enabled status", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
// Simulates a base config (like from remote .well-known) with disabled MCP
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.jsonc"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1308,7 +1309,7 @@ test("project config can override MCP server enabled status", async () => {
}),
)
// Project config enables just jira
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1347,7 +1348,7 @@ test("MCP config deep merges preserving base config properties", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
// Base config with full MCP definition
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.jsonc"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1364,7 +1365,7 @@ test("MCP config deep merges preserving base config properties", async () => {
}),
)
// Override just enables it, should preserve other properties
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1399,7 +1400,7 @@ test("local .opencode config can override MCP from project config", async () =>
await using tmp = await tmpdir({
init: async (dir) => {
// Project config with disabled MCP
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1415,7 +1416,7 @@ test("local .opencode config can override MCP from project config", async () =>
// Local .opencode directory config enables it
const opencodeDir = path.join(dir, ".opencode")
await fs.mkdir(opencodeDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(opencodeDir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1483,7 +1484,7 @@ test("project config overrides remote well-known config", async () => {
git: true,
init: async (dir) => {
// Project config enables jira (overriding remote default)
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1576,7 +1577,7 @@ describe("deduplicatePlugins", () => {
const pluginDir = path.join(opencodeDir, "plugin")
await fs.mkdir(pluginDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1584,7 +1585,7 @@ describe("deduplicatePlugins", () => {
}),
)
await Bun.write(path.join(pluginDir, "my-plugin.js"), "export default {}")
await Filesystem.write(path.join(pluginDir, "my-plugin.js"), "export default {}")
},
})
@@ -1611,7 +1612,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await using tmp = await tmpdir({
init: async (dir) => {
// Create a project config that would normally be loaded
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1649,7 +1650,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
// Create a .opencode directory with a command
const opencodeDir = path.join(dir, ".opencode", "command")
await fs.mkdir(opencodeDir, { recursive: true })
await Bun.write(path.join(opencodeDir, "test-cmd.md"), "# Test Command\nThis is a test command.")
await Filesystem.write(path.join(opencodeDir, "test-cmd.md"), "# Test Command\nThis is a test command.")
},
})
await Instance.provide({
@@ -1706,7 +1707,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await using tmp = await tmpdir({
init: async (dir) => {
// Create a config with relative instruction path
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1714,7 +1715,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
}),
)
// Create the instruction file (should be skipped)
await Bun.write(path.join(dir, "CUSTOM.md"), "# Custom Instructions")
await Filesystem.write(path.join(dir, "CUSTOM.md"), "# Custom Instructions")
},
})
@@ -1752,7 +1753,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await using configDirTmp = await tmpdir({
init: async (dir) => {
// Create config in the custom config dir
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1765,7 +1766,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await using projectTmp = await tmpdir({
init: async (dir) => {
// Create config in project (should be ignored)
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",