services: configuration: Report the location of field type errors.
[jackhill/guix/guix.git] / tests / services / configuration.scm
CommitLineData
a77e3a58
MC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
2ad89675 3;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
fb7e6ccb 4;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org>
a77e3a58
MC
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
b7297d66 21(define-module (tests services configuration)
a77e3a58 22 #:use-module (gnu services configuration)
fb7e6ccb 23 #:use-module (guix diagnostics)
a77e3a58
MC
24 #:use-module (guix gexp)
25 #:use-module (srfi srfi-34)
26 #:use-module (srfi srfi-64))
27
28;;; Tests for the (gnu services configuration) module.
29
30(test-begin "services-configuration")
31
e1151705
AL
32(define (serialize-number field value)
33 (format #f "~a=~a" field value))
34
a77e3a58
MC
35\f
36;;;
37;;; define-configuration macro.
38;;;
39
40(define-configuration port-configuration
41 (port (number 80) "The port number.")
42 (no-serialization))
43
44(test-equal "default value, no serialization"
45 80
46 (port-configuration-port (port-configuration)))
47
fb7e6ccb
LC
48(test-equal "wrong type for a field"
49 '("configuration.scm" 57 11) ;error location
50 (guard (c ((configuration-error? c)
51 (let ((loc (error-location c)))
52 (list (basename (location-file loc))
53 (location-line loc)
54 (location-column loc)))))
55 (port-configuration
56 ;; This is line 56; the test relies on line/column numbers!
57 (port "This is not a number!"))))
58
a77e3a58
MC
59(define-configuration port-configuration-cs
60 (port (number 80) "The port number." empty-serializer))
61
62(test-equal "default value, custom serializer"
63 80
64 (port-configuration-cs-port (port-configuration-cs)))
65
a77e3a58
MC
66(define-configuration port-configuration-ndv
67 (port (number) "The port number."))
68
69(test-equal "no default value, provided"
70 55
71 (port-configuration-ndv-port (port-configuration-ndv
72 (port 55))))
73
74(test-assert "no default value, not provided"
75 (guard (c ((configuration-error? c)
76 #t))
77 (port-configuration-ndv-port (port-configuration-ndv))))
78
79(define (custom-number-serializer name value)
b7297d66 80 (format #f "~a = ~a;" name value))
a77e3a58
MC
81
82(define-configuration serializable-configuration
83 (port (number 80) "The port number." custom-number-serializer))
84
85(test-assert "serialize-configuration"
86 (gexp?
87 (let ((config (serializable-configuration)))
88 (serialize-configuration config serializable-configuration-fields))))
89
90(define-configuration serializable-configuration
91 (port (number 80) "The port number." custom-number-serializer)
92 (no-serialization))
93
94(test-assert "serialize-configuration with no-serialization"
95 ;; When serialization is disabled, the serializer is set to #f, so
96 ;; attempting to use it fails with a 'wrong-type-arg' error.
97 (not (false-if-exception
98 (let ((config (serializable-configuration)))
99 (serialize-configuration config serializable-configuration-fields)))))
b7297d66 100
2ad89675
XC
101(define (custom-prefix-serialize-integer field-name name) name)
102
103(define-configuration configuration-with-prefix
104 (port (integer 10) "The port number.")
105 (prefix custom-prefix-))
106
107(test-assert "serialize-configuration with prefix"
108 (gexp?
109 (let ((config (configuration-with-prefix)))
110 (serialize-configuration config configuration-with-prefix-fields))))
111
b7297d66
MC
112\f
113;;;
114;;; define-maybe macro.
115;;;
116(define-maybe number)
117
118(define-configuration config-with-maybe-number
e1151705
AL
119 (port (maybe-number 80) "")
120 (count maybe-number ""))
b7297d66
MC
121
122(test-equal "maybe value serialization"
123 "port=80"
124 (serialize-maybe-number "port" 80))
125
e1151705
AL
126(define (config-with-maybe-number->string x)
127 (eval (gexp->approximate-sexp
128 (serialize-configuration x config-with-maybe-number-fields))
129 (current-module)))
130
131(test-equal "maybe value serialization of the instance"
132 "port=42count=43"
133 (config-with-maybe-number->string
134 (config-with-maybe-number
135 (port 42)
136 (count 43))))
137
138(test-equal "maybe value serialization of the instance, unspecified"
139 "port=42"
140 (config-with-maybe-number->string
141 (config-with-maybe-number
142 (port 42))))
143
b7297d66
MC
144(define-maybe/no-serialization string)
145
146(define-configuration config-with-maybe-string/no-serialization
147 (name (maybe-string) "The name of the item.")
148 (no-serialization))
149
150(test-assert "maybe value without serialization no procedure bound"
151 (not (defined? 'serialize-maybe-string)))
8cb1a49a
AL
152
153(test-assert "maybe type, no default"
154 (unspecified?
155 (config-with-maybe-string/no-serialization-name
156 (config-with-maybe-string/no-serialization))))
157
158(test-assert "maybe type, with default"
159 (equal?
160 "foo"
161 (config-with-maybe-string/no-serialization-name
162 (config-with-maybe-string/no-serialization
163 (name "foo")))))