This is an automated email from the git hooks/post-receive script. guillem pushed a commit to branch main in repository dpkg.
View the commit online: https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=00f0fb17355717a8ed80134fed66eb9c264915b9 commit 00f0fb17355717a8ed80134fed66eb9c264915b9 Author: Guillem Jover <[email protected]> AuthorDate: Fri Feb 23 00:14:25 2024 +0100 Dpkg::Substvars: Add support for required substvars assigned with != Some packaging helpers (namely debhelper) set various substvars that must always be used by the packaging. Marking those as required means the maintainer will never forget to use them. We add a new operator «!=» to make it possible for other tools to mark those variables as required. Track those internally too so that they can be printed back properly. --- man/deb-substvars.pod | 13 ++++++++----- scripts/Dpkg/Substvars.pm | 34 +++++++++++++++++++++++++++------- scripts/Makefile.am | 1 + scripts/t/Dpkg_Substvars.t | 28 +++++++++++++++++++++++++++- scripts/t/Dpkg_Substvars/substvars-req | 1 + 5 files changed, 64 insertions(+), 13 deletions(-) diff --git a/man/deb-substvars.pod b/man/deb-substvars.pod index c6fca2975..3a0cc2826 100644 --- a/man/deb-substvars.pod +++ b/man/deb-substvars.pod @@ -54,11 +54,14 @@ is rescanned to look for more substitutions. =head2 File Syntax Substitution variables can be specified in a file. -These files consist of lines of the form I<name>B<=>I<value> or -I<name>B<?=>I<value>. -The B<=> operator assigns a normal substitution variable, while the B<?=> -operator (since dpkg 1.21.8) assigns an optional substitution variable -which will emit no warnings even if unused. +These files consist of lines of the form I<name>B<=>I<value>, +I<name>B<?=>I<value>, or +I<name>B<!=>I<value>. +The B<=> operator assigns a normal substitution variable, +the B<?=> operator (since dpkg 1.21.8) assigns an optional substitution +variable which will emit no warnings even if unused, +and the B<!=> operator (since dpkg 1.22.7) assigns a required substitution +variable which will error out if unused. Trailing whitespace on each line, blank lines, and lines starting with a B<#> symbol (comments) are ignored. diff --git a/scripts/Dpkg/Substvars.pm b/scripts/Dpkg/Substvars.pm index cf551949e..7150c3095 100644 --- a/scripts/Dpkg/Substvars.pm +++ b/scripts/Dpkg/Substvars.pm @@ -1,4 +1,4 @@ -# Copyright © 2006-2009, 2012-2020, 2022 Guillem Jover <[email protected]> +# Copyright © 2006-2024 Guillem Jover <[email protected]> # Copyright © 2007-2010 Raphaël Hertzog <[email protected]> # # This program is free software; you can redistribute it and/or modify @@ -26,7 +26,7 @@ It provides a class which is able to substitute variables in strings. =cut -package Dpkg::Substvars 2.01; +package Dpkg::Substvars 2.02; use strict; use warnings; @@ -48,6 +48,7 @@ use constant { SUBSTVAR_ATTR_AGED => 4, SUBSTVAR_ATTR_OPT => 8, SUBSTVAR_ATTR_DEEP => 16, + SUBSTVAR_ATTR_REQ => 32, }; =head1 METHODS @@ -190,13 +191,14 @@ sub parse { next if m/^\s*\#/ || !m/\S/; s/\s*\n$//; - if (! m/^(\w[-:0-9A-Za-z]*)(\?)?\=(.*)$/) { + if (! m/^(\w[-:0-9A-Za-z]*)([?!])?\=(.*)$/) { error(g_('bad line in substvars file %s at line %d'), $varlistfile, $.); } ## no critic (RegularExpressions::ProhibitCaptureWithoutTest) if (defined $2) { $attr = (SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_OPT) if $2 eq '?'; + $attr = (SUBSTVAR_ATTR_REQ) if $2 eq '!'; } $self->set($1, $3, $attr); $count++; @@ -378,9 +380,16 @@ sub warn_about_unused { # that they are not required in the current situation # (example: debhelper's misc:Depends in many cases) next if $self->{vars}{$vn} eq ''; - warning($opts{msg_prefix} . - g_('substitution variable ${%s} unused, but is defined'), - $vn); + + if ($self->{attr}{$vn} & SUBSTVAR_ATTR_REQ) { + error($opts{msg_prefix} . + g_('required substitution variable ${%s} not used'), + $vn); + } else { + warning($opts{msg_prefix} . + g_('substitution variable ${%s} unused, but is defined'), + $vn); + } } } @@ -434,7 +443,14 @@ sub output { # Store all non-automatic substitutions only foreach my $vn (sort keys %{$self->{vars}}) { next if $self->{attr}{$vn} & SUBSTVAR_ATTR_AUTO; - my $op = $self->{attr}{$vn} & SUBSTVAR_ATTR_OPT ? '?=' : '='; + my $op; + if ($self->{attr}{$vn} & SUBSTVAR_ATTR_OPT) { + $op = '?='; + } elsif ($self->{attr}{$vn} & SUBSTVAR_ATTR_REQ) { + $op = '!='; + } else { + $op = '='; + } my $line = "$vn$op" . $self->{vars}{$vn} . "\n"; print { $fh } $line if defined $fh; $str .= $line; @@ -451,6 +467,10 @@ indicated file. =head1 CHANGES +=head2 Version 2.02 (dpkg 1.22.7) + +New feature: Add support for required substitution variables. + =head2 Version 2.01 (dpkg 1.21.8) New feature: Add support for optional substitution variables. diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 8bbcab329..005c11587 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -369,6 +369,7 @@ test_data = \ t/Dpkg_Source_Package/package_1.0.orig.tar \ t/Dpkg_Source_Package/package_1.0.orig.tar.asc \ t/Dpkg_Source_Package/package_1.0.orig.tar.sig \ + t/Dpkg_Substvars/substvars-req \ t/Dpkg_Substvars/substvars1 \ t/Dpkg_Substvars/substvars2 \ t/dpkg_buildpackage/dpkgdb/status \ diff --git a/scripts/t/Dpkg_Substvars.t b/scripts/t/Dpkg_Substvars.t index 61ac0276b..b1c5b1b9a 100644 --- a/scripts/t/Dpkg_Substvars.t +++ b/scripts/t/Dpkg_Substvars.t @@ -16,7 +16,7 @@ use strict; use warnings; -use Test::More tests => 56; +use Test::More tests => 60; use Test::Dpkg qw(:paths); use Dpkg (); @@ -247,6 +247,32 @@ is($output, '', 'disabled unused variables warnings'); $s->delete('var_used'); +# Required variables +my $sr; + +$expected = <<'VARS'; +required-var!=Required value +VARS +$sr = Dpkg::Substvars->new("$datadir/substvars-req"); +is($sr->output(), $expected, 'Required variable preserved'); + +is($sr->substvars('This is a string with missing the required variable'), + 'This is a string with missing the required variable', + 'substvars required substitution missing'); + +eval { + $sr->warn_about_unused(); + 1; +}; +$output = $@ // q{}; +is($output, + 'Dpkg_Substvars.t: error: required substitution variable ${required-var} not used' . "\n", + 'substvars required substitution not used'); + +is($sr->substvars('This is a string with a required variable ${required-var}'), + 'This is a string with a required variable Required value', + 'substvars required substitution present'); + # Variable filters my $sf; diff --git a/scripts/t/Dpkg_Substvars/substvars-req b/scripts/t/Dpkg_Substvars/substvars-req new file mode 100644 index 000000000..442a27fd8 --- /dev/null +++ b/scripts/t/Dpkg_Substvars/substvars-req @@ -0,0 +1 @@ +required-var!=Required value -- Dpkg.Org's dpkg

