Dpkg_Conf.t 3.5 KB

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