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
This commit is contained in:
Carlo Zancanaro 2023-05-20 23:05:06 +10:00 committed by Ludovic Courtès
parent 6da03fcc45
commit 994fbc0ac6
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5
2 changed files with 15 additions and 12 deletions

View file

@ -9,6 +9,7 @@
;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com> ;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;; Copyright © 2021, 2022 Maxime Devos <maximedevos@telenet.be> ;;; Copyright © 2021, 2022 Maxime Devos <maximedevos@telenet.be>
;;; Copyright © 2021 Brendan Tildesley <mail@brendan.scot> ;;; Copyright © 2021 Brendan Tildesley <mail@brendan.scot>
;;; Copyright © 2023 Carlo Zancanaro <carlo@zancanaro.id.au>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -729,18 +730,22 @@ (define (every* pred lst)
(define* (alist-cons-before reference key value alist (define* (alist-cons-before reference key value alist
#:optional (key=? equal?)) #:optional (key=? equal?))
"Insert the KEY/VALUE pair before the first occurrence of a pair whose key "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) (let-values (((before after)
(break (match-lambda (break (match-lambda
((k . _) ((k . _)
(key=? k reference))) (key=? k reference)))
alist))) 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 (define* (alist-cons-after reference key value alist
#:optional (key=? equal?)) #:optional (key=? equal?))
"Insert the KEY/VALUE pair after the first occurrence of a pair whose key "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) (let-values (((before after)
(break (match-lambda (break (match-lambda
((k . _) ((k . _)
@ -748,9 +753,7 @@ (define* (alist-cons-after reference key value alist
alist))) alist)))
(match after (match after
((reference after ...) ((reference after ...)
(append before (cons* reference `(,key . ,value) after))) (append before (cons* reference `(,key . ,value) after))))))
(()
(append before `((,key . ,value)))))))
(define* (alist-replace key value alist #:optional (key=? equal?)) (define* (alist-replace key value alist #:optional (key=? equal?))
"Replace the first pair in ALIST whose car is KEY with the KEY/VALUE pair. "Replace the first pair in ALIST whose car is KEY with the KEY/VALUE pair.

View file

@ -41,17 +41,17 @@ (define-module (test build-utils)
'((a . 1) (x . 42) (b . 2) (c . 3)) '((a . 1) (x . 42) (b . 2) (c . 3))
(alist-cons-before 'b 'x 42 '((a . 1) (b . 2) (c . 3)))) (alist-cons-before 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
(test-equal "alist-cons-before, reference not found" (test-assert "alist-cons-before, reference not found"
'((a . 1) (b . 2) (c . 3) (x . 42)) (not (false-if-exception
(alist-cons-before 'z 'x 42 '((a . 1) (b . 2) (c . 3)))) (alist-cons-before 'z 'x 42 '((a . 1) (b . 2) (c . 3))))))
(test-equal "alist-cons-after" (test-equal "alist-cons-after"
'((a . 1) (b . 2) (x . 42) (c . 3)) '((a . 1) (b . 2) (x . 42) (c . 3))
(alist-cons-after 'b 'x 42 '((a . 1) (b . 2) (c . 3)))) (alist-cons-after 'b 'x 42 '((a . 1) (b . 2) (c . 3))))
(test-equal "alist-cons-after, reference not found" (test-assert "alist-cons-after, reference not found"
'((a . 1) (b . 2) (c . 3) (x . 42)) (not (false-if-exception
(alist-cons-after 'z 'x 42 '((a . 1) (b . 2) (c . 3)))) (alist-cons-after 'z 'x 42 '((a . 1) (b . 2) (c . 3))))))
(test-equal "alist-replace" (test-equal "alist-replace"
'((a . 1) (b . 77) (c . 3)) '((a . 1) (b . 77) (c . 3))