home: import: Clarify "destination directory".
[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>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; 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 Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (test-home-import)
20 #:use-module (guix scripts home import)
21 #:use-module (guix utils)
22 #:use-module (guix build utils)
23 #:use-module (guix packages)
24 #:use-module (ice-9 match)
25 #:use-module ((guix profiles) #:hide (manifest->code))
26 #:use-module ((guix build syscalls) #:select (mkdtemp!))
27 #:use-module (gnu packages)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-26)
30 #:use-module (srfi srfi-64))
31
32;; Test the (guix scripts home import) tools.
33
34(test-begin "home-import")
35
36;; Example manifest entries.
37
38(define guile-2.0.9
39 (manifest-entry
40 (name "guile")
41 (version "2.0.9")
42 (item "/gnu/store/...")))
43
44(define glibc
45 (manifest-entry
46 (name "glibc")
47 (version "2.19")
48 (item "/gnu/store/...")))
49
50(define gcc
51 (manifest-entry
52 (name "gcc")
53 (version "10.3.0")
54 (item "/gnu/store/...")))
55
56;; Helpers for checking and generating home environments.
57
58(define %destination-directory "/tmp/guix-config")
59(mkdir-p %destination-directory)
60
61(define %temporary-home-directory (mkdtemp! "/tmp/guix-home-import.XXXXXX"))
62
63(define-syntax-rule (define-home-environment-matcher name pattern)
64 (define (name obj)
65 (match obj
66 (pattern #t)
67 (x (pk 'fail x #f)))))
68
69(define (create-temporary-home files-alist)
70 "Create a temporary home directory in '%temporary-home-directory'.
71FILES-ALIST is an association list of files and the content of the
72corresponding file."
73 (define (create-file file content)
74 (let ((absolute-path (string-append %temporary-home-directory "/" file)))
75 (unless (file-exists? absolute-path)
76 (mkdir-p (dirname absolute-path)))
77 (call-with-output-file absolute-path
78 (cut display content <>))))
79
80 (for-each (match-lambda
81 ((file . content) (create-file file content)))
82 files-alist))
83
84;; Copied from (guix profiles)
85(define (version-spec entry)
86 (let ((name (manifest-entry-name entry)))
87 (match (map package-version (find-packages-by-name name))
88 ((_)
89 ;; A single version of NAME is available, so do not specify the
90 ;; version number, even if the available version doesn't match ENTRY.
91 "")
92 (versions
93 ;; If ENTRY uses the latest version, don't specify any version.
94 ;; Otherwise return the shortest unique version prefix. Note that
95 ;; this is based on the currently available packages, which could
96 ;; differ from the packages available in the revision that was used
97 ;; to build MANIFEST.
98 (let ((current (manifest-entry-version entry)))
99 (if (every (cut version>? current <>)
100 (delete current versions))
101 ""
102 (version-unique-prefix (manifest-entry-version entry)
103 versions)))))))
104
105(define (eval-test-with-home-environment files-alist manifest matcher)
106 (create-temporary-home files-alist)
107 (setenv "HOME" %temporary-home-directory)
108 (mkdir-p %temporary-home-directory)
109 (let* ((home-environment (manifest->code manifest %destination-directory
110 #:entry-package-version version-spec
111 #:home-environment? #t))
112 (result (matcher home-environment)))
113 (delete-file-recursively %temporary-home-directory)
114 result))
115
116(define-home-environment-matcher match-home-environment-no-services
117 ('begin
118 ('use-modules
119 ('gnu 'home)
120 ('gnu 'packages)
121 ('gnu 'services))
122 ('home-environment
123 ('packages
124 ('map 'specification->package
125 ('list "guile@2.0.9" "gcc" "glibc@2.19")))
126 ('services
127 ('list)))))
128
129(define-home-environment-matcher match-home-environment-no-services-nor-packages
130 ('begin
131 ('use-modules
132 ('gnu 'home)
133 ('gnu 'packages)
134 ('gnu 'services))
135 ('home-environment
136 ('packages
137 ('map 'specification->package
138 ('list)))
139 ('services
140 ('list)))))
141
142(define-home-environment-matcher match-home-environment-bash-service
143 ('begin
144 ('use-modules
145 ('gnu 'home)
146 ('gnu 'packages)
147 ('gnu 'services)
148 ('guix 'gexp)
149 ('gnu 'home 'services 'shells))
150 ('home-environment
151 ('packages
152 ('map 'specification->package
153 ('list)))
154 ('services
155 ('list ('service
156 'home-bash-service-type
157 ('home-bash-configuration
158 ('bashrc
ea19381b
XC
159 ('list ('local-file "/tmp/guix-config/.bashrc"
160 "bashrc"))))))))))
40acbaf0
XC
161
162(test-assert "manifest->code: No services"
163 (eval-test-with-home-environment
164 '()
165 (make-manifest (list guile-2.0.9 gcc glibc))
166 match-home-environment-no-services))
167
168(test-assert "manifest->code: No packages nor services"
169 (eval-test-with-home-environment
170 '()
171 (make-manifest '())
172 match-home-environment-no-services-nor-packages))
173
174(test-assert "manifest->code: Bash service"
175 (eval-test-with-home-environment
176 '((".bashrc" . "echo 'hello guix'"))
177 (make-manifest '())
178 match-home-environment-bash-service))
179
180(test-end "home-import")