dpkg_source.t 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 => 10;
  18. use Test::Dpkg qw(test_neutralize_checksums);
  19. use File::Spec::Functions qw(rel2abs);
  20. use File::Compare;
  21. use File::Path qw(make_path);
  22. use Dpkg::IPC;
  23. use Dpkg::Substvars;
  24. my $srcdir = rel2abs($ENV{srcdir} || '.');
  25. my $datadir = "$srcdir/t/dpkg_source";
  26. my $tmpdir = 't.tmp/dpkg_source';
  27. $ENV{$_} = rel2abs($ENV{$_}) foreach qw(DPKG_DATADIR DPKG_ORIGINS_DIR);
  28. # Delete variables that can affect the tests.
  29. delete $ENV{SOURCE_DATE_EPOCH};
  30. make_path($tmpdir);
  31. chdir $tmpdir;
  32. my $tmpl_format = <<'TMPL_FORMAT';
  33. 3.0 (native)
  34. TMPL_FORMAT
  35. my $tmpl_changelog = <<'TMPL_CHANGELOG';
  36. ${source-name} (${source-version}) ${suite}; urgency=${urgency}
  37. * Test package.
  38. -- ${maintainer} Sat, 05 Jul 2014 21:11:22 +0200
  39. TMPL_CHANGELOG
  40. my $tmpl_control = <<'TMPL_CONTROL';
  41. Source: ${source-name}
  42. Section: ${source-section}
  43. Priority: ${source-priority}
  44. Maintainer: ${maintainer}
  45. Standards-Version: 1.0
  46. Testsuite: ${source-testsuite}
  47. Package: test-binary
  48. Architecture: all
  49. Description: test package
  50. TMPL_CONTROL
  51. my $tmpl_tests_control = <<'TMPL_TESTS_CONTROL';
  52. Test-Command: ${test-command}
  53. Depends: ${test-depends}
  54. Restrictions: ${test-restrictions}
  55. TMPL_TESTS_CONTROL
  56. my %default_substvars = (
  57. 'source-name' => 'test-source',
  58. 'source-version' => 0,
  59. 'source-section' => 'test',
  60. 'source-priority' => 'optional',
  61. 'source-testsuite' => 'autopkgtest',
  62. 'test-command' => 'true',
  63. 'test-depends' => '@',
  64. 'suite' => 'unstable',
  65. 'urgency' => 'low',
  66. 'maintainer' => 'Dpkg Developers <debian-dpkg@lists.debian.org>',
  67. );
  68. sub gen_from_tmpl
  69. {
  70. my ($pathname, $tmpl, $substvars) = @_;
  71. open my $fh, '>', $pathname or die;
  72. print { $fh } $substvars->substvars($tmpl);
  73. close $fh or die;
  74. }
  75. sub gen_source
  76. {
  77. my (%options) = @_;
  78. my $substvars = Dpkg::Substvars->new();
  79. foreach my $var ((keys %default_substvars, keys %options)) {
  80. my $value = $options{$var} // $default_substvars{$var};
  81. $substvars->set_as_auto($var, $value);
  82. }
  83. my $source = $substvars->get('source-name');
  84. my $version = $substvars->get('source-version');
  85. my $dirname = "$source-$version";
  86. make_path("$dirname/debian/source");
  87. gen_from_tmpl("$dirname/debian/source/format", $tmpl_format, $substvars);
  88. gen_from_tmpl("$dirname/debian/changelog", $tmpl_changelog, $substvars);
  89. gen_from_tmpl("$dirname/debian/control", $tmpl_control, $substvars);
  90. if (defined $options{'control-test'}) {
  91. make_path("$dirname/debian/tests");
  92. gen_from_tmpl("$dirname/debian/tests/control", $options{'control-test'}, $substvars);
  93. }
  94. return $dirname;
  95. }
  96. sub test_diff
  97. {
  98. my $filename = shift;
  99. my $expected_file = "$datadir/$filename";
  100. my $generated_file = $filename;
  101. test_neutralize_checksums($generated_file);
  102. my $res = compare($expected_file, $generated_file);
  103. if ($res) {
  104. system "diff -u $expected_file $generated_file >&2";
  105. }
  106. ok($res == 0, "generated file matches expected one ($expected_file)");
  107. }
  108. sub test_build_source
  109. {
  110. my ($name) = shift;
  111. my $stderr;
  112. spawn(exec => [ "$srcdir/dpkg-source.pl", '--build', $name ],
  113. error_to_string => \$stderr,
  114. wait_child => 1, nocheck => 1);
  115. ok($? == 0, 'dpkg-source --build succeeded');
  116. diag($stderr) unless $? == 0;
  117. my $basename = $name =~ tr/-/_/r;
  118. test_diff("$basename.dsc");
  119. }
  120. my $dirname;
  121. $dirname = gen_source('source-name' => 'testsuite',
  122. 'source-version' => 0,
  123. 'control-test' => '');
  124. test_build_source($dirname);
  125. $dirname = gen_source('source-name' => 'testsuite',
  126. 'source-version' => 1,
  127. 'control-test' => '');
  128. test_build_source($dirname);
  129. $dirname = gen_source('source-name' => 'testsuite',
  130. 'source-version' => 2,
  131. 'source-testsuite' => 'smokepkgtest, unitpkgtest, funcpkgtest',
  132. 'control-test' => '');
  133. test_build_source($dirname);
  134. $dirname = gen_source('source-name' => 'testsuite',
  135. 'source-version' => 3);
  136. test_build_source($dirname);
  137. $dirname = gen_source('source-name' => 'testsuite',
  138. 'source-version' => 4,
  139. 'test-restrictions' => 'needs-root,build-needed allow-stderr',
  140. 'control-test' => $tmpl_tests_control);
  141. test_build_source($dirname);
  142. 1;