3 use Test::More tests => 236;
9 use File::Temp qw(tempfile tempdir);
10 use File::Basename ();
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);
16 use_ok( 'Debian::Debhelper::Dh_Lib' );
17 use_ok( 'Debian::Debhelper::Buildsystem' );
18 use_ok( 'Debian::Debhelper::Dh_Buildsystems' );
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';
26 my ($tmp, @tmp, %tmp);
27 my ($tmpdir, $builddir, $default_builddir);
33 open FILE, ">", $file and close FILE or die "Unable to touch $file";
34 chmod $chmod, $file if defined $chmod;
39 system ("find", $dir, "-type", "f", "-delete");
50 my ($cmdline, $stdin) = @_;
51 my ($reader, $writer);
53 open2($reader, $writer, $cmdline) or die "Unable to exec $cmdline";
54 print $writer $stdin if $stdin;
56 return readlines($reader);
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->canonpath("/absolute/path/./somewhere/../to/nowhere"),
68 "/absolute/path/to/nowhere", "canonpath no5" );
69 is( $BS_CLASS->_rel2rel("path/my/file", "path/my", "/tmp"),
70 "file", "_rel2rel no1" );
71 is( $BS_CLASS->_rel2rel("path/dir/file", "path/my", "/tmp"),
72 "../dir/file", "_rel2rel no2" );
73 is( $BS_CLASS->_rel2rel("file", "/root/path/my", "/root"),
74 "/root/file", "_rel2rel abs no3" );
75 is( $BS_CLASS->_rel2rel(".", ".", "/tmp"), ".", "_rel2rel no4" );
76 is( $BS_CLASS->_rel2rel("path", "path/", "/tmp"), ".", "_rel2rel no5" );
77 is( $BS_CLASS->_rel2rel("/absolute/path", "anybase", "/tmp"),
78 "/absolute/path", "_rel2rel abs no6");
79 is( $BS_CLASS->_rel2rel("relative/path", "/absolute/base", "/tmp"),
80 "/tmp/relative/path", "_rel2rel abs no7");
82 ### Test Buildsystem class path API methods under different configurations
83 sub test_buildsystem_paths_api {
84 my ($bs, $config, $expected)=@_;
88 is( $got, $expected->{$name}, "paths API ($config): $name")
91 &$api_is( $bs->get_sourcedir(), 'get_sourcedir()' );
92 &$api_is( $bs->get_sourcepath("a/b"), 'get_sourcepath(a/b)' );
93 &$api_is( $bs->get_builddir(), 'get_builddir()' );
94 &$api_is( $bs->get_buildpath(), 'get_buildpath()' );
95 &$api_is( $bs->get_buildpath("a/b"), 'get_buildpath(a/b)' );
96 &$api_is( $bs->get_source_rel2builddir(), 'get_source_rel2builddir()' );
97 &$api_is( $bs->get_source_rel2builddir("a/b"), 'get_source_rel2builddir(a/b)' );
98 &$api_is( $bs->get_build_rel2sourcedir(), 'get_build_rel2sourcedir()' );
99 &$api_is( $bs->get_build_rel2sourcedir("a/b"), 'get_build_rel2sourcedir(a/b)' );
103 $bs = $BS_CLASS->new();
104 $default_builddir = $bs->DEFAULT_BUILD_DIRECTORY();
106 "get_sourcedir()" => ".",
107 "get_sourcepath(a/b)" => "./a/b",
108 "get_builddir()" => undef,
109 "get_buildpath()" => ".",
110 "get_buildpath(a/b)" => "./a/b",
111 "get_source_rel2builddir()" => ".",
112 "get_source_rel2builddir(a/b)" => "./a/b",
113 "get_build_rel2sourcedir()" => ".",
114 "get_build_rel2sourcedir(a/b)" => "./a/b",
116 test_buildsystem_paths_api($bs, "no builddir, no sourcedir", \%tmp);
119 $bs = $BS_CLASS->new(builddir => "bld/dir");
121 "get_sourcedir()" => ".",
122 "get_sourcepath(a/b)" => "./a/b",
123 "get_builddir()" => "bld/dir",
124 "get_buildpath()" => "bld/dir",
125 "get_buildpath(a/b)" => "bld/dir/a/b",
126 "get_source_rel2builddir()" => "../..",
127 "get_source_rel2builddir(a/b)" => "../../a/b",
128 "get_build_rel2sourcedir()" => "bld/dir",
129 "get_build_rel2sourcedir(a/b)" => "bld/dir/a/b",
131 test_buildsystem_paths_api($bs, "builddir=bld/dir, no sourcedir", \%tmp);
133 # Default builddir, sourcedir=autoconf
134 $bs = $BS_CLASS->new(builddir => undef, sourcedir => "autoconf");
136 "get_sourcedir()" => "autoconf",
137 "get_sourcepath(a/b)" => "autoconf/a/b",
138 "get_builddir()" => "$default_builddir",
139 "get_buildpath()" => "$default_builddir",
140 "get_buildpath(a/b)" => "$default_builddir/a/b",
141 "get_source_rel2builddir()" => "../autoconf",
142 "get_source_rel2builddir(a/b)" => "../autoconf/a/b",
143 "get_build_rel2sourcedir()" => "../$default_builddir",
144 "get_build_rel2sourcedir(a/b)" => "../$default_builddir/a/b",
146 test_buildsystem_paths_api($bs, "default builddir, sourcedir=autoconf", \%tmp);
148 # Enforce out of source tree building
149 # sourcedir=builddir=autoconf hence default builddir is implied
150 $bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf/");
151 $bs->enforce_out_of_source_building();
152 test_buildsystem_paths_api($bs, "hard out of source enforced, sourcedir=builddir", \%tmp);
154 # sourcedir=autoconf (builddir should be dropped)
155 $bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf");
157 "get_sourcedir()" => "autoconf",
158 "get_sourcepath(a/b)" => "autoconf/a/b",
159 "get_builddir()" => undef,
160 "get_buildpath()" => "autoconf",
161 "get_buildpath(a/b)" => "autoconf/a/b",
162 "get_source_rel2builddir()" => ".",
163 "get_source_rel2builddir(a/b)" => "./a/b",
164 "get_build_rel2sourcedir()" => ".",
165 "get_build_rel2sourcedir(a/b)" => "./a/b",
167 test_buildsystem_paths_api($bs, "no builddir, sourcedir=autoconf", \%tmp);
169 # Prefer out of source tree building when
170 # sourcedir=builddir=autoconf hence builddir should be dropped.
171 $bs->prefer_out_of_source_building(builddir => "autoconf");
172 test_buildsystem_paths_api($bs, "out of source prefered, sourcedir=builddir", \%tmp);
174 # builddir=bld/dir, sourcedir=autoconf. Should be the same as sourcedir=autoconf.
175 $bs = $BS_CLASS->new(builddir => "bld/dir", sourcedir => "autoconf");
176 $bs->enforce_in_source_building();
177 test_buildsystem_paths_api($bs, "in source enforced, sourcedir=autoconf", \%tmp);
179 # builddir=../bld/dir (relative to the curdir)
180 $bs = $BS_CLASS->new(builddir => "bld/dir/", sourcedir => "autoconf");
182 "get_sourcedir()" => "autoconf",
183 "get_sourcepath(a/b)" => "autoconf/a/b",
184 "get_builddir()" => "bld/dir",
185 "get_buildpath()" => "bld/dir",
186 "get_buildpath(a/b)" => "bld/dir/a/b",
187 "get_source_rel2builddir()" => "../../autoconf",
188 "get_source_rel2builddir(a/b)" => "../../autoconf/a/b",
189 "get_build_rel2sourcedir()" => "../bld/dir",
190 "get_build_rel2sourcedir(a/b)" => "../bld/dir/a/b",
192 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
194 ### Test if all buildsystems can be loaded
195 @bs = load_all_buildsystems([ $INC[0] ]);
196 @tmp = map { $_->NAME() } @bs;
197 is_deeply( \@tmp, \@BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );
199 ### Test check_auto_buildable() of each buildsystem
200 sub test_check_auto_buildable {
204 my @steps=@_ || @STEPS;
206 if (! ref $expected) {
208 $all_steps{$_} = $expected foreach (@steps);
209 $expected = \%all_steps;
211 for my $step (@steps) {
213 if (exists $expected->{$step}) {
214 $e = $expected->{$step};
215 } elsif (exists $expected->{default}) {
216 $e = $expected->{default};
219 ok( $bs->check_auto_buildable($step),
220 $bs->NAME() . "($config): check_auto_buildable($step)" );
223 ok( ! $bs->check_auto_buildable($step),
224 $bs->NAME() . "($config): ! check_auto_buildable($step)" );
229 $tmpdir = tempdir("tmp.XXXXXX");
230 $builddir = "$tmpdir/builddir";
233 builddir => "$tmpdir/builddir",
237 $bs{autoconf} = load_buildsystem("autoconf", undef, %tmp);
238 $bs{cmake} = load_buildsystem("cmake", undef, %tmp);
239 $bs{perl_mm} = load_buildsystem("perl_makemaker", undef, %tmp);
240 $bs = load_buildsystem("makefile", undef, %tmp);
242 test_check_auto_buildable($bs{autoconf}, "no configure", 0);
243 test_check_auto_buildable($bs{cmake}, "no CMakeLists.txt", 0);
244 test_check_auto_buildable($bs{perl_mm}, "no Makefile.PL", 0);
245 test_check_auto_buildable($bs, "no Makefile", 0);
247 touch "$tmpdir/configure", 0755;
248 test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1 });
250 touch "$tmpdir/CMakeLists.txt";
251 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1 });
253 touch "$tmpdir/Makefile.PL";
254 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL",
255 { configure => 1, install => 1 });
258 touch "$builddir/Makefile";
259 test_check_auto_buildable($bs, "Makefile", { configure => 0, default => 1 });
260 test_check_auto_buildable($bs{autoconf}, "configure+Makefile", { configure => 1 });
261 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt+Makefile", 1);
263 # Makefile.PL forces in-source
264 #(see note in check_auto_buildable() why always 1 here)
265 unlink "$builddir/Makefile";
266 touch "$tmpdir/Makefile";
267 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL+Makefile", 1);
269 # Perl Build.PL - handles always
270 $bs = load_buildsystem("perl_build", undef, %tmp);
271 test_check_auto_buildable($bs, "no Build.PL", 0);
272 touch "$tmpdir/Build.PL";
273 test_check_auto_buildable($bs, "Build.PL", { configure => 1 });
274 touch "$tmpdir/Build"; # forced in source
275 test_check_auto_buildable($bs, "Build.PL+Build", 1);
278 $bs = load_buildsystem("python_distutils", undef, %tmp);
279 test_check_auto_buildable($bs, "no setup.py", 0);
280 touch "$tmpdir/setup.py";
281 test_check_auto_buildable($bs, "setup.py", 1);
285 ### Now test if it can autoselect a proper buildsystem for a typical package
286 sub test_autoselection {
289 for my $step (@STEPS) {
290 my $bs = load_buildsystem(undef, $step, @_);
292 $e = $expected->{$step} if ref $expected;
294 is( $bs->NAME(), $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
297 is ( undef, $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
303 touch "$tmpdir/configure", 0755;
304 touch "$builddir/Makefile";
305 test_autoselection("autoconf",
306 { configure => "autoconf", build => "makefile",
307 test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
310 # Perl Makemaker (build, test, clean fail with builddir set [not supported])
311 touch "$tmpdir/Makefile.PL";
312 touch "$tmpdir/Makefile";
313 test_autoselection("perl_makemaker", "perl_makemaker", %tmp);
317 touch "$builddir/Makefile";
318 test_autoselection("makefile", { build => "makefile", test => "makefile",
319 install => "makefile", clean => "makefile" }, %tmp);
323 touch "$tmpdir/setup.py";
324 test_autoselection("python_distutils", "python_distutils", %tmp);
328 touch "$tmpdir/Build.PL";
329 touch "$tmpdir/Build";
330 test_autoselection("perl_build", "perl_build", %tmp);
334 touch "$tmpdir/CMakeLists.txt";
335 touch "$builddir/Makefile";
336 test_autoselection("cmake",
337 { configure => "cmake", build => "makefile",
338 test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
341 ### Test Buildsystem::rmdir_builddir()
342 sub do_rmdir_builddir {
345 $system = $BS_CLASS->new(builddir => $builddir, sourcedir => $tmpdir);
346 $system->mkdir_builddir();
347 $system->rmdir_builddir();
350 $builddir = "$tmpdir/builddir";
351 do_rmdir_builddir($builddir);
352 ok ( ! -e $builddir, "testing rmdir_builddir() 1: builddir parent '$builddir' deleted" );
353 ok ( -d $tmpdir, "testing rmdir_builddir() 1: sourcedir '$tmpdir' remains" );
355 $builddir = "$tmpdir/bld";
356 do_rmdir_builddir("$builddir/dir");
357 ok ( ! -e $builddir, "testing rmdir_builddir() 2: builddir parent '$builddir' deleted" );
358 ok ( -d $tmpdir, "testing rmdir_builddir() 2: sourcedir '$tmpdir' remains" );
360 $builddir = "$tmpdir/bld";
362 touch "$builddir/afile";
363 mkdir "$builddir/dir";
364 touch "$builddir/dir/afile2";
365 do_rmdir_builddir("$builddir/dir");
366 ok ( ! -e "$builddir/dir", "testing rmdir_builddir() 3: builddir '$builddir/dir' not empty, but deleted" );
367 ok ( -d $builddir, "testing rmdir_builddir() 3: builddir parent '$builddir' not empty, remains" );
371 ### Test buildsystems_init() and commandline/env argument handling
372 sub get_load_bs_source {
373 my ($system, $step)=@_;
374 $step = (defined $step) ? "'$step'" : 'undef';
375 $system = (defined $system) ? "'$system'" : 'undef';
380 use Debian::Debhelper::Dh_Buildsystems;
383 my \$bs = load_buildsystem($system, $step);
385 print 'NAME=', \$bs->NAME(), "\\n";
386 print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
387 foreach (sort keys \%\$bs);
392 $tmp = Cwd::getcwd();
393 is_deeply( process_stdout("$^X -- - --builddirectory='autoconf/bld dir' --sourcedirectory autoconf",
394 get_load_bs_source(undef, "configure")),
395 [ 'NAME=autoconf', 'builddir=autoconf/bld dir', "cwd=$tmp", 'makecmd=make', 'sourcedir=autoconf' ],
396 "autoconf autoselection and sourcedir/builddir" );
398 is_deeply( process_stdout("$^X -- - -Sautoconf -D autoconf", get_load_bs_source("autoconf", "build")),
399 [ 'NAME=autoconf', 'builddir=undef', "cwd=$tmp", 'makecmd=make', 'sourcedir=autoconf' ],
400 "forced autoconf and sourcedir" );
402 is_deeply( process_stdout("$^X -- - -B -Sautoconf", get_load_bs_source("autoconf", "build")),
403 [ 'NAME=autoconf', "builddir=$default_builddir", "cwd=$tmp", 'makecmd=make', 'sourcedir=.' ],
404 "forced autoconf and default build directory" );
406 # Build the autoconf test package
407 sub dh_auto_do_autoconf {
412 my (@lines, @extra_args);
413 my $buildpath = $sourcedir;
414 my @dh_auto_args = ("-D", $sourcedir);
415 my $dh_auto_str = "-D $sourcedir";
417 push @dh_auto_args, "-B", $builddir;
418 $dh_auto_str .= " -B $builddir";
419 $buildpath = $builddir;
422 my $do_dh_auto = sub {
426 if (exists $args{"${step}_args"}) {
427 push @extra_args, @{$args{"${step}_args"}};
428 $extra_str .= " $_" foreach (@extra_args);
430 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
431 "dh_auto_$step $dh_auto_str$extra_str" );
435 @extra_args = &$do_dh_auto('configure');
436 ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
438 if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
439 @lines = @{readlines(\*FILE)};
441 is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
443 &$do_dh_auto('build');
444 ok ( -f "$buildpath/stamp_build", "$buildpath/stamp_build exists" );
445 &$do_dh_auto('test');
446 ok ( -f "$buildpath/stamp_test", "$buildpath/stamp_test exists" );
447 &$do_dh_auto('install');
449 if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
450 @lines = @{readlines(\*FILE)};
452 is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
453 "$buildpath/stamp_install contains DESTDIR" );
454 &$do_dh_auto('clean');
456 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
459 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
461 ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
464 dh_auto_do_autoconf('autoconf');
465 dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
466 ok ( ! -e 'bld', "bld got deleted too" );
469 system("rm", "-rf", $tmpdir);
470 system("$TOPDIR/dh_clean");