]> git.donarmstrong.com Git - debhelper.git/blob - t/buildsystems/buildsystem_tests
Use another root directory in _rel2rel.
[debhelper.git] / t / buildsystems / buildsystem_tests
1 #!/usr/bin/perl
2
3 use Test::More tests => 227;
4
5 use strict;
6 use warnings;
7 use IPC::Open2;
8 use Cwd ();
9 use File::Temp qw(tempfile tempdir);
10 use File::Basename ();
11
12 # Let the tests to be run from anywhere but currect directory
13 # is expected to be the one where this test lives in.
14 chdir File::Basename::dirname($0) or die "Unable to chdir to ".File::Basename::dirname($0);
15
16 use_ok( 'Debian::Debhelper::Dh_Lib' );
17 use_ok( 'Debian::Debhelper::Buildsystem' );
18 use_ok( 'Debian::Debhelper::Dh_Buildsystems' );
19
20 my $TOPDIR = "../..";
21 my @STEPS = qw(configure build test install clean);
22 my @BUILDSYSTEMS = qw(autoconf perl_makemaker makefile python_distutils perl_build cmake);
23 my $BS_CLASS = 'Debian::Debhelper::Buildsystem';
24
25 my ($bs, @bs, %bs);
26 my ($tmp, @tmp, %tmp);
27 my ($tmpdir, $builddir, $default_builddir);
28
29 ### Common subs ####
30 sub touch {
31         my $file=shift;
32         my $chmod=shift;
33         open FILE, ">", $file and close FILE or die "Unable to touch $file";
34         chmod $chmod, $file if defined $chmod;
35 }
36
37 sub cleandir {
38         my $dir=shift;
39         system ("find", $dir, "-type", "f", "-delete");
40 }
41 sub readlines {
42         my $h=shift;
43         my @lines = <$h>;
44         close $h;
45         chop @lines;
46         return \@lines;
47 }
48
49 sub process_stdout {
50         my ($cmdline, $stdin) = @_;
51         my ($reader, $writer);
52
53         open2($reader, $writer, $cmdline) or die "Unable to exec $cmdline";
54         print $writer $stdin if $stdin;
55         close $writer;
56         return readlines($reader);
57 }
58
59 ### Test Buildsystem class API methods
60 is( $BS_CLASS->_canonpath("path/to/the/./nowhere/../../somewhere"),
61     "path/to/somewhere", "_canonpath no1" );
62 is( $BS_CLASS->_canonpath("path/to/../forward/../../somewhere"),
63     "somewhere","_canonpath no2" );
64 is( $BS_CLASS->_canonpath("path/to/../../../somewhere"),
65     "../somewhere","_canonpath no3" );
66 is( $BS_CLASS->_canonpath("./"), ".", "_canonpath no4" );
67 is( $BS_CLASS->_rel2rel("path/my/file", "path/my"),
68     "file", "_rel2rel no1" );
69 is( $BS_CLASS->_rel2rel("path/dir/file", "path/my"),
70     "../dir/file", "_rel2rel no2" );
71 is( $BS_CLASS->_rel2rel("file", "/root/path/my", "/root"),
72     "../../file", "_rel2rel no3" );
73 is( $BS_CLASS->_rel2rel(".", "."), ".", "_rel2rel no4" );
74 is( $BS_CLASS->_rel2rel("path", "path/"), ".", "_rel2rel no5" );
75
76 ### Test Buildsystem class path API methods under different configurations
77 sub test_buildsystem_paths_api {
78         my ($bs, $config, $expected)=@_;
79
80         my $api_is = sub {
81                 my ($got, $name)=@_;
82                 is( $got, $expected->{$name}, "paths API ($config): $name")
83         };
84
85         &$api_is( $bs->get_sourcedir(), 'get_sourcedir()' );
86         &$api_is( $bs->get_sourcepath("a/b"), 'get_sourcepath(a/b)' );
87         &$api_is( $bs->get_builddir(), 'get_builddir()' );
88         &$api_is( $bs->get_buildpath(), 'get_buildpath()' );
89         &$api_is( $bs->get_buildpath("a/b"), 'get_buildpath(a/b)' );
90         &$api_is( $bs->get_source_rel2builddir(), 'get_source_rel2builddir()' );
91         &$api_is( $bs->get_source_rel2builddir("a/b"), 'get_source_rel2builddir(a/b)' );
92         &$api_is( $bs->get_build_rel2sourcedir(), 'get_build_rel2sourcedir()' );
93         &$api_is( $bs->get_build_rel2sourcedir("a/b"), 'get_build_rel2sourcedir(a/b)' );
94 }
95
96 # Defaults
97 $bs = $BS_CLASS->new();
98 $default_builddir = $bs->DEFAULT_BUILD_DIRECTORY();
99 %tmp = (
100         "get_sourcedir()" => ".",
101         "get_sourcepath(a/b)" => "./a/b",
102         "get_builddir()" => undef,
103         "get_buildpath()" => ".",
104         "get_buildpath(a/b)" =>  "./a/b",
105         "get_source_rel2builddir()" => ".",
106         "get_source_rel2builddir(a/b)" => "./a/b",
107         "get_build_rel2sourcedir()" => ".",
108         "get_build_rel2sourcedir(a/b)" => "./a/b",
109 );
110 test_buildsystem_paths_api($bs, "no builddir, no sourcedir", \%tmp);
111
112 # builddir=bld/dir
113 $bs = $BS_CLASS->new(builddir => "bld/dir");
114 %tmp = (
115         "get_sourcedir()" => ".",
116         "get_sourcepath(a/b)" => "./a/b",
117         "get_builddir()" => "bld/dir",
118         "get_buildpath()" => "bld/dir",
119         "get_buildpath(a/b)" =>  "bld/dir/a/b",
120         "get_source_rel2builddir()" => "../..",
121         "get_source_rel2builddir(a/b)" => "../../a/b",
122         "get_build_rel2sourcedir()" => "bld/dir",
123         "get_build_rel2sourcedir(a/b)" => "bld/dir/a/b",
124 );
125 test_buildsystem_paths_api($bs, "builddir=bld/dir, no sourcedir", \%tmp);
126
127 # Default builddir, sourcedir=autoconf
128 $bs = $BS_CLASS->new(builddir => undef, sourcedir => "autoconf");
129 %tmp = (
130         "get_sourcedir()" => "autoconf",
131         "get_sourcepath(a/b)" => "autoconf/a/b",
132         "get_builddir()" => "$default_builddir",
133         "get_buildpath()" => "$default_builddir",
134         "get_buildpath(a/b)" =>  "$default_builddir/a/b",
135         "get_source_rel2builddir()" => "../autoconf",
136         "get_source_rel2builddir(a/b)" => "../autoconf/a/b",
137         "get_build_rel2sourcedir()" => "../$default_builddir",
138         "get_build_rel2sourcedir(a/b)" => "../$default_builddir/a/b",
139 );
140 test_buildsystem_paths_api($bs, "default builddir, sourcedir=autoconf", \%tmp);
141
142 # Enforce "hard" out of source tree building
143 # sourcedir=builddir=autoconf hence default builddir is implied
144 $bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf/");
145 $bs->enforce_out_of_source_building();
146 test_buildsystem_paths_api($bs, "hard out of source enforced, sourcedir=builddir", \%tmp);
147
148 # sourcedir=autoconf (builddir should be dropped)
149 $bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf");
150 %tmp = (
151         "get_sourcedir()" => "autoconf",
152         "get_sourcepath(a/b)" => "autoconf/a/b",
153         "get_builddir()" => undef,
154         "get_buildpath()" => "autoconf",
155         "get_buildpath(a/b)" =>  "autoconf/a/b",
156         "get_source_rel2builddir()" => ".",
157         "get_source_rel2builddir(a/b)" => "./a/b",
158         "get_build_rel2sourcedir()" => ".",
159         "get_build_rel2sourcedir(a/b)" => "./a/b",
160 );
161 test_buildsystem_paths_api($bs, "no builddir, sourcedir=autoconf", \%tmp);
162
163 # Enforce "soft" out of source tree building when
164 # sourcedir=builddir=autoconf hence builddir should be dropped.
165 $bs->enforce_out_of_source_building("autoconf");
166 test_buildsystem_paths_api($bs, "soft out of source enforced, sourcedir=builddir", \%tmp);
167
168 # builddir=bld/dir, sourcedir=autoconf. Should be the same as sourcedir=autoconf.
169 $bs = $BS_CLASS->new(builddir => "bld/dir", sourcedir => "autoconf");
170 $bs->enforce_in_source_building();
171 test_buildsystem_paths_api($bs, "in source enforced, sourcedir=autoconf", \%tmp);
172
173 # builddir=../bld/dir (relative to the curdir)
174 $bs = $BS_CLASS->new(builddir => "bld/dir/", sourcedir => "autoconf");
175 %tmp = (
176         "get_sourcedir()" => "autoconf",
177         "get_sourcepath(a/b)" => "autoconf/a/b",
178         "get_builddir()" => "bld/dir",
179         "get_buildpath()" => "bld/dir",
180         "get_buildpath(a/b)" =>  "bld/dir/a/b",
181         "get_source_rel2builddir()" => "../../autoconf",
182         "get_source_rel2builddir(a/b)" => "../../autoconf/a/b",
183         "get_build_rel2sourcedir()" => "../bld/dir",
184         "get_build_rel2sourcedir(a/b)" => "../bld/dir/a/b",
185 );
186 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
187
188 ### Test if all buildsystems can be loaded
189 @bs = load_all_buildsystems([ $INC[0] ]);
190 @tmp = map { $_->NAME() } @bs;
191 is_deeply( \@tmp, \@BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );
192
193 ### Test check_auto_buildable() of each buildsystem
194 sub test_check_auto_buildable {
195         my $bs=shift;
196         my $config=shift;
197         my $expected=shift;
198         my @steps=@_ || @STEPS;
199
200         if (! ref $expected) {
201                 my %all_steps;
202                 $all_steps{$_} = $expected foreach (@steps);
203                 $expected = \%all_steps;
204         }
205         for my $step (@steps) {
206                 my $e = 0;
207                 if (exists $expected->{$step}) {
208                         $e = $expected->{$step};
209                 } elsif (exists $expected->{default}) {
210                         $e = $expected->{default};
211                 }
212                 if ($e) {
213                         ok( $bs->check_auto_buildable($step),
214                             $bs->NAME() . "($config): check_auto_buildable($step)" );
215                 }
216                 else {
217                         ok( ! $bs->check_auto_buildable($step),
218                             $bs->NAME() . "($config): ! check_auto_buildable($step)" );
219                 }
220         }
221 }
222
223 $tmpdir = tempdir("tmp.XXXXXX");
224 $builddir = "$tmpdir/builddir";
225 mkdir $builddir;
226 %tmp = (
227         builddir => "$tmpdir/builddir",
228         sourcedir => $tmpdir
229 );
230
231 $bs{autoconf} = load_buildsystem("autoconf", undef, %tmp);
232 $bs{cmake} = load_buildsystem("cmake", undef, %tmp);
233 $bs{perl_mm} = load_buildsystem("perl_makemaker", undef, %tmp);
234 $bs = load_buildsystem("makefile", undef, %tmp);
235
236 test_check_auto_buildable($bs{autoconf}, "no configure", 0);
237 test_check_auto_buildable($bs{cmake}, "no CMakeLists.txt", 0);
238 test_check_auto_buildable($bs{perl_mm}, "no Makefile.PL", 0);
239 test_check_auto_buildable($bs, "no Makefile", 0);
240
241 touch "$tmpdir/configure", 0755;
242 test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1 });
243
244 touch "$tmpdir/CMakeLists.txt";
245 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1 });
246
247 touch "$tmpdir/Makefile.PL";
248 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL",
249     { configure => 1, install => 1 });
250
251 # With Makefile
252 touch "$builddir/Makefile";
253 test_check_auto_buildable($bs, "Makefile", { configure => 0, default => 1 });
254 test_check_auto_buildable($bs{autoconf}, "configure+Makefile", { configure => 1 });
255 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt+Makefile", 1);
256
257 # Makefile.PL forces in-source
258 #(see note in check_auto_buildable() why always 1 here)
259 unlink "$builddir/Makefile";
260 touch "$tmpdir/Makefile";
261 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL+Makefile", 1);
262
263 # Perl Build.PL - handles always
264 $bs = load_buildsystem("perl_build", undef, %tmp);
265 test_check_auto_buildable($bs, "no Build.PL", 0);
266 touch "$tmpdir/Build.PL";
267 test_check_auto_buildable($bs, "Build.PL", { configure => 1 });
268 touch "$tmpdir/Build"; # forced in source
269 test_check_auto_buildable($bs, "Build.PL+Build", 1);
270
271 # Python Distutils
272 $bs = load_buildsystem("python_distutils", undef, %tmp);
273 test_check_auto_buildable($bs, "no setup.py", 0);
274 touch "$tmpdir/setup.py";
275 test_check_auto_buildable($bs, "setup.py", 1);
276
277 cleandir($tmpdir);
278
279 ### Now test if it can autoselect a proper buildsystem for a typical package
280 sub test_autoselection {
281         my $system=shift;
282         my $expected=shift;
283         for my $step (@STEPS) {
284                 my $bs = load_buildsystem(undef, $step, @_);
285                 my $e = $expected;
286                 $e = $expected->{$step} if ref $expected;
287                 if (defined $bs) {
288                         is( $bs->NAME(), $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
289                 }
290                 else {
291                         is ( undef, $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
292                 }
293         }
294 }
295
296 # Autoconf
297 touch "$tmpdir/configure", 0755;
298 touch "$builddir/Makefile";
299 test_autoselection("autoconf",
300     { configure => "autoconf", build => "makefile",
301       test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
302 cleandir $tmpdir;
303
304 # Perl Makemaker (build, test, clean fail with builddir set [not supported])
305 touch "$tmpdir/Makefile.PL";
306 touch "$tmpdir/Makefile";
307 test_autoselection("perl_makemaker", "perl_makemaker", %tmp);
308 cleandir $tmpdir;
309
310 # Makefile
311 touch "$builddir/Makefile";
312 test_autoselection("makefile", { build => "makefile", test => "makefile",
313                 install => "makefile", clean => "makefile" }, %tmp);
314 cleandir $tmpdir;
315
316 # Python Distutils
317 touch "$tmpdir/setup.py";
318 test_autoselection("python_distutils", "python_distutils", %tmp);
319 cleandir $tmpdir;
320
321 # Perl Build
322 touch "$tmpdir/Build.PL";
323 touch "$tmpdir/Build";
324 test_autoselection("perl_build", "perl_build", %tmp);
325 cleandir $tmpdir;
326
327 # CMake
328 touch "$tmpdir/CMakeLists.txt";
329 touch "$builddir/Makefile";
330 test_autoselection("cmake",
331     { configure => "cmake", build => "makefile",
332       test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
333 cleandir $tmpdir;
334
335 ### Test buildsystems_init() and commandline/env argument handling
336 sub get_load_bs_source {
337         my ($system, $step)=@_;
338         $step = (defined $step) ? "'$step'" : 'undef';
339         $system = (defined $system) ? "'$system'" : 'undef';
340
341 return <<EOF;
342 use strict;
343 use warnings;
344 use Debian::Debhelper::Dh_Buildsystems;
345
346 buildsystems_init();
347 my \$bs = load_buildsystem($system, $step);
348 if (defined \$bs) {
349         print 'NAME=', \$bs->NAME(), "\\n";
350         print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
351             foreach (sort keys \%\$bs);
352 }
353 EOF
354 }
355
356 is_deeply( process_stdout("DH_AUTO_OPTIONS='--builddirectory=autoconf/bld\\ dir --sourcedirectory autoconf' $^X -- -",
357                           get_load_bs_source(undef, "configure")),
358     [ 'NAME=autoconf', 'builddir=autoconf/bld dir', 'makecmd=make', 'sourcedir=autoconf' ],
359     "dh_auto_options w/space, autoconf autoselection and sourcedir/builddir" );
360
361 is_deeply( process_stdout("$^X -- - -cautoconf -d autoconf", get_load_bs_source("autoconf", "build")),
362     [ 'NAME=autoconf', 'builddir=undef', 'makecmd=make', 'sourcedir=autoconf' ],
363     "forced autoconf and sourcedir" );
364
365 is_deeply( process_stdout("$^X -- - -b -cautoconf", get_load_bs_source("autoconf", "build")),
366     [ 'NAME=autoconf', "builddir=$default_builddir", 'makecmd=make', 'sourcedir=.' ],
367     "forced autoconf and default build directory" );
368
369 # Build the autoconf test package
370 sub dh_auto_do_autoconf {
371         my $sourcedir=shift;
372         my $builddir=shift;
373         my %args=@_;
374
375         my (@lines, @extra_args);
376         my $buildpath = $sourcedir;
377         my @dh_auto_args = ("-d", $sourcedir);
378         my $dh_auto_str = "-d $sourcedir";
379         if ($builddir) {
380                 push @dh_auto_args, "-b", $builddir;
381                 $dh_auto_str .= " -b $builddir";
382                 $buildpath = $builddir;
383         }
384
385         my $do_dh_auto = sub {
386                 my $step=shift;
387                 my @extra_args;
388                 my $extra_str = "";
389                 if (exists $args{"${step}_args"}) {
390                         push @extra_args, @{$args{"${step}_args"}};
391                         $extra_str .= " $_" foreach (@extra_args);
392                 }
393                 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
394                          "dh_auto_$step $dh_auto_str$extra_str" );
395                 return @extra_args;
396         };
397         
398         @extra_args = &$do_dh_auto('configure');
399         ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
400         @lines=();
401         if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
402                 @lines = @{readlines(\*FILE)};
403         }
404         is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
405
406         &$do_dh_auto('build');
407         ok ( -f "$buildpath/stamp_build", "$buildpath/stamp_build exists" );
408         &$do_dh_auto('test');
409         ok ( -f "$buildpath/stamp_test", "$buildpath/stamp_test exists" );
410         &$do_dh_auto('install');
411         @lines=();
412         if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
413                 @lines = @{readlines(\*FILE)};
414         } 
415         is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
416             "$buildpath/stamp_install contains DESTDIR" );
417         &$do_dh_auto('clean');
418         if ($builddir) {
419                 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
420         }
421         else {
422                 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
423         }
424         ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
425 }
426
427 dh_auto_do_autoconf('autoconf');
428 dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
429 ok ( ! -e 'autoconf/bld', "autoconf/bld got deleted too" );
430
431 END {
432         system("rm", "-rf", $tmpdir);
433         system("$TOPDIR/dh_clean");
434 }