Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend test coverage of OpenQA::Git #5854

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions t/16-utils-runcmd.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: GPL-2.0-or-later

use Test::Most;
use Mojo::Base -signatures;

use FindBin;
use lib "$FindBin::Bin/lib", "$FindBin::Bin/../external/os-autoinst-common/lib";
Expand Down Expand Up @@ -57,20 +58,20 @@ subtest 'make git commit (error handling)' => sub {

# 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;
});

sub _run_cmd_mock ($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 +129,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 +144,35 @@ 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 ($cmd) {
push @executed_commands, $cmd;
if ($cmd->[3] eq 'push') {
$mock_return_value{status} = 0;
}
return \%mock_return_value;
});
like $git->commit({message => 'failed push test'}), qr/Unable to push Git commit/, 'error handled 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
Loading