3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 use Test::More tests => 37;
20 use Test::Dpkg qw(:paths);
23 use Dpkg::Arch qw(get_host_arch);
25 use_ok('Dpkg::Substvars');
27 my $datadir = test_get_data_path('t/Dpkg_Substvars');
31 my $s = Dpkg::Substvars->new();
33 $s->load("$datadir/substvars1");
36 is($s->get('var1'), 'Some value', 'var1');
37 is($s->get('var2'), 'Some other value', 'var2');
38 is($s->get('var3'), 'Yet another value', 'var3');
39 is($s->get('var4'), undef, 'no var4');
41 # Set automatic variable
42 $s->set_as_auto('var_auto', 'auto');
43 is($s->get('var_auto'), 'auto', 'get var_auto');
48 var3=Yet another value
50 is($s->output(), $expected, 'No automatic variables output');
53 $s->set('var1', 'New value');
54 is($s->get('var1'), 'New value', 'var1 updated');
58 is($s->get('var3'), undef, 'var3 deleted');
61 is($s->get('Newline'), "\n", 'newline');
62 is($s->get('Space'), ' ', 'space');
63 is($s->get('Tab'), "\t", 'tab');
64 is($s->get('dpkg:Version'), $Dpkg::PROGVERSION, 'dpkg version 1');
67 is($s->get('Arch'), undef, 'no arch');
68 $s->set_arch_substvars();
69 is($s->get('Arch'), get_host_arch(), 'arch');
71 is($s->get($_), undef, 'no ' . $_) for qw/binary:Version source:Version source:Upstream-Version/;
72 $s->set_version_substvars('1:2.3.4~5-6.7.8~nmu9', '1:2.3.4~5-6.7.8~nmu9+bin0');
73 is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+bin0', 'binary:Version');
74 is($s->get('source:Version'), '1:2.3.4~5-6.7.8~nmu9', 'source:Version');
75 is($s->get('source:Upstream-Version'), '1:2.3.4~5', 'source:Upstream-Version');
76 $s->set_version_substvars('2.3.4~5-6.7.8~nmu9+b1', '1:2.3.4~5-6.7.8~nmu9+b1');
77 is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+b1', 'binary:Version');
78 is($s->get('source:Version'), '2.3.4~5-6.7.8~nmu9', 'source:Version');
79 is($s->get('source:Upstream-Version'), '2.3.4~5', 'source:Upstream-Version');
80 $s->set_version_substvars('1:2.3.4~5-6.7.8~nmu9+b0');
81 is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+b0', 'binary:Version');
82 is($s->get('source:Version'), '1:2.3.4~5-6.7.8~nmu9', 'source:Version');
83 is($s->get('source:Upstream-Version'), '1:2.3.4~5', 'source:Upstream-Version');
86 is($s->substvars('This is a string ${var1} with variables ${binary:Version}'),
87 'This is a string New value with variables 1:2.3.4~5-6.7.8~nmu9+b0',
90 # Add a test prefix to error and warning messages.
91 $s->set_msg_prefix('test ');
94 $SIG{__WARN__} = sub { $output .= $_[0] };
95 is($s->substvars('This is a string with unknown variable ${blubb}'),
96 'This is a string with unknown variable ',
98 delete $SIG{__WARN__};
100 'Dpkg_Substvars.t: warning: test unknown substitution variable ${blubb}' . "\n",
101 'missing variables warning');
104 $s->set('rvar', 'recursive ${var1}');
105 is($s->substvars('This is a string with ${rvar}'),
106 'This is a string with recursive New value',
107 'substvars recursive');
110 is($s->substvars('Nothing to $ ${substitute here}, is it ${}?, it ${is'),
111 'Nothing to $ ${substitute here}, is it ${}?, it ${is',
112 'substvars strange');
114 # Warnings about unused variables
116 $SIG{__WARN__} = sub { $output .= $_[0] };
117 $s->warn_about_unused();
118 delete $SIG{__WARN__};
120 'Dpkg_Substvars.t: warning: test unused substitution variable ${var2}' . "\n",
121 'unused variables warnings');
123 # Disable warnings for a certain variable
124 $s->set_as_used('var_used', 'used');
125 $s->mark_as_used('var2');
127 $SIG{__WARN__} = sub { $output .= $_[0] };
128 $s->warn_about_unused();
129 delete $SIG{__WARN__};
130 is($output, '', 'disabled unused variables warnings');
132 $s->delete('var_used');
137 $expected = <<'VARS';
138 name3=Yet another value
140 otherprefix:var7=Quux
142 var2=Some other value
144 $sf = Dpkg::Substvars->new("$datadir/substvars2");
145 $sf->filter(remove => sub { $_[0] =~ m/^prefix:/ });
146 is($sf->output(), $expected, 'Filter remove variables');
148 $expected = <<'VARS';
149 otherprefix:var7=Quux
152 var2=Some other value
154 $sf = Dpkg::Substvars->new("$datadir/substvars2");
155 $sf->filter(keep => sub { $_[0] =~ m/var/ });
156 is($sf->output(), $expected, 'Filter keep variables');
158 $expected = <<'VARS';
161 $sf = Dpkg::Substvars->new("$datadir/substvars2");
162 $sf->filter(remove => sub { $_[0] =~ m/var/ },
163 keep => sub { $_[0] =~ m/^prefix:/ });
164 is($sf->output(), $expected, 'Filter keep and remove variables');