-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathis-tool-on-path.test.ts
More file actions
112 lines (97 loc) · 3.75 KB
/
is-tool-on-path.test.ts
File metadata and controls
112 lines (97 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { afterEach, describe, test, expect } from "bun:test"
import { $ } from "bun"
import fs from "fs/promises"
import path from "path"
import { tmpdir } from "../fixture/fixture"
import { isToolOnPath } from "../../src/cli/cmd/skill-helpers"
import { Instance } from "../../src/project/instance"
/** Create a tmpdir with git initialized (signing disabled for CI). */
async function tmpdirGit(init?: (dir: string) => Promise<void>) {
return tmpdir({
init: async (dir) => {
await $`git init`.cwd(dir).quiet()
await $`git config core.fsmonitor false`.cwd(dir).quiet()
await $`git config commit.gpgsign false`.cwd(dir).quiet()
await $`git config user.email "test@opencode.test"`.cwd(dir).quiet()
await $`git config user.name "Test"`.cwd(dir).quiet()
await $`git commit --allow-empty -m "root"`.cwd(dir).quiet()
await init?.(dir)
},
})
}
describe("isToolOnPath", () => {
const savedEnv: Record<string, string | undefined> = {}
afterEach(() => {
for (const [key, val] of Object.entries(savedEnv)) {
if (val === undefined) delete process.env[key]
else process.env[key] = val
}
Object.keys(savedEnv).forEach((k) => delete savedEnv[k])
})
test("returns true when tool exists in .opencode/tools/ under cwd", async () => {
await using tmp = await tmpdirGit(async (dir) => {
const toolsDir = path.join(dir, ".opencode", "tools")
await fs.mkdir(toolsDir, { recursive: true })
const toolPath = path.join(toolsDir, "my-test-tool")
await fs.writeFile(toolPath, "#!/bin/sh\necho ok\n")
await fs.chmod(toolPath, 0o755)
})
await Instance.provide({
directory: tmp.path,
fn: async () => {
const found = await isToolOnPath("my-test-tool", tmp.path)
expect(found).toBe(true)
},
})
})
test("returns false when tool does not exist anywhere", async () => {
savedEnv.ALTIMATE_BIN_DIR = process.env.ALTIMATE_BIN_DIR
delete process.env.ALTIMATE_BIN_DIR
await using tmp = await tmpdirGit()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const found = await isToolOnPath("altimate-nonexistent-tool-xyz-99999", tmp.path)
expect(found).toBe(false)
},
})
})
test("returns true when tool is found via ALTIMATE_BIN_DIR", async () => {
await using tmp = await tmpdirGit(async (dir) => {
const binDir = path.join(dir, "custom-bin")
await fs.mkdir(binDir, { recursive: true })
const toolPath = path.join(binDir, "my-bin-tool")
await fs.writeFile(toolPath, "#!/bin/sh\necho ok\n")
await fs.chmod(toolPath, 0o755)
})
savedEnv.ALTIMATE_BIN_DIR = process.env.ALTIMATE_BIN_DIR
process.env.ALTIMATE_BIN_DIR = path.join(tmp.path, "custom-bin")
await Instance.provide({
directory: tmp.path,
fn: async () => {
const found = await isToolOnPath("my-bin-tool", tmp.path)
expect(found).toBe(true)
},
})
})
test("returns true when tool is on PATH via prepended directory", async () => {
await using tmp = await tmpdirGit(async (dir) => {
const pathDir = path.join(dir, "path-bin")
await fs.mkdir(pathDir, { recursive: true })
const toolPath = path.join(pathDir, "my-path-tool")
await fs.writeFile(toolPath, "#!/bin/sh\necho ok\n")
await fs.chmod(toolPath, 0o755)
})
savedEnv.ALTIMATE_BIN_DIR = process.env.ALTIMATE_BIN_DIR
delete process.env.ALTIMATE_BIN_DIR
savedEnv.PATH = process.env.PATH
process.env.PATH = path.join(tmp.path, "path-bin") + ":" + (process.env.PATH ?? "")
await Instance.provide({
directory: tmp.path,
fn: async () => {
const found = await isToolOnPath("my-path-tool", tmp.path)
expect(found).toBe(true)
},
})
})
})