From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id gBhfDeySomLTowAAbAwnHQ (envelope-from ) for ; Fri, 10 Jun 2022 02:40:12 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id 8JP7DOySomJCWwEAauVa8A (envelope-from ) for ; Fri, 10 Jun 2022 02:40:12 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 9A310DFA4 for ; Fri, 10 Jun 2022 02:40:11 +0200 (CEST) Received: from localhost ([::1]:51304 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nzSh8-0003aX-Li for larch@yhetil.org; Thu, 09 Jun 2022 20:40:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57428) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nzSh1-0003aH-1i for guix-patches@gnu.org; Thu, 09 Jun 2022 20:40:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:54357) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nzSh0-0003Im-P7 for guix-patches@gnu.org; Thu, 09 Jun 2022 20:40:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nzSh0-0003M6-Mo for guix-patches@gnu.org; Thu, 09 Jun 2022 20:40:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#55845] [PATCH 1/1] ui: Improve pager selection logic when less is not installed. Resent-From: Taiju HIGASHI Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 10 Jun 2022 00:40:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 55845 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Maxime Devos Cc: 55845@debbugs.gnu.org, me@tobias.gr Received: via spool by 55845-submit@debbugs.gnu.org id=B55845.165482159312875 (code B ref 55845); Fri, 10 Jun 2022 00:40:02 +0000 Received: (at 55845) by debbugs.gnu.org; 10 Jun 2022 00:39:53 +0000 Received: from localhost ([127.0.0.1]:48254 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nzSgn-0003LX-B5 for submit@debbugs.gnu.org; Thu, 09 Jun 2022 20:39:53 -0400 Received: from mail-pj1-f52.google.com ([209.85.216.52]:41910) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nzSgX-0003L6-H9 for 55845@debbugs.gnu.org; Thu, 09 Jun 2022 20:39:48 -0400 Received: by mail-pj1-f52.google.com with SMTP id l20-20020a17090a409400b001dd2a9d555bso821909pjg.0 for <55845@debbugs.gnu.org>; Thu, 09 Jun 2022 17:39:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=taiju-info.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=ta7+VgfdWAaTYKB0Ky3ekonCxrs93fYzXv4aKxiShUk=; b=Rlca3I7VyGvyQcSwSYyTtFfuVEtXrGn0L+Ryw16bJ2K1rkpll9Sa8Y9+iXQTBlzPZQ m+x5QcyCqbSwirzl+I531XtOtNh6A00t//oMiSbCBxK/WaCnGPA4hV9U6Z0+F6nhTmGy zoKyhubwxT0FOognEjyJuTNulRcIld58G8L3g6+Twk6HEr00V2NxlwS9Kn52nuLUQxKI e96Gn0cBVWS36+hncILxgNiWc56UKAUqe+ho5gNO7axNyEESpa9ZNI95+OcihfuA37Wn fb7uZGTF73xGOLKgrBirOs9PXAaJML/N71eEbUk4gcMFMwzjAK5prnDi+a+j8lbPMgaZ WoKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=ta7+VgfdWAaTYKB0Ky3ekonCxrs93fYzXv4aKxiShUk=; b=0ZZpeibTGq4nKb0DdAM1BdGvp68IDyFCM5lcUTHUZHgQYTkK3QGJaD/U1enUSRzNIP lOwkvblxJej+tpMt5kIjTaPXH5Is5YNDO1a1BWoj3eGvGadYKWbX9cCOVCoFvgNgD1ZL ctXcofpvjTG58LoO7+ccjwXXnzsdFSa+Yd756os3U3kKgDIjt2dyLAJFRWBfaZOU1HtB i6gEc59d1QsB4bJfrD/sYdsJvJcehhRiE85VthVi52s/Q6TFdG2C4HYGJaNUlG8GOaaT vbnALFnZUlLaCERzJ5bi3Bx7UfgkOG2Cv3oDP3TOHqUrU6M0VdXuS+NrXB4hCLrunVsU EQoA== X-Gm-Message-State: AOAM533igEjh5iDFeTCg9SyBN0lCxBNb6dtM0Yy9f7A4fAnQJ9MmwoL0 L3Hw2z0ReCQWVXtNvPcbvCrWx+67xEFmJQay X-Google-Smtp-Source: ABdhPJx+JeX89WFmdggVd+nrRnj4MvZQgW7lHscWcshAxW3f9f3zcCViKSeHm+LQdZC6/CjMLpGBBQ== X-Received: by 2002:a17:90a:6602:b0:1e3:17e:6290 with SMTP id l2-20020a17090a660200b001e3017e6290mr6115708pjj.37.1654821567366; Thu, 09 Jun 2022 17:39:27 -0700 (PDT) Received: from Taix ([240b:253:ec40:2400:b7d1:436e:2d61:e925]) by smtp.gmail.com with ESMTPSA id bt19-20020a056a00439300b0050dc76281f8sm7360513pfb.210.2022.06.09.17.39.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jun 2022 17:39:26 -0700 (PDT) From: Taiju HIGASHI References: <20220608102124.14865-1-higashi@taiju.info> <20220608102257.15042-1-higashi@taiju.info> <87leu72sbo.fsf@taiju.info> <55fe6cdbed891707aca419ff4eedd7c37ef3eb03.camel@telenet.be> <87fskexiyc.fsf@taiju.info> <874k0uxhif.fsf@taiju.info> <5ccb68a4dc80a02baaf6d19fd782145b6b62e7b5.camel@telenet.be> Date: Fri, 10 Jun 2022 09:39:24 +0900 In-Reply-To: <5ccb68a4dc80a02baaf6d19fd782145b6b62e7b5.camel@telenet.be> (Maxime Devos's message of "Thu, 09 Jun 2022 21:43:11 +0200") Message-ID: <87a6alwdvn.fsf@taiju.info> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" 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+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1654821611; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:resent-cc:resent-from:resent-sender: resent-message-id:in-reply-to:in-reply-to:references:references: list-id:list-help:list-unsubscribe:list-subscribe:list-post: dkim-signature; bh=ta7+VgfdWAaTYKB0Ky3ekonCxrs93fYzXv4aKxiShUk=; b=ZOlwyv9bWeU0+6jSOXotns9MxjcIfdQNtZdGB1ojcvJumto2qYWB2ilHdi0Gs9jRXDZ0z8 uHxrnYpu7Nc09vUGVzMvSsyDRbo4AprPLemGBNRz+CXIFEzlyfjphtqtZYtB6fV2ymmuqI TqXY1WW/9XeVtcMraj1PwW6js6lHrctYvbTzybWXz/CL56vp/U7MpgbQKMnF2pZ9wXuQLa K2NFZFloXGteTWR39OIFHIJyGQCwvFMn0puUH22My0E4TwzYWQsZamhmyN+cztGPOutoj1 0qrwVjW4Q75PFDzVau+KqKKTMIJiGRbkP96227udCOqIsYjJk46kSoggZn0vWw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1654821611; a=rsa-sha256; cv=none; b=AEQVCJkYKh0dfdHxWpYC1cMKU9Dtehva0dxKgpGo9OIIWvr50IidyZHLr02pvvgfRB89hG nzvo8oEoar0jtXPawxoMS85QBWW8p5+QkcT+pfMdOfAcI4QGKHw+jAKqeUNVC0OyjwlRzs reyS5c1Ek6UNXrMJY4buaPmgrD0/18loha+5vmxCUgwUIG0byijIjemyWVxWQ99YCjbJxk zBq7PMpJKUtnBSCwkT41bTrsxYvH/yT+Hx2I3S/h/qHxBgVjVAmer2NUU/Hgu27Aj8Qe2j yC5EHMkowJ5yetXdoCIpP8Mhf2iEHumrPhybiftkP1BHoAEANNG6cLIvebjBYw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=taiju-info.20210112.gappssmtp.com header.s=20210112 header.b=Rlca3I7V; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: 0.91 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=taiju-info.20210112.gappssmtp.com header.s=20210112 header.b=Rlca3I7V; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 9A310DFA4 X-Spam-Score: 0.91 X-Migadu-Scanner: scn0.migadu.com X-TUID: lQTNfp7duBOe --=-=-= Content-Type: text/plain Maxime Devos writes: > Taiju HIGASHI schreef op do 09-06-2022 om 19:23 [+0900]: >> Hi Maxime, >> >> I tried to mock open-pipe* and isatty?* using the mock macro and also >> add a test to inspect the program coming across to open-pipe*, but gave >> up because I could not get the return value of the >> with-paginated-output-port macro. > > The return value of 'with-paginated-output-port' is just whatever the > last expression put in that macro evaluates to. Also 'close-pipe' > needs to be mocked, otherwise an error will result. > > Try: > > (test-assert "with-paginated-output-port: finds less in PATH" > (call-with-temporary-directory > (lambda (dir) > (define used-command #false) > (with-environment-variables > `(("PATH" ,dir)) > (make-dummy-executable-file dir "less") > (mock ((ice-9 popen) open-pipe* > (lambda (mode command . args) > (when used-command ; <--- an extra test > (error "open-pipe* should only be called once")) > (set! used-command command) ; <--- this captures the passed command > (%make-void-port ""))) ; return a dummy port > (mock ((ice-9 popen) close-pipe (const 'ok)) > (mock ((guix colors) isatty?* (const #t)) > (with-paginated-output-port port 'ok))))) > (and (pk 'used-command used-command dir) ; <-- fails on my computer because a non-absolute path is passed and I haven't applied our patch > (string=? (in-vicinity dir "less") used-command))))) Thank you very much! It worked as expected! I made a v3 patch. --=-=-= Content-Type: text/x-patch; charset=iso-8859-1 Content-Disposition: attachment; filename=v3-0001-ui-Improve-pager-selection-logic-when-less-is-not.patch Content-Transfer-Encoding: quoted-printable Content-Description: v3 patch >From b499be5cf73916005150ddf777ae705070f077d1 Mon Sep 17 00:00:00 2001 From: Taiju HIGASHI Date: Wed, 8 Jun 2022 18:50:28 +0900 Subject: [PATCH v3] ui: Improve pager selection logic when less is not installed. * guix/ui.scm (find-available-pager): New procedure. Return a available pag= er. (call-with-paginated-output-port): Change to use find-available-pager to select pager. * tests/ui.scm: Add tests for find-available-pager. --- guix/ui.scm | 16 +++++++-- tests/ui.scm | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/guix/ui.scm b/guix/ui.scm index cb68a07c6c..93707a7a4b 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -17,6 +17,7 @@ ;;; Copyright =A9 2020 Arun Isaac ;;; Copyright =A9 2020 Maxim Cournoyer ;;; Copyright =A9 2018 Steve Sprang +;;; Copyright =A9 2022 Taiju HIGASHI ;;; ;;; This file is part of GNU Guix. ;;; @@ -1672,11 +1673,20 @@ (define* (pager-wrapped-port #:optional (port (curr= ent-output-port))) (_ #f))) =20 +(define (find-available-pager) + "Returns the program name or path of an available pager. +If neither less nor more is installed, return an empty string so that +call-with-paginated-output-port will not call pager." + (or (getenv "GUIX_PAGER") + (getenv "PAGER") + (which "less") + (which "more") + "" ;; Returns an empty string so that call-with-paginated-output-por= t does not call pager. + )) + (define* (call-with-paginated-output-port proc #:key (less-options "FrX")) - (let ((pager-command-line (or (getenv "GUIX_PAGER") - (getenv "PAGER") - "less"))) + (let ((pager-command-line (find-available-pager))) ;; Setting PAGER to the empty string conventionally disables paging. (if (and (not (string-null? pager-command-line)) (isatty?* (current-output-port))) diff --git a/tests/ui.scm b/tests/ui.scm index 3dc6952e1f..ca01d8f03d 100644 --- a/tests/ui.scm +++ b/tests/ui.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright =A9 2013, 2014, 2015, 2016, 2017, 2019, 2020 Ludovic Court= =E8s +;;; Copyright =A9 2022 Taiju HIGASHI ;;; ;;; This file is part of GNU Guix. ;;; @@ -24,6 +25,7 @@ (define-module (test-ui) #:use-module (guix derivations) #:use-module ((gnu packages) #:select (specification->package)) #:use-module (guix tests) + #:use-module (guix utils) #:use-module (srfi srfi-1) #:use-module (srfi srfi-11) #:use-module (srfi srfi-19) @@ -292,4 +294,98 @@ (define guile-2.0.9 (>0 (package-relevance libb2 (map rx '("crypto" "library"))))))) =20 +(define make-dummy-file + (compose + close-port + open-output-file + (cut in-vicinity <> <>))) + +(test-assert "find-available-pager, All environment variables are specifie= d and both less and more are installed" + (call-with-temporary-directory + (lambda (dir) + (with-environment-variables + `(("PATH" ,dir) + ("GUIX_PAGER" "guix-pager") + ("PAGER" "pager")) + (make-dummy-file dir "less") + (make-dummy-file dir "more") + (string=3D? ((@@ (guix ui) find-available-pager)) "guix-pager"))))) + +(test-assert "find-available-pager, GUIX_PAGER is not specified" + (call-with-temporary-directory + (lambda (dir) + (with-environment-variables + `(("PATH" ,dir) + ("PAGER" "pager")) + (make-dummy-file dir "less") + (make-dummy-file dir "more") + (string=3D? ((@@ (guix ui) find-available-pager)) "pager"))))) + +(test-assert "find-available-pager, All environment variables are not spec= ified and both less and more are installed" + (call-with-temporary-directory + (lambda (dir) + (with-environment-variables + `(("PATH" ,dir)) + (make-dummy-file dir "less") + (make-dummy-file dir "more") + (string=3D? ((@@ (guix ui) find-available-pager)) + (in-vicinity dir "less")))))) + +(test-assert "find-available-pager, All environment variables are not spec= ified and more is installed" + (call-with-temporary-directory + (lambda (dir) + (with-environment-variables + `(("PATH" ,dir)) + (make-dummy-file dir "more") + (string=3D? ((@@ (guix ui) find-available-pager)) + (in-vicinity dir "more")))))) + +(test-assert "find-available-pager, All environment variables are not spec= ified and both less and more are not installed" + (call-with-temporary-directory + (lambda (dir) + (with-environment-variables + `(("PATH" ,dir)) + (string=3D? ((@@ (guix ui) find-available-pager)) ""))))) + + +(test-assert "with-paginated-output-port: finds a pager in enviroment vari= ables" + (call-with-temporary-directory + (lambda (dir) + (define used-command #false) + (with-environment-variables + `(("PATH" ,dir) + ("GUIX_PAGER" "guix-pager") + ("PAGER" "pager")) + (make-dummy-file dir "less") + (make-dummy-file dir "more") + (mock ((ice-9 popen) open-pipe* + (lambda (mode command . args) + (when used-command + (error "open-pipe* should only be called once")) + (set! used-command command) + (%make-void-port ""))) + (mock ((ice-9 popen) close-pipe (const 'ok)) + (mock ((guix colors) isatty?* (const #t)) + (with-paginated-output-port port 'ok))))) + (string=3D? "guix-pager" used-command)))) + +(test-assert "with-paginated-output-port: finds less in PATH" + (call-with-temporary-directory + (lambda (dir) + (define used-command #false) + (with-environment-variables + `(("PATH" ,dir)) + (make-dummy-file dir "less") + (make-dummy-file dir "more") + (mock ((ice-9 popen) open-pipe* + (lambda (mode command . args) + (when used-command + (error "open-pipe* should only be called once")) + (set! used-command command) + (%make-void-port ""))) + (mock ((ice-9 popen) close-pipe (const 'ok)) + (mock ((guix colors) isatty?* (const #t)) + (with-paginated-output-port port 'ok))))) + (string=3D? (in-vicinity dir "less") used-command)))) + (test-end "ui") --=20 2.36.1 --=-=-= Content-Type: text/plain Two tests have been added: one to select pager from the environment variable and the other to select less from the PATH. I also made some improvements to the existing tests based on your answers. There are many ways to do this. I learned a lot. Thanks, -- Taiju --=-=-=--