AboutSummaryRefsLogTreeCommitDiffStats
path: root/tests/environment.scm
blob: 05ad3be873d8ead0a6649501bcbd1f37341affd3 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
;;;; environment.scm -- tests for (mcron environment) module
;;; Copyright © 2016, 2018 Mathieu Lirzin <mthl@gnu.org>
;;;
;;; This file is part of GNU Mcron.
;;;
;;; GNU Mcron is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; GNU Mcron is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Mcron.  If not, see <http://www.gnu.org/licenses/>.

(use-modules (srfi srfi-64)
             (srfi srfi-111)
             (mcron environment))

(test-begin "environment")

;;; Check 'current-environment-mods' initial value which should be empty.
(test-equal "current-environment-mods: init"
  '()
  (unbox (@@ (mcron environment) %current-environment-mods)))

;;; Check 'current-environment-mods-copy' with an empty environment
(test-assert "current-environment-mods-copy: empty"
  (let* ((env (box '()))
         (copy0 (get-current-environment-mods-copy #:environ env))
         (copy1 (get-current-environment-mods-copy #:environ env)))
    (set! copy1 (assoc-set! copy1 "FOO" "BAR"))
    (and (equal? '() (unbox env))
         (equal? '() copy0)
         (equal? '(("FOO" . "BAR")) copy1))))

;;; Check 'current-environment-mods-copy' with a basic environment
(test-assert "current-environment-mods-copy: basic"
  (let* ((init-env '(("a" . "1") ("b" . "2")))
         (env (box init-env))
         (copy0 (get-current-environment-mods-copy #:environ env))
         (copy1 (get-current-environment-mods-copy #:environ env)))
    (set! copy1 (assoc-set! copy1 "c" "3"))
    (and (equal? init-env (unbox env))
         (equal? init-env copy0)
         (equal? `(("c" . "3") . ,init-env) copy1))))

;;; Check 'append-environment-mods' basic call
(test-equal "append-environment-mods: basic"
  "BAR"
  (let ((env (box '())))
    (append-environment-mods "FOO" "BAR" #:environ env)
    (assoc-ref (unbox env) "FOO")))

;;; Check 'append-environment-mods' that when adding the same key twice the
;;; later is placed after the previous one.
(test-equal "append-environment-mods: twice"
  '(("FOO" . "BAR") ("FOO" . "BAZ"))
  (let ((env (box '())))
    (append-environment-mods "FOO" "BAR" #:environ env)
    (append-environment-mods "FOO" "BAZ" #:environ env)
    (unbox env)))

;;; Check 'clear-environment-mods' side effect
(test-equal "clear-environment-mods: effect"
  '()
  (let ((env (box '())))
    (append-environment-mods "FOO" "BAR" #:environ env)
    (append-environment-mods "FOO" "BAZ" #:environ env)
    (clear-environment-mods #:environ env)
    (unbox env)))

;;; Check 'modify-environment' basic call
(test-assert "modifiy-environment: basic"
  (begin
    (modify-environment '(("FOO" . "bar")) (getpw))
    (equal? (getenv "FOO") "bar")))

(test-assert "modifiy-environment: user & logname"
  ;; Check that USER and LOGNAME environment variables can't be changed.
  (let* ((user-entry (pk (getpw)))
         (user-name  (passwd:name user-entry)))
    (modify-environment '(("USER" . "alice")) user-entry)
    (modify-environment '(("LOGNAME" . "bob")) user-entry)
    (equal? user-name
            (pk (getenv "USER"))
            (pk (getenv "LOGNAME")))))

(test-end)