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 @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"),
70 "file", "_rel2rel no1" );
71 is( $BS_CLASS->_rel2rel("path/dir/file", "path/my"),
72 "../dir/file", "_rel2rel no2" );
73 is( $BS_CLASS->_rel2rel("file", "/root/path/my", "/root"),
74 "../../file", "_rel2rel no3" );
75 is( $BS_CLASS->_rel2rel(".", "."), ".", "_rel2rel no4" );
76 is( $BS_CLASS->_rel2rel("path", "path/"), ".", "_rel2rel no5" );
78 ### Test Buildsystem class path API methods under different configurations
79 sub test_buildsystem_paths_api {
80 my ($bs, $config, $expected)=@_;
84 is( $got, $expected->{$name}, "paths API ($config): $name")
87 &$api_is( $bs->get_sourcedir(), 'get_sourcedir()' );
88 &$api_is( $bs->get_sourcepath("a/b"), 'get_sourcepath(a/b)' );
89 &$api_is( $bs->get_builddir(), 'get_builddir()' );
90 &$api_is( $bs->get_buildpath(), 'get_buildpath()' );
91 &$api_is( $bs->get_buildpath("a/b"), 'get_buildpath(a/b)' );
92 &$api_is( $bs->get_source_rel2builddir(), 'get_source_rel2builddir()' );
93 &$api_is( $bs->get_source_rel2builddir("a/b"), 'get_source_rel2builddir(a/b)' );
94 &$api_is( $bs->get_build_rel2sourcedir(), 'get_build_rel2sourcedir()' );
95 &$api_is( $bs->get_build_rel2sourcedir("a/b"), 'get_build_rel2sourcedir(a/b)' );
99 $bs = $BS_CLASS->new();
100 $default_builddir = $bs->DEFAULT_BUILD_DIRECTORY();
102 "get_sourcedir()" => ".",
103 "get_sourcepath(a/b)" => "./a/b",
104 "get_builddir()" => undef,
105 "get_buildpath()" => ".",
106 "get_buildpath(a/b)" => "./a/b",
107 "get_source_rel2builddir()" => ".",
108 "get_source_rel2builddir(a/b)" => "./a/b",
109 "get_build_rel2sourcedir()" => ".",
110 "get_build_rel2sourcedir(a/b)" => "./a/b",
112 test_buildsystem_paths_api($bs, "no builddir, no sourcedir", \%tmp);
115 $bs = $BS_CLASS->new(builddir => "bld/dir");
117 "get_sourcedir()" => ".",
118 "get_sourcepath(a/b)" => "./a/b",
119 "get_builddir()" => "bld/dir",
120 "get_buildpath()" => "bld/dir",
121 "get_buildpath(a/b)" => "bld/dir/a/b",
122 "get_source_rel2builddir()" => "../..",
123 "get_source_rel2builddir(a/b)" => "../../a/b",
124 "get_build_rel2sourcedir()" => "bld/dir",
125 "get_build_rel2sourcedir(a/b)" => "bld/dir/a/b",
127 test_buildsystem_paths_api($bs, "builddir=bld/dir, no sourcedir", \%tmp);
129 # Default builddir, sourcedir=autoconf
130 $bs = $BS_CLASS->new(builddir => undef, sourcedir => "autoconf");
132 "get_sourcedir()" => "autoconf",
133 "get_sourcepath(a/b)" => "autoconf/a/b",
134 "get_builddir()" => "$default_builddir",
135 "get_buildpath()" => "$default_builddir",
136 "get_buildpath(a/b)" => "$default_builddir/a/b",
137 "get_source_rel2builddir()" => "../autoconf",
138 "get_source_rel2builddir(a/b)" => "../autoconf/a/b",
139 "get_build_rel2sourcedir()" => "../$default_builddir",
140 "get_build_rel2sourcedir(a/b)" => "../$default_builddir/a/b",
142 test_buildsystem_paths_api($bs, "default builddir, sourcedir=autoconf", \%tmp);
144 # Enforce out of source tree building
145 # sourcedir=builddir=autoconf hence default builddir is implied
146 $bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf/");
147 $bs->enforce_out_of_source_building();
148 test_buildsystem_paths_api($bs, "hard out of source enforced, sourcedir=builddir", \%tmp);
150 # sourcedir=autoconf (builddir should be dropped)
151 $bs = $BS_CLASS->new(builddir => "autoconf", sourcedir => "autoconf");
153 "get_sourcedir()" => "autoconf",
154 "get_sourcepath(a/b)" => "autoconf/a/b",
155 "get_builddir()" => undef,
156 "get_buildpath()" => "autoconf",
157 "get_buildpath(a/b)" => "autoconf/a/b",
158 "get_source_rel2builddir()" => ".",
159 "get_source_rel2builddir(a/b)" => "./a/b",
160 "get_build_rel2sourcedir()" => ".",
161 "get_build_rel2sourcedir(a/b)" => "./a/b",
163 test_buildsystem_paths_api($bs, "no builddir, sourcedir=autoconf", \%tmp);
165 # Prefer out of source tree building when
166 # sourcedir=builddir=autoconf hence builddir should be dropped.
167 $bs->prefer_out_of_source_building(builddir => "autoconf");
168 test_buildsystem_paths_api($bs, "out of source prefered, sourcedir=builddir", \%tmp);
170 # builddir=bld/dir, sourcedir=autoconf. Should be the same as sourcedir=autoconf.
171 $bs = $BS_CLASS->new(builddir => "bld/dir", sourcedir => "autoconf");
172 $bs->enforce_in_source_building();
173 test_buildsystem_paths_api($bs, "in source enforced, sourcedir=autoconf", \%tmp);
175 # builddir=../bld/dir (relative to the curdir)
176 $bs = $BS_CLASS->new(builddir => "bld/dir/", sourcedir => "autoconf");
178 "get_sourcedir()" => "autoconf",
179 "get_sourcepath(a/b)" => "autoconf/a/b",
180 "get_builddir()" => "bld/dir",
181 "get_buildpath()" => "bld/dir",
182 "get_buildpath(a/b)" => "bld/dir/a/b",
183 "get_source_rel2builddir()" => "../../autoconf",
184 "get_source_rel2builddir(a/b)" => "../../autoconf/a/b",
185 "get_build_rel2sourcedir()" => "../bld/dir",
186 "get_build_rel2sourcedir(a/b)" => "../bld/dir/a/b",
188 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
190 ### Test if all buildsystems can be loaded
191 @bs = load_all_buildsystems([ $INC[0] ]);
192 @tmp = map { $_->NAME() } @bs;
193 is_deeply( \@tmp, \@BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );
195 ### Test check_auto_buildable() of each buildsystem
196 sub test_check_auto_buildable {
200 my @steps=@_ || @STEPS;
202 if (! ref $expected) {
204 $all_steps{$_} = $expected foreach (@steps);
205 $expected = \%all_steps;
207 for my $step (@steps) {
209 if (exists $expected->{$step}) {
210 $e = $expected->{$step};
211 } elsif (exists $expected->{default}) {
212 $e = $expected->{default};
215 ok( $bs->check_auto_buildable($step),
216 $bs->NAME() . "($config): check_auto_buildable($step)" );
219 ok( ! $bs->check_auto_buildable($step),
220 $bs->NAME() . "($config): ! check_auto_buildable($step)" );
225 $tmpdir = tempdir("tmp.XXXXXX");
226 $builddir = "$tmpdir/builddir";
229 builddir => "$tmpdir/builddir",
233 $bs{autoconf} = load_buildsystem("autoconf", undef, %tmp);
234 $bs{cmake} = load_buildsystem("cmake", undef, %tmp);
235 $bs{perl_mm} = load_buildsystem("perl_makemaker", undef, %tmp);
236 $bs = load_buildsystem("makefile", undef, %tmp);
238 test_check_auto_buildable($bs{autoconf}, "no configure", 0);
239 test_check_auto_buildable($bs{cmake}, "no CMakeLists.txt", 0);
240 test_check_auto_buildable($bs{perl_mm}, "no Makefile.PL", 0);
241 test_check_auto_buildable($bs, "no Makefile", 0);
243 touch "$tmpdir/configure", 0755;
244 test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1 });
246 touch "$tmpdir/CMakeLists.txt";
247 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1 });
249 touch "$tmpdir/Makefile.PL";
250 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL",
251 { configure => 1, install => 1 });
254 touch "$builddir/Makefile";
255 test_check_auto_buildable($bs, "Makefile", { configure => 0, default => 1 });
256 test_check_auto_buildable($bs{autoconf}, "configure+Makefile", { configure => 1 });
257 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt+Makefile", 1);
259 # Makefile.PL forces in-source
260 #(see note in check_auto_buildable() why always 1 here)
261 unlink "$builddir/Makefile";
262 touch "$tmpdir/Makefile";
263 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL+Makefile", 1);
265 # Perl Build.PL - handles always
266 $bs = load_buildsystem("perl_build", undef, %tmp);
267 test_check_auto_buildable($bs, "no Build.PL", 0);
268 touch "$tmpdir/Build.PL";
269 test_check_auto_buildable($bs, "Build.PL", { configure => 1 });
270 touch "$tmpdir/Build"; # forced in source
271 test_check_auto_buildable($bs, "Build.PL+Build", 1);
274 $bs = load_buildsystem("python_distutils", undef, %tmp);
275 test_check_auto_buildable($bs, "no setup.py", 0);
276 touch "$tmpdir/setup.py";
277 test_check_auto_buildable($bs, "setup.py", 1);
281 ### Now test if it can autoselect a proper buildsystem for a typical package
282 sub test_autoselection {
285 for my $step (@STEPS) {
286 my $bs = load_buildsystem(undef, $step, @_);
288 $e = $expected->{$step} if ref $expected;
290 is( $bs->NAME(), $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
293 is ( undef, $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
299 touch "$tmpdir/configure", 0755;
300 touch "$builddir/Makefile";
301 test_autoselection("autoconf",
302 { configure => "autoconf", build => "makefile",
303 test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
306 # Perl Makemaker (build, test, clean fail with builddir set [not supported])
307 touch "$tmpdir/Makefile.PL";
308 touch "$tmpdir/Makefile";
309 test_autoselection("perl_makemaker", "perl_makemaker", %tmp);
313 touch "$builddir/Makefile";
314 test_autoselection("makefile", { build => "makefile", test => "makefile",
315 install => "makefile", clean => "makefile" }, %tmp);
319 touch "$tmpdir/setup.py";
320 test_autoselection("python_distutils", "python_distutils", %tmp);
324 touch "$tmpdir/Build.PL";
325 touch "$tmpdir/Build";
326 test_autoselection("perl_build", "perl_build", %tmp);
330 touch "$tmpdir/CMakeLists.txt";
331 touch "$builddir/Makefile";
332 test_autoselection("cmake",
333 { configure => "cmake", build => "makefile",
334 test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
337 ### Test buildsystems_init() and commandline/env argument handling
338 sub get_load_bs_source {
339 my ($system, $step)=@_;
340 $step = (defined $step) ? "'$step'" : 'undef';
341 $system = (defined $system) ? "'$system'" : 'undef';
346 use Debian::Debhelper::Dh_Buildsystems;
349 my \$bs = load_buildsystem($system, $step);
351 print 'NAME=', \$bs->NAME(), "\\n";
352 print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
353 foreach (sort keys \%\$bs);
358 is_deeply( process_stdout("$^X -- - --builddirectory='autoconf/bld dir' --sourcedirectory autoconf",
359 get_load_bs_source(undef, "configure")),
360 [ 'NAME=autoconf', 'builddir=autoconf/bld dir', 'makecmd=make', 'sourcedir=autoconf' ],
361 "autoconf autoselection and sourcedir/builddir" );
363 is_deeply( process_stdout("$^X -- - -Sautoconf -D autoconf", get_load_bs_source("autoconf", "build")),
364 [ 'NAME=autoconf', 'builddir=undef', 'makecmd=make', 'sourcedir=autoconf' ],
365 "forced autoconf and sourcedir" );
367 is_deeply( process_stdout("$^X -- - -B -Sautoconf", get_load_bs_source("autoconf", "build")),
368 [ 'NAME=autoconf', "builddir=$default_builddir", 'makecmd=make', 'sourcedir=.' ],
369 "forced autoconf and default build directory" );
371 # Build the autoconf test package
372 sub dh_auto_do_autoconf {
377 my (@lines, @extra_args);
378 my $buildpath = $sourcedir;
379 my @dh_auto_args = ("-D", $sourcedir);
380 my $dh_auto_str = "-D $sourcedir";
382 push @dh_auto_args, "-B", $builddir;
383 $dh_auto_str .= " -B $builddir";
384 $buildpath = $builddir;
387 my $do_dh_auto = sub {
391 if (exists $args{"${step}_args"}) {
392 push @extra_args, @{$args{"${step}_args"}};
393 $extra_str .= " $_" foreach (@extra_args);
395 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
396 "dh_auto_$step $dh_auto_str$extra_str" );
400 @extra_args = &$do_dh_auto('configure');
401 ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
403 if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
404 @lines = @{readlines(\*FILE)};
406 is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
408 &$do_dh_auto('build');
409 ok ( -f "$buildpath/stamp_build", "$buildpath/stamp_build exists" );
410 &$do_dh_auto('test');
411 ok ( -f "$buildpath/stamp_test", "$buildpath/stamp_test exists" );
412 &$do_dh_auto('install');
414 if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
415 @lines = @{readlines(\*FILE)};
417 is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
418 "$buildpath/stamp_install contains DESTDIR" );
419 &$do_dh_auto('clean');
421 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
424 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
426 ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
429 dh_auto_do_autoconf('autoconf');
430 dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
431 ok ( ! -e 'autoconf/bld', "autoconf/bld got deleted too" );
434 system("rm", "-rf", $tmpdir);
435 system("$TOPDIR/dh_clean");