mirror of
https://git.savannah.gnu.org/git/guix.git
synced 2026-06-14 00:04:06 +02:00
scripts: lint: Add 'whole-file' option with ordering checker.
* 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 <ludo@gnu.org> Merges: #8796
This commit is contained in:
committed by
Ludovic Courtès
parent
1d3d136bb0
commit
4514f4cf19
+23
-3
@@ -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
|
||||
|
||||
+49
-6
@@ -11,6 +11,7 @@
|
||||
;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net>
|
||||
;;; Copyright © 2019, 2020 Simon Tournier <zimon.toutoune@gmail.com>
|
||||
;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
|
||||
;;; Copyright © 2024, 2026 Herman Rimm <herman@rimm.ee>
|
||||
;;;
|
||||
;;; This file is part of GNU Guix.
|
||||
;;;
|
||||
@@ -28,8 +29,10 @@
|
||||
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
(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-location<?)))
|
||||
(fold (lambda (package previous)
|
||||
(let ((line (location-line (package-location package)))
|
||||
(name (package-name package)))
|
||||
(run-checkers package checkers #:store store)
|
||||
(and (string<? name previous)
|
||||
(emit-warnings
|
||||
(list (lint-warning
|
||||
(package package)
|
||||
(location (location file line 0))
|
||||
(message-text
|
||||
(G_ "breaks from alphabetical order"))
|
||||
(message-data '())))))
|
||||
name))
|
||||
"" packages)))
|
||||
|
||||
|
||||
;;;
|
||||
;;; Command-line options.
|
||||
@@ -115,6 +140,9 @@ run the checkers on all packages.\n"))
|
||||
-L, --load-path=DIR prepend DIR to the package module search path"))
|
||||
(newline)
|
||||
(display (G_ "
|
||||
-f, --whole-file check the packages defined in the given file(s)"))
|
||||
(newline)
|
||||
(display (G_ "
|
||||
-h, --help display this help and exit"))
|
||||
(display (G_ "
|
||||
-l, --list-checkers display the list of available lint checkers"))
|
||||
@@ -161,6 +189,9 @@ run the checkers on all packages.\n"))
|
||||
(lambda args
|
||||
(leave-on-EPIPE (show-help))
|
||||
(exit 0)))
|
||||
(option '(#\f "whole-file") #f #f
|
||||
(lambda (opt name arg result)
|
||||
(alist-cons 'whole-file? #t result)))
|
||||
(option '(#\l "list-checkers") #f #f
|
||||
(lambda (opt name arg result)
|
||||
(alist-cons 'list? #t result)))
|
||||
@@ -187,12 +218,17 @@ run the checkers on all packages.\n"))
|
||||
#:build-options? #f))
|
||||
|
||||
(let* ((opts (parse-options))
|
||||
(args (filter-map (match-lambda
|
||||
(('argument . spec)
|
||||
(specification->package 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)) '()))
|
||||
|
||||
+18
-2
@@ -1,5 +1,6 @@
|
||||
# GNU Guix --- Functional package management for GNU
|
||||
# Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com>
|
||||
# Copyright © 2026 Herman Rimm <herman@rimm.ee>
|
||||
#
|
||||
# This file is part of GNU Guix.
|
||||
#
|
||||
@@ -35,12 +36,20 @@ cat > "$module_dir/foo.scm"<<EOF
|
||||
#:use-module (guix packages)
|
||||
#:use-module (gnu packages base))
|
||||
|
||||
;; This definition uses (the line number of) the hello package location
|
||||
;; instead of generating a new one.
|
||||
(define-public hi hello)
|
||||
|
||||
(define-public dummy
|
||||
(package (inherit hello)
|
||||
(name "dummy")
|
||||
(version "42")
|
||||
(synopsis "dummy package")
|
||||
(description "dummy package. Only used for testing purposes.")))
|
||||
|
||||
(define-public bar
|
||||
(package (inherit dummy)
|
||||
(name "bar")))
|
||||
EOF
|
||||
|
||||
GUIX_PACKAGE_PATH="$module_dir"
|
||||
@@ -52,10 +61,11 @@ grep_warning ()
|
||||
echo $res
|
||||
}
|
||||
|
||||
# Three issues with the dummy package:
|
||||
# Issues with the dummy package:
|
||||
# 1) the synopsis starts with the package name;
|
||||
# 2) the synopsis starts with a lower-case letter;
|
||||
# 3) the description has a single space following the end-of-sentence period.
|
||||
# 3) the description has a single space following the end-of-sentence period;
|
||||
# 4) the alphabetically lesser bar package succeeds it.
|
||||
|
||||
out=`guix lint -c synopsis,description dummy 2>&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'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user