aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorJim Porter2022-10-11 22:11:04 -0700
committerJim Porter2022-10-17 18:48:52 -0700
commit3cc356abfef8294abcb91dc421e3c63a561a11b4 (patch)
tree79fd49b558b5880a04c41461794d380b682d93f1 /test
parent1beb389e472ab8132b478c9f24dd0ab6b7398670 (diff)
downloademacs-3cc356abfef8294abcb91dc421e3c63a561a11b4.tar.gz
emacs-3cc356abfef8294abcb91dc421e3c63a561a11b4.zip
Add helpers to dynamically assign connection-local values
* lisp/files-x.el (connection-local-criteria) (connection-local-profile-name-for-setq): New variables. (with-connection-local-variables-1): ... let-bind them here. (connection-local-update-profile-variables) (connection-local-profile-name-for-criteria): New functions. (with-connection-local-application-variables, setq-connection-local): New macros. * test/lisp/files-x-tests.el: Require 'tramp-integration' (files-x-test--variable5, remote-lazy-var): New variables. (files-x-test-hack-connection-local-variables-apply): Expand checks. (files-x-test-with-connection-local-variables): Remove 'hack-connection-local-variables-apply' check (it belongs in the above test), and expand some other checks. (files-x-test--get-lazy-var, files-x-test--set-lazy-var): New functions. (files-x-test-connection-local-update-profile-variables) (files-x-test-setq-connection-local): New tests. * doc/lispref/variables.texi (Connection Local Variables): Split into two subsections and document the new features. * etc/NEWS: Announce 'setq-connection-local'.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/files-x-tests.el148
1 files changed, 107 insertions, 41 deletions
diff --git a/test/lisp/files-x-tests.el b/test/lisp/files-x-tests.el
index 2f6d0d4a991..b1555a02664 100644
--- a/test/lisp/files-x-tests.el
+++ b/test/lisp/files-x-tests.el
@@ -23,6 +23,7 @@
23 23
24(require 'ert) 24(require 'ert)
25(require 'files-x) 25(require 'files-x)
26(require 'tramp-integration)
26 27
27(defconst files-x-test--variables1 28(defconst files-x-test--variables1
28 '((remote-shell-file-name . "/bin/bash") 29 '((remote-shell-file-name . "/bin/bash")
@@ -35,7 +36,11 @@
35 '((remote-null-device . "/dev/null"))) 36 '((remote-null-device . "/dev/null")))
36(defconst files-x-test--variables4 37(defconst files-x-test--variables4
37 '((remote-null-device . "null"))) 38 '((remote-null-device . "null")))
39(defconst files-x-test--variables5
40 '((remote-lazy-var . nil)
41 (remote-null-device . "/dev/null")))
38(defvar remote-null-device) 42(defvar remote-null-device)
43(defvar remote-lazy-var nil)
39(put 'remote-shell-file-name 'safe-local-variable #'identity) 44(put 'remote-shell-file-name 'safe-local-variable #'identity)
40(put 'remote-shell-command-switch 'safe-local-variable #'identity) 45(put 'remote-shell-command-switch 'safe-local-variable #'identity)
41(put 'remote-shell-interactive-switch 'safe-local-variable #'identity) 46(put 'remote-shell-interactive-switch 'safe-local-variable #'identity)
@@ -91,6 +96,28 @@
91 (connection-local-get-profile-variables 'remote-nullfile) 96 (connection-local-get-profile-variables 'remote-nullfile)
92 files-x-test--variables4)))) 97 files-x-test--variables4))))
93 98
99(ert-deftest files-x-test-connection-local-update-profile-variables ()
100 "Test updating connection-local profile variables."
101
102 ;; Declare (PROFILE VARIABLES) objects.
103 (let (connection-local-profile-alist connection-local-criteria-alist)
104 (connection-local-set-profile-variables
105 'remote-bash (copy-alist files-x-test--variables1))
106 (should
107 (equal
108 (connection-local-get-profile-variables 'remote-bash)
109 files-x-test--variables1))
110
111 ;; Updating overwrites only the values specified in this call, but
112 ;; retains all the other values from previous calls.
113 (connection-local-update-profile-variables
114 'remote-bash files-x-test--variables2)
115 (should
116 (equal
117 (connection-local-get-profile-variables 'remote-bash)
118 (cons (car files-x-test--variables2)
119 (cdr files-x-test--variables1))))))
120
94(ert-deftest files-x-test-connection-local-set-profiles () 121(ert-deftest files-x-test-connection-local-set-profiles ()
95 "Test setting connection-local profiles." 122 "Test setting connection-local profiles."
96 123
@@ -233,9 +260,12 @@
233 (nreverse (copy-tree files-x-test--variables2))))) 260 (nreverse (copy-tree files-x-test--variables2)))))
234 ;; The variables exist also as local variables. 261 ;; The variables exist also as local variables.
235 (should (local-variable-p 'remote-shell-file-name)) 262 (should (local-variable-p 'remote-shell-file-name))
263 (should (local-variable-p 'remote-null-device))
236 ;; The proper variable value is set. 264 ;; The proper variable value is set.
237 (should 265 (should
238 (string-equal (symbol-value 'remote-shell-file-name) "/bin/ksh")))) 266 (string-equal (symbol-value 'remote-shell-file-name) "/bin/ksh"))
267 (should
268 (string-equal (symbol-value 'remote-null-device) "/dev/null"))))
239 269
240 ;; The third test case. Both criteria `files-x-test--criteria1' 270 ;; The third test case. Both criteria `files-x-test--criteria1'
241 ;; and `files-x-test--criteria2' apply, but there are no double 271 ;; and `files-x-test--criteria2' apply, but there are no double
@@ -274,13 +304,11 @@
274 (should-not (local-variable-p 'remote-shell-file-name)) 304 (should-not (local-variable-p 'remote-shell-file-name))
275 (should-not (boundp 'remote-shell-file-name)))))) 305 (should-not (boundp 'remote-shell-file-name))))))
276 306
277(defvar tramp-connection-local-default-shell-variables)
278(defvar tramp-connection-local-default-system-variables)
279
280(ert-deftest files-x-test-with-connection-local-variables () 307(ert-deftest files-x-test-with-connection-local-variables ()
281 "Test setting connection-local variables." 308 "Test setting connection-local variables."
282 309
283 (let (connection-local-profile-alist connection-local-criteria-alist) 310 (let ((connection-local-profile-alist connection-local-profile-alist)
311 (connection-local-criteria-alist connection-local-criteria-alist))
284 (connection-local-set-profile-variables 312 (connection-local-set-profile-variables
285 'remote-bash files-x-test--variables1) 313 'remote-bash files-x-test--variables1)
286 (connection-local-set-profile-variables 314 (connection-local-set-profile-variables
@@ -292,29 +320,6 @@
292 nil 'remote-ksh 'remote-nullfile) 320 nil 'remote-ksh 'remote-nullfile)
293 321
294 (with-temp-buffer 322 (with-temp-buffer
295 (let ((enable-connection-local-variables t))
296 (hack-connection-local-variables-apply nil)
297
298 ;; All connection-local variables are set. They apply in
299 ;; reverse order in `connection-local-variables-alist'.
300 (should
301 (equal connection-local-variables-alist
302 (append
303 (nreverse (copy-tree files-x-test--variables3))
304 (nreverse (copy-tree files-x-test--variables2)))))
305 ;; The variables exist also as local variables.
306 (should (local-variable-p 'remote-shell-file-name))
307 (should (local-variable-p 'remote-null-device))
308 ;; The proper variable values are set.
309 (should
310 (string-equal (symbol-value 'remote-shell-file-name) "/bin/ksh"))
311 (should
312 (string-equal (symbol-value 'remote-null-device) "/dev/null"))
313
314 ;; A candidate connection-local variable is not bound yet.
315 (should-not (local-variable-p 'remote-shell-command-switch))))
316
317 (with-temp-buffer
318 ;; Use the macro. We need a remote `default-directory'. 323 ;; Use the macro. We need a remote `default-directory'.
319 (let ((enable-connection-local-variables t) 324 (let ((enable-connection-local-variables t)
320 (default-directory "/method:host:") 325 (default-directory "/method:host:")
@@ -331,18 +336,18 @@
331 (with-connection-local-variables 336 (with-connection-local-variables
332 ;; All connection-local variables are set. They apply in 337 ;; All connection-local variables are set. They apply in
333 ;; reverse order in `connection-local-variables-alist'. 338 ;; reverse order in `connection-local-variables-alist'.
334 ;; Since we ha a remote default directory, Tramp's settings 339 ;; Since we have a remote default directory, Tramp's settings
335 ;; are appended as well. 340 ;; are appended as well.
336 (should 341 (should
337 (equal 342 (equal
338 connection-local-variables-alist 343 connection-local-variables-alist
339 (append 344 (append
340 (nreverse (copy-tree files-x-test--variables3))
341 (nreverse (copy-tree files-x-test--variables2))
342 (nreverse 345 (nreverse
343 (copy-tree tramp-connection-local-default-shell-variables)) 346 (copy-tree tramp-connection-local-default-shell-variables))
344 (nreverse 347 (nreverse
345 (copy-tree tramp-connection-local-default-system-variables))))) 348 (copy-tree tramp-connection-local-default-system-variables))
349 (nreverse (copy-tree files-x-test--variables3))
350 (nreverse (copy-tree files-x-test--variables2)))))
346 ;; The variables exist also as local variables. 351 ;; The variables exist also as local variables.
347 (should (local-variable-p 'remote-shell-file-name)) 352 (should (local-variable-p 'remote-shell-file-name))
348 (should (local-variable-p 'remote-null-device)) 353 (should (local-variable-p 'remote-null-device))
@@ -352,15 +357,21 @@
352 (should 357 (should
353 (string-equal (symbol-value 'remote-null-device) "/dev/null")) 358 (string-equal (symbol-value 'remote-null-device) "/dev/null"))
354 359
355 ;; Run another instance of `with-connection-local-variables' 360 ;; Run `with-connection-local-application-variables' to use a
356 ;; with a different application. 361 ;; different application.
357 (let ((connection-local-default-application (cadr files-x-test--application))) 362 (with-connection-local-application-variables
358 (with-connection-local-variables 363 (cadr files-x-test--application)
359 ;; The proper variable values are set. 364 (should
360 (should 365 (equal
361 (string-equal (symbol-value 'remote-shell-file-name) "/bin/bash")) 366 connection-local-variables-alist
362 (should 367 (append
363 (string-equal (symbol-value 'remote-null-device) "/dev/null")))) 368 (nreverse (copy-tree files-x-test--variables3))
369 (nreverse (copy-tree files-x-test--variables1)))))
370 ;; The proper variable values are set.
371 (should
372 (string-equal (symbol-value 'remote-shell-file-name) "/bin/bash"))
373 (should
374 (string-equal (symbol-value 'remote-null-device) "/dev/null")))
364 ;; The variable values are reset. 375 ;; The variable values are reset.
365 (should 376 (should
366 (string-equal (symbol-value 'remote-shell-file-name) "/bin/ksh")) 377 (string-equal (symbol-value 'remote-shell-file-name) "/bin/ksh"))
@@ -376,5 +387,60 @@
376 (should-not (boundp 'remote-shell-file-name)) 387 (should-not (boundp 'remote-shell-file-name))
377 (should (string-equal (symbol-value 'remote-null-device) "null")))))) 388 (should (string-equal (symbol-value 'remote-null-device) "null"))))))
378 389
390(defun files-x-test--get-lazy-var ()
391 "Get the connection-local value of `remote-lazy-var'.
392If it's not initialized yet, initialize it."
393 (with-connection-local-application-variables
394 (cadr files-x-test--application)
395 (or remote-lazy-var
396 (setq-connection-local remote-lazy-var
397 (or (file-remote-p default-directory 'host)
398 "local")))))
399
400(defun files-x-test--set-lazy-var (value)
401 "Set the connection-local value of `remote-lazy-var'"
402 (with-connection-local-application-variables
403 (cadr files-x-test--application)
404 (setq-connection-local remote-lazy-var value)))
405
406(ert-deftest files-x-test-setq-connection-local ()
407 "Test dynamically setting connection local variables."
408 (let (connection-local-profile-alist connection-local-criteria-alist)
409 (connection-local-set-profile-variables
410 'remote-lazy files-x-test--variables5)
411 (connection-local-set-profiles
412 files-x-test--application
413 'remote-lazy)
414
415 ;; Test the initial local value.
416 (should (equal (files-x-test--get-lazy-var) "local"))
417
418 ;; Set the local value and make sure it retains the value we set.
419 (should (equal (files-x-test--set-lazy-var "here") "here"))
420 (should (equal (files-x-test--get-lazy-var) "here"))
421
422 (let ((default-directory "/method:host:"))
423 ;; Test the initial remote value.
424 (should (equal (files-x-test--get-lazy-var) "host"))
425
426 ;; Set the remote value and make sure it retains the value we set.
427 (should (equal (files-x-test--set-lazy-var "there") "there"))
428 (should (equal (files-x-test--get-lazy-var) "there"))
429 ;; Set another connection-local variable.
430 (with-connection-local-application-variables
431 (cadr files-x-test--application)
432 (setq-connection-local remote-null-device "null")))
433
434 ;; Make sure we get the local value we set above.
435 (should (equal (files-x-test--get-lazy-var) "here"))
436 (should-not (boundp 'remote-null-device))
437
438 ;; Make sure we get the remote values we set above.
439 (let ((default-directory "/method:host:"))
440 (should (equal (files-x-test--get-lazy-var) "there"))
441 (with-connection-local-application-variables
442 (cadr files-x-test--application)
443 (should (equal remote-null-device "null"))))))
444
379(provide 'files-x-tests) 445(provide 'files-x-tests)
380;;; files-x-tests.el ends here 446;;; files-x-tests.el ends here