#!perl
use Cassandane::Tiny;

sub test_fork_noexec ($self)
{
    # need some not-executable file to test with, crash.c will do
    my $noexec_file = realpath('utils/crash.c');
    # it had better exist, and not be executable
    $self->assert_file_test($noexec_file, '-e');
    $self->assert_not_file_test($noexec_file, '-x');

    my $expect_pid = $$;
    my $expect_cwd = getcwd();

    # try to run it... the exec in the forked child process will fail
    eval {
        $self->{instance}->run_command({ cyrus => 0, }, $noexec_file);
    };
    my $e = $@;
    $self->assert_not_null($e);
    # the child process had better exit!
    $self->assert_matches(qr{exited with code 71}, $e->get_message());

    # this test had better still be running in the same process!
    $self->assert_num_equals($expect_pid, $$);

    # cwd better not have changed!
    $self->assert_str_equals($expect_cwd, getcwd());
}
