diff options
| author | Jim Porter | 2022-09-25 21:47:26 -0700 |
|---|---|---|
| committer | Jim Porter | 2022-10-17 18:48:52 -0700 |
| commit | 7c41016fca5ab0638f1e2fed260e2ee41f3400c2 (patch) | |
| tree | 11fe29e20ed60074578e2f2f10d47ed8bd65d404 /test/lisp/eshell | |
| parent | f1caa10f04c980034f5ee6e0748cf3b03f460b2b (diff) | |
| download | emacs-7c41016fca5ab0638f1e2fed260e2ee41f3400c2.tar.gz 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')
| -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 @@ | |||
| 25 | 25 | ||
| 26 | (require 'ert) | 26 | (require 'ert) |
| 27 | (require 'esh-mode) | 27 | (require 'esh-mode) |
| 28 | (require 'esh-var) | ||
| 28 | (require 'eshell) | 29 | (require 'eshell) |
| 29 | 30 | ||
| 30 | (require 'eshell-tests-helpers | 31 | (require 'eshell-tests-helpers |
| @@ -440,6 +441,150 @@ inside double-quotes" | |||
| 440 | "000")) | 441 | "000")) |
| 441 | 442 | ||
| 442 | 443 | ||
| 444 | ;; Variable-related commands | ||
| 445 | |||
| 446 | (ert-deftest esh-var-test/set/env-var () | ||
| 447 | "Test that `set' with a string variable name sets an environment variable." | ||
| 448 | (with-temp-eshell | ||
| 449 | (eshell-match-command-output "set VAR hello" "hello\n") | ||
| 450 | (should (equal (getenv "VAR") "hello"))) | ||
| 451 | (should-not (equal (getenv "VAR") "hello"))) | ||
| 452 | |||
| 453 | (ert-deftest esh-var-test/set/symbol () | ||
| 454 | "Test that `set' with a symbol variable name sets a Lisp variable." | ||
| 455 | (let (eshell-test-value) | ||
| 456 | (eshell-command-result-equal "set #'eshell-test-value hello" | ||
| 457 | "hello") | ||
| 458 | (should (equal eshell-test-value "hello")))) | ||
| 459 | |||
| 460 | (ert-deftest esh-var-test/unset/env-var () | ||
| 461 | "Test that `unset' with a string variable name unsets an env var." | ||
| 462 | (let ((process-environment (cons "VAR=value" process-environment))) | ||
| 463 | (with-temp-eshell | ||
| 464 | (eshell-match-command-output "unset VAR" "\\`\\'") | ||
| 465 | (should (equal (getenv "VAR") nil))) | ||
| 466 | (should (equal (getenv "VAR") "value")))) | ||
| 467 | |||
| 468 | (ert-deftest esh-var-test/unset/symbol () | ||
| 469 | "Test that `unset' with a symbol variable name unsets a Lisp variable." | ||
| 470 | (let ((eshell-test-value "value")) | ||
| 471 | (eshell-command-result-equal "unset #'eshell-test-value" nil) | ||
| 472 | (should (equal eshell-test-value nil)))) | ||
| 473 | |||
| 474 | (ert-deftest esh-var-test/setq () | ||
| 475 | "Test that `setq' sets Lisp variables." | ||
| 476 | (let (eshell-test-value) | ||
| 477 | (eshell-command-result-equal "setq eshell-test-value hello" | ||
| 478 | "hello") | ||
| 479 | (should (equal eshell-test-value "hello")))) | ||
| 480 | |||
| 481 | (ert-deftest esh-var-test/export () | ||
| 482 | "Test that `export' sets environment variables." | ||
| 483 | (with-temp-eshell | ||
| 484 | (eshell-match-command-output "export VAR=hello" "\\`\\'") | ||
| 485 | (should (equal (getenv "VAR") "hello")))) | ||
| 486 | |||
| 487 | (ert-deftest esh-var-test/local-variables () | ||
| 488 | "Test that \"VAR=value command\" temporarily sets variables." | ||
| 489 | (with-temp-eshell | ||
| 490 | (push "VAR=value" process-environment) | ||
| 491 | (eshell-match-command-output "VAR=hello env" "VAR=hello\n") | ||
| 492 | (should (equal (getenv "VAR") "value")))) | ||
| 493 | |||
| 494 | |||
| 495 | ;; Variable aliases | ||
| 496 | |||
| 497 | (ert-deftest esh-var-test/alias/function () | ||
| 498 | "Test using a variable alias defined as a function." | ||
| 499 | (with-temp-eshell | ||
| 500 | (push `("ALIAS" ,(lambda () "value") nil t) eshell-variable-aliases-list) | ||
| 501 | (eshell-match-command-output "echo $ALIAS" "value\n") | ||
| 502 | (eshell-match-command-output "set ALIAS hello" | ||
| 503 | "Variable `ALIAS' is not settable\n" | ||
| 504 | nil t))) | ||
| 505 | |||
| 506 | (ert-deftest esh-var-test/alias/function-pair () | ||
| 507 | "Test using a variable alias defined as a pair of getter/setter functions." | ||
| 508 | (with-temp-eshell | ||
| 509 | (let ((eshell-test-value "value")) | ||
| 510 | (push `("ALIAS" (,(lambda () eshell-test-value) | ||
| 511 | . (lambda (_ value) | ||
| 512 | (setq eshell-test-value (upcase value)))) | ||
| 513 | nil t) | ||
| 514 | eshell-variable-aliases-list) | ||
| 515 | (eshell-match-command-output "echo $ALIAS" "value\n") | ||
| 516 | (eshell-match-command-output "set ALIAS hello" "HELLO\n") | ||
| 517 | (should (equal eshell-test-value "HELLO"))))) | ||
| 518 | |||
| 519 | (ert-deftest esh-var-test/alias/string () | ||
| 520 | "Test using a variable alias defined as a string. | ||
| 521 | This should get/set the aliased environment variable." | ||
| 522 | (with-temp-eshell | ||
| 523 | (let ((eshell-test-value "lisp-value")) | ||
| 524 | (push "eshell-test-value=env-value" process-environment) | ||
| 525 | (push `("ALIAS" "eshell-test-value") eshell-variable-aliases-list) | ||
| 526 | (eshell-match-command-output "echo $ALIAS" "env-value\n") | ||
| 527 | (eshell-match-command-output "set ALIAS hello" "hello\n") | ||
| 528 | (should (equal (getenv "eshell-test-value") "hello")) | ||
| 529 | (should (equal eshell-test-value "lisp-value"))))) | ||
| 530 | |||
| 531 | (ert-deftest esh-var-test/alias/string/prefer-lisp () | ||
| 532 | "Test using a variable alias defined as a string. | ||
| 533 | This sets `eshell-prefer-lisp-variables' to t and should get/set | ||
| 534 | the aliased Lisp variable." | ||
| 535 | (with-temp-eshell | ||
| 536 | (let ((eshell-test-value "lisp-value") | ||
| 537 | (eshell-prefer-lisp-variables t)) | ||
| 538 | (push "eshell-test-value=env-value" process-environment) | ||
| 539 | (push `("ALIAS" "eshell-test-value") eshell-variable-aliases-list) | ||
| 540 | (eshell-match-command-output "echo $ALIAS" "lisp-value\n") | ||
| 541 | (eshell-match-command-output "set ALIAS hello" "hello\n") | ||
| 542 | (should (equal (car process-environment) "eshell-test-value=env-value")) | ||
| 543 | (should (equal eshell-test-value "hello"))))) | ||
| 544 | |||
| 545 | (ert-deftest esh-var-test/alias/symbol () | ||
| 546 | "Test using a variable alias defined as a symbol. | ||
| 547 | This should get/set the value bound to the symbol." | ||
| 548 | (with-temp-eshell | ||
| 549 | (let ((eshell-test-value "value")) | ||
| 550 | (push '("ALIAS" eshell-test-value) eshell-variable-aliases-list) | ||
| 551 | (eshell-match-command-output "echo $ALIAS" "value\n") | ||
| 552 | (eshell-match-command-output "set ALIAS hello" "hello\n") | ||
| 553 | (should (equal eshell-test-value "hello"))))) | ||
| 554 | |||
| 555 | (ert-deftest esh-var-test/alias/symbol-pair () | ||
| 556 | "Test using a variable alias defined as a pair of symbols. | ||
| 557 | This should get the value bound to the symbol, but fail to set | ||
| 558 | it, since the setter is nil." | ||
| 559 | (with-temp-eshell | ||
| 560 | (let ((eshell-test-value "value")) | ||
| 561 | (push '("ALIAS" (eshell-test-value . nil)) eshell-variable-aliases-list) | ||
| 562 | (eshell-match-command-output "echo $ALIAS" "value\n") | ||
| 563 | (eshell-match-command-output "set ALIAS hello" | ||
| 564 | "Variable `ALIAS' is not settable\n" | ||
| 565 | nil t)))) | ||
| 566 | |||
| 567 | (ert-deftest esh-var-test/alias/export () | ||
| 568 | "Test that `export' properly sets variable aliases." | ||
| 569 | (with-temp-eshell | ||
| 570 | (let ((eshell-test-value "value")) | ||
| 571 | (push `("ALIAS" (,(lambda () eshell-test-value) | ||
| 572 | . (lambda (_ value) (setq eshell-test-value value))) | ||
| 573 | nil t) | ||
| 574 | eshell-variable-aliases-list) | ||
| 575 | (eshell-match-command-output "export ALIAS=hello" "\\`\\'") | ||
| 576 | (should (equal eshell-test-value "hello"))))) | ||
| 577 | |||
| 578 | (ert-deftest esh-var-test/alias/local-variables () | ||
| 579 | "Test that \"VAR=value cmd\" temporarily sets read-only variable aliases." | ||
| 580 | (with-temp-eshell | ||
| 581 | (let ((eshell-test-value "value")) | ||
| 582 | (push `("ALIAS" ,(lambda () eshell-test-value) t t) | ||
| 583 | eshell-variable-aliases-list) | ||
| 584 | (eshell-match-command-output "ALIAS=hello env" "ALIAS=hello\n") | ||
| 585 | (should (equal eshell-test-value "value"))))) | ||
| 586 | |||
| 587 | |||
| 443 | ;; Built-in variables | 588 | ;; Built-in variables |
| 444 | 589 | ||
| 445 | (ert-deftest esh-var-test/lines-var () | 590 | (ert-deftest esh-var-test/lines-var () |