dpkg_buildpackage.t 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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;
  18. use Test::Dpkg qw(:needs test_neutralize_checksums);
  19. use File::Spec::Functions qw(rel2abs);
  20. use File::Compare;
  21. use File::Path qw(make_path);
  22. use File::Copy;
  23. use Dpkg::IPC;
  24. use Dpkg::Build::Types;
  25. use Dpkg::Substvars;
  26. test_needs_command('fakeroot');
  27. plan tests => 12;
  28. my $srcdir = rel2abs($ENV{srcdir} || '.');
  29. my $datadir = "$srcdir/t/dpkg_buildpackage";
  30. my $tmpdir = 't.tmp/dpkg_buildpackage';
  31. $ENV{$_} = rel2abs($ENV{$_}) foreach qw(DPKG_DATADIR DPKG_ORIGINS_DIR);
  32. # Any paralellization from the parent should be ignored, we are testing
  33. # the makefiles serially anyway.
  34. delete $ENV{MAKEFLAGS};
  35. # Delete variables that can affect the tests.
  36. delete $ENV{SOURCE_DATE_EPOCH};
  37. # Delete other variables that can affect the tests.
  38. delete $ENV{$_} foreach grep { m/^DEB_/ } keys %ENV;
  39. make_path($tmpdir);
  40. chdir $tmpdir;
  41. my $tmpl_format = <<'TMPL_FORMAT';
  42. 3.0 (native)
  43. TMPL_FORMAT
  44. my $tmpl_changelog = <<'TMPL_CHANGELOG';
  45. ${source-name} (${source-version}) ${suite}; urgency=${urgency}
  46. * Entry. Closes: #12345
  47. -- ${maintainer} Thu, 30 Jun 2016 20:15:12 +0200
  48. TMPL_CHANGELOG
  49. my $tmpl_control = <<'TMPL_CONTROL';
  50. Source: ${source-name}
  51. Section: ${source-section}
  52. Priority: ${source-priority}
  53. Maintainer: ${maintainer}
  54. Package: test-binary-all
  55. Architecture: all
  56. Description: architecture independent binary package
  57. Package: test-binary-any
  58. Architecture: any
  59. Description: architecture dependent binary package
  60. TMPL_CONTROL
  61. my $tmpl_rules = <<'TMPL_RULES';
  62. #!/usr/bin/make -f
  63. DI := debian/${binary-name-all}
  64. DA := debian/${binary-name-any}
  65. clean:
  66. rm -f debian/files
  67. rm -rf $(DI) $(DA)
  68. build-indep:
  69. build-arch:
  70. build: build-indep build-arch
  71. binary-indep: build-indep
  72. rm -rf $(DI)
  73. mkdir -p $(DI)/DEBIAN
  74. dpkg-gencontrol -P$(DI) -p${binary-name-all}
  75. dpkg-deb --build $(DI) ..
  76. binary-arch: build-arch
  77. rm -rf $(DA)
  78. mkdir -p $(DA)/DEBIAN
  79. dpkg-gencontrol -P$(DA) -p${binary-name-any}
  80. dpkg-deb --build $(DA) ..
  81. binary: binary-indep binary-arch
  82. .PHONY: clean build-indep build-arch build binary-indexp binary-arch binary
  83. TMPL_RULES
  84. my %default_substvars = (
  85. 'source-name' => 'test-source',
  86. 'source-version' => 0,
  87. 'source-section' => 'test',
  88. 'source-priority' => 'optional',
  89. 'binary-name-all' => 'test-binary-all',
  90. 'binary-name-any' => 'test-binary-any',
  91. 'suite' => 'unstable',
  92. 'urgency' => 'low',
  93. 'maintainer' => 'Dpkg Developers <debian-dpkg@lists.debian.org>',
  94. );
  95. sub gen_from_tmpl
  96. {
  97. my ($pathname, $tmpl, $substvars) = @_;
  98. open my $fh, '>', $pathname or die;
  99. print { $fh } $substvars->substvars($tmpl);
  100. close $fh or die;
  101. }
  102. sub gen_source
  103. {
  104. my (%options) = @_;
  105. my $substvars = Dpkg::Substvars->new();
  106. foreach my $var (%default_substvars) {
  107. my $value = $options{$var} // $default_substvars{$var};
  108. $substvars->set_as_auto($var, $value);
  109. }
  110. my $source = $substvars->get('source-name');
  111. my $version = $substvars->get('source-version');
  112. my $basename = "$source\_$version";
  113. my $dirname = $basename =~ tr/_/-/r;
  114. make_path("$dirname/debian/source");
  115. gen_from_tmpl("$dirname/debian/source/format", $tmpl_format, $substvars);
  116. gen_from_tmpl("$dirname/debian/changelog", $tmpl_changelog, $substvars);
  117. gen_from_tmpl("$dirname/debian/control", $tmpl_control, $substvars);
  118. gen_from_tmpl("$dirname/debian/rules", $tmpl_rules, $substvars);
  119. return $basename;
  120. }
  121. sub test_diff
  122. {
  123. my $filename = shift;
  124. my $expected_file = "$datadir/$filename";
  125. my $generated_file = $filename;
  126. test_neutralize_checksums($generated_file);
  127. my $res = compare($expected_file, $generated_file);
  128. if ($res) {
  129. system "diff -u $expected_file $generated_file >&2";
  130. }
  131. ok($res == 0, "generated file matches expected one ($expected_file)");
  132. }
  133. sub test_build
  134. {
  135. my ($basename, $type) = @_;
  136. my $dirname = $basename =~ tr/_/-/r;
  137. set_build_type($type, 'buildtype', nocheck => 1);
  138. my $typename = get_build_options_from_type();
  139. my $stderr;
  140. chdir $dirname;
  141. spawn(exec => [ "$srcdir/dpkg-buildpackage.pl", '--host-arch=amd64',
  142. '--unsigned-source', '--unsigned-changes',
  143. "--build=$typename", '--check-command=' ],
  144. error_to_string => \$stderr,
  145. wait_child => 1, nocheck => 1);
  146. chdir '..';
  147. ok($? == 0, "dpkg-buildpackage --build=$typename succeeded");
  148. diag($stderr) unless $? == 0;
  149. if (build_has_all(BUILD_ARCH_DEP)) {
  150. # Rename the file to preserve on consecutive invokations.
  151. move("$basename\_amd64.changes", "$basename\_$typename.changes");
  152. }
  153. if (build_has_all(BUILD_SOURCE)) {
  154. test_diff("$basename.dsc");
  155. }
  156. test_diff("$basename\_$typename.changes");
  157. }
  158. my $basename = gen_source();
  159. test_build($basename, BUILD_SOURCE);
  160. test_build($basename, BUILD_ARCH_INDEP);
  161. test_build($basename, BUILD_ARCH_DEP);
  162. test_build($basename, BUILD_BINARY);
  163. test_build($basename, BUILD_FULL);
  164. 1;