From patchwork Thu Aug 27 05:12:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Maxim Cournoyer X-Patchwork-Id: 23791 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 6B56A27BBE6; Thu, 27 Aug 2020 06:15:08 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTP id EBF4D27BBE5 for ; Thu, 27 Aug 2020 06:15:07 +0100 (BST) Received: from localhost ([::1]:56058 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBAFf-0001Uk-CO for patchwork@mira.cbaines.net; Thu, 27 Aug 2020 01:15:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35784) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBAFa-0001Ub-EQ for guix-patches@gnu.org; Thu, 27 Aug 2020 01:15:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:58053) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBAFa-0006io-5O for guix-patches@gnu.org; Thu, 27 Aug 2020 01:15:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kBAFZ-0005Kr-VC for guix-patches@gnu.org; Thu, 27 Aug 2020 01:15:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#43064] [PATCH] gexp: computed-file: Prevent mistakenly overriding default option values. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 27 Aug 2020 05:15:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 43064 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 43064@debbugs.gnu.org Cc: Maxim Cournoyer X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.159850529320475 (code B ref -1); Thu, 27 Aug 2020 05:15:01 +0000 Received: (at submit) by debbugs.gnu.org; 27 Aug 2020 05:14:53 +0000 Received: from localhost ([127.0.0.1]:41366 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kBAFQ-0005KB-Ln for submit@debbugs.gnu.org; Thu, 27 Aug 2020 01:14:53 -0400 Received: from lists.gnu.org ([209.51.188.17]:35724) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kBAFM-0005K1-Oj for submit@debbugs.gnu.org; Thu, 27 Aug 2020 01:14:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35744) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBAFM-0001Tt-Jp for guix-patches@gnu.org; Thu, 27 Aug 2020 01:14:48 -0400 Received: from mail-qk1-x731.google.com ([2607:f8b0:4864:20::731]:35083) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBAFK-0006gA-SO for guix-patches@gnu.org; Thu, 27 Aug 2020 01:14:48 -0400 Received: by mail-qk1-x731.google.com with SMTP id p25so4905023qkp.2 for ; Wed, 26 Aug 2020 22:14:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=T4tZLkhcVqNWkplElLEsdqQ/7U/CXUzLU6orPqVLDYU=; b=QpmzJL81vJdxXL4ij+u9N1WTYa+VYNOp/LC7Fza4dxSv5D1rskipeX5nScTSxTyisv 2Q3tZSTSIjHtzAQis1fD3z4N9FysH7gG3z1szBLzHa3o5kZBemkCGpc2Qc72qzlVWNl3 baMTlPemnPRQanCt6FVMJlg/AMo85RcyBp+jkIfe43LxAg+dBsAe5wKaNuE7GZKzzZ87 rOzoIdhEQZB5DiHp8tMloLXWc0iVW6TTctYYv5A0VcAi+dC1y8AJYDXaxlERu1pFrZj9 JZbc2SUI3yYK6DnWqRPhQpEjjrwG7kSn/azJhs/zubSui1ZH3VzTRfomGanP5dVZ9QWF ZZeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=T4tZLkhcVqNWkplElLEsdqQ/7U/CXUzLU6orPqVLDYU=; b=jok4DH4WzeKFxVbrruBivgr2E2lbms/a2Ml+dFc04VG3dip+Snop5dpqeUbc3XuTmT 56WY0fQQbBQqnp3FQNTv2fkAnCKitUMAFscTjWbOqssSCxM6919VvHz0/kG2h2josZ6v 0bY/Te/01LkiRVxqzr/QXaObDe1k1sveb4wwMjLX8emdoV44MM7Xe8PKWLv1jS01wMA+ aJcnX9bfSg9byMUmL2YDfPIJnSbYeZiofJDUv6YOlj+ypaAh6MBbwX/SIEqvvq507inq cbcmxfz6rKzAWzojxAN4T0EFRwQ+Bd6FmHh6mJXcIk0lrThfoYqK8RyrfYkBKm86uhUs Tj9w== X-Gm-Message-State: AOAM531KirHRFOdGkUbsWrqKc+xTaqUWI/xxRUjQ2DG/wNoLEnSVwFhb 6fePWn+jDcALPtvPkxiscZIpEcy/KVg= X-Google-Smtp-Source: ABdhPJzoyw4xVKD27QuLu9BAv3xB2PqrA72xn7Y+t+fc7AgGdPqMgcosMYKTLOB6c4SpTV/pLumntg== X-Received: by 2002:a37:b247:: with SMTP id b68mr3170209qkf.90.1598505285091; Wed, 26 Aug 2020 22:14:45 -0700 (PDT) Received: from localhost.localdomain (dsl-205-151-56-239.b2b2c.ca. [205.151.56.239]) by smtp.gmail.com with ESMTPSA id z14sm950469qtn.92.2020.08.26.22.14.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Aug 2020 22:14:44 -0700 (PDT) From: Maxim Cournoyer Date: Thu, 27 Aug 2020 01:12:26 -0400 Message-Id: <20200827051226.28117-1-maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::731; envelope-from=maxim.cournoyer@gmail.com; helo=mail-qk1-x731.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches Because the options are passed as a list rather than actual keyword arguments, omitting to repeat the default values in that list easily causes the default values to be lost. * guix/gexp.scm (%computed-file-default-options): New variable. (alist->plist): New procedure. (computed-file-combine-options-with-defaults): New procedure. (computed-file): Use the above procedures to form the default OPTIONS value. Update doc. Use the COMPUTED-FILE-COMBINE-OPTIONS-WITH-DEFAULTS procedure to combine the user options with the default options, when they aren't overridden. * tests/gexp.scm ("computed-file options defaults honored") ("computed-file options defaults overridden"): Add tests. --- guix/gexp.scm | 42 +++++++++++++++++++++++++++++++++++++++--- tests/gexp.scm | 12 ++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/guix/gexp.scm b/guix/gexp.scm index 67b6121313..14e07e8fe6 100644 --- a/guix/gexp.scm +++ b/guix/gexp.scm @@ -3,6 +3,7 @@ ;;; Copyright © 2018 Clément Lassieur ;;; Copyright © 2018 Jan Nieuwenhuizen ;;; Copyright © 2019, 2020 Mathieu Othacehe +;;; Copyright © 2020 Maxim Cournoyer ;;; ;;; This file is part of GNU Guix. ;;; @@ -503,14 +504,49 @@ This is the declarative counterpart of 'text-file'." (guile computed-file-guile) ; (options computed-file-options)) ;list of arguments +;;; Alist containing the default options for computed-file. +(define %computed-file-default-options '((#:local-build? . #t))) + +(define (alist->plist alist) + "Transform an association list into a property list." + (fold (lambda (current acc) + (match current + ((x . y) + (append acc (list x y))))) + '() + alist)) + +(define (computed-file-combine-options-with-defaults options) + + (define alist->keys + (match-lambda + (((key . value) ...) + key))) + + (define (plist->keys plist) + (filter keyword? plist)) + + (define (default-keys->plist keys) + (append-map (lambda (key) + (list key (assq-ref %computed-file-default-options key))) + keys)) + + (let ((default-keys (lset-difference + eq? + (alist->keys %computed-file-default-options) + (plist->keys options)))) + (append options (default-keys->plist default-keys)))) + (define* (computed-file name gexp - #:key guile (options '(#:local-build? #t))) + #:key guile (options (alist->plist + %computed-file-default-options))) "Return an object representing the store item NAME, a file or directory computed by GEXP. OPTIONS is a list of additional arguments to pass -to 'gexp->derivation'. +to 'gexp->derivation', which defaults to %COMPUTED-FILE-DEFAULT-OPTIONS. This is the declarative counterpart of 'gexp->derivation'." - (%computed-file name gexp guile options)) + (let ((options* (computed-file-combine-options-with-defaults options))) + (%computed-file name gexp guile options*))) (define-gexp-compiler (computed-file-compiler (file ) system target) diff --git a/tests/gexp.scm b/tests/gexp.scm index 1beeb67c21..350065b58d 100644 --- a/tests/gexp.scm +++ b/tests/gexp.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès +;;; Copyright © 2020 Maxim Cournoyer ;;; ;;; This file is part of GNU Guix. ;;; @@ -62,6 +63,9 @@ #:target target) #:guile-for-build (%guile-for-build))) +(define computed-file-combine-options-with-defaults + (@@ (guix gexp) computed-file-combine-options-with-defaults)) + (define %extension-package ;; Example of a package to use when testing 'with-extensions'. (dummy-package "extension" @@ -1367,6 +1371,14 @@ (return (and (derivation? drv1) (derivation? drv2) (store-path? item))))) +(test-equal "computed-file options defaults honored" + '(#:substitutable? #t #:local-build? #t) + (computed-file-combine-options-with-defaults '(#:substitutable? #t))) + +(test-equal "computed-file options defaults overridden" + '(#:local-build? #f) + (computed-file-combine-options-with-defaults '(#:local-build? #f))) + (test-assertm "lower-object, computed-file" (let* ((text (plain-file "foo" "Hello!")) (exp #~(begin