Dpkg_Conf.t 3.2 KB

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