From 4514f4cf198e76bf4ca6df002ea3b38c516be5ce Mon Sep 17 00:00:00 2001 From: Herman Rimm Date: Sun, 5 May 2024 08:26:42 +0200 Subject: [PATCH] scripts: lint: Add 'whole-file' option with ordering checker. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * guix/scripts/lint.scm (show-help): Describe option. (%options): Add 'whole-file' option. (guix-lint): Run checkers on packages defined in files. * doc/guix.texi (Invoking guix lint): Document option. * tests/guix-lint.sh: Define unordered package and invoke new option. Change-Id: I52b48a9a6982d0c4a03416e3d070887c64716485 Signed-off-by: Ludovic Courtès Merges: #8796 --- doc/guix.texi | 26 +++++++++++++++++--- guix/scripts/lint.scm | 55 ++++++++++++++++++++++++++++++++++++++----- tests/guix-lint.sh | 20 ++++++++++++++-- 3 files changed, 90 insertions(+), 11 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 209becd722a..30c91d0df9d 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -124,7 +124,7 @@ Copyright @copyright{} 2023 Thomas Ieong@* Copyright @copyright{} 2023 Saku Laesvuori@* Copyright @copyright{} 2023 Graham James Addis@* Copyright @copyright{} 2023-2025 Tomas Volf@* -Copyright @copyright{} 2024, 2025 Herman Rimm@* +Copyright @copyright{} 2024-2026 Herman Rimm@* Copyright @copyright{} 2024 Matthew Trzcinski@* Copyright @copyright{} 2024 Richard Sent@* Copyright @copyright{} 2024 Dariqq@* @@ -16710,6 +16710,12 @@ guix lint @var{options} @var{package}@dots{} @end example If no package is given on the command line, then all packages are checked. +To check packages in particular source files, the syntax is: + +@example +guix lint @var{options} --whole-file @var{file}@dots{} +@end example + The @var{options} may be zero or more of the following: @table @code @@ -16747,9 +16753,23 @@ Only enable the checkers that do not depend on Internet access. Add @var{directory} to the front of the package module search path (@pxref{Package Modules}). -This allows users to define their own packages and make them visible to -the command-line tools. +@item --whole-file +@itemx -f +Check the top-level package definitions in the given files; subsequent +arguments are treated as file names rather than package names. This +option also enables a special checker which checks if a package +alphabetically succeeds the one above it. +For example, to check if the packages in @file{gnu/packages/matrix.scm} +are sorted alphabetically, and if the package names therein follow +established conventions, run: + +@example +guix lint -c name -f gnu/packages/matrix.scm +@end example + +The previous two options allow users to define their own packages and +make them visible to the command-line tools. @end table @node Invoking guix size diff --git a/guix/scripts/lint.scm b/guix/scripts/lint.scm index ee3de51fb1a..6a317bf64d5 100644 --- a/guix/scripts/lint.scm +++ b/guix/scripts/lint.scm @@ -11,6 +11,7 @@ ;;; Copyright © 2018, 2019 Arun Isaac ;;; Copyright © 2019, 2020 Simon Tournier ;;; Copyright © 2020 Brice Waegeneire +;;; Copyright © 2024, 2026 Herman Rimm ;;; ;;; This file is part of GNU Guix. ;;; @@ -28,8 +29,10 @@ ;;; along with GNU Guix. If not, see . (define-module (guix scripts lint) + #:use-module (guix diagnostics) #:use-module (guix packages) #:use-module (guix lint) + #:use-module (guix modules) #:use-module (guix ui) #:use-module (guix store) #:use-module (guix scripts) @@ -87,6 +90,28 @@ checkers) (exit 0)) +(define* (process-whole-file file checkers #:key store) + "Run the given CHECKERS on packages in FILE and check that the +packages are sorted alphabetically." + (load* file '()) + (let* ((module (resolve-interface (file-name->module-name file))) + (packages (sort (fold-packages cons '() (list module)) + package-locationpackage spec)) - (('expression . exp) - (read/eval-package-expression exp)) - (_ #f)) + (whole-file? (assoc-ref opts 'whole-file?)) + (args (filter-map (if whole-file? + (match-lambda + (('argument . file) file) + (_ #f)) + (match-lambda + (('argument . spec) + (specification->package spec)) + (('expression . exp) + (read/eval-package-expression exp)) + (_ #f))) (reverse opts))) (no-checkers (or (assoc-ref opts 'exclude) '())) (the-checkers (filter (lambda (checker) @@ -221,6 +257,13 @@ run the checkers on all packages.\n")) (call-maybe-with-store (lambda (store) (cond + (whole-file? + (when (null? args) + (warning (G_ "no files specified, nothing to do~%"))) + (for-each + (lambda (file) + (process-whole-file file checkers #:store store)) + args)) ((null? args) (fold-packages (lambda (p r) (run-checkers p checkers #:store store)) '())) diff --git a/tests/guix-lint.sh b/tests/guix-lint.sh index 97c2ea83fef..4a77315b3a2 100644 --- a/tests/guix-lint.sh +++ b/tests/guix-lint.sh @@ -1,5 +1,6 @@ # GNU Guix --- Functional package management for GNU # Copyright © 2014 Cyril Roelandt +# Copyright © 2026 Herman Rimm # # This file is part of GNU Guix. # @@ -35,12 +36,20 @@ cat > "$module_dir/foo.scm"<&1` test `grep_warning "$out"` -eq 3 @@ -69,6 +79,12 @@ test `grep_warning "$out"` -eq 1 out=`guix lint -c description,synopsis dummy 2>&1` test `grep_warning "$out"` -eq 3 +working_dir="$(pwd)" +cd "$module_dir" +out=`guix lint -c name -f foo.scm 2>&1` +cd "$working_dir" +test `echo "$out" | grep -E -c "breaks from alphabetical order"` -eq 1 + guix lint -c synopsis,invalid-checker dummy 2>&1 | \ grep -q 'invalid-checker: invalid checker'