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()" => "$default_builddir",
130 "get_buildpath()" => "$default_builddir",
131 "get_buildpath(a/b)" => "$default_builddir/a/b",
132 "get_source_rel2builddir()" => "../autoconf",
133 "get_source_rel2builddir(a/b)" => "../autoconf/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 # Enforce "hard" 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, "hard out of source enforced, sourcedir=builddir", \%tmp);
145 # sourcedir=autoconf (builddir should be dropped)
146 $bs = $BS_CLASS->new(builddir => "autoconf", 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 # Enforce "soft" out of source tree building when
161 # sourcedir=builddir=autoconf hence builddir should be dropped.
162 $bs->enforce_out_of_source_building("autoconf");
163 test_buildsystem_paths_api($bs, "soft out of source enforced, sourcedir=builddir", \%tmp);
165 # builddir=bld/dir, sourcedir=autoconf. Should be the same as sourcedir=autoconf.
166 $bs = $BS_CLASS->new(builddir => "bld/dir", sourcedir => "autoconf");
167 $bs->enforce_in_source_building();
168 test_buildsystem_paths_api($bs, "in source enforced, sourcedir=autoconf", \%tmp);
170 # builddir=../bld/dir (relative to the curdir)
171 $bs = $BS_CLASS->new(builddir => "bld/dir/", sourcedir => "autoconf");
173 "get_sourcedir()" => "autoconf",
174 "get_sourcepath(a/b)" => "autoconf/a/b",
175 "get_builddir()" => "bld/dir",
176 "get_buildpath()" => "bld/dir",
177 "get_buildpath(a/b)" => "bld/dir/a/b",
178 "get_source_rel2builddir()" => "../../autoconf",
179 "get_source_rel2builddir(a/b)" => "../../autoconf/a/b",
180 "get_build_rel2sourcedir()" => "../bld/dir",
181 "get_build_rel2sourcedir(a/b)" => "../bld/dir/a/b",
183 test_buildsystem_paths_api($bs, "builddir=../bld/dir, sourcedir=autoconf", \%tmp);
185 ### Test if all buildsystems can be loaded
186 @bs = load_all_buildsystems([ $INC[0] ]);
187 @tmp = map { $_->NAME() } @bs;
188 is_deeply( \@tmp, \@BUILDSYSTEMS, "load_all_buildsystems() loads all built-in buildsystems" );
190 ### Test check_auto_buildable() of each buildsystem
191 sub test_check_auto_buildable {
195 my @steps=@_ || @STEPS;
197 if (! ref $expected) {
199 $all_steps{$_} = $expected foreach (@steps);
200 $expected = \%all_steps;
202 for my $step (@steps) {
204 if (exists $expected->{$step}) {
205 $e = $expected->{$step};
206 } elsif (exists $expected->{default}) {
207 $e = $expected->{default};
210 ok( $bs->check_auto_buildable($step),
211 $bs->NAME() . "($config): check_auto_buildable($step)" );
214 ok( ! $bs->check_auto_buildable($step),
215 $bs->NAME() . "($config): ! check_auto_buildable($step)" );
220 $tmpdir = tempdir("tmp.XXXXXX");
221 $builddir = "$tmpdir/builddir";
224 builddir => "$tmpdir/builddir",
228 $bs{autoconf} = load_buildsystem("autoconf", undef, %tmp);
229 $bs{cmake} = load_buildsystem("cmake", undef, %tmp);
230 $bs{perl_mm} = load_buildsystem("perl_makemaker", undef, %tmp);
231 $bs = load_buildsystem("makefile", undef, %tmp);
233 test_check_auto_buildable($bs{autoconf}, "no configure", 0);
234 test_check_auto_buildable($bs{cmake}, "no CMakeLists.txt", 0);
235 test_check_auto_buildable($bs{perl_mm}, "no Makefile.PL", 0);
236 test_check_auto_buildable($bs, "no Makefile", 0);
238 touch "$tmpdir/configure", 0755;
239 test_check_auto_buildable($bs{autoconf}, "configure", { configure => 1 });
241 touch "$tmpdir/CMakeLists.txt";
242 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt", { configure => 1 });
244 touch "$tmpdir/Makefile.PL";
245 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL",
246 { configure => 1, install => 1 });
249 touch "$builddir/Makefile";
250 test_check_auto_buildable($bs, "Makefile", { configure => 0, default => 1 });
251 test_check_auto_buildable($bs{autoconf}, "configure+Makefile", { configure => 1 });
252 test_check_auto_buildable($bs{cmake}, "CMakeLists.txt+Makefile", 1);
254 # Makefile.PL forces in-source
255 #(see note in check_auto_buildable() why always 1 here)
256 unlink "$builddir/Makefile";
257 touch "$tmpdir/Makefile";
258 test_check_auto_buildable($bs{perl_mm}, "Makefile.PL+Makefile", 1);
260 # Perl Build.PL - handles always
261 $bs = load_buildsystem("perl_build", undef, %tmp);
262 test_check_auto_buildable($bs, "no Build.PL", 0);
263 touch "$tmpdir/Build.PL";
264 test_check_auto_buildable($bs, "Build.PL", { configure => 1 });
265 touch "$tmpdir/Build"; # forced in source
266 test_check_auto_buildable($bs, "Build.PL+Build", 1);
269 $bs = load_buildsystem("python_distutils", undef, %tmp);
270 test_check_auto_buildable($bs, "no setup.py", 0);
271 touch "$tmpdir/setup.py";
272 test_check_auto_buildable($bs, "setup.py", 1);
276 ### Now test if it can autoselect a proper buildsystem for a typical package
277 sub test_autoselection {
280 for my $step (@STEPS) {
281 my $bs = load_buildsystem(undef, $step, @_);
283 $e = $expected->{$step} if ref $expected;
285 is( $bs->NAME(), $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
288 is ( undef, $e, "autoselection($system): $step=".((defined $e)?$e:'undef') );
294 touch "$tmpdir/configure", 0755;
295 touch "$builddir/Makefile";
296 test_autoselection("autoconf",
297 { configure => "autoconf", build => "makefile",
298 test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
301 # Perl Makemaker (build, test, clean fail with builddir set [not supported])
302 touch "$tmpdir/Makefile.PL";
303 touch "$tmpdir/Makefile";
304 test_autoselection("perl_makemaker", "perl_makemaker", %tmp);
308 touch "$builddir/Makefile";
309 test_autoselection("makefile", { build => "makefile", test => "makefile",
310 install => "makefile", clean => "makefile" }, %tmp);
314 touch "$tmpdir/setup.py";
315 test_autoselection("python_distutils", "python_distutils", %tmp);
319 touch "$tmpdir/Build.PL";
320 touch "$tmpdir/Build";
321 test_autoselection("perl_build", "perl_build", %tmp);
325 touch "$tmpdir/CMakeLists.txt";
326 touch "$builddir/Makefile";
327 test_autoselection("cmake",
328 { configure => "cmake", build => "makefile",
329 test => "makefile", install => "makefile", clean => "makefile" }, %tmp);
332 ### Test buildsystems_init() and commandline/env argument handling
333 sub get_load_bs_source {
334 my ($system, $step)=@_;
335 $step = (defined $step) ? "'$step'" : 'undef';
336 $system = (defined $system) ? "'$system'" : 'undef';
341 use Debian::Debhelper::Dh_Buildsystems;
344 my \$bs = load_buildsystem($system, $step);
346 print 'NAME=', \$bs->NAME(), "\\n";
347 print \$_, "=", (defined \$bs->{\$_}) ? \$bs->{\$_} : 'undef', "\\n"
348 foreach (sort keys \%\$bs);
353 is_deeply( process_stdout("DH_AUTO_OPTIONS='--builddirectory=autoconf/bld\\ dir --sourcedirectory autoconf' $^X -- -",
354 get_load_bs_source(undef, "configure")),
355 [ 'NAME=autoconf', 'builddir=autoconf/bld dir', 'makecmd=make', 'sourcedir=autoconf' ],
356 "dh_auto_options w/space, autoconf autoselection and sourcedir/builddir" );
358 is_deeply( process_stdout("$^X -- - -cautoconf -d autoconf", get_load_bs_source("autoconf", "build")),
359 [ 'NAME=autoconf', 'builddir=undef', 'makecmd=make', 'sourcedir=autoconf' ],
360 "forced autoconf and sourcedir" );
362 is_deeply( process_stdout("$^X -- - -b -cautoconf", get_load_bs_source("autoconf", "build")),
363 [ 'NAME=autoconf', "builddir=$default_builddir", 'makecmd=make', 'sourcedir=.' ],
364 "forced autoconf and default build directory" );
366 # Build the autoconf test package
367 sub dh_auto_do_autoconf {
372 my (@lines, @extra_args);
373 my $buildpath = $sourcedir;
374 my @dh_auto_args = ("-d", $sourcedir);
375 my $dh_auto_str = "-d $sourcedir";
377 push @dh_auto_args, "-b", $builddir;
378 $dh_auto_str .= " -b $builddir";
379 $buildpath = $builddir;
382 my $do_dh_auto = sub {
386 if (exists $args{"${step}_args"}) {
387 push @extra_args, @{$args{"${step}_args"}};
388 $extra_str .= " $_" foreach (@extra_args);
390 is ( system("$TOPDIR/dh_auto_$step", @dh_auto_args, "--", @extra_args), 0,
391 "dh_auto_$step $dh_auto_str$extra_str" );
395 @extra_args = &$do_dh_auto('configure');
396 ok ( -f "$buildpath/Makefile", "$buildpath/Makefile exists" );
398 if (ok( open(FILE, "$buildpath/stamp_configure"), "$buildpath/stamp_configure exists") ) {
399 @lines = @{readlines(\*FILE)};
401 is_deeply( \@lines, \@extra_args, "$buildpath/stamp_configure contains extra args" );
403 &$do_dh_auto('build');
404 ok ( -f "$buildpath/stamp_build", "$buildpath/stamp_build exists" );
405 &$do_dh_auto('test');
406 ok ( -f "$buildpath/stamp_test", "$buildpath/stamp_test exists" );
407 &$do_dh_auto('install');
409 if ( ok(open(FILE, "$buildpath/stamp_install"), "$buildpath/stamp_install exists") ) {
410 @lines = @{readlines(\*FILE)};
412 is_deeply( \@lines, [ "DESTDIR=".Cwd::getcwd()."/debian/testpackage" ],
413 "$buildpath/stamp_install contains DESTDIR" );
414 &$do_dh_auto('clean');
416 ok ( ! -e "$buildpath", "builddir $buildpath was removed" );
419 ok ( ! -e "$buildpath/Makefile" && ! -e "$buildpath/stamp_configure", "Makefile and stamps gone" );
421 ok ( -x "$sourcedir/configure", "configure script renamins after clean" );
424 dh_auto_do_autoconf('autoconf');
425 dh_auto_do_autoconf('autoconf', 'bld/dir', configure_args => [ "--extra-autoconf-configure-arg" ]);
426 ok ( ! -e 'autoconf/bld', "autoconf/bld got deleted too" );
429 system("rm", "-rf", $tmpdir);
430 system("$TOPDIR/dh_clean");