Dpkg_Substvars.t 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 => 35;
  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 $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. # overriding
  32. $s->set('var1', 'New value');
  33. is($s->get('var1'), 'New value', 'var1 updated');
  34. # deleting
  35. $s->delete('var3');
  36. is($s->get('var3'), undef, 'var3 deleted');
  37. # default variables
  38. is($s->get('Newline'), "\n", 'newline');
  39. is($s->get('Space'), ' ', 'space');
  40. is($s->get('Tab'), "\t", 'tab');
  41. is($s->get('dpkg:Version'), $Dpkg::PROGVERSION, 'dpkg version 1');
  42. # special variables
  43. is($s->get('Arch'), undef, 'no arch');
  44. $s->set_arch_substvars();
  45. is($s->get('Arch'), get_host_arch(), 'arch');
  46. is($s->get($_), undef, 'no ' . $_) for qw/binary:Version source:Version source:Upstream-Version/;
  47. $s->set_version_substvars('1:2.3.4~5-6.7.8~nmu9', '1:2.3.4~5-6.7.8~nmu9+bin0');
  48. is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+bin0', 'binary:Version');
  49. is($s->get('source:Version'), '1:2.3.4~5-6.7.8~nmu9', 'source:Version');
  50. is($s->get('source:Upstream-Version'), '1:2.3.4~5', 'source:Upstream-Version');
  51. $s->set_version_substvars('2.3.4~5-6.7.8~nmu9+b1', '1:2.3.4~5-6.7.8~nmu9+b1');
  52. is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+b1', 'binary:Version');
  53. is($s->get('source:Version'), '2.3.4~5-6.7.8~nmu9', 'source:Version');
  54. is($s->get('source:Upstream-Version'), '2.3.4~5', 'source:Upstream-Version');
  55. $s->set_version_substvars('1:2.3.4~5-6.7.8~nmu9+b0');
  56. is($s->get('binary:Version'), '1:2.3.4~5-6.7.8~nmu9+b0', 'binary:Version');
  57. is($s->get('source:Version'), '1:2.3.4~5-6.7.8~nmu9', 'source:Version');
  58. is($s->get('source:Upstream-Version'), '1:2.3.4~5', 'source:Upstream-Version');
  59. # Replace stuff
  60. is($s->substvars('This is a string ${var1} with variables ${binary:Version}'),
  61. 'This is a string New value with variables 1:2.3.4~5-6.7.8~nmu9+b0',
  62. 'substvars simple');
  63. my $output;
  64. $SIG{__WARN__} = sub { $output .= $_[0] };
  65. is($s->substvars('This is a string with unknown variable ${blubb}'),
  66. 'This is a string with unknown variable ',
  67. 'substvars missing');
  68. delete $SIG{__WARN__};
  69. is($output, 'Dpkg_Substvars.t: warning: unknown substitution variable ${blubb}'."\n"
  70. , 'missing variables warning');
  71. # Recursive replace
  72. $s->set('rvar', 'recursive ${var1}');
  73. is($s->substvars('This is a string with ${rvar}'),
  74. 'This is a string with recursive New value',
  75. 'substvars recursive');
  76. # Strange input
  77. is($s->substvars('Nothing to $ ${substitute here}, is it ${}?, it ${is'),
  78. 'Nothing to $ ${substitute here}, is it ${}?, it ${is',
  79. 'substvars strange');
  80. # Warnings about unused variables
  81. $output = '';
  82. $SIG{__WARN__} = sub { $output .= $_[0] };
  83. $s->warn_about_unused();
  84. delete $SIG{__WARN__};
  85. is($output, "Dpkg_Substvars.t: warning: unused substitution variable \${var2}\n",
  86. , 'unused variables warnings');
  87. # Disable warnings for a certain variable
  88. $s->mark_as_used('var2');
  89. $output = '';
  90. $SIG{__WARN__} = sub { $output .= $_[0] };
  91. $s->warn_about_unused();
  92. delete $SIG{__WARN__};
  93. is($output, '', 'disabled unused variables warnings');
  94. # Variable filters
  95. my $sf;
  96. $expected = <<'VARS';
  97. name3=Yet another value
  98. name4=Name value
  99. otherprefix:var7=Quux
  100. var1=Some value
  101. var2=Some other value
  102. VARS
  103. $sf = Dpkg::Substvars->new("$datadir/substvars2");
  104. $sf->filter(remove => sub { $_[0] =~ m/^prefix:/ });
  105. is($sf->output(), $expected, 'Filter remove variables');
  106. $expected = <<'VARS';
  107. otherprefix:var7=Quux
  108. prefix:var5=Foo
  109. var1=Some value
  110. var2=Some other value
  111. VARS
  112. $sf = Dpkg::Substvars->new("$datadir/substvars2");
  113. $sf->filter(keep => sub { $_[0] =~ m/var/ });
  114. is($sf->output(), $expected, 'Filter keep variables');
  115. $expected = <<'VARS';
  116. prefix:name6=Bar
  117. VARS
  118. $sf = Dpkg::Substvars->new("$datadir/substvars2");
  119. $sf->filter(remove => sub { $_[0] =~ m/var/ },
  120. keep => sub { $_[0] =~ m/^prefix:/ });
  121. is($sf->output(), $expected, 'Filter keep and remove variables');