Dpkg_Substvars.t 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/perl
  2. #
  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.
  7. #
  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.
  12. #
  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/>.
  15. use strict;
  16. use warnings;
  17. use Test::More tests => 37;
  18. use Test::Dpkg qw(:paths);
  19. use Dpkg ();
  20. use Dpkg::Arch qw(get_host_arch);
  21. use_ok('Dpkg::Substvars');
  22. my $datadir = test_get_data_path('t/Dpkg_Substvars');
  23. my $expected;
  24. my $s = Dpkg::Substvars->new();
  25. $s->load("$datadir/substvars1");
  26. # simple value tests
  27. is($s->get('var1'), 'Some value', 'var1');
  28. is($s->get('var2'), 'Some other value', 'var2');
  29. is($s->get('var3'), 'Yet another value', 'var3');
  30. is($s->get('var4'), undef, 'no var4');
  31. # Set automatic variable
  32. $s->set_as_auto('var_auto', 'auto');
  33. is($s->get('var_auto'), 'auto', 'get var_auto');
  34. $expected = <<'VARS';
  35. var1=Some value
  36. var2=Some other value
  37. var3=Yet another value
  38. VARS
  39. is($s->output(), $expected, 'No automatic variables output');
  40. # overriding
  41. $s->set('var1', 'New value');
  42. is($s->get('var1'), 'New value', 'var1 updated');
  43. # deleting
  44. $s->delete('var3');
  45. is($s->get('var3'), undef, 'var3 deleted');
  46. # default variables
  47. is($s->get('Newline'), "\n", 'newline');
  48. is($s->get('Space'), ' ', 'space');
  49. is($s->get('Tab'), "\t", 'tab');
  50. is($s->get('dpkg:Version'), $Dpkg::PROGVERSION, 'dpkg version 1');
  51. # special variables
  52. is($s->get('Arch'), undef, 'no arch');
  53. $s->set_arch_substvars();
  54. is($s->get('Arch'), get_host_arch(), 'arch');
  55. is($s->get($_), undef, 'no ' . $_) for qw/binary:Version source:Version source:Upstream-Version/;
  56. $s->set_version_substvars('1:2.3.4~5-6.7.8~nmu9', '1:2.3.4~5-6.7.8~nmu9+bin0');
  57. is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+bin0', 'binary:Version');
  58. is($s->get('source:Version'), '1:2.3.4~5-6.7.8~nmu9', 'source:Version');
  59. is($s->get('source:Upstream-Version'), '1:2.3.4~5', 'source:Upstream-Version');
  60. $s->set_version_substvars('2.3.4~5-6.7.8~nmu9+b1', '1:2.3.4~5-6.7.8~nmu9+b1');
  61. is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+b1', 'binary:Version');
  62. is($s->get('source:Version'), '2.3.4~5-6.7.8~nmu9', 'source:Version');
  63. is($s->get('source:Upstream-Version'), '2.3.4~5', 'source:Upstream-Version');
  64. $s->set_version_substvars('1:2.3.4~5-6.7.8~nmu9+b0');
  65. is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+b0', 'binary:Version');
  66. is($s->get('source:Version'), '1:2.3.4~5-6.7.8~nmu9', 'source:Version');
  67. is($s->get('source:Upstream-Version'), '1:2.3.4~5', 'source:Upstream-Version');
  68. # Replace stuff
  69. is($s->substvars('This is a string ${var1} with variables ${binary:Version}'),
  70. 'This is a string New value with variables 1:2.3.4~5-6.7.8~nmu9+b0',
  71. 'substvars simple');
  72. # Add a test prefix to error and warning messages.
  73. $s->set_msg_prefix('test ');
  74. my $output;
  75. $SIG{__WARN__} = sub { $output .= $_[0] };
  76. is($s->substvars('This is a string with unknown variable ${blubb}'),
  77. 'This is a string with unknown variable ',
  78. 'substvars missing');
  79. delete $SIG{__WARN__};
  80. is($output,
  81. 'Dpkg_Substvars.t: warning: test unknown substitution variable ${blubb}' . "\n",
  82. 'missing variables warning');
  83. # Recursive replace
  84. $s->set('rvar', 'recursive ${var1}');
  85. is($s->substvars('This is a string with ${rvar}'),
  86. 'This is a string with recursive New value',
  87. 'substvars recursive');
  88. # Strange input
  89. is($s->substvars('Nothing to $ ${substitute here}, is it ${}?, it ${is'),
  90. 'Nothing to $ ${substitute here}, is it ${}?, it ${is',
  91. 'substvars strange');
  92. # Warnings about unused variables
  93. $output = '';
  94. $SIG{__WARN__} = sub { $output .= $_[0] };
  95. $s->warn_about_unused();
  96. delete $SIG{__WARN__};
  97. is($output,
  98. 'Dpkg_Substvars.t: warning: test unused substitution variable ${var2}' . "\n",
  99. 'unused variables warnings');
  100. # Disable warnings for a certain variable
  101. $s->set_as_used('var_used', 'used');
  102. $s->mark_as_used('var2');
  103. $output = '';
  104. $SIG{__WARN__} = sub { $output .= $_[0] };
  105. $s->warn_about_unused();
  106. delete $SIG{__WARN__};
  107. is($output, '', 'disabled unused variables warnings');
  108. $s->delete('var_used');
  109. # Variable filters
  110. my $sf;
  111. $expected = <<'VARS';
  112. name3=Yet another value
  113. name4=Name value
  114. otherprefix:var7=Quux
  115. var1=Some value
  116. var2=Some other value
  117. VARS
  118. $sf = Dpkg::Substvars->new("$datadir/substvars2");
  119. $sf->filter(remove => sub { $_[0] =~ m/^prefix:/ });
  120. is($sf->output(), $expected, 'Filter remove variables');
  121. $expected = <<'VARS';
  122. otherprefix:var7=Quux
  123. prefix:var5=Foo
  124. var1=Some value
  125. var2=Some other value
  126. VARS
  127. $sf = Dpkg::Substvars->new("$datadir/substvars2");
  128. $sf->filter(keep => sub { $_[0] =~ m/var/ });
  129. is($sf->output(), $expected, 'Filter keep variables');
  130. $expected = <<'VARS';
  131. prefix:name6=Bar
  132. VARS
  133. $sf = Dpkg::Substvars->new("$datadir/substvars2");
  134. $sf->filter(remove => sub { $_[0] =~ m/var/ },
  135. keep => sub { $_[0] =~ m/^prefix:/ });
  136. is($sf->output(), $expected, 'Filter keep and remove variables');