forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.test.ts
More file actions
178 lines (160 loc) · 6.71 KB
/
git.test.ts
File metadata and controls
178 lines (160 loc) · 6.71 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { $ } from "bun"
import { describe, expect } from "bun:test"
import fs from "fs/promises"
import path from "path"
import { Effect } from "effect"
import { Git } from "../../src/git"
import { tmpdir } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
const weird = process.platform === "win32" ? "space file.txt" : "tab\tfile.txt"
const it = testEffect(Git.defaultLayer)
const scopedTmpdir = (options?: Parameters<typeof tmpdir>[0]) =>
Effect.acquireRelease(
Effect.promise(() => tmpdir(options)),
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
)
describe("Git", () => {
it.live("branch() returns current branch name", () =>
Effect.gen(function* () {
const tmp = yield* scopedTmpdir({ git: true })
const git = yield* Git.Service
const branch = yield* git.branch(tmp.path)
expect(branch).toBeDefined()
expect(typeof branch).toBe("string")
}),
)
it.live("branch() returns undefined for non-git directories", () =>
Effect.gen(function* () {
const tmp = yield* scopedTmpdir()
const git = yield* Git.Service
const branch = yield* git.branch(tmp.path)
expect(branch).toBeUndefined()
}),
)
it.live("branch() returns undefined for detached HEAD", () =>
Effect.gen(function* () {
const tmp = yield* scopedTmpdir({ git: true })
const hash = (yield* Effect.promise(() => $`git rev-parse HEAD`.cwd(tmp.path).quiet().text())).trim()
yield* Effect.promise(() => $`git checkout --detach ${hash}`.cwd(tmp.path).quiet())
const git = yield* Git.Service
const branch = yield* git.branch(tmp.path)
expect(branch).toBeUndefined()
}),
)
it.live("defaultBranch() uses init.defaultBranch when available", () =>
Effect.gen(function* () {
const tmp = yield* scopedTmpdir({ git: true })
yield* Effect.promise(() => $`git branch -M trunk`.cwd(tmp.path).quiet())
yield* Effect.promise(() => $`git config init.defaultBranch trunk`.cwd(tmp.path).quiet())
const git = yield* Git.Service
const branch = yield* git.defaultBranch(tmp.path)
expect(branch?.name).toBe("trunk")
expect(branch?.ref).toBe("trunk")
}),
)
it.live("status() handles special filenames", () =>
Effect.gen(function* () {
const tmp = yield* scopedTmpdir({ git: true })
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "hello\n", "utf-8"))
const git = yield* Git.Service
const status = yield* git.status(tmp.path)
expect(status).toEqual(
expect.arrayContaining([
expect.objectContaining({
file: weird,
status: "added",
}),
]),
)
}),
)
it.live("diff(), stats(), and mergeBase() parse tracked changes", () =>
Effect.gen(function* () {
const tmp = yield* scopedTmpdir({ git: true })
yield* Effect.promise(() => $`git branch -M main`.cwd(tmp.path).quiet())
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "before\n", "utf-8"))
yield* Effect.promise(() => $`git add .`.cwd(tmp.path).quiet())
yield* Effect.promise(() => $`git commit --no-gpg-sign -m "add file"`.cwd(tmp.path).quiet())
yield* Effect.promise(() => $`git checkout -b feature/test`.cwd(tmp.path).quiet())
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "after\n", "utf-8"))
const git = yield* Git.Service
const [base, diff, stats] = yield* Effect.all([
git.mergeBase(tmp.path, "main"),
git.diff(tmp.path, "HEAD"),
git.stats(tmp.path, "HEAD"),
])
expect(base).toBeTruthy()
expect(diff).toEqual(
expect.arrayContaining([
expect.objectContaining({
file: weird,
status: "modified",
}),
]),
)
expect(stats).toEqual(
expect.arrayContaining([
expect.objectContaining({
file: weird,
additions: 1,
deletions: 1,
}),
]),
)
}),
)
it.live("patch() returns capped native patch output", () =>
Effect.gen(function* () {
const tmp = yield* scopedTmpdir({ git: true })
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "before\n", "utf-8"))
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "other.txt"), "old\n", "utf-8"))
yield* Effect.promise(() => $`git add .`.cwd(tmp.path).quiet())
yield* Effect.promise(() => $`git commit --no-gpg-sign -m "add file"`.cwd(tmp.path).quiet())
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "after\n", "utf-8"))
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "other.txt"), "new\n", "utf-8"))
const git = yield* Git.Service
const [patch, all, capped] = yield* Effect.all([
git.patch(tmp.path, "HEAD", weird, { context: 2_147_483_647 }),
git.patchAll(tmp.path, "HEAD", { context: 2_147_483_647 }),
git.patch(tmp.path, "HEAD", weird, { maxOutputBytes: 1 }),
])
expect(patch.truncated).toBe(false)
expect(patch.text).toContain("diff --git")
expect(patch.text).toContain("-before")
expect(patch.text).toContain("+after")
expect(all.truncated).toBe(false)
expect(all.text).toContain("diff --git")
expect(all.text).toContain("other.txt")
expect(all.text).toContain("+new")
expect(capped.truncated).toBe(true)
expect(capped.text).toBe("")
}),
)
it.live("patchUntracked() and statUntracked() handle added files", () =>
Effect.gen(function* () {
const tmp = yield* scopedTmpdir({ git: true })
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "one\ntwo\n", "utf-8"))
const git = yield* Git.Service
const [patch, stat] = yield* Effect.all([
git.patchUntracked(tmp.path, weird, { context: 2_147_483_647 }),
git.statUntracked(tmp.path, weird),
])
expect(patch.truncated).toBe(false)
expect(patch.text).toContain("diff --git")
expect(patch.text).toContain("+one")
expect(patch.text).toContain("+two")
expect(stat).toEqual(expect.objectContaining({ file: weird, additions: 2, deletions: 0 }))
}),
)
it.live("show() returns empty text for binary blobs", () =>
Effect.gen(function* () {
const tmp = yield* scopedTmpdir({ git: true })
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "bin.dat"), new Uint8Array([0, 1, 2, 3])))
yield* Effect.promise(() => $`git add .`.cwd(tmp.path).quiet())
yield* Effect.promise(() => $`git commit --no-gpg-sign -m "add binary"`.cwd(tmp.path).quiet())
const git = yield* Git.Service
const text = yield* git.show(tmp.path, "HEAD", "bin.dat")
expect(text).toBe("")
}),
)
})