Dpkg_Substvars.t 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 => 32;
  18. use Dpkg ();
  19. use Dpkg::Arch qw(get_host_arch);
  20. use_ok('Dpkg::Substvars');
  21. my $srcdir = $ENV{srcdir} || '.';
  22. my $datadir = $srcdir . '/t/Dpkg_Substvars';
  23. my $s = Dpkg::Substvars->new();
  24. $s->load("$datadir/substvars1");
  25. # simple value tests
  26. is($s->get('var1'), 'Some value', 'var1');
  27. is($s->get('var2'), 'Some other value', 'var2');
  28. is($s->get('var3'), 'Yet another value', 'var3');
  29. is($s->get('var4'), undef, 'no var4');
  30. # overriding
  31. $s->set('var1', 'New value');
  32. is($s->get('var1'), 'New value', 'var1 updated');
  33. # deleting
  34. $s->delete('var3');
  35. is($s->get('var3'), undef, 'var3 deleted');
  36. # default variables
  37. is($s->get('Newline'), "\n", 'newline');
  38. is($s->get('Space'), ' ', 'space');
  39. is($s->get('Tab'), "\t", 'tab');
  40. is($s->get('dpkg:Version'), $Dpkg::PROGVERSION, 'dpkg version 1');
  41. # special variables
  42. is($s->get('Arch'), undef, 'no arch');
  43. $s->set_arch_substvars();
  44. is($s->get('Arch'), get_host_arch(), 'arch');
  45. is($s->get($_), undef, 'no ' . $_) for qw/binary:Version source:Version source:Upstream-Version/;
  46. $s->set_version_substvars('1:2.3.4~5-6.7.8~nmu9', '1:2.3.4~5-6.7.8~nmu9+bin0');
  47. is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+bin0', 'binary:Version');
  48. is($s->get('source:Version'), '1:2.3.4~5-6.7.8~nmu9', 'source:Version');
  49. is($s->get('source:Upstream-Version'), '1:2.3.4~5', 'source:Upstream-Version');
  50. $s->set_version_substvars('2.3.4~5-6.7.8~nmu9+b1', '1:2.3.4~5-6.7.8~nmu9+b1');
  51. is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+b1', 'binary:Version');
  52. is($s->get('source:Version'), '2.3.4~5-6.7.8~nmu9', 'source:Version');
  53. is($s->get('source:Upstream-Version'), '2.3.4~5', 'source:Upstream-Version');
  54. $s->set_version_substvars('1:2.3.4~5-6.7.8~nmu9+b0');
  55. is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+b0', 'binary:Version');
  56. is($s->get('source:Version'), '1:2.3.4~5-6.7.8~nmu9', 'source:Version');
  57. is($s->get('source:Upstream-Version'), '1:2.3.4~5', 'source:Upstream-Version');
  58. # Replace stuff
  59. is($s->substvars('This is a string ${var1} with variables ${binary:Version}'),
  60. 'This is a string New value with variables 1:2.3.4~5-6.7.8~nmu9+b0',
  61. 'substvars simple');
  62. my $output;
  63. $SIG{__WARN__} = sub { $output .= $_[0] };
  64. is($s->substvars('This is a string with unknown variable ${blubb}'),
  65. 'This is a string with unknown variable ',
  66. 'substvars missing');
  67. delete $SIG{__WARN__};
  68. is($output, 'Dpkg_Substvars.t: warning: unknown substitution variable ${blubb}'."\n"
  69. , 'missing variables warning');
  70. # Recursive replace
  71. $s->set('rvar', 'recursive ${var1}');
  72. is($s->substvars('This is a string with ${rvar}'),
  73. 'This is a string with recursive New value',
  74. 'substvars recursive');
  75. # Strange input
  76. is($s->substvars('Nothing to $ ${substitute here}, is it ${}?, it ${is'),
  77. 'Nothing to $ ${substitute here}, is it ${}?, it ${is',
  78. 'substvars strange');
  79. # Warnings about unused variables
  80. $output = '';
  81. $SIG{__WARN__} = sub { $output .= $_[0] };
  82. $s->warn_about_unused();
  83. delete $SIG{__WARN__};
  84. is($output, "Dpkg_Substvars.t: warning: unused substitution variable \${var2}\n",
  85. , 'unused variables warnings');
  86. # Disable warnings for a certain variable
  87. $s->mark_as_used('var2');
  88. $output = '';
  89. $SIG{__WARN__} = sub { $output .= $_[0] };
  90. $s->warn_about_unused();
  91. delete $SIG{__WARN__};
  92. is($output, '', 'disabled unused variables warnings');