dpkg_buildpackage.t 5.5 KB

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