22
22
; ;; Code:
23
23
24
24
(require 'ert )
25
- (require 'lsp-mode )
26
25
(require 'url )
27
- (require 'cl-lib )
28
-
29
- (defmacro lsp-download-test--with-mocked-url-retrieve (response-data &rest body )
30
- " Mock url-retrieve to return RESPONSE-DATA and execute BODY."
31
- (declare (indent 1 ))
32
- `(cl-letf* ((url-retrieve-calls 0 )
33
- ((symbol-function 'url-retrieve )
34
- (lambda (url callback &optional cbargs silent inhibit-cookies )
35
- (cl-incf url-retrieve-calls)
36
- (run-at-time 0.01 nil
37
- (lambda ()
38
- (with-temp-buffer
39
- (insert , response-data )
40
- (goto-char (point-min ))
41
- (funcall callback nil cbargs)))))))
42
- ,@body ))
43
-
44
- (ert-deftest lsp-download-install-callback-success ()
45
- " Test that lsp-download-install calls success callback on successful download."
46
- (let* ((temp-file (make-temp-file " lsp-test-download" ))
47
- (callback-called nil )
48
- (error-called nil )
49
- (test-content " test file content" ))
50
- (unwind-protect
51
- (lsp-download-test--with-mocked-url-retrieve
52
- (concat " HTTP/1.1 200 OK\r\n\r\n " test-content)
53
- (lsp-download-install
54
- (lambda () (setq callback-called t ))
55
- (lambda (_err ) (setq error-called t ))
56
- :url " http://example.com/test.jar"
57
- :store-path temp-file)
58
-
59
- ; ; Wait for async operation
60
- (sleep-for 0.1 )
61
-
62
- (should callback-called)
63
- (should-not error-called)
64
- (should (f-exists? temp-file))
65
- (should (string= test-content (f-read temp-file))))
66
- (when (f-exists? temp-file)
67
- (f-delete temp-file)))))
68
-
69
- (ert-deftest lsp-download-install-callback-error ()
70
- " Test that lsp-download-install calls error callback on failed download."
71
- (let* ((temp-file (make-temp-file " lsp-test-download" ))
72
- (callback-called nil )
73
- (error-called nil ))
74
- (unwind-protect
75
- (cl-letf (((symbol-function 'url-retrieve )
76
- (lambda (url callback &optional cbargs silent inhibit-cookies )
77
- (run-at-time 0.01 nil
78
- (lambda ()
79
- (funcall callback '(:error (error " Network error " )) cbargs))))))
80
- (lsp-download-install
81
- (lambda () (setq callback-called t ))
82
- (lambda (_err ) (setq error-called t ))
83
- :url " http://example.com/test.jar"
84
- :store-path temp-file)
85
-
86
- ; ; Wait for async operation
87
- (sleep-for 0.1 )
88
-
89
- (should-not callback-called)
90
- (should error-called))
91
- (when (f-exists? temp-file)
92
- (f-delete temp-file)))))
93
-
94
- (ert-deftest lsp-download-install-large-file-async ()
95
- " Test that lsp-download-install doesn't block UI with large files."
96
- (let* ((temp-file (make-temp-file " lsp-test-download" ))
97
- (download-started nil )
98
- (download-completed nil )
99
- ; ; Simulate a large file with 10MB of data
100
- (large-content (make-string (* 10 1024 1024 ) ?x )))
101
- (unwind-protect
102
- (lsp-download-test--with-mocked-url-retrieve
103
- (concat " HTTP/1.1 200 OK\r\n\r\n " large-content)
104
- (lsp-download-install
105
- (lambda () (setq download-completed t ))
106
- (lambda (_err ) (error " Download failed " ))
107
- :url " http://example.com/large.jar"
108
- :store-path temp-file)
109
-
110
- (setq download-started t )
111
-
112
- ; ; UI should not be blocked - download-started should be set
113
- ; ; but download-completed should still be nil
114
- (should download-started)
115
- (should-not download-completed)
116
-
117
- ; ; Wait for async completion
118
- (sleep-for 0.2 )
119
-
120
- (should download-completed)
121
- (should (f-exists? temp-file))
122
- ; ; Verify file size
123
- (should (= (f-size temp-file) (* 10 1024 1024 ))))
124
- (when (f-exists? temp-file)
125
- (f-delete temp-file)))))
126
-
127
- (ert-deftest lsp-download-install-with-decompress ()
128
- " Test that lsp-download-install handles decompression options."
129
- (let* ((temp-dir (make-temp-file " lsp-test-dir" t ))
130
- (store-path (f-join temp-dir " test.jar" ))
131
- (download-path (concat store-path " .zip" ))
132
- (callback-called nil ))
133
- (unwind-protect
134
- (cl-letf* (((symbol-function 'lsp-unzip )
135
- (lambda (file dir )
136
- ; ; Mock unzip - just create the target file
137
- (f-write " unzipped content" 'utf-8 store-path)))
138
- ((symbol-function 'url-retrieve )
139
- (lambda (url callback &rest args )
140
- (run-at-time 0.01 nil
141
- (lambda ()
142
- (with-temp-buffer
143
- (insert " HTTP/1.1 200 OK\r\n\r\n ZIP_CONTENT" )
144
- (goto-char (point-min ))
145
- (funcall callback nil args)))))))
146
-
147
- (lsp-download-install
148
- (lambda () (setq callback-called t ))
149
- (lambda (_err ) (error " Download failed " ))
150
- :url " http://example.com/test.zip"
151
- :store-path store-path
152
- :decompress :zip )
153
-
154
- ; ; Wait for async operation
155
- (sleep-for 0.1 )
156
-
157
- (should callback-called)
158
- (should (f-exists? store-path))
159
- (should (string= " unzipped content" (f-read store-path))))
160
- (when (f-exists? temp-dir)
161
- (f-delete temp-dir t )))))
162
-
163
- (ert-deftest lsp-download-install-creates-parent-dirs ()
164
- " Test that lsp-download-install creates parent directories if needed."
165
- (let* ((temp-base (make-temp-file " lsp-test-base" t ))
166
- (nested-path (f-join temp-base " a" " b" " c" " test.jar" ))
167
- (callback-called nil ))
26
+
27
+ ; ; Set up load path and load lsp-mode to get our functions
28
+ (add-to-list 'load-path (expand-file-name " ." ))
29
+
30
+ ; ; Mock the dependencies that lsp-mode needs
31
+ (unless (fboundp 'lsp-resolve-value )
32
+ (defun lsp-resolve-value (value ) value))
33
+
34
+ (unless (fboundp 'lsp--info )
35
+ (defun lsp--info (msg &rest args )
36
+ (apply #'message msg args)))
37
+
38
+ (unless (fboundp 'lsp--error )
39
+ (defun lsp--error (msg &rest args )
40
+ (apply #'message msg args)))
41
+
42
+ (unless (fboundp 'lsp--warn )
43
+ (defun lsp--warn (msg &rest args )
44
+ (apply #'message msg args)))
45
+
46
+ ; ; Load lsp-mode to get our download functions
47
+ (require 'lsp-mode )
48
+
49
+ ; ; Test the core async download function in isolation
50
+ (ert-deftest lsp-download-url-retrieve-async ()
51
+ " Test that url-retrieve based download works asynchronously."
52
+ (let ((temp-file (make-temp-file " lsp-test-download" ))
53
+ (download-completed nil )
54
+ (download-content " test content from server" ))
168
55
(unwind-protect
169
- (lsp-download-test--with-mocked-url-retrieve
170
- " HTTP/1.1 200 OK\r\n\r\n test content"
171
- (should-not (f-exists? (f-parent nested-path)))
172
-
173
- (lsp-download-install
174
- (lambda () (setq callback-called t ))
175
- (lambda (_err ) (error " Download failed " ))
176
- :url " http://example.com/test.jar"
177
- :store-path nested-path)
178
-
179
- ; ; Wait for async operation
180
- (sleep-for 0.1 )
181
-
182
- (should callback-called)
183
- (should (f-exists? nested-path))
184
- (should (f-exists? (f-parent nested-path))))
185
- (when (f-exists? temp-base)
186
- (f-delete temp-base t )))))
187
-
188
- (ert-deftest lsp-package-ensure-with-download-provider ()
189
- " Test that lsp-package-ensure works with download provider."
190
- (let* ((temp-file (make-temp-file " lsp-test-download" ))
191
- (callback-called nil )
192
- (test-dependency 'test-server ))
56
+ (progn
57
+ ; ; Mock url-retrieve to simulate async behavior
58
+ (cl-letf (((symbol-function 'url-retrieve )
59
+ (lambda (url callback &rest args )
60
+ (run-at-time 0.01 nil
61
+ (lambda ()
62
+ (with-temp-buffer
63
+ (insert " HTTP/1.1 200 OK\r\n\r\n " )
64
+ (insert download-content)
65
+ (goto-char (point-min ))
66
+ (funcall callback nil args)))))))
67
+
68
+ ; ; Test our helper function directly
69
+ (lsp-download-install--url-retrieve
70
+ " http://example.com/test"
71
+ temp-file
72
+ (lambda () (setq download-completed t ))
73
+ (lambda (err ) (error " Download failed: %s " err)))
74
+
75
+ ; ; Should not be completed immediately (async behavior)
76
+ (should-not download-completed)
77
+
78
+ ; ; Wait for async completion
79
+ (sleep-for 0.1 )
80
+
81
+ ; ; Should be completed now
82
+ (should download-completed)
83
+ (should (file-exists-p temp-file))
84
+
85
+ ; ; Verify content
86
+ (with-temp-buffer
87
+ (insert-file-contents temp-file)
88
+ (should (string= download-content (buffer-string ))))))
89
+
90
+ ; ; Cleanup
91
+ (when (file-exists-p temp-file)
92
+ (delete-file temp-file)))))
93
+
94
+ (ert-deftest lsp-download-url-retrieve-error-handling ()
95
+ " Test that url-retrieve properly handles errors."
96
+ (let ((temp-file (make-temp-file " lsp-test-download" ))
97
+ (error-called nil )
98
+ (success-called nil ))
193
99
(unwind-protect
194
100
(progn
195
- ; ; Register a test dependency
196
- (puthash test-dependency
197
- `( :download : url " http://example.com/test.jar "
198
- :store-path , temp-file )
199
- lsp--dependencies )
200
-
201
- (lsp-download-test--with-mocked-url-retrieve
202
- " HTTP/1.1 200 OK \r\n\r\n server content "
203
- (lsp-package-ensure
204
- test-dependency
205
- (lambda () (setq callback -called t ))
206
- (lambda (_err ) (error " Install failed " )))
101
+ ; ; Mock url-retrieve to simulate error
102
+ (cl-letf ((( symbol-function 'url-retrieve )
103
+ ( lambda ( url callback &rest args )
104
+ ( run-at-time 0.01 nil
105
+ ( lambda ( )
106
+ ( funcall callback '( :error ( error " Network error " )) args))))))
107
+
108
+ (lsp-download-install--url-retrieve
109
+ " http://example.com/test "
110
+ temp-file
111
+ (lambda () (setq success -called t ))
112
+ (lambda (err ) (setq error-called t )))
207
113
208
- ; ; Wait for async operation
114
+ ; ; Wait for async completion
209
115
(sleep-for 0.1 )
210
116
211
- (should callback-called)
212
- (should (f-exists? temp-file))))
117
+ ; ; Should have called error callback
118
+ (should error-called)
119
+ (should-not success-called)))
120
+
213
121
; ; Cleanup
214
- (when (f-exists? temp-file)
215
- (f-delete temp-file))
216
- (remhash test-dependency lsp--dependencies))))
122
+ (when (file-exists-p temp-file)
123
+ (delete-file temp-file)))))
124
+
125
+ (ert-deftest lsp-download-verify-signature-function-exists ()
126
+ " Test that signature verification function exists and has correct signature."
127
+ (should (fboundp 'lsp-download-install--verify-signature ))
128
+ ; ; Test that it can be called without error when epg is not available
129
+ (cl-letf (((symbol-function 'executable-find ) (lambda (prog ) nil )))
130
+ (should-not (lsp-download-install--verify-signature nil " /tmp/test" nil nil ))))
217
131
218
132
(provide 'lsp-download-test )
219
133
; ;; lsp-download-test.el ends here
0 commit comments