tests: configuration: Add a test to cover 'unset regression.
[jackhill/guix/guix.git] / tests / home-import.scm
CommitLineData
40acbaf0
XC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
d7fb57bc 3;;; Copyright © 2022 Arjan Adriaanse <arjan@adriaan.se>
40acbaf0
XC
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (test-home-import)
21 #:use-module (guix scripts home import)
22 #:use-module (guix utils)
23 #:use-module (guix build utils)
24 #:use-module (guix packages)
25 #:use-module (ice-9 match)
26 #:use-module ((guix profiles) #:hide (manifest->code))
27 #:use-module ((guix build syscalls) #:select (mkdtemp!))
96728c54
LC
28 #:use-module ((guix scripts package)
29 #:select (manifest-entry-version-prefix))
40acbaf0
XC
30 #:use-module (gnu packages)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-64))
34
35;; Test the (guix scripts home import) tools.
36
37(test-begin "home-import")
38
39;; Example manifest entries.
40
41(define guile-2.0.9
42 (manifest-entry
43 (name "guile")
44 (version "2.0.9")
45 (item "/gnu/store/...")))
46
47(define glibc
48 (manifest-entry
49 (name "glibc")
50 (version "2.19")
51 (item "/gnu/store/...")))
52
53(define gcc
54 (manifest-entry
55 (name "gcc")
d7fb57bc
AA
56 (version "")
57 (output "lib")
40acbaf0
XC
58 (item "/gnu/store/...")))
59
60;; Helpers for checking and generating home environments.
61
62(define %destination-directory "/tmp/guix-config")
63(mkdir-p %destination-directory)
64
65(define %temporary-home-directory (mkdtemp! "/tmp/guix-home-import.XXXXXX"))
66
67(define-syntax-rule (define-home-environment-matcher name pattern)
68 (define (name obj)
69 (match obj
70 (pattern #t)
71 (x (pk 'fail x #f)))))
72
73(define (create-temporary-home files-alist)
74 "Create a temporary home directory in '%temporary-home-directory'.
75FILES-ALIST is an association list of files and the content of the
76corresponding file."
77 (define (create-file file content)
78 (let ((absolute-path (string-append %temporary-home-directory "/" file)))
79 (unless (file-exists? absolute-path)
80 (mkdir-p (dirname absolute-path)))
81 (call-with-output-file absolute-path
82 (cut display content <>))))
83
84 (for-each (match-lambda
85 ((file . content) (create-file file content)))
86 files-alist))
87
40acbaf0
XC
88(define (eval-test-with-home-environment files-alist manifest matcher)
89 (create-temporary-home files-alist)
90 (setenv "HOME" %temporary-home-directory)
91 (mkdir-p %temporary-home-directory)
6f4ca787
LC
92 (let* ((home-environment (manifest+configuration-files->code
93 manifest %destination-directory))
40acbaf0
XC
94 (result (matcher home-environment)))
95 (delete-file-recursively %temporary-home-directory)
96 result))
97
98(define-home-environment-matcher match-home-environment-no-services
99 ('begin
100 ('use-modules
101 ('gnu 'home)
102 ('gnu 'packages)
103 ('gnu 'services))
104 ('home-environment
105 ('packages
7041fe06
AM
106 ('specifications->packages
107 ('list "guile@2.0.9" "gcc:lib" "glibc@2.19")))
40acbaf0
XC
108 ('services
109 ('list)))))
110
6f4ca787
LC
111(define-home-environment-matcher match-home-environment-transformations
112 ('begin
113 ('use-modules
114 ('gnu 'home)
115 ('gnu 'packages)
116 ('gnu 'services)
117 ('guix 'transformations))
118
119 ('define transform ('options->transformation _))
120 ('home-environment
121 ('packages
122 ('list (transform ('specification->package "guile@2.0.9"))
d7fb57bc 123 ('list ('specification->package "gcc") "lib")
6f4ca787
LC
124 ('specification->package "glibc@2.19")))
125 ('services ('list)))))
126
40acbaf0
XC
127(define-home-environment-matcher match-home-environment-no-services-nor-packages
128 ('begin
129 ('use-modules
130 ('gnu 'home)
131 ('gnu 'packages)
132 ('gnu 'services))
133 ('home-environment
134 ('packages
7041fe06 135 ('specifications->packages ('list)))
40acbaf0
XC
136 ('services
137 ('list)))))
138
139(define-home-environment-matcher match-home-environment-bash-service
140 ('begin
141 ('use-modules
142 ('gnu 'home)
143 ('gnu 'packages)
144 ('gnu 'services)
145 ('guix 'gexp)
146 ('gnu 'home 'services 'shells))
147 ('home-environment
148 ('packages
7041fe06 149 ('specifications->packages ('list)))
40acbaf0
XC
150 ('services
151 ('list ('service
152 'home-bash-service-type
153 ('home-bash-configuration
f3597658 154 ('aliases ('quote ()))
40acbaf0 155 ('bashrc
ea19381b
XC
156 ('list ('local-file "/tmp/guix-config/.bashrc"
157 "bashrc"))))))))))
40acbaf0 158
6da2a5a5
LC
159(define-home-environment-matcher match-home-environment-bash-service-with-alias
160 ('begin
161 ('use-modules
162 ('gnu 'home)
163 ('gnu 'packages)
164 ('gnu 'services)
165 ('guix 'gexp)
166 ('gnu 'home 'services 'shells))
167 ('home-environment
168 ('packages
7041fe06 169 ('specifications->packages ('list)))
6da2a5a5
LC
170 ('services
171 ('list ('service
172 'home-bash-service-type
173 ('home-bash-configuration
174 ('aliases
175 ('quote (("grep" . "grep --exclude-from=\"$HOME/.grep-exclude\"")
176 ("ls" . "ls -p"))))
177 ('bashrc
178 ('list ('local-file "/tmp/guix-config/.bashrc"
179 "bashrc"))))))))))
180
6f4ca787 181
40acbaf0
XC
182(test-assert "manifest->code: No services"
183 (eval-test-with-home-environment
184 '()
185 (make-manifest (list guile-2.0.9 gcc glibc))
186 match-home-environment-no-services))
187
6f4ca787
LC
188(test-assert "manifest->code: No services, package transformations"
189 (eval-test-with-home-environment
190 '()
191 (make-manifest (list (manifest-entry
192 (inherit guile-2.0.9)
193 (properties `((transformations
194 . ((foo . "bar"))))))
195 gcc glibc))
196 match-home-environment-transformations))
197
40acbaf0
XC
198(test-assert "manifest->code: No packages nor services"
199 (eval-test-with-home-environment
200 '()
201 (make-manifest '())
202 match-home-environment-no-services-nor-packages))
203
204(test-assert "manifest->code: Bash service"
205 (eval-test-with-home-environment
206 '((".bashrc" . "echo 'hello guix'"))
207 (make-manifest '())
208 match-home-environment-bash-service))
209
6da2a5a5
LC
210(test-assert "manifest->code: Bash service with aliases"
211 (eval-test-with-home-environment
212 '((".bashrc"
213 . "# Aliases
214alias ls=\"ls -p\"; alias grep='grep --exclude-from=\"$HOME/.grep-exclude\"'\n"))
215 (make-manifest '())
216 match-home-environment-bash-service-with-alias))
217
40acbaf0 218(test-end "home-import")