diff options
author | Jim Porter <jporterbugs@gmail.com> | 2022-09-25 21:47:26 -0700 |
---|---|---|
committer | Jim Porter <jporterbugs@gmail.com> | 2022-10-17 18:48:52 -0700 |
commit | 7c41016fca5ab0638f1e2fed260e2ee41f3400c2 (patch) | |
tree | 11fe29e20ed60074578e2f2f10d47ed8bd65d404 /test/lisp/eshell/esh-var-tests.el | |
parent | f1caa10f04c980034f5ee6e0748cf3b03f460b2b (diff) | |
download | emacs-7c41016fca5ab0638f1e2fed260e2ee41f3400c2.tar.gz emacs-7c41016fca5ab0638f1e2fed260e2ee41f3400c2.tar.bz2 emacs-7c41016fca5ab0638f1e2fed260e2ee41f3400c2.zip |
Allow setting the values of variable aliases in Eshell
This makes commands like "COLUMNS=40 some-command" work as expected.
* lisp/eshell/esh-cmd.el (eshell-subcommand-bindings): Remove
'process-environment' from here...
* lisp/eshell/esh-var.el (eshell-var-initialize): ... and add to here,
along with 'eshell-variable-aliases-list'.
(eshell-inside-emacs): Convert to a 'defvar-local' to make it settable
in a particular Eshell buffer.
(eshell-variable-aliases-list): Make $?, $$, and $* read-only and
update docstring.
(eshell-set-variable): New function...
(eshell-handle-local-variables, eshell/export, eshell/unset): ... use
it.
(eshell/set, pcomplete/eshell-mode/set): New functions.
(eshell-get-variable): Get the variable alias's getter function when
appropriate and use a safer method for checking function arity.
* test/lisp/eshell/esh-var-tests.el (esh-var-test/set/env-var)
(esh-var-test/set/symbol, esh-var-test/unset/env-var)
(esh-var-test/unset/symbol, esh-var-test/setq, esh-var-test/export)
(esh-var-test/local-variables, esh-var-test/alias/function)
(esh-var-test/alias/function-pair, esh-var-test/alias/string)
(esh-var-test/alias/string/prefer-lisp, esh-var-test/alias/symbol)
(esh-var-test/alias/symbol-pair, esh-var-test/alias/export)
(esh-var-test/alias/local-variables): New tests.
* doc/misc/eshell.texi (Built-ins): Add 'set' and update 'unset'
documentation.
(Variables): Expand documentation of how to get/set variables.
Diffstat (limited to 'test/lisp/eshell/esh-var-tests.el')
-rw-r--r-- | test/lisp/eshell/esh-var-tests.el | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/test/lisp/eshell/esh-var-tests.el b/test/lisp/eshell/esh-var-tests.el index ad695e45d7e..a7ac52ed24a 100644 --- a/test/lisp/eshell/esh-var-tests.el +++ b/test/lisp/eshell/esh-var-tests.el @@ -25,6 +25,7 @@ (require 'ert) (require 'esh-mode) +(require 'esh-var) (require 'eshell) (require 'eshell-tests-helpers @@ -440,6 +441,150 @@ inside double-quotes" "000")) +;; Variable-related commands + +(ert-deftest esh-var-test/set/env-var () + "Test that `set' with a string variable name sets an environment variable." + (with-temp-eshell + (eshell-match-command-output "set VAR hello" "hello\n") + (should (equal (getenv "VAR") "hello"))) + (should-not (equal (getenv "VAR") "hello"))) + +(ert-deftest esh-var-test/set/symbol () + "Test that `set' with a symbol variable name sets a Lisp variable." + (let (eshell-test-value) + (eshell-command-result-equal "set #'eshell-test-value hello" + "hello") + (should (equal eshell-test-value "hello")))) + +(ert-deftest esh-var-test/unset/env-var () + "Test that `unset' with a string variable name unsets an env var." + (let ((process-environment (cons "VAR=value" process-environment))) + (with-temp-eshell + (eshell-match-command-output "unset VAR" "\\`\\'") + (should (equal (getenv "VAR") nil))) + (should (equal (getenv "VAR") "value")))) + +(ert-deftest esh-var-test/unset/symbol () + "Test that `unset' with a symbol variable name unsets a Lisp variable." + (let ((eshell-test-value "value")) + (eshell-command-result-equal "unset #'eshell-test-value" nil) + (should (equal eshell-test-value nil)))) + +(ert-deftest esh-var-test/setq () + "Test that `setq' sets Lisp variables." + (let (eshell-test-value) + (eshell-command-result-equal "setq eshell-test-value hello" + "hello") + (should (equal eshell-test-value "hello")))) + +(ert-deftest esh-var-test/export () + "Test that `export' sets environment variables." + (with-temp-eshell + (eshell-match-command-output "export VAR=hello" "\\`\\'") + (should (equal (getenv "VAR") "hello")))) + +(ert-deftest esh-var-test/local-variables () + "Test that \"VAR=value command\" temporarily sets variables." + (with-temp-eshell + (push "VAR=value" process-environment) + (eshell-match-command-output "VAR=hello env" "VAR=hello\n") + (should (equal (getenv "VAR") "value")))) + + +;; Variable aliases + +(ert-deftest esh-var-test/alias/function () + "Test using a variable alias defined as a function." + (with-temp-eshell + (push `("ALIAS" ,(lambda () "value") nil t) eshell-variable-aliases-list) + (eshell-match-command-output "echo $ALIAS" "value\n") + (eshell-match-command-output "set ALIAS hello" + "Variable `ALIAS' is not settable\n" + nil t))) + +(ert-deftest esh-var-test/alias/function-pair () + "Test using a variable alias defined as a pair of getter/setter functions." + (with-temp-eshell + (let ((eshell-test-value "value")) + (push `("ALIAS" (,(lambda () eshell-test-value) + . (lambda (_ value) + (setq eshell-test-value (upcase value)))) + nil t) + eshell-variable-aliases-list) + (eshell-match-command-output "echo $ALIAS" "value\n") + (eshell-match-command-output "set ALIAS hello" "HELLO\n") + (should (equal eshell-test-value "HELLO"))))) + +(ert-deftest esh-var-test/alias/string () + "Test using a variable alias defined as a string. +This should get/set the aliased environment variable." + (with-temp-eshell + (let ((eshell-test-value "lisp-value")) + (push "eshell-test-value=env-value" process-environment) + (push `("ALIAS" "eshell-test-value") eshell-variable-aliases-list) + (eshell-match-command-output "echo $ALIAS" "env-value\n") + (eshell-match-command-output "set ALIAS hello" "hello\n") + (should (equal (getenv "eshell-test-value") "hello")) + (should (equal eshell-test-value "lisp-value"))))) + +(ert-deftest esh-var-test/alias/string/prefer-lisp () + "Test using a variable alias defined as a string. +This sets `eshell-prefer-lisp-variables' to t and should get/set +the aliased Lisp variable." + (with-temp-eshell + (let ((eshell-test-value "lisp-value") + (eshell-prefer-lisp-variables t)) + (push "eshell-test-value=env-value" process-environment) + (push `("ALIAS" "eshell-test-value") eshell-variable-aliases-list) + (eshell-match-command-output "echo $ALIAS" "lisp-value\n") + (eshell-match-command-output "set ALIAS hello" "hello\n") + (should (equal (car process-environment) "eshell-test-value=env-value")) + (should (equal eshell-test-value "hello"))))) + +(ert-deftest esh-var-test/alias/symbol () + "Test using a variable alias defined as a symbol. +This should get/set the value bound to the symbol." + (with-temp-eshell + (let ((eshell-test-value "value")) + (push '("ALIAS" eshell-test-value) eshell-variable-aliases-list) + (eshell-match-command-output "echo $ALIAS" "value\n") + (eshell-match-command-output "set ALIAS hello" "hello\n") + (should (equal eshell-test-value "hello"))))) + +(ert-deftest esh-var-test/alias/symbol-pair () + "Test using a variable alias defined as a pair of symbols. +This should get the value bound to the symbol, but fail to set +it, since the setter is nil." + (with-temp-eshell + (let ((eshell-test-value "value")) + (push '("ALIAS" (eshell-test-value . nil)) eshell-variable-aliases-list) + (eshell-match-command-output "echo $ALIAS" "value\n") + (eshell-match-command-output "set ALIAS hello" + "Variable `ALIAS' is not settable\n" + nil t)))) + +(ert-deftest esh-var-test/alias/export () + "Test that `export' properly sets variable aliases." + (with-temp-eshell + (let ((eshell-test-value "value")) + (push `("ALIAS" (,(lambda () eshell-test-value) + . (lambda (_ value) (setq eshell-test-value value))) + nil t) + eshell-variable-aliases-list) + (eshell-match-command-output "export ALIAS=hello" "\\`\\'") + (should (equal eshell-test-value "hello"))))) + +(ert-deftest esh-var-test/alias/local-variables () + "Test that \"VAR=value cmd\" temporarily sets read-only variable aliases." + (with-temp-eshell + (let ((eshell-test-value "value")) + (push `("ALIAS" ,(lambda () eshell-test-value) t t) + eshell-variable-aliases-list) + (eshell-match-command-output "ALIAS=hello env" "ALIAS=hello\n") + (should (equal eshell-test-value "value"))))) + + ;; Built-in variables (ert-deftest esh-var-test/lines-var () |