diff options
author | Niels Thykier <niels@thykier.net> | 2017-07-02 10:49:21 +0000 |
---|---|---|
committer | Niels Thykier <niels@thykier.net> | 2017-07-02 10:49:21 +0000 |
commit | ea233bc63cbc19a7a732721d932169fe6def96df (patch) | |
tree | 6eff7a3611c3dd3ce13468f0dfc66e3247305755 /t | |
parent | 7de6ea56113fcf133cb1d25ac2f8cc40b7b0da0d (diff) | |
download | debhelper-ea233bc63cbc19a7a732721d932169fe6def96df.tar.gz |
Test::DH: Support tests requiring (fake)root
Signed-off-by: Niels Thykier <niels@thykier.net>
Diffstat (limited to 't')
-rw-r--r-- | t/Test/DH.pm | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/t/Test/DH.pm b/t/Test/DH.pm index fb3793c4..6abf41e2 100644 --- a/t/Test/DH.pm +++ b/t/Test/DH.pm @@ -32,9 +32,10 @@ use Debian::Debhelper::Dh_Lib; our @EXPORT = qw( each_compat_up_to_and_incl_subtest each_compat_subtest each_compat_from_and_above_subtest run_dh_tool + uid_0_test_is_ok ); -our $TEST_DH_COMPAT; +our ($TEST_DH_COMPAT, $ROOT_OK, $ROOT_CMD); my $START_DIR = cwd(); @@ -42,7 +43,15 @@ sub run_dh_tool { my (@cmd) = @_; my $compat = $TEST_DH_COMPAT; my $options = ref($cmd[0]) ? shift(@cmd) : {}; - my $pid = fork() // BAIL_OUT("fork failed: $!"); + my $pid; + + if ($options->{'needs_root'}) { + BAIL_OUT('BROKEN TEST - Attempt to run "needs_root" test when not possible') + if not uid_0_test_is_ok(); + unshift(@cmd, $ROOT_CMD) if defined($ROOT_CMD); + } + + $pid = fork() // BAIL_OUT("fork failed: $!"); if (not $pid) { $ENV{DH_COMPAT} = $compat; $ENV{DH_INTERNAL_TESTSUITE_SILENT_WARNINGS} = 1; @@ -60,6 +69,22 @@ sub run_dh_tool { return 0; } +sub uid_0_test_is_ok { + return $ROOT_OK if defined($ROOT_OK); + my $ok = 0; + if ($< == 0) { + $ok = 1; + } else { + system('fakeroot true 2>/dev/null'); + if ($? == 0) { + $ROOT_CMD = 'fakeroot'; + $ok = 1; + } + } + $ROOT_OK = $ok; + return $ok; +} + sub _prepare_test_root { my $dir = tempdir(CLEANUP => 1); if (not mkdir("$dir/debian", 0777)) { |