3 use Test::More tests => 228;
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 $BS_CLASS = 'Debian::Debhelper::Buildsystem';
25 my ($tmp, @tmp, %tmp);
26 my ($tmpdir, $builddir, $default_builddir);
32 open FILE, ">", $file and close FILE or die "Unable to touch $file";
33 chmod $chmod, $file if defined $chmod;
38 system ("find", $dir, "-type", "f", "-delete");
49 my ($cmdline, $stdin) = @_;
50 my ($reader, $writer);
52 open2($reader, $writer, $cmdline) or die "Unable to exec $cmdline";
53 print $writer $stdin if $stdin;
55 return readlines($reader);
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");
81 ### Test Buildsystem class path API methods under different configurations
82 sub test_buildsystem_paths_api {
83 my ($bs, $config, $expected)=@_;
87 is( $got, $expected->{$name}, "paths API ($config): $name")
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)' );
102 $bs = $BS_CLASS->new();
103 $default_builddir = $bs->DEFAULT_BUILD_DIRECTORY();
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",
115 test_buildsystem_paths_api($bs, "no builddir, no sourcedir", \%tmp);
118 $bs = $BS_CLASS->new(builddir => "bld/dir");
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",
130 test_buildsystem_paths_api($bs, "builddir=bld/dir, no sourcedir", \%tmp);
132 # Default builddir, sourcedir=autoconf
133 $bs = $BS_CLASS->new(builddir => undef, sourcedir => "autoconf");
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",
145 test_buildsystem_paths_api($bs, "default builddir, sourcedir=autoconf", \%tmp);
147 # sourcedir=autoconf (builddir should be dropped)
148 $bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf");
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",
160 test_buildsystem_paths_api($bs, "no builddir, sourcedir=autoconf", \%tmp);
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);
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);
172 # builddir=../bld/dir (relative to the curdir)
173 $bs = $BS_CLASS->new(builddir => "bld/dir/", sourcedir => "autoconf");
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",
185 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
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" );
193 ### Test check_auto_buildable() of each buildsystem
194 sub test_check_auto_buildable {
198 my @steps=@_ || @STEPS;
200 if (! ref $expected) {
202 $all_steps{$_} = $expected foreach (@steps);
203 $expected = \%all_steps;
205 for my $step (@steps) {
207 if (exists $expected->{$step}) {
208 $e = $expected->{$step};
209 } elsif (exists $expected->{default}) {
210 $e = $expected->{default};
213 ok( $bs->check_auto_buildable($step),
214 $bs->NAME() . "($config): check_auto_buildable($step)" );
217 ok( ! $bs->check_auto_buildable($step),
218 $bs->NAME() . "($config): ! check_auto_buildable($step)" );
223 $tmpdir = tempdir("tmp.XXXXXX");
224 $builddir = "$tmpdir/builddir";
227 builddir => "$tmpdir/builddir",
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);
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);
241 touch "$tmpdir/configure", 0755;
242 test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1 });
244 touch "$tmpdir/CMakeLists.txt";
245 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1 });
247 touch "$tmpdir/Makefile.PL";
248 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL",
249 { configure => 1, install => 1 });
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);
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);
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);
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);
279 ### Now test if it can autoselect a proper buildsystem for a typical package
280 sub test_autoselection {
283 for my $step (@STEPS) {
284 my $bs = load_buildsystem(undef, $step, @_);
286 $e = $expected->{$step} if ref $expected;
288 is( $bs->NAME(), $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
291 is ( undef, $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
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);
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);
311 touch "$builddir/Makefile";
312 test_autoselection("makefile", { build => "makefile", test => "makefile",
313 install => "makefile", clean => "makefile" }, %tmp);
317 touch "$tmpdir/setup.py";
318 test_autoselection("python_distutils", "python_distutils", %tmp);
322 touch "$tmpdir/Build.PL";
323 touch "$tmpdir/Build";
324 test_autoselection("perl_build", "perl_build", %tmp);
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);
335 ### Test Buildsystem::rmdir_builddir()
336 sub do_rmdir_builddir {
339 $system = $BS_CLASS->new(builddir => $builddir, sourcedir => $tmpdir);
340 $system->mkdir_builddir();
341 $system->rmdir_builddir();
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" );
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" );
354 $builddir = "$tmpdir/bld";
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" );
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';
374 use Debian::Debhelper::Dh_Buildsystems;
377 my \$bs = load_buildsystem($system, $step);
379 print 'NAME=', \$bs->NAME(), "\\n";
380 print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
381 foreach (sort keys \%\$bs);
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" );
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" );
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" );
400 # Build the autoconf test package
401 sub dh_auto_do_autoconf {
406 my (@lines, @extra_args);
407 my $buildpath = $sourcedir;
408 my @dh_auto_args = ("-D", $sourcedir);
409 my $dh_auto_str = "-D $sourcedir";
411 push @dh_auto_args, "-B", $builddir;
412 $dh_auto_str .= " -B $builddir";
413 $buildpath = $builddir;
416 my $do_dh_auto = sub {
420 if (exists $args{"${step}_args"}) {
421 push @extra_args, @{$args{"${step}_args"}};
422 $extra_str .= " $_" foreach (@extra_args);
424 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
425 "dh_auto_$step $dh_auto_str$extra_str" );
429 @extra_args = &$do_dh_auto('configure');
430 ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
432 if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
433 @lines = @{readlines(\*FILE)};
435 is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
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');
443 if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
444 @lines = @{readlines(\*FILE)};
446 is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
447 "$buildpath/stamp_install contains DESTDIR" );
448 &$do_dh_auto('clean');
450 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
453 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
455 ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
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" );
463 system("rm", "-rf", $tmpdir);
464 system("$TOPDIR/dh_clean");