750_Dpkg_Substvars.t 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- mode: cperl;-*-
  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 <http://www.gnu.org/licenses/>.
  15. use Test::More tests => 26;
  16. use strict;
  17. use warnings;
  18. use Dpkg qw($version);
  19. use Dpkg::Arch qw(get_host_arch);
  20. use_ok('Dpkg::Substvars');
  21. my $srcdir = $ENV{srcdir} || '.';
  22. my $datadir = $srcdir . '/t/750_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'), $version, '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+b0");
  47. is($s->get("binary:Version"), "1:2.3.4~5-6.7.8~nmu9+b0", "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. # Replace stuff
  51. is($s->substvars('This is a string ${var1} with variables ${binary:Version}'),
  52. "This is a string New value with variables 1:2.3.4~5-6.7.8~nmu9+b0",
  53. "substvars simple");
  54. my $output;
  55. $SIG{'__WARN__'} = sub { $output .= $_[0] };
  56. is($s->substvars('This is a string with unknown variable ${blubb}'),
  57. "This is a string with unknown variable ",
  58. "substvars missing");
  59. delete $SIG{'__WARN__'};
  60. is($output, '750_Dpkg_Substvars.t: warning: unknown substitution variable ${blubb}'."\n"
  61. , 'missing variables warning');
  62. # Recursive replace
  63. $s->set("rvar", 'recursive ${var1}');
  64. is($s->substvars('This is a string with ${rvar}'),
  65. "This is a string with recursive New value",
  66. "substvars recursive");
  67. # Strange input
  68. is($s->substvars('Nothing to $ ${substitute here}, is it ${}?, it ${is'),
  69. 'Nothing to $ ${substitute here}, is it ${}?, it ${is',
  70. "substvars strange");
  71. # Warnings about unused variables
  72. $output = '';
  73. $SIG{'__WARN__'} = sub { $output .= $_[0] };
  74. $s->warn_about_unused();
  75. delete $SIG{'__WARN__'};
  76. is($output, "750_Dpkg_Substvars.t: warning: unused substitution variable \${var2}\n",
  77. , 'unused variables warnings');
  78. # Disable warnings for a certain variable
  79. $s->no_warn('var2');
  80. $output = '';
  81. $SIG{'__WARN__'} = sub { $output .= $_[0] };
  82. $s->warn_about_unused();
  83. delete $SIG{'__WARN__'};
  84. is($output, '', 'disabled unused variables warnings');