environment.scm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ;;;; environment.scm -- tests for (mcron environment) module
  2. ;;; Copyright © 2016, 2018 Mathieu Lirzin <mthl@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Mcron.
  5. ;;;
  6. ;;; GNU Mcron is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation, either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; GNU Mcron is distributed in the hope that it will be useful,
  12. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Mcron. If not, see <http://www.gnu.org/licenses/>.
  18. (use-modules (srfi srfi-64)
  19. (srfi srfi-111)
  20. (mcron environment))
  21. (test-begin "environment")
  22. ;;; Check 'current-environment-mods' initial value which should be empty.
  23. (test-equal "current-environment-mods: init"
  24. '()
  25. (unbox (@@ (mcron environment) %current-environment-mods)))
  26. ;;; Check 'current-environment-mods-copy' with an empty environment
  27. (test-assert "current-environment-mods-copy: empty"
  28. (let* ((env (box '()))
  29. (copy0 (get-current-environment-mods-copy #:environ env))
  30. (copy1 (get-current-environment-mods-copy #:environ env)))
  31. (set! copy1 (assoc-set! copy1 "FOO" "BAR"))
  32. (and (equal? '() (unbox env))
  33. (equal? '() copy0)
  34. (equal? '(("FOO" . "BAR")) copy1))))
  35. ;;; Check 'current-environment-mods-copy' with a basic environment
  36. (test-assert "current-environment-mods-copy: basic"
  37. (let* ((init-env '(("a" . "1") ("b" . "2")))
  38. (env (box init-env))
  39. (copy0 (get-current-environment-mods-copy #:environ env))
  40. (copy1 (get-current-environment-mods-copy #:environ env)))
  41. (set! copy1 (assoc-set! copy1 "c" "3"))
  42. (and (equal? init-env (unbox env))
  43. (equal? init-env copy0)
  44. (equal? `(("c" . "3") . ,init-env) copy1))))
  45. ;;; Check 'append-environment-mods' basic call
  46. (test-equal "append-environment-mods: basic"
  47. "BAR"
  48. (let ((env (box '())))
  49. (append-environment-mods "FOO" "BAR" #:environ env)
  50. (assoc-ref (unbox env) "FOO")))
  51. ;;; Check 'append-environment-mods' that when adding the same key twice the
  52. ;;; later is placed after the previous one.
  53. (test-equal "append-environment-mods: twice"
  54. '(("FOO" . "BAR") ("FOO" . "BAZ"))
  55. (let ((env (box '())))
  56. (append-environment-mods "FOO" "BAR" #:environ env)
  57. (append-environment-mods "FOO" "BAZ" #:environ env)
  58. (unbox env)))
  59. ;;; Check 'clear-environment-mods' side effect
  60. (test-equal "clear-environment-mods: effect"
  61. '()
  62. (let ((env (box '())))
  63. (append-environment-mods "FOO" "BAR" #:environ env)
  64. (append-environment-mods "FOO" "BAZ" #:environ env)
  65. (clear-environment-mods #:environ env)
  66. (unbox env)))
  67. ;;; Check 'modify-environment' basic call
  68. (test-assert "modifiy-environment: basic"
  69. (begin
  70. (modify-environment '(("FOO" . "bar")) (getpw))
  71. (equal? (getenv "FOO") "bar")))
  72. (test-assert "modifiy-environment: user & logname"
  73. ;; Check that USER and LOGNAME environment variables can't be changed.
  74. (let* ((user-entry (pk (getpw)))
  75. (user-name (passwd:name user-entry)))
  76. (modify-environment '(("USER" . "alice")) user-entry)
  77. (modify-environment '(("LOGNAME" . "bob")) user-entry)
  78. (equal? user-name
  79. (pk (getenv "USER"))
  80. (pk (getenv "LOGNAME")))))
  81. (test-end)