forked from tninja/ai-code-interface.el
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ai-code-git.el
More file actions
185 lines (163 loc) · 7.81 KB
/
test_ai-code-git.el
File metadata and controls
185 lines (163 loc) · 7.81 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
;;; test_ai-code-git.el --- Tests for ai-code-git.el -*- lexical-binding: t; -*-
;; Author: Kang Tu <tninja@gmail.com>
;; SPDX-License-Identifier: Apache-2.0
;;; Commentary:
;; Tests for the ai-code-git module, specifically testing
;; the .gitignore update logic.
;;; Code:
(require 'ert)
(require 'ai-code-git)
(require 'ai-code-prompt-mode)
(require 'ai-code-discussion)
(ert-deftest test-ai-code-gitignore-regex-pattern ()
"Test that the regex pattern correctly matches entries in .gitignore.
This is a unit test for the regex pattern used in ai-code-update-git-ignore."
(let ((gitignore-content "# Test .gitignore file
.ai.code.prompt.org
.ai.code.notes.org
.projectile
GTAGS
GRTAGS
GPATH
# End of file
"))
;; Test that existing entries are found
(should (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote ".ai.code.prompt.org")
"\\s-*\\(?:\n\\|$\\)")
gitignore-content))
(should (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote ".ai.code.notes.org")
"\\s-*\\(?:\n\\|$\\)")
gitignore-content))
(should (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote ".projectile")
"\\s-*\\(?:\n\\|$\\)")
gitignore-content))
(should (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote "GTAGS")
"\\s-*\\(?:\n\\|$\\)")
gitignore-content))
(should (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote "GRTAGS")
"\\s-*\\(?:\n\\|$\\)")
gitignore-content))
(should (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote "GPATH")
"\\s-*\\(?:\n\\|$\\)")
gitignore-content))
;; Test that a missing entry is not found
(should-not (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote "MISSING_ENTRY")
"\\s-*\\(?:\n\\|$\\)")
gitignore-content))
;; Test entries with whitespace
(let ((gitignore-with-whitespace " .projectile
GTAGS
"))
(should (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote ".projectile")
"\\s-*\\(?:\n\\|$\\)")
gitignore-with-whitespace))
(should (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote "GTAGS")
"\\s-*\\(?:\n\\|$\\)")
gitignore-with-whitespace)))
;; Test entry at beginning of file (no leading newline)
(let ((gitignore-start ".ai.code.prompt.org
other-file"))
(should (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote ".ai.code.prompt.org")
"\\s-*\\(?:\n\\|$\\)")
gitignore-start)))
;; Test entry at end of file (no trailing newline)
(let ((gitignore-end "other-file
.ai.code.prompt.org"))
(should (string-match-p (concat "\\(?:^\\|\n\\)\\s-*"
(regexp-quote ".ai.code.prompt.org")
"\\s-*\\(?:\n\\|$\\)")
gitignore-end)))))
(ert-deftest test-ai-code-update-git-ignore-no-duplicates ()
"Test that ai-code-update-git-ignore does not add duplicate entries.
When .gitignore already contains the required entries, they should
not be added again."
(let* ((temp-dir (make-temp-file "ai-code-test-" t))
(gitignore-path (expand-file-name ".gitignore" temp-dir))
(required-entries (list ai-code-prompt-file-name
ai-code-notes-file-name
".projectile"
"GTAGS"
"GRTAGS"
"GPATH")))
(unwind-protect
(progn
;; Initialize git repository
(let ((default-directory temp-dir))
(shell-command "git init"))
;; Create .gitignore with entries already present
(with-temp-file gitignore-path
(insert "# Existing entries\n")
(dolist (entry required-entries)
(insert entry "\n"))
(insert "# End of file\n"))
;; Store original content
(let ((original-content (with-temp-buffer
(insert-file-contents gitignore-path)
(buffer-string))))
;; Mock magit-toplevel to return temp-dir
(cl-letf (((symbol-function 'magit-toplevel)
(lambda () temp-dir)))
;; Call the function
(ai-code-update-git-ignore))
;; Read the updated content
(let ((updated-content (with-temp-buffer
(insert-file-contents gitignore-path)
(buffer-string))))
;; Content should be the same (no duplicates added)
(should (string= original-content updated-content))
;; Each entry should appear exactly once
(dolist (entry required-entries)
(let ((count 0))
(with-temp-buffer
(insert updated-content)
(goto-char (point-min))
(while (re-search-forward (concat "^\\s-*" (regexp-quote entry) "\\s-*$") nil t)
(setq count (1+ count))))
(should (= count 1)))))))
;; Cleanup
(delete-directory temp-dir t))))
(ert-deftest test-ai-code-update-git-ignore-adds-missing ()
"Test that ai-code-update-git-ignore adds missing entries.
When .gitignore is missing some entries, they should be added."
(let* ((temp-dir (make-temp-file "ai-code-test-" t))
(gitignore-path (expand-file-name ".gitignore" temp-dir)))
(unwind-protect
(progn
;; Initialize git repository
(let ((default-directory temp-dir))
(shell-command "git init"))
;; Create .gitignore with only some entries
(with-temp-file gitignore-path
(insert "# Existing entries\n")
(insert ".projectile\n")
(insert "GTAGS\n"))
;; Mock magit-toplevel to return temp-dir
(cl-letf (((symbol-function 'magit-toplevel)
(lambda () temp-dir)))
;; Call the function
(ai-code-update-git-ignore))
;; Read the updated content
(let ((updated-content (with-temp-buffer
(insert-file-contents gitignore-path)
(buffer-string))))
;; All required entries should be present
(should (string-match-p (regexp-quote ai-code-prompt-file-name) updated-content))
(should (string-match-p (regexp-quote ai-code-notes-file-name) updated-content))
(should (string-match-p (regexp-quote ".projectile") updated-content))
(should (string-match-p (regexp-quote "GTAGS") updated-content))
(should (string-match-p (regexp-quote "GRTAGS") updated-content))
(should (string-match-p (regexp-quote "GPATH") updated-content))))
;; Cleanup
(delete-directory temp-dir t))))
(provide 'test_ai-code-git)
;;; test_ai-code-git.el ends here