Debian Codegraph @

core/replacement_test.go

raw
1 package core
2
3 import (
4 "os"
5 "testing"
6
7 "github.com/miniscruff/changie/then"
8 )
9
10 func TestFindAndReplaceContentsInAFile(t *testing.T) {
11 then.WithTempDir(t)
12
13 filepath := "file.txt"
14 startData := `first line
15 second line
16 third line
17 ignore me`
18 endData := `first line
19 replaced here
20 third line
21 ignore me`
22
23 err := os.WriteFile(filepath, []byte(startData), os.ModePerm)
24 then.Nil(t, err)
25
26 rep := Replacement{
27 Path: filepath,
28 Find: "second line",
29 Replace: "replaced here",
30 }
31 err = rep.Execute(ReplaceData{})
32 then.Nil(t, err)
33 then.FileContents(t, endData, filepath)
34 }
35
36 func TestFindAndReplaceWithTemplate(t *testing.T) {
37 then.WithTempDir(t)
38
39 filepath := "template.txt"
40 startData := `{
41 "name": "demo-project",
42 "version": "1.0.0",
43 }`
44 endData := `{
45 "name": "demo-project",
46 "version": "1.1.0",
47 }`
48
49 err := os.WriteFile(filepath, []byte(startData), os.ModePerm)
50 then.Nil(t, err)
51
52 rep := Replacement{
53 Path: filepath,
54 Find: ` "version": ".*",`,
55 Replace: ` "version": "{{.VersionNoPrefix}}",`,
56 }
57 err = rep.Execute(ReplaceData{
58 VersionNoPrefix: "1.1.0",
59 })
60 then.Nil(t, err)
61 then.FileContents(t, endData, filepath)
62 }
63
64 func TestFindAndReplaceStartOfLine(t *testing.T) {
65 then.WithTempDir(t)
66
67 filepath := "start.txt"
68 startData := `# yaml file
69 version: 0.0.1
70 level1:
71 level2:
72 version: 0.0.1
73 `
74 endData := `# yaml file
75 version: 1.2.3
76 level1:
77 level2:
78 version: 0.0.1
79 `
80
81 err := os.WriteFile(filepath, []byte(startData), os.ModePerm)
82 then.Nil(t, err)
83
84 rep := Replacement{
85 Path: filepath,
86 Find: "^version: .*",
87 Replace: "version: {{.VersionNoPrefix}}",
88 }
89 err = rep.Execute(ReplaceData{
90 VersionNoPrefix: "1.2.3",
91 })
92 then.Nil(t, err)
93 then.FileContents(t, endData, filepath)
94 }
95
96 func TestFindAndReplaceCaseInsensitive(t *testing.T) {
97 then.WithTempDir(t)
98
99 filepath := "insensitive.txt"
100 startData := `# yaml file
101 Version: 0.0.1
102 level1:
103 level2:
104 version: 0.0.1
105 `
106 endData := `# yaml file
107 version: 1.2.3
108 level1:
109 level2:
110 version: 0.0.1
111 `
112
113 err := os.WriteFile(filepath, []byte(startData), os.ModePerm)
114 then.Nil(t, err)
115
116 rep := Replacement{
117 Path: filepath,
118 Find: "^version: .*",
119 Replace: "version: {{.VersionNoPrefix}}",
120 Flags: "im",
121 }
122 err = rep.Execute(ReplaceData{
123 VersionNoPrefix: "1.2.3",
124 })
125 then.Nil(t, err)
126 then.FileContents(t, endData, filepath)
127 }
128
129 func TestErrorBadTemplateParse(t *testing.T) {
130 then.WithTempDir(t)
131
132 rep := Replacement{
133 Replace: "{{.bad..}}",
134 }
135 err := rep.Execute(ReplaceData{})
136 then.NotNil(t, err)
137 }
138
139 func TestErrorBadRegex(t *testing.T) {
140 then.WithTempDir(t)
141
142 filepath := "insensitive.txt"
143 startData := `# yaml file
144 Version: 0.0.1
145 level1:
146 level2:
147 version: 0.0.1
148 `
149
150 err := os.WriteFile(filepath, []byte(startData), os.ModePerm)
151 then.Nil(t, err)
152
153 rep := Replacement{
154 Path: filepath,
155 Find: "a(b",
156 Flags: "im",
157 }
158 err = rep.Execute(ReplaceData{
159 VersionNoPrefix: "1.2.3",
160 })
161 then.NotNil(t, err)
162 }
163
164 func TestErrorBadTemplateExec(t *testing.T) {
165 then.WithTempDir(t)
166
167 rep := Replacement{
168 Replace: "{{.bad}}",
169 }
170 err := rep.Execute(ReplaceData{})
171 then.NotNil(t, err)
172 }
173
174 func TestErrorBadFileRead(t *testing.T) {
175 then.WithTempDir(t)
176
177 rep := Replacement{
178 Path: "does not exist",
179 }
180 err := rep.Execute(ReplaceData{})
181 then.Err(t, ErrNoReplacementFilesFound, err)
182 }
183
184 func TestGlobs(t *testing.T) {
185 then.WithTempDir(t)
186
187 toReplace := []byte(`{
188 "version": "1.0.0"
189 }`)
190 then.WriteFile(t, toReplace, "a", "b", "c.json")
191 then.WriteFile(t, toReplace, "a", "b", "d.xml")
192 then.WriteFile(t, toReplace, "a", "b", "e.jsonl")
193 then.WriteFile(t, toReplace, "a", "b", "f.png")
194 then.WriteFile(t, toReplace, "a", "c", "g.json")
195
196 rep := Replacement{
197 Path: "a/*/*.json",
198 Find: ` "version": ".*"`,
199 Replace: ` "version": "{{.VersionNoPrefix}}"`,
200 }
201
202 err := rep.Execute(ReplaceData{
203 VersionNoPrefix: "1.1.0",
204 })
205 then.Nil(t, err)
206
207 changedFile := `{
208 "version": "1.1.0"
209 }`
210 unchangedFile := `{
211 "version": "1.0.0"
212 }`
213
214 then.FileContents(t, changedFile, "a", "b", "c.json")
215 then.FileContents(t, unchangedFile, "a", "b", "d.xml")
216 then.FileContents(t, unchangedFile, "a", "b", "e.jsonl")
217 then.FileContents(t, unchangedFile, "a", "b", "f.png")
218 then.FileContents(t, changedFile, "a", "c", "g.json")
219 }
220
221 func TestBadGlob(t *testing.T) {
222 rep := Replacement{
223 Path: `[]`,
224 }
225
226 err := rep.Execute(ReplaceData{})
227 then.NotNil(t, err)
228 }