750_Dpkg_Substvars.t 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. use Test::More tests => 32;
  15. use strict;
  16. use warnings;
  17. use Dpkg qw($version);
  18. use Dpkg::Arch qw(get_host_arch);
  19. use_ok('Dpkg::Substvars');
  20. my $srcdir = $ENV{srcdir} || '.';
  21. my $datadir = $srcdir . '/t/750_Dpkg_Substvars';
  22. my $s = Dpkg::Substvars->new();
  23. $s->load("$datadir/substvars1");
  24. # simple value tests
  25. is($s->get('var1'), 'Some value', 'var1');
  26. is($s->get('var2'), 'Some other value', 'var2');
  27. is($s->get('var3'), 'Yet another value', 'var3');
  28. is($s->get('var4'), undef, 'no var4');
  29. # overriding
  30. $s->set('var1', 'New value');
  31. is($s->get('var1'), 'New value','var1 updated');
  32. # deleting
  33. $s->delete('var3');
  34. is($s->get('var3'), undef, 'var3 deleted');
  35. # default variables
  36. is($s->get('Newline'), "\n", 'newline');
  37. is($s->get('Space'), " ", 'space');
  38. is($s->get('Tab'), "\t", 'tab');
  39. is($s->get('dpkg:Version'), $version, 'dpkg version 1');
  40. # special variables
  41. is($s->get('Arch'), undef, 'no arch');
  42. $s->set_arch_substvars();
  43. is($s->get('Arch'), get_host_arch(),'arch');
  44. is($s->get($_), undef, 'no ' . $_) for qw/binary:Version source:Version source:Upstream-Version/;
  45. $s->set_version_substvars("1:2.3.4~5-6.7.8~nmu9", "1:2.3.4~5-6.7.8~nmu9+bin0");
  46. is($s->get("binary:Version"), "1:2.3.4~5-6.7.8~nmu9+bin0", "binary:Version");
  47. is($s->get("source:Version"), "1:2.3.4~5-6.7.8~nmu9", "source:Version");
  48. is($s->get("source:Upstream-Version"), "1:2.3.4~5", "source:Upstream-Version");
  49. $s->set_version_substvars("2.3.4~5-6.7.8~nmu9+b1", "1:2.3.4~5-6.7.8~nmu9+b1");
  50. is($s->get("binary:Version"), "1:2.3.4~5-6.7.8~nmu9+b1", "binary:Version");
  51. is($s->get("source:Version"), "2.3.4~5-6.7.8~nmu9", "source:Version");
  52. is($s->get("source:Upstream-Version"), "2.3.4~5", "source:Upstream-Version");
  53. $s->set_version_substvars("1:2.3.4~5-6.7.8~nmu9+b0");
  54. is($s->get("binary:Version"), "1:2.3.4~5-6.7.8~nmu9+b0", "binary:Version");
  55. is($s->get("source:Version"), "1:2.3.4~5-6.7.8~nmu9", "source:Version");
  56. is($s->get("source:Upstream-Version"), "1:2.3.4~5", "source:Upstream-Version");
  57. # Replace stuff
  58. is($s->substvars('This is a string ${var1} with variables ${binary:Version}'),
  59. "This is a string New value with variables 1:2.3.4~5-6.7.8~nmu9+b0",
  60. "substvars simple");
  61. my $output;
  62. $SIG{'__WARN__'} = sub { $output .= $_[0] };
  63. is($s->substvars('This is a string with unknown variable ${blubb}'),
  64. "This is a string with unknown variable ",
  65. "substvars missing");
  66. delete $SIG{'__WARN__'};
  67. is($output, '750_Dpkg_Substvars.t: warning: unknown substitution variable ${blubb}'."\n"
  68. , 'missing variables warning');
  69. # Recursive replace
  70. $s->set("rvar", 'recursive ${var1}');
  71. is($s->substvars('This is a string with ${rvar}'),
  72. "This is a string with recursive New value",
  73. "substvars recursive");
  74. # Strange input
  75. is($s->substvars('Nothing to $ ${substitute here}, is it ${}?, it ${is'),
  76. 'Nothing to $ ${substitute here}, is it ${}?, it ${is',
  77. "substvars strange");
  78. # Warnings about unused variables
  79. $output = '';
  80. $SIG{'__WARN__'} = sub { $output .= $_[0] };
  81. $s->warn_about_unused();
  82. delete $SIG{'__WARN__'};
  83. is($output, "750_Dpkg_Substvars.t: warning: unused substitution variable \${var2}\n",
  84. , 'unused variables warnings');
  85. # Disable warnings for a certain variable
  86. $s->mark_as_used('var2');
  87. $output = '';
  88. $SIG{'__WARN__'} = sub { $output .= $_[0] };
  89. $s->warn_about_unused();
  90. delete $SIG{'__WARN__'};
  91. is($output, '', 'disabled unused variables warnings');