Dpkg_Conf.t 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 => 9;
  18. BEGIN {
  19. use_ok('Dpkg::Conf');
  20. }
  21. my $srcdir = $ENV{srcdir} // '.';
  22. my $datadir = $srcdir . '/t/Dpkg_Conf';
  23. my ($conf, $count, @opts);
  24. my @expected_long_opts = (
  25. '--option-double-quotes=value double quotes',
  26. '--option-single-quotes=value single quotes',
  27. '--option-space=value words space',
  28. qw(
  29. --option-name=value-name
  30. --option-indent=value-indent
  31. --option-equal=value-equal=subvalue-equal
  32. --option-noequal=value-noequal
  33. --option-simple
  34. --option-dash=value-dash
  35. --l=v
  36. ));
  37. my @expected_short_opts = qw(
  38. -o=vd
  39. -s
  40. );
  41. $conf = Dpkg::Conf->new();
  42. local $SIG{__WARN__} = sub { };
  43. $count = $conf->load("$datadir/config-mixed");
  44. delete $SIG{__WARN__};
  45. is($count, 10, 'Load a config file, only long options');
  46. @opts = $conf->get_options();
  47. is_deeply(\@opts, \@expected_long_opts, 'Parse long options');
  48. $conf = Dpkg::Conf->new(allow_short => 1);
  49. $count = $conf->load("$datadir/config-mixed");
  50. is($count, 12, 'Load a config file, mixed options');
  51. @opts = $conf->get_options();
  52. my @expected_mixed_opts = ( @expected_long_opts, @expected_short_opts );
  53. is_deeply(\@opts, \@expected_mixed_opts, 'Parse mixed options');
  54. my $expected_mixed_output = <<'MIXED';
  55. option-double-quotes = "value double quotes"
  56. option-single-quotes = "value single quotes"
  57. option-space = "value words space"
  58. option-name = "value-name"
  59. option-indent = "value-indent"
  60. option-equal = "value-equal=subvalue-equal"
  61. option-noequal = "value-noequal"
  62. option-simple
  63. option-dash = "value-dash"
  64. l = "v"
  65. -o = "vd"
  66. -s
  67. MIXED
  68. is($conf->output, $expected_mixed_output, 'Output mixed options');
  69. my $expected_filter;
  70. $expected_filter = <<'FILTER';
  71. l = "v"
  72. -o = "vd"
  73. -s
  74. FILTER
  75. $conf = Dpkg::Conf->new(allow_short => 1);
  76. $conf->load("$datadir/config-mixed");
  77. $conf->filter(remove => sub { $_[0] =~ m/^--option/ });
  78. is($conf->output, $expected_filter, 'Filter remove');
  79. $expected_filter = <<'FILTER';
  80. option-double-quotes = "value double quotes"
  81. option-single-quotes = "value single quotes"
  82. FILTER
  83. $conf = Dpkg::Conf->new(allow_short => 1);
  84. $conf->load("$datadir/config-mixed");
  85. $conf->filter(keep => sub { $_[0] =~ m/^--option-[a-z]+-quotes/ });
  86. is($conf->output, $expected_filter, 'Filter keep');
  87. $expected_filter = <<'FILTER';
  88. l = "v"
  89. FILTER
  90. $conf = Dpkg::Conf->new(allow_short => 1);
  91. $conf->load("$datadir/config-mixed");
  92. $conf->filter(remove => sub { $_[0] =~ m/^--option/ },
  93. keep => sub { $_[0] =~ m/^--/ });
  94. is($conf->output, $expected_filter, 'Filter keep and remove');
  95. 1;