summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@zancanaro.id.au>2023-05-20 23:05:06 +1000
committerLudovic Courtès <ludo@gnu.org>2024-06-17 22:51:33 +0200
commit72724673c80149228bc1028849c00985fce92f93 (patch)
treefc487ae03946997c54d7399da9e96a49e1c5aff2
parent4f742e3c0e717de0aacc7a0f37e29d824bd22b53 (diff)
build: utils: Raise error in modify-phases upon missing key.
* guix/build/utils.scm (alist-cons-before) (alist-cons-after): Error with a match failure if the reference is not found, instead of appending to the alist. * tests/build-utils.scm: Update tests to match the new behavior. Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com> Change-Id: I3044b101bd06231d5cd55a544ac1009e6ce6f9a0
-rw-r--r--guix/build/utils.scm15
-rw-r--r--tests/build-utils.scm12
2 files changed, 15 insertions, 12 deletions
diff --git a/guix/build/utils.scm b/guix/build/utils.scm
index 2352a627e9..8e630ad586 100644
--- a/guix/build/utils.scm
+++ b/guix/build/utils.scm
@@ -9,6 +9,7 @@
;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;; Copyright © 2021, 2022 Maxime Devos <maximedevos@telenet.be>
;;; Copyright © 2021 Brendan Tildesley <mail@brendan.scot>
+;;; Copyright © 2023 Carlo Zancanaro <carlo@zancanaro.id.au>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -729,18 +730,22 @@ effects, such as displaying warnings or error messages."
(define* (alist-cons-before reference key value alist
#:optional (key=? equal?))
"Insert the KEY/VALUE pair before the first occurrence of a pair whose key
-is REFERENCE in ALIST. Use KEY=? to compare keys."
+is REFERENCE in ALIST. Use KEY=? to compare keys. An error is raised when no
+such pair exists."
(let-values (((before after)
(break (match-lambda
((k . _)
(key=? k reference)))
alist)))
- (append before (alist-cons key value after))))
+ (match after
+ ((_ _ ...)
+ (append before (alist-cons key value after))))))
(define* (alist-cons-after reference key value alist
#:optional (key=? equal?))
"Insert the KEY/VALUE pair after the first occurrence of a pair whose key
-is REFERENCE in ALIST. Use KEY=? to compare keys."
+is REFERENCE in ALIST. Use KEY=? to compare keys. An error is raised when
+no such pair exists."
(let-values (((before after)
(break (match-lambda
((k . _)
@@ -748,9 +753,7 @@ is REFERENCE in ALIST. Use KEY=? to compare keys."
alist)))
(match after
((reference after ...)
- (append before (cons* reference `(,key . ,value) after)))
- (()
- (append before `((,key . ,value)))))))
+ (append before (cons* reference `(,key . ,value) after))))))
(define* (alist-replace key value alist #:optional (key=? equal?))
"Replace the first pair in ALIST whose car is KEY with the KEY/VALUE pair.
diff --git a/tests/build-utils.scm b/tests/build-utils.scm
index 7f4f12ccc7..3babf5d544 100644
--- a/tests/build-utils.scm
+++ b/tests/build-utils.scm
@@ -41,17 +41,17 @@
'((a . 1) (x . 42) (b . 2) (c . 3))
(alist-cons-before 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
-(test-equal "alist-cons-before, reference not found"
- '((a . 1) (b . 2) (c . 3) (x . 42))
- (alist-cons-before 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
+(test-assert "alist-cons-before, reference not found"
+ (not (false-if-exception
+ (alist-cons-before 'z 'x 42 '((a . 1) (b . 2) (c . 3))))))
(test-equal "alist-cons-after"
'((a . 1) (b . 2) (x . 42) (c . 3))
(alist-cons-after 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
-(test-equal "alist-cons-after, reference not found"
- '((a . 1) (b . 2) (c . 3) (x . 42))
- (alist-cons-after 'z 'x 42 '((a . 1) (b . 2) (c . 3))))
+(test-assert "alist-cons-after, reference not found"
+ (not (false-if-exception
+ (alist-cons-after 'z 'x 42 '((a . 1) (b . 2) (c . 3))))))
(test-equal "alist-replace"
'((a . 1) (b . 77) (c . 3))