3 use Test::More tests => 224;
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->_rel2rel("path/my/file", "path/my"),
67 "file", "_rel2rel no1" );
68 is( $BS_CLASS->_rel2rel("path/dir/file", "path/my"),
69 "../dir/file", "_rel2rel no2" );
70 is( $BS_CLASS->_rel2rel("file", "/root/path/my", "/root"),
71 "../../file", "_rel2rel no3" );
73 ### Test Buildsystem class path API methods under different configurations
74 sub test_buildsystem_paths_api {
75 my ($bs, $config, $expected)=@_;
79 is( $got, $expected->{$name}, "paths API ($config): $name")
82 &$api_is( $bs->get_sourcedir(), 'get_sourcedir()' );
83 &$api_is( $bs->get_sourcepath("a/b"), 'get_sourcepath(a/b)' );
84 &$api_is( $bs->get_builddir(), 'get_builddir()' );
85 &$api_is( $bs->get_buildpath(), 'get_buildpath()' );
86 &$api_is( $bs->get_buildpath("a/b"), 'get_buildpath(a/b)' );
87 &$api_is( $bs->get_source_rel2builddir(), 'get_source_rel2builddir()' );
88 &$api_is( $bs->get_source_rel2builddir("a/b"), 'get_source_rel2builddir(a/b)' );
89 &$api_is( $bs->get_build_rel2sourcedir(), 'get_build_rel2sourcedir()' );
90 &$api_is( $bs->get_build_rel2sourcedir("a/b"), 'get_build_rel2sourcedir(a/b)' );
94 $bs = $BS_CLASS->new();
95 $default_builddir = $bs->DEFAULT_BUILD_DIRECTORY();
97 "get_sourcedir()" => ".",
98 "get_sourcepath(a/b)" => "./a/b",
99 "get_builddir()" => undef,
100 "get_buildpath()" => ".",
101 "get_buildpath(a/b)" => "./a/b",
102 "get_source_rel2builddir()" => ".",
103 "get_source_rel2builddir(a/b)" => "./a/b",
104 "get_build_rel2sourcedir()" => ".",
105 "get_build_rel2sourcedir(a/b)" => "./a/b",
107 test_buildsystem_paths_api($bs, "no builddir, no sourcedir", \%tmp);
110 $bs = $BS_CLASS->new(builddir => "bld/dir");
112 "get_sourcedir()" => ".",
113 "get_sourcepath(a/b)" => "./a/b",
114 "get_builddir()" => "bld/dir",
115 "get_buildpath()" => "bld/dir",
116 "get_buildpath(a/b)" => "bld/dir/a/b",
117 "get_source_rel2builddir()" => "../..",
118 "get_source_rel2builddir(a/b)" => "../../a/b",
119 "get_build_rel2sourcedir()" => "bld/dir",
120 "get_build_rel2sourcedir(a/b)" => "bld/dir/a/b",
122 test_buildsystem_paths_api($bs, "builddir=bld/dir, no sourcedir", \%tmp);
124 # Default builddir, sourcedir=autoconf
125 $bs = $BS_CLASS->new(builddir => undef, sourcedir => "autoconf");
127 "get_sourcedir()" => "autoconf",
128 "get_sourcepath(a/b)" => "autoconf/a/b",
129 "get_builddir()" => "autoconf/$default_builddir",
130 "get_buildpath()" => "autoconf/$default_builddir",
131 "get_buildpath(a/b)" => "autoconf/$default_builddir/a/b",
132 "get_source_rel2builddir()" => "..",
133 "get_source_rel2builddir(a/b)" => "../a/b",
134 "get_build_rel2sourcedir()" => "$default_builddir",
135 "get_build_rel2sourcedir(a/b)" => "$default_builddir/a/b",
137 test_buildsystem_paths_api($bs, "default builddir, sourcedir=autoconf", \%tmp);
139 # Enforced out of source tree building
140 # sourcedir=builddir=autoconf hence default builddir is implied
141 $bs = $BS_CLASS->new(builddir => "./autoconf", sourcedir => "autoconf/");
142 $bs->enforce_out_of_source_building();
143 test_buildsystem_paths_api($bs, "out of source enforced, sourcedir=autoconf/", \%tmp);
145 # sourcedir=autoconf (builddir should be dropped)
146 $bs = $BS_CLASS->new(builddir => ".", sourcedir => "autoconf");
148 "get_sourcedir()" => "autoconf",
149 "get_sourcepath(a/b)" => "autoconf/a/b",
150 "get_builddir()" => undef,
151 "get_buildpath()" => "autoconf",
152 "get_buildpath(a/b)" => "autoconf/a/b",
153 "get_source_rel2builddir()" => ".",
154 "get_source_rel2builddir(a/b)" => "./a/b",
155 "get_build_rel2sourcedir()" => ".",
156 "get_build_rel2sourcedir(a/b)" => "./a/b",
158 test_buildsystem_paths_api($bs, "no builddir, sourcedir=autoconf", \%tmp);
160 # builddir=bld/dir, sourcedir=autoconf. Should be the same as sourcedir=autoconf.
161 $bs = $BS_CLASS->new(builddir => "bld/dir", sourcedir => "autoconf");
162 $bs->enforce_in_source_building();
163 test_buildsystem_paths_api($bs, "in source enforced, sourcedir=autoconf", \%tmp);
165 # builddir=../bld/dir (relative to the sourcedir)
166 $bs = $BS_CLASS->new(builddir => "../bld/dir/", sourcedir => "autoconf");
168 "get_sourcedir()" => "autoconf",
169 "get_sourcepath(a/b)" => "autoconf/a/b",
170 "get_builddir()" => "bld/dir",
171 "get_buildpath()" => "bld/dir",
172 "get_buildpath(a/b)" => "bld/dir/a/b",
173 "get_source_rel2builddir()" => "../../autoconf",
174 "get_source_rel2builddir(a/b)" => "../../autoconf/a/b",
175 "get_build_rel2sourcedir()" => "../bld/dir",
176 "get_build_rel2sourcedir(a/b)" => "../bld/dir/a/b",
178 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
180 # Builddir relative to the pwd (same path as above).
181 $bs = $BS_CLASS->new(builddir => "./bld/dir", sourcedir => "autoconf");
182 test_buildsystem_paths_api($bs, "builddir=./bld/dir, sourcedir=autoconf", \%tmp);
184 ### Test if all buildsystems can be loaded
185 @bs = load_all_buildsystems([ $INC[0] ]);
186 @tmp = map { $_->NAME() } @bs;
187 is_deeply( \@tmp, \@BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );
189 ### Test check_auto_buildable() of each buildsystem
190 sub test_check_auto_buildable {
194 my @steps=@_ || @STEPS;
196 if (! ref $expected) {
198 $all_steps{$_} = $expected foreach (@steps);
199 $expected = \%all_steps;
201 for my $step (@steps) {
203 if (exists $expected->{$step}) {
204 $e = $expected->{$step};
205 } elsif (exists $expected->{default}) {
206 $e = $expected->{default};
209 ok( $bs->check_auto_buildable($step),
210 $bs->NAME() . "($config): check_auto_buildable($step)" );
213 ok( ! $bs->check_auto_buildable($step),
214 $bs->NAME() . "($config): ! check_auto_buildable($step)" );
219 $tmpdir = tempdir("tmp.XXXXXX");
220 $builddir = "$tmpdir/builddir";
223 builddir => 'builddir',
227 $bs{autoconf} = load_buildsystem("autoconf", undef, %tmp);
228 $bs{cmake} = load_buildsystem("cmake", undef, %tmp);
229 $bs{perl_mm} = load_buildsystem("perl_makemaker", undef, %tmp);
230 $bs = load_buildsystem("makefile", undef, %tmp);
232 test_check_auto_buildable($bs{autoconf}, "no configure", 0);
233 test_check_auto_buildable($bs{cmake}, "no CMakeLists.txt", 0);
234 test_check_auto_buildable($bs{perl_mm}, "no Makefile.PL", 0);
235 test_check_auto_buildable($bs, "no Makefile", 0);
237 touch "$tmpdir/configure", 0755;
238 test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1 });
240 touch "$tmpdir/CMakeLists.txt";
241 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1 });
243 touch "$tmpdir/Makefile.PL";
244 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL",
245 { configure => 1, install => 1 });
248 touch "$builddir/Makefile";
249 test_check_auto_buildable($bs, "Makefile", { configure => 0, default => 1 });
250 test_check_auto_buildable($bs{autoconf}, "configure+Makefile", { configure => 1 });
251 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt+Makefile", 1);
253 # Makefile.PL forces in-source
254 #(see note in check_auto_buildable() why always 1 here)
255 unlink "$builddir/Makefile";
256 touch "$tmpdir/Makefile";
257 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL+Makefile", 1);
259 # Perl Build.PL - handles always
260 $bs = load_buildsystem("perl_build", undef, %tmp);
261 test_check_auto_buildable($bs, "no Build.PL", 0);
262 touch "$tmpdir/Build.PL";
263 test_check_auto_buildable($bs, "Build.PL", { configure => 1 });
264 touch "$tmpdir/Build"; # forced in source
265 test_check_auto_buildable($bs, "Build.PL+Build", 1);
268 $bs = load_buildsystem("python_distutils", undef, %tmp);
269 test_check_auto_buildable($bs, "no setup.py", 0);
270 touch "$tmpdir/setup.py";
271 test_check_auto_buildable($bs, "setup.py", 1);
275 ### Now test if it can autoselect a proper buildsystem for a typical package
276 sub test_autoselection {
279 for my $step (@STEPS) {
280 my $bs = load_buildsystem(undef, $step, @_);
282 $e = $expected->{$step} if ref $expected;
284 is( $bs->NAME(), $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
287 is ( undef, $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
293 touch "$tmpdir/configure", 0755;
294 touch "$builddir/Makefile";
295 test_autoselection("autoconf",
296 { configure => "autoconf", build => "makefile",
297 test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
300 # Perl Makemaker (build, test, clean fail with builddir set [not supported])
301 touch "$tmpdir/Makefile.PL";
302 touch "$tmpdir/Makefile";
303 test_autoselection("perl_makemaker", "perl_makemaker", %tmp);
307 touch "$builddir/Makefile";
308 test_autoselection("makefile", { build => "makefile", test => "makefile",
309 install => "makefile", clean => "makefile" }, %tmp);
313 touch "$tmpdir/setup.py";
314 test_autoselection("python_distutils", "python_distutils", %tmp);
318 touch "$tmpdir/Build.PL";
319 touch "$tmpdir/Build";
320 test_autoselection("perl_build", "perl_build", %tmp);
324 touch "$tmpdir/CMakeLists.txt";
325 touch "$builddir/Makefile";
326 test_autoselection("cmake",
327 { configure => "cmake", build => "makefile",
328 test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
331 ### Test buildsystems_init() and commandline/env argument handling
332 sub get_load_bs_source {
333 my ($system, $step)=@_;
334 $step = (defined $step) ? "'$step'" : 'undef';
335 $system = (defined $system) ? "'$system'" : 'undef';
340 use Debian::Debhelper::Dh_Buildsystems;
343 my \$bs = load_buildsystem($system, $step);
345 print 'NAME=', \$bs->NAME(), "\\n";
346 print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
347 foreach (sort keys \%\$bs);
352 is_deeply( process_stdout("DH_AUTO_OPTIONS='--builddirectory=bld\\ dir --sourcedirectory autoconf' $^X -- -",
353 get_load_bs_source(undef, "configure")),
354 [ 'NAME=autoconf', 'builddir=autoconf/bld dir', 'makecmd=make', 'sourcedir=autoconf' ],
355 "dh_auto_options w/space, autoconf autoselection and sourcedir/builddir" );
357 is_deeply( process_stdout("$^X -- - -cautoconf -d autoconf", get_load_bs_source("autoconf", "build")),
358 [ 'NAME=autoconf', 'builddir=undef', 'makecmd=make', 'sourcedir=autoconf' ],
359 "forced autoconf and sourcedir" );
361 is_deeply( process_stdout("$^X -- - -b -cautoconf", get_load_bs_source("autoconf", "build")),
362 [ 'NAME=autoconf', "builddir=$default_builddir", 'makecmd=make', 'sourcedir=.' ],
363 "forced autoconf and default build directory" );
365 # Build the autoconf test package
366 sub dh_auto_do_autoconf {
371 my (@lines, @extra_args);
372 my $buildpath = $sourcedir;
373 my @dh_auto_args = ("-d", $sourcedir);
374 my $dh_auto_str = "-d $sourcedir";
376 push @dh_auto_args, "-b", $builddir;
377 $dh_auto_str .= " -b $builddir";
378 $buildpath .= "/$builddir";
381 my $do_dh_auto = sub {
385 if (exists $args{"${step}_args"}) {
386 push @extra_args, @{$args{"${step}_args"}};
387 $extra_str .= " $_" foreach (@extra_args);
389 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
390 "dh_auto_$step $dh_auto_str$extra_str" );
394 @extra_args = &$do_dh_auto('configure');
395 ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
397 if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
398 @lines = @{readlines(\*FILE)};
400 is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
402 &$do_dh_auto('build');
403 ok ( -f "$buildpath/stamp_build", "$buildpath/stamp_build exists" );
404 &$do_dh_auto('test');
405 ok ( -f "$buildpath/stamp_test", "$buildpath/stamp_test exists" );
406 &$do_dh_auto('install');
408 if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
409 @lines = @{readlines(\*FILE)};
411 is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
412 "$buildpath/stamp_install contains DESTDIR" );
413 &$do_dh_auto('clean');
415 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
418 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
420 ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
423 dh_auto_do_autoconf('autoconf');
424 dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
425 ok ( ! -e 'autoconf/bld', "autoconf/bld got deleted too" );
428 system("rm", "-rf", $tmpdir);
429 system("$TOPDIR/dh_clean");