3 use Test::More tests => 227;
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->_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" );
76 ### Test Buildsystem class path API methods under different configurations
77 sub test_buildsystem_paths_api {
78 my ($bs, $config, $expected)=@_;
82 is( $got, $expected->{$name}, "paths API ($config): $name")
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)' );
97 $bs = $BS_CLASS->new();
98 $default_builddir = $bs->DEFAULT_BUILD_DIRECTORY();
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",
110 test_buildsystem_paths_api($bs, "no builddir, no sourcedir", \%tmp);
113 $bs = $BS_CLASS->new(builddir => "bld/dir");
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",
125 test_buildsystem_paths_api($bs, "builddir=bld/dir, no sourcedir", \%tmp);
127 # Default builddir, sourcedir=autoconf
128 $bs = $BS_CLASS->new(builddir => undef, sourcedir => "autoconf");
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",
140 test_buildsystem_paths_api($bs, "default builddir, sourcedir=autoconf", \%tmp);
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);
148 # sourcedir=autoconf (builddir should be dropped)
149 $bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf");
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",
161 test_buildsystem_paths_api($bs, "no builddir, sourcedir=autoconf", \%tmp);
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);
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);
173 # builddir=../bld/dir (relative to the curdir)
174 $bs = $BS_CLASS->new(builddir => "bld/dir/", sourcedir => "autoconf");
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",
186 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
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" );
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 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';
344 use Debian::Debhelper::Dh_Buildsystems;
347 my \$bs = load_buildsystem($system, $step);
349 print 'NAME=', \$bs->NAME(), "\\n";
350 print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
351 foreach (sort keys \%\$bs);
356 is_deeply( process_stdout("$^X -- - --builddirectory='autoconf/bld dir' --sourcedirectory autoconf",
357 get_load_bs_source(undef, "configure")),
358 [ 'NAME=autoconf', 'builddir=autoconf/bld dir', 'makecmd=make', 'sourcedir=autoconf' ],
359 "autoconf autoselection and sourcedir/builddir" );
361 is_deeply( process_stdout("$^X -- - -Sautoconf -D autoconf", get_load_bs_source("autoconf", "build")),
362 [ 'NAME=autoconf', 'builddir=undef', 'makecmd=make', 'sourcedir=autoconf' ],
363 "forced autoconf and sourcedir" );
365 is_deeply( process_stdout("$^X -- - -B -Sautoconf", get_load_bs_source("autoconf", "build")),
366 [ 'NAME=autoconf', "builddir=$default_builddir", 'makecmd=make', 'sourcedir=.' ],
367 "forced autoconf and default build directory" );
369 # Build the autoconf test package
370 sub dh_auto_do_autoconf {
375 my (@lines, @extra_args);
376 my $buildpath = $sourcedir;
377 my @dh_auto_args = ("-D", $sourcedir);
378 my $dh_auto_str = "-D $sourcedir";
380 push @dh_auto_args, "-B", $builddir;
381 $dh_auto_str .= " -B $builddir";
382 $buildpath = $builddir;
385 my $do_dh_auto = sub {
389 if (exists $args{"${step}_args"}) {
390 push @extra_args, @{$args{"${step}_args"}};
391 $extra_str .= " $_" foreach (@extra_args);
393 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
394 "dh_auto_$step $dh_auto_str$extra_str" );
398 @extra_args = &$do_dh_auto('configure');
399 ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
401 if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
402 @lines = @{readlines(\*FILE)};
404 is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
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');
412 if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
413 @lines = @{readlines(\*FILE)};
415 is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
416 "$buildpath/stamp_install contains DESTDIR" );
417 &$do_dh_auto('clean');
419 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
422 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
424 ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
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" );
432 system("rm", "-rf", $tmpdir);
433 system("$TOPDIR/dh_clean");