-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommit_test.go
More file actions
210 lines (200 loc) · 7.21 KB
/
Copy pathcommit_test.go
File metadata and controls
210 lines (200 loc) · 7.21 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package hpatch
import (
"bytes"
"github.com/yusing/hpatch/internal/patchtest"
"reflect"
"strings"
"testing"
)
func TestCommitMaterializesGenerationBaseline(t *testing.T) {
tests := []struct {
name string
initial map[string]string
script string
want map[string]string
}{
{
name: "introduced text becomes selectable",
initial: map[string]string{"file.txt": "old\n"},
script: "in file.txt\ntsel 1 \"old\"\ntype \"middle\"\ncommit\ntsel 1 \"middle\"\ntype \"final\"\n",
want: map[string]string{"file.txt": "final\n"},
},
{
name: "new file accepts another generation edit",
script: "new note.txt\ntype \"first\"\ncommit\ntsel 1 \"first\"\ntype \"second\"\n",
want: map[string]string{"note.txt": "second"},
},
{
name: "materialized existing edit can be removed",
initial: map[string]string{"file.txt": "old\n"},
script: "in file.txt\ntsel 1 \"old\"\ntype \"new\"\ncommit\nrm\n",
want: map[string]string{},
},
{
name: "moves collapse across generations",
initial: map[string]string{"file.txt": "old\n"},
script: "in file.txt\nmv middle.txt\ncommit\nmv final.txt\n",
want: map[string]string{"final.txt": "old\n"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
for path, content := range test.initial {
writeTestFile(t, root, path, content, 0o644)
}
stdout, stderr, exitCode := runForTest(root, nil, test.script)
if exitCode != 0 || stdout != "" || stderr != "" {
t.Fatalf("Run() = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
if got := readTree(t, root); !reflect.DeepEqual(got, test.want) {
t.Fatalf("tree = %#v, want %#v", got, test.want)
}
})
}
}
func TestCommitPreservesClipboardAcrossGenerations(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "source.txt", "copied\n", 0o644)
script := "in source.txt\nrsel 1:1\ncopy\nnew destination.txt\ncommit\npaste\n"
stdout, stderr, exitCode := runForTest(root, nil, script)
if exitCode != 0 || stdout != "" || stderr != "" {
t.Fatalf("Run() = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
if got := readTestFile(t, root, "destination.txt"); got != "copied\n" {
t.Fatalf("destination.txt = %q", got)
}
}
func TestCommitReleasesLogicalPathsForLaterGenerations(t *testing.T) {
tests := []struct {
name string
script string
want map[string]string
}{
{
name: "moved original path can be reused",
script: "in a.txt\nmv b.txt\ncommit\nnew a.txt\ntype \"new\\n\"\n",
want: map[string]string{"a.txt": "new\n", "b.txt": "old\n"},
},
{
name: "removed path can be recreated",
script: "in a.txt\nrm\ncommit\nnew a.txt\ntype \"new\\n\"\n",
want: map[string]string{"a.txt": "new\n"},
},
}
for _, test := range tests {
for _, args := range [][]string{nil, {"translate"}} {
mode := "normal"
if len(args) != 0 {
mode = "translate"
}
t.Run(test.name+"/"+mode, func(t *testing.T) {
root := t.TempDir()
initial := map[string]string{"a.txt": "old\n"}
writeTestFile(t, root, "a.txt", initial["a.txt"], 0o644)
stdout, stderr, exitCode := runForTest(root, args, test.script)
if exitCode != 0 || stderr != "" {
t.Fatalf("Run() = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
got := readTree(t, root)
if len(args) != 0 {
if !reflect.DeepEqual(got, initial) {
t.Fatalf("translate mutated tree: %#v", got)
}
var err error
got, err = patchtest.Apply(initial, stdout)
if err != nil {
t.Fatalf("applying translated patch: %v\n%s", err, stdout)
}
} else if stdout != "" {
t.Fatalf("normal stdout = %q", stdout)
}
if !reflect.DeepEqual(got, test.want) {
t.Fatalf("tree = %#v, want %#v", got, test.want)
}
})
}
}
}
func TestCommitAllowsMoveBackToOriginalPathAsNetNoop(t *testing.T) {
const script = "in a.txt\nmv b.txt\ncommit\nmv a.txt\n"
root := t.TempDir()
writeTestFile(t, root, "a.txt", "old\n", 0o644)
stdout, stderr, exitCode := runForTest(root, nil, script)
if exitCode != 0 || stdout != "" || stderr != "" {
t.Fatalf("normal = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
if got := readTree(t, root); !reflect.DeepEqual(got, map[string]string{"a.txt": "old\n"}) {
t.Fatalf("normal tree = %#v", got)
}
stdout, stderr, exitCode = runForTest(root, []string{"translate"}, script)
if exitCode == 0 || stdout != "" || !strings.Contains(stderr, "does not change") {
t.Fatalf("translate no-op = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
}
func TestGenerationReservationPreventsSameGenerationPathReuse(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "a.txt", "old\n", 0o644)
stdout, stderr, exitCode := runForTest(root, []string{"translate"}, "in a.txt\nmv b.txt\nnew a.txt\n")
if exitCode != 1 || stdout != "" || !strings.Contains(stderr, "destination a.txt already exists") {
t.Fatalf("Run() = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
}
func TestFailureAfterCommitRemainsAtomic(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "old\n", 0o644)
script := "in file.txt\ntsel 1 \"old\"\ntype \"middle\"\ncommit\ntsel 1 \"missing\"\n"
stdout, stderr, exitCode := runForTest(root, []string{"translate"}, script)
if exitCode != 1 || stdout != "" || !strings.Contains(stderr, "found 0 of 1 requested matches of \"missing\" at or after line 1") {
t.Fatalf("Run() = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
if got := readTestFile(t, root, "file.txt"); got != "old\n" {
t.Fatalf("failure exposed materialized content: %q", got)
}
}
func TestCommitResetsStateButScriptEndDoesNot(t *testing.T) {
tests := []struct {
name string
script string
wantHeader string
wantLine string
}{
{
name: "explicit commit resets cursor",
script: "in file.txt\ntsel 2 \"beta\"\ntype \"B\"\ncommit\n",
wantHeader: "in file.txt 1:1",
wantLine: "2|B",
},
{
name: "no-op commit clears selection",
script: "in file.txt\ntsel 2 \"beta\"\ncommit\n",
wantHeader: "in file.txt 1:1",
wantLine: "2|beta",
},
{
name: "script end preserves pending cursor",
script: "in file.txt\ntsel 2 \"beta\"\ntype \"B\"\ncommit\ntsel 2 \"B\"\ntype \"CC\"\n",
wantHeader: "in file.txt 2:3",
wantLine: "2|CC",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "alpha\nbeta\ngamma\n", 0o644)
var stdout, stderr bytes.Buffer
if exitCode := Run(nil, strings.NewReader(test.script), &stdout, &stderr, root, ""); exitCode != 0 || stdout.Len() != 0 {
t.Fatalf("Run() = exit %d, stdout %q, stderr %q", exitCode, stdout.String(), stderr.String())
}
if !strings.HasPrefix(stderr.String(), test.wantHeader+"\n") || !strings.Contains(stderr.String(), test.wantLine+"\n") {
t.Fatalf("report %q does not contain %q and %q", stderr.String(), test.wantHeader, test.wantLine)
}
})
}
}
func TestCommitWithoutActiveFileSucceeds(t *testing.T) {
stdout, stderr, exitCode := runForTest(t.TempDir(), nil, "commit\n")
if exitCode != 0 || stdout != "" || stderr != "" {
t.Fatalf("Run() = exit %d, stdout %q, stderr %q", exitCode, stdout, stderr)
}
}