Skip to content

Commit

Permalink
Extend test coverage of OpenQA::Git
Browse files Browse the repository at this point in the history
  • Loading branch information
r-richardson committed Aug 15, 2024
1 parent 3f88197 commit e933443
Showing 1 changed file with 46 additions and 29 deletions.
75 changes: 46 additions & 29 deletions t/16-utils-runcmd.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use Test::Most;
use FindBin;
use lib "$FindBin::Bin/lib", "$FindBin::Bin/../external/os-autoinst-common/lib";
use Data::Dumper;
use OpenQA::Git;
#use OpenQA::Git;
use OpenQA::Utils;
use OpenQA::Task::Needle::Save;
use OpenQA::Test::Case;
use OpenQA::Test::TimeLimit '10';
use Mojo::File 'tempdir';
Expand Down Expand Up @@ -38,39 +37,26 @@ subtest 'run (arbitrary) command' => sub {
ok(!$res->{status}, 'status not ok (non-zero status returned)');
};

subtest 'make git commit (error handling)' => sub {
throws_ok(
sub {
OpenQA::Git->new({app => $t->app, dir => 'foo/bar'})->commit();
},
qr/no user specified/,
'OpenQA::Git throws an exception if parameter missing'
);

my $empty_tmp_dir = tempdir();
my $git = OpenQA::Git->new({app => $t->app, dir => $empty_tmp_dir, user => $first_user});
my $res;
stdout_like { $res = $git->commit({cmd => 'status', message => 'test'}) }
qr/.*\[warn\].*fatal: Not a git repository/i, 'git message found';
like $res, qr'^Unable to commit via Git: fatal: (N|n)ot a git repository \(or any', 'Git error message returned';
};

# setup mocking
my @executed_commands;
my $utils_mock = Test::MockModule->new('OpenQA::Git');
my %mock_return_value = (
status => 1,
stderr => undef,
);
$utils_mock->redefine(
run_cmd_with_log_return_error => sub {
my ($cmd) = @_;
push(@executed_commands, $cmd);
return \%mock_return_value;
});

subtest 'git commands with mocked run_cmd_with_log_return_error' => sub {
sub _run_cmd_mock{
my ($cmd) = @_;
push(@executed_commands, $cmd);
return \%mock_return_value;
}

subtest 'git commands with mocked run_cmd_with_log_return_error
' => sub {
# check default config
my $utils_mock = Test::MockModule->new('OpenQA::Git');
$utils_mock->redefine(
run_cmd_with_log_return_error => \&_run_cmd_mock
);
my $git = OpenQA::Git->new(app => $t->app, dir => 'foo/bar', user => $first_user);
is($git->app, $t->app, 'app is set');
is($git->dir, 'foo/bar', 'dir is set');
Expand Down Expand Up @@ -128,7 +114,7 @@ subtest 'git commands with mocked run_cmd_with_log_return_error' => sub {
is(
$git->dir('/repo/path')->commit(
{
message => 'some test',
message => 'add rm test',
add => [qw(foo.png foo.json)],
rm => [qw(bar.png bar.json)],
}
Expand All @@ -143,16 +129,47 @@ subtest 'git commands with mocked run_cmd_with_log_return_error' => sub {
[qw(git -C /repo/path rm bar.png bar.json)],
[
qw(git -C /repo/path commit -q -m),
'some test',
'add rm test',
'--author=openQA system user <[email protected]>',
qw(foo.png foo.json bar.png bar.json)
],
],
'changes staged and committed',
) or diag explain \@executed_commands;

$git->config->{do_push} = 'yes';

local $mock_return_value{status} = 1;
local $mock_return_value{stderr} = 'mocked push error';
local $mock_return_value{stdout} = '';

$utils_mock->redefine(
run_cmd_with_log_return_error => sub {
my ($cmd) = @_;
push(@executed_commands, $cmd);
if ($cmd->[3] eq 'push') {
$mock_return_value{status} = 0;
}
return \%mock_return_value;
});
is(
$git->commit(
{
message => 'failed push test',
}
),
'Unable to push Git commit: mocked push error',
'an error occurred during push'
);

$git->config->{do_push} = '';
};

subtest 'saving needle via Git' => sub {
my $utils_mock = Test::MockModule->new('OpenQA::Git');
$utils_mock->redefine(
run_cmd_with_log_return_error => \&_run_cmd_mock
);
{
package Test::FakeMinionJob; # uncoverable statement
sub finish { }
Expand Down

0 comments on commit e933443

Please sign in to comment.