]> git.donarmstrong.com Git - debhelper.git/blob - t/buildsystems/buildsystem_tests
test suite should not need modification to add a new build system
[debhelper.git] / t / buildsystems / buildsystem_tests
1 #!/usr/bin/perl
2
3 use Test::More tests => 228;
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 $BS_CLASS = 'Debian::Debhelper::Buildsystem';
23
24 my ($bs, @bs, %bs);
25 my ($tmp, @tmp, %tmp);
26 my ($tmpdir, $builddir, $default_builddir);
27
28 ### Common subs ####
29 sub touch {
30         my $file=shift;
31         my $chmod=shift;
32         open FILE, ">", $file and close FILE or die "Unable to touch $file";
33         chmod $chmod, $file if defined $chmod;
34 }
35
36 sub cleandir {
37         my $dir=shift;
38         system ("find", $dir, "-type", "f", "-delete");
39 }
40 sub readlines {
41         my $h=shift;
42         my @lines = <$h>;
43         close $h;
44         chop @lines;
45         return \@lines;
46 }
47
48 sub process_stdout {
49         my ($cmdline, $stdin) = @_;
50         my ($reader, $writer);
51
52         open2($reader, $writer, $cmdline) or die "Unable to exec $cmdline";
53         print $writer $stdin if $stdin;
54         close $writer;
55         return readlines($reader);
56 }
57
58 ### Test Buildsystem class API methods
59 is( $BS_CLASS->canonpath("path/to/the/./nowhere/../../somewhere"),
60     "path/to/somewhere", "canonpath no1" );
61 is( $BS_CLASS->canonpath("path/to/../forward/../../somewhere"),
62     "somewhere","canonpath no2" );
63 is( $BS_CLASS->canonpath("path/to/../../../somewhere"),
64     "../somewhere","canonpath no3" );
65 is( $BS_CLASS->canonpath("./"), ".", "canonpath no4" );
66 is( $BS_CLASS->canonpath("/absolute/path/./somewhere/../to/nowhere"),
67     "/absolute/path/to/nowhere", "canonpath no5" );
68 is( $BS_CLASS->_rel2rel("path/my/file", "path/my", "/tmp"),
69     "file", "_rel2rel no1" );
70 is( $BS_CLASS->_rel2rel("path/dir/file", "path/my", "/tmp"),
71     "../dir/file", "_rel2rel no2" );
72 is( $BS_CLASS->_rel2rel("file", "/root/path/my", "/root"),
73     "/root/file", "_rel2rel abs no3" );
74 is( $BS_CLASS->_rel2rel(".", ".", "/tmp"), ".", "_rel2rel no4" );
75 is( $BS_CLASS->_rel2rel("path", "path/", "/tmp"), ".", "_rel2rel no5" );
76 is( $BS_CLASS->_rel2rel("/absolute/path", "anybase", "/tmp"),
77     "/absolute/path", "_rel2rel abs no6");
78 is( $BS_CLASS->_rel2rel("relative/path", "/absolute/base", "/tmp"),
79     "/tmp/relative/path", "_rel2rel abs no7");
80
81 ### Test Buildsystem class path API methods under different configurations
82 sub test_buildsystem_paths_api {
83         my ($bs, $config, $expected)=@_;
84
85         my $api_is = sub {
86                 my ($got, $name)=@_;
87                 is( $got, $expected->{$name}, "paths API ($config): $name")
88         };
89
90         &$api_is( $bs->get_sourcedir(), 'get_sourcedir()' );
91         &$api_is( $bs->get_sourcepath("a/b"), 'get_sourcepath(a/b)' );
92         &$api_is( $bs->get_builddir(), 'get_builddir()' );
93         &$api_is( $bs->get_buildpath(), 'get_buildpath()' );
94         &$api_is( $bs->get_buildpath("a/b"), 'get_buildpath(a/b)' );
95         &$api_is( $bs->get_source_rel2builddir(), 'get_source_rel2builddir()' );
96         &$api_is( $bs->get_source_rel2builddir("a/b"), 'get_source_rel2builddir(a/b)' );
97         &$api_is( $bs->get_build_rel2sourcedir(), 'get_build_rel2sourcedir()' );
98         &$api_is( $bs->get_build_rel2sourcedir("a/b"), 'get_build_rel2sourcedir(a/b)' );
99 }
100
101 # Defaults
102 $bs = $BS_CLASS->new();
103 $default_builddir = $bs->DEFAULT_BUILD_DIRECTORY();
104 %tmp = (
105         "get_sourcedir()" => ".",
106         "get_sourcepath(a/b)" => "./a/b",
107         "get_builddir()" => undef,
108         "get_buildpath()" => ".",
109         "get_buildpath(a/b)" =>  "./a/b",
110         "get_source_rel2builddir()" => ".",
111         "get_source_rel2builddir(a/b)" => "./a/b",
112         "get_build_rel2sourcedir()" => ".",
113         "get_build_rel2sourcedir(a/b)" => "./a/b",
114 );
115 test_buildsystem_paths_api($bs, "no builddir, no sourcedir", \%tmp);
116
117 # builddir=bld/dir
118 $bs = $BS_CLASS->new(builddir => "bld/dir");
119 %tmp = (
120         "get_sourcedir()" => ".",
121         "get_sourcepath(a/b)" => "./a/b",
122         "get_builddir()" => "bld/dir",
123         "get_buildpath()" => "bld/dir",
124         "get_buildpath(a/b)" =>  "bld/dir/a/b",
125         "get_source_rel2builddir()" => "../..",
126         "get_source_rel2builddir(a/b)" => "../../a/b",
127         "get_build_rel2sourcedir()" => "bld/dir",
128         "get_build_rel2sourcedir(a/b)" => "bld/dir/a/b",
129 );
130 test_buildsystem_paths_api($bs, "builddir=bld/dir, no sourcedir", \%tmp);
131
132 # Default builddir, sourcedir=autoconf
133 $bs = $BS_CLASS->new(builddir => undef, sourcedir => "autoconf");
134 %tmp = (
135         "get_sourcedir()" => "autoconf",
136         "get_sourcepath(a/b)" => "autoconf/a/b",
137         "get_builddir()" => "$default_builddir",
138         "get_buildpath()" => "$default_builddir",
139         "get_buildpath(a/b)" =>  "$default_builddir/a/b",
140         "get_source_rel2builddir()" => "../autoconf",
141         "get_source_rel2builddir(a/b)" => "../autoconf/a/b",
142         "get_build_rel2sourcedir()" => "../$default_builddir",
143         "get_build_rel2sourcedir(a/b)" => "../$default_builddir/a/b",
144 );
145 test_buildsystem_paths_api($bs, "default builddir, sourcedir=autoconf", \%tmp);
146
147 # sourcedir=autoconf (builddir should be dropped)
148 $bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf");
149 %tmp = (
150         "get_sourcedir()" => "autoconf",
151         "get_sourcepath(a/b)" => "autoconf/a/b",
152         "get_builddir()" => undef,
153         "get_buildpath()" => "autoconf",
154         "get_buildpath(a/b)" =>  "autoconf/a/b",
155         "get_source_rel2builddir()" => ".",
156         "get_source_rel2builddir(a/b)" => "./a/b",
157         "get_build_rel2sourcedir()" => ".",
158         "get_build_rel2sourcedir(a/b)" => "./a/b",
159 );
160 test_buildsystem_paths_api($bs, "no builddir, sourcedir=autoconf", \%tmp);
161
162 # Prefer out of source tree building when
163 # sourcedir=builddir=autoconf hence builddir should be dropped.
164 $bs->prefer_out_of_source_building(builddir => "autoconf");
165 test_buildsystem_paths_api($bs, "out of source prefered, sourcedir=builddir", \%tmp);
166
167 # builddir=bld/dir, sourcedir=autoconf. Should be the same as sourcedir=autoconf.
168 $bs = $BS_CLASS->new(builddir => "bld/dir", sourcedir => "autoconf");
169 $bs->enforce_in_source_building();
170 test_buildsystem_paths_api($bs, "in source enforced, sourcedir=autoconf", \%tmp);
171
172 # builddir=../bld/dir (relative to the curdir)
173 $bs = $BS_CLASS->new(builddir => "bld/dir/", sourcedir => "autoconf");
174 %tmp = (
175         "get_sourcedir()" => "autoconf",
176         "get_sourcepath(a/b)" => "autoconf/a/b",
177         "get_builddir()" => "bld/dir",
178         "get_buildpath()" => "bld/dir",
179         "get_buildpath(a/b)" =>  "bld/dir/a/b",
180         "get_source_rel2builddir()" => "../../autoconf",
181         "get_source_rel2builddir(a/b)" => "../../autoconf/a/b",
182         "get_build_rel2sourcedir()" => "../bld/dir",
183         "get_build_rel2sourcedir(a/b)" => "../bld/dir/a/b",
184 );
185 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
186
187 ### Test if all buildsystems can be loaded
188 @bs = load_all_buildsystems([ $INC[0] ]);
189 @tmp = map { $_->NAME() } @bs;
190 ok(@Debian::Debhelper::Dh_Buildsystems::BUILDSYSTEMS >= 1, "some build systems are built in" );
191 is_deeply( \@tmp, \@Debian::Debhelper::Dh_Buildsystems::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 Buildsystem::rmdir_builddir()
336 sub do_rmdir_builddir {
337         my $builddir=shift;
338         my $system;
339         $system = $BS_CLASS->new(builddir => $builddir, sourcedir => $tmpdir);
340         $system->mkdir_builddir();
341         $system->rmdir_builddir();
342 }
343
344 $builddir = "$tmpdir/builddir";
345 do_rmdir_builddir($builddir);
346 ok ( ! -e $builddir, "testing rmdir_builddir() 1: builddir parent '$builddir' deleted" );
347 ok ( -d $tmpdir, "testing rmdir_builddir() 1: sourcedir '$tmpdir' remains" );
348
349 $builddir = "$tmpdir/bld";
350 do_rmdir_builddir("$builddir/dir");
351 ok ( ! -e $builddir, "testing rmdir_builddir() 2: builddir parent '$builddir' deleted" );
352 ok ( -d $tmpdir, "testing rmdir_builddir() 2: sourcedir '$tmpdir' remains" );
353
354 $builddir = "$tmpdir/bld";
355 mkdir "$builddir";
356 touch "$builddir/afile";
357 mkdir "$builddir/dir";
358 touch "$builddir/dir/afile2";
359 do_rmdir_builddir("$builddir/dir");
360 ok ( ! -e "$builddir/dir", "testing rmdir_builddir() 3: builddir '$builddir/dir' not empty, but deleted" );
361 ok ( -d $builddir, "testing rmdir_builddir() 3: builddir parent '$builddir' not empty, remains" );
362
363 cleandir $tmpdir;
364
365 ### Test buildsystems_init() and commandline/env argument handling
366 sub get_load_bs_source {
367         my ($system, $step)=@_;
368         $step = (defined $step) ? "'$step'" : 'undef';
369         $system = (defined $system) ? "'$system'" : 'undef';
370
371 return <<EOF;
372 use strict;
373 use warnings;
374 use Debian::Debhelper::Dh_Buildsystems;
375
376 buildsystems_init();
377 my \$bs = load_buildsystem($system, $step);
378 if (defined \$bs) {
379         print 'NAME=', \$bs->NAME(), "\\n";
380         print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
381             foreach (sort keys \%\$bs);
382 }
383 EOF
384 }
385
386 $tmp = Cwd::getcwd();
387 is_deeply( process_stdout("$^X -- - --builddirectory='autoconf/bld dir' --sourcedirectory autoconf",
388                           get_load_bs_source(undef, "configure")),
389     [ 'NAME=autoconf', 'builddir=autoconf/bld dir', "cwd=$tmp",  'makecmd=make', 'sourcedir=autoconf' ],
390     "autoconf autoselection and sourcedir/builddir" );
391
392 is_deeply( process_stdout("$^X -- - -Sautoconf -D autoconf", get_load_bs_source("autoconf", "build")),
393     [ 'NAME=autoconf', 'builddir=undef', "cwd=$tmp", 'makecmd=make', 'sourcedir=autoconf' ],
394     "forced autoconf and sourcedir" );
395
396 is_deeply( process_stdout("$^X -- - -B -Sautoconf", get_load_bs_source("autoconf", "build")),
397     [ 'NAME=autoconf', "builddir=$default_builddir", "cwd=$tmp", 'makecmd=make', 'sourcedir=.' ],
398     "forced autoconf and default build directory" );
399
400 # Build the autoconf test package
401 sub dh_auto_do_autoconf {
402         my $sourcedir=shift;
403         my $builddir=shift;
404         my %args=@_;
405
406         my (@lines, @extra_args);
407         my $buildpath = $sourcedir;
408         my @dh_auto_args = ("-D", $sourcedir);
409         my $dh_auto_str = "-D $sourcedir";
410         if ($builddir) {
411                 push @dh_auto_args, "-B", $builddir;
412                 $dh_auto_str .= " -B $builddir";
413                 $buildpath = $builddir;
414         }
415
416         my $do_dh_auto = sub {
417                 my $step=shift;
418                 my @extra_args;
419                 my $extra_str = "";
420                 if (exists $args{"${step}_args"}) {
421                         push @extra_args, @{$args{"${step}_args"}};
422                         $extra_str .= " $_" foreach (@extra_args);
423                 }
424                 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
425                          "dh_auto_$step $dh_auto_str$extra_str" );
426                 return @extra_args;
427         };
428         
429         @extra_args = &$do_dh_auto('configure');
430         ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
431         @lines=();
432         if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
433                 @lines = @{readlines(\*FILE)};
434         }
435         is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
436
437         &$do_dh_auto('build');
438         ok ( -f "$buildpath/stamp_build", "$buildpath/stamp_build exists" );
439         &$do_dh_auto('test');
440         ok ( -f "$buildpath/stamp_test", "$buildpath/stamp_test exists" );
441         &$do_dh_auto('install');
442         @lines=();
443         if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
444                 @lines = @{readlines(\*FILE)};
445         } 
446         is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
447             "$buildpath/stamp_install contains DESTDIR" );
448         &$do_dh_auto('clean');
449         if ($builddir) {
450                 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
451         }
452         else {
453                 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
454         }
455         ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
456 }
457
458 dh_auto_do_autoconf('autoconf');
459 dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
460 ok ( ! -e 'bld', "bld got deleted too" );
461
462 END {
463         system("rm", "-rf", $tmpdir);
464         system("$TOPDIR/dh_clean");
465 }