blob: d689274a4ae7bafb7409d75ec25c45faaa2d2370 (
plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
;;; test-update-text-file.el --- Tests for update_text_file gptel tool -*- lexical-binding: t; -*-
;;; Commentary:
;; Normal / Boundary / Error tests for each operation in
;; gptel-tools/update_text_file.el, plus file-level wrapper tests.
;; The pure-string helpers carry most of the coverage; the wrapper
;; only adds the I/O surface (backup, write, validation).
;;; Code:
(require 'ert)
(require 'cl-lib)
(eval-and-compile
(add-to-list 'load-path (expand-file-name "tests" user-emacs-directory))
(add-to-list 'load-path (expand-file-name "gptel-tools" user-emacs-directory))
(setq load-prefer-newer t)
;; Stub gptel so the tool file can be loaded without the real package.
(unless (featurep 'gptel)
(defvar gptel-tools nil)
(defun gptel-make-tool (&rest _args) nil)
(defun gptel-get-tool (&rest _args) nil)
(provide 'gptel)))
(require 'update_text_file)
;; ----------------------------------------------------- helpers
(defun test-update-text-file--with-temp (content fn)
"Write CONTENT to a temp file, call FN with its path, then delete."
(let ((path (make-temp-file "test-update-text-file-")))
(unwind-protect
(progn
(with-temp-file path (insert content))
(funcall fn path))
(when (file-exists-p path) (delete-file path)))))
;; ----------------------------------------------------- replace
(ert-deftest test-update-text-file-replace-normal ()
"Normal: replace all occurrences of the literal pattern."
(should (equal (cj/update-text-file--replace "foo bar foo" "foo" "BAZ")
"BAZ bar BAZ")))
(ert-deftest test-update-text-file-replace-boundary-no-match ()
"Boundary: pattern absent returns content unchanged."
(should (equal (cj/update-text-file--replace "abc" "xyz" "QQ") "abc")))
(ert-deftest test-update-text-file-replace-boundary-special-chars ()
"Boundary: regex metacharacters in pattern are treated as literals."
(should (equal (cj/update-text-file--replace "a.b.c" "." "-") "a-b-c"))
(should (equal (cj/update-text-file--replace "(x)(y)" "(x)" "_") "_(y)"))
(should (equal (cj/update-text-file--replace "a$b" "$" "S") "aSb")))
(ert-deftest test-update-text-file-replace-boundary-unicode ()
"Boundary: unicode in both pattern and replacement."
(should (equal (cj/update-text-file--replace "café résumé" "café" "thé")
"thé résumé")))
(ert-deftest test-update-text-file-replace-boundary-replacement-with-backref-like ()
"Boundary: replacement strings with \\1 etc. are literal, not back-refs."
(should (equal (cj/update-text-file--replace "foo" "foo" "\\1bar")
"\\1bar")))
(ert-deftest test-update-text-file-replace-error-empty-pattern ()
"Error: empty pattern signals."
(should-error (cj/update-text-file--replace "abc" "" "x")))
(ert-deftest test-update-text-file-replace-error-nil-pattern ()
"Error: nil pattern signals."
(should-error (cj/update-text-file--replace "abc" nil "x")))
(ert-deftest test-update-text-file-replace-error-nil-replacement ()
"Error: nil replacement signals."
(should-error (cj/update-text-file--replace "abc" "a" nil)))
;; ----------------------------------------------------- append
(ert-deftest test-update-text-file-append-normal ()
"Normal: append adds text plus a trailing newline."
(should (equal (cj/update-text-file--append "line1\n" "line2")
"line1\nline2\n")))
(ert-deftest test-update-text-file-append-boundary-no-trailing-newline ()
"Boundary: appends still produce a newline when content has none."
(should (equal (cj/update-text-file--append "abc" "def")
"abc\ndef\n")))
(ert-deftest test-update-text-file-append-boundary-empty-content ()
"Boundary: appending to empty content yields just the new text + newline."
(should (equal (cj/update-text-file--append "" "hello") "hello\n")))
(ert-deftest test-update-text-file-append-boundary-text-with-trailing-newline ()
"Boundary: text that already ends in newline isn't duplicated."
(should (equal (cj/update-text-file--append "a\n" "b\n") "a\nb\n")))
(ert-deftest test-update-text-file-append-error-empty-text ()
"Error: empty text signals."
(should-error (cj/update-text-file--append "foo" "")))
(ert-deftest test-update-text-file-append-error-nil-text ()
"Error: nil text signals."
(should-error (cj/update-text-file--append "foo" nil)))
;; ----------------------------------------------------- prepend
(ert-deftest test-update-text-file-prepend-normal ()
"Normal: prepend adds text plus a separator newline."
(should (equal (cj/update-text-file--prepend "line1\n" "line0")
"line0\nline1\n")))
(ert-deftest test-update-text-file-prepend-boundary-empty-content ()
"Boundary: prepending to empty content keeps just the new text + sep."
(should (equal (cj/update-text-file--prepend "" "hello") "hello\n")))
(ert-deftest test-update-text-file-prepend-boundary-text-with-trailing-newline ()
"Boundary: text already terminated by newline is not double-broken."
(should (equal (cj/update-text-file--prepend "rest" "first\n")
"first\nrest")))
(ert-deftest test-update-text-file-prepend-error-empty-text ()
"Error: empty text signals."
(should-error (cj/update-text-file--prepend "foo" "")))
(ert-deftest test-update-text-file-prepend-error-nil-text ()
"Error: nil text signals."
(should-error (cj/update-text-file--prepend "foo" nil)))
;; ----------------------------------------------------- insert-at-line
(ert-deftest test-update-text-file-insert-at-line-normal ()
"Normal: insert before line 2 of a 3-line file."
(should (equal (cj/update-text-file--insert-at-line "a\nb\nc\n" 2 "X")
"a\nX\nb\nc\n")))
(ert-deftest test-update-text-file-insert-at-line-boundary-first-line ()
"Boundary: inserting at line 1 prepends."
(should (equal (cj/update-text-file--insert-at-line "a\nb\n" 1 "X")
"X\na\nb\n")))
(ert-deftest test-update-text-file-insert-at-line-boundary-one-past-end ()
"Boundary: inserting one past the last line appends."
(should (equal (cj/update-text-file--insert-at-line "a\nb\n" 3 "X")
"a\nb\nX\n")))
(ert-deftest test-update-text-file-insert-at-line-boundary-no-trailing-newline ()
"Boundary: works on content without a trailing newline."
(should (equal (cj/update-text-file--insert-at-line "a\nb" 2 "X")
"a\nX\nb")))
(ert-deftest test-update-text-file-insert-at-line-error-out-of-range ()
"Error: line number beyond file length signals."
(should-error (cj/update-text-file--insert-at-line "a\nb\n" 5 "X")))
(ert-deftest test-update-text-file-insert-at-line-error-zero ()
"Error: line number 0 signals."
(should-error (cj/update-text-file--insert-at-line "a\n" 0 "X")))
(ert-deftest test-update-text-file-insert-at-line-error-negative ()
"Error: negative line number signals."
(should-error (cj/update-text-file--insert-at-line "a\n" -1 "X")))
(ert-deftest test-update-text-file-insert-at-line-error-empty-text ()
"Error: empty text signals."
(should-error (cj/update-text-file--insert-at-line "a\n" 1 "")))
;; ----------------------------------------------------- delete-lines
(ert-deftest test-update-text-file-delete-lines-normal ()
"Normal: removes lines containing the literal pattern."
(should (equal (cj/update-text-file--delete-lines "keep\nkill me\nkeep\n" "kill")
"keep\nkeep\n")))
(ert-deftest test-update-text-file-delete-lines-boundary-no-match ()
"Boundary: pattern matches nothing returns content unchanged."
(should (equal (cj/update-text-file--delete-lines "a\nb\nc\n" "z")
"a\nb\nc\n")))
(ert-deftest test-update-text-file-delete-lines-boundary-all-lines-match ()
"Boundary: every line removed yields the empty string."
(should (equal (cj/update-text-file--delete-lines "x\nx\nx\n" "x") "")))
(ert-deftest test-update-text-file-delete-lines-boundary-special-chars-literal ()
"Boundary: regex metacharacters in pattern are treated as literals."
(should (equal (cj/update-text-file--delete-lines "a.b\naxb\n" ".")
"axb\n")))
(ert-deftest test-update-text-file-delete-lines-boundary-no-trailing-newline ()
"Boundary: content without trailing newline keeps that shape."
(should (equal (cj/update-text-file--delete-lines "keep\ndrop" "drop")
"keep")))
(ert-deftest test-update-text-file-delete-lines-error-empty-pattern ()
"Error: empty pattern signals."
(should-error (cj/update-text-file--delete-lines "a\nb\n" "")))
(ert-deftest test-update-text-file-delete-lines-error-nil-pattern ()
"Error: nil pattern signals."
(should-error (cj/update-text-file--delete-lines "a\nb\n" nil)))
;; ----------------------------------------------------- apply-operation
(ert-deftest test-update-text-file-apply-operation-dispatch ()
"Each operation name dispatches to its transform."
(should (equal (cj/update-text-file--apply-operation "abc" "replace" "b" "B" nil)
"aBc"))
(should (equal (cj/update-text-file--apply-operation "a" "append" "b" nil nil)
"a\nb\n"))
(should (equal (cj/update-text-file--apply-operation "a" "prepend" "b" nil nil)
"b\na"))
(should (equal (cj/update-text-file--apply-operation "a\nb\n" "insert-at-line" "X" nil 2)
"a\nX\nb\n"))
(should (equal (cj/update-text-file--apply-operation "a\nb\n" "delete-lines" "a" nil nil)
"b\n")))
(ert-deftest test-update-text-file-apply-operation-error-unknown ()
"Unknown operation signals."
(should-error (cj/update-text-file--apply-operation "x" "frobnicate" nil nil nil)))
;; ----------------------------------------------------- validate-path
(ert-deftest test-update-text-file-validate-path-normal ()
"Normal: an existing readable+writable file under HOME passes."
(let* ((file (make-temp-file "test-update-text-file-")))
(unwind-protect
(progn
;; make-temp-file may land in /tmp; rebase to HOME for the test.
(let* ((home-file (expand-file-name
(concat ".test-update-text-file-" (format-time-string "%s") ".tmp")
"~")))
(unwind-protect
(progn
(copy-file file home-file t)
(should (equal (cj/update-text-file--validate-path home-file)
(file-truename home-file))))
(when (file-exists-p home-file) (delete-file home-file)))))
(when (file-exists-p file) (delete-file file)))))
(ert-deftest test-update-text-file-validate-path-error-missing ()
"Error: a missing file under HOME signals."
(let ((path (expand-file-name
(concat ".test-update-text-file-missing-"
(format-time-string "%s") ".tmp")
"~")))
(when (file-exists-p path) (delete-file path))
(should-error (cj/update-text-file--validate-path path))))
(ert-deftest test-update-text-file-validate-path-error-outside-home ()
"Error: a path outside HOME signals."
(should-error (cj/update-text-file--validate-path "/etc/hostname")))
(ert-deftest test-update-text-file-validate-path-error-directory ()
"Error: a directory signals."
(should-error (cj/update-text-file--validate-path "~")))
;; ----------------------------------------------------- backup-name
(ert-deftest test-update-text-file-backup-name-shape ()
"Backup names append a timestamped .bak suffix."
(let ((name (cj/update-text-file--backup-name "/home/user/foo.txt")))
(should (string-prefix-p "/home/user/foo.txt-" name))
(should (string-suffix-p ".bak" name))
;; Format is YYYY-MM-DD-HHMMSS.
(should (string-match-p "-[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}-[0-9]\\{6\\}\\.bak\\'"
name))))
;; ----------------------------------------------------- file-level wrapper
(defun test-update-text-file--in-home (suffix content fn)
"Write CONTENT to a temp file under HOME with SUFFIX, call FN, then delete.
Backups (path-TS.bak) are cleaned up after FN returns."
(let* ((name (format ".test-update-text-file-%s-%s.tmp"
suffix (format-time-string "%s%N")))
(path (expand-file-name name "~")))
(unwind-protect
(progn
(with-temp-file path (insert content))
(funcall fn path))
(when (file-exists-p path) (delete-file path))
(dolist (b (file-expand-wildcards (concat path "-*.bak")))
(when (file-exists-p b) (delete-file b))))))
(ert-deftest test-update-text-file-run-replace-normal ()
"Wrapper: replace operation rewrites the file and creates a backup."
(test-update-text-file--in-home
"replace" "alpha bravo alpha\n"
(lambda (path)
(let ((result (cj/update-text-file--run path "replace" "alpha" "GAMMA" nil)))
(should (string-match-p "Updated" result))
(should (string-match-p "backup:" result))
(with-temp-buffer
(insert-file-contents path)
(should (equal (buffer-string) "GAMMA bravo GAMMA\n")))
(should (file-expand-wildcards (concat path "-*.bak")))))))
(ert-deftest test-update-text-file-run-no-change-no-backup ()
"Wrapper: no-op operation leaves the file untouched and creates no backup."
(test-update-text-file--in-home
"noop" "abc\n"
(lambda (path)
(let ((result (cj/update-text-file--run path "replace" "zzz" "QQ" nil)))
(should (string-match-p "No changes" result))
(with-temp-buffer
(insert-file-contents path)
(should (equal (buffer-string) "abc\n")))
(should-not (file-expand-wildcards (concat path "-*.bak")))))))
(ert-deftest test-update-text-file-run-append-normal ()
"Wrapper: append operation adds a line to the file."
(test-update-text-file--in-home
"append" "first\n"
(lambda (path)
(cj/update-text-file--run path "append" "second" nil nil)
(with-temp-buffer
(insert-file-contents path)
(should (equal (buffer-string) "first\nsecond\n"))))))
(ert-deftest test-update-text-file-run-insert-at-line-normal ()
"Wrapper: insert-at-line inserts and rewrites the file."
(test-update-text-file--in-home
"insert" "a\nb\nc\n"
(lambda (path)
(cj/update-text-file--run path "insert-at-line" "X" nil 2)
(with-temp-buffer
(insert-file-contents path)
(should (equal (buffer-string) "a\nX\nb\nc\n"))))))
(ert-deftest test-update-text-file-run-delete-lines-normal ()
"Wrapper: delete-lines removes matching lines."
(test-update-text-file--in-home
"delete" "keep1\nkill\nkeep2\nkill\n"
(lambda (path)
(cj/update-text-file--run path "delete-lines" "kill" nil nil)
(with-temp-buffer
(insert-file-contents path)
(should (equal (buffer-string) "keep1\nkeep2\n"))))))
(ert-deftest test-update-text-file-run-error-missing-file ()
"Wrapper: missing file signals."
(let ((path (expand-file-name
(concat ".test-update-text-file-absent-"
(format-time-string "%s") ".tmp")
"~")))
(when (file-exists-p path) (delete-file path))
(should-error (cj/update-text-file--run path "append" "x" nil nil))))
(ert-deftest test-update-text-file-run-error-outside-home ()
"Wrapper: path outside home signals."
(should-error (cj/update-text-file--run "/etc/hostname" "append" "x" nil nil)))
(provide 'test-update-text-file)
;;; test-update-text-file.el ends here
|