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

WIP Extend test coverage of openQA::Git #5851

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ test-fullstack-unstable: node_modules
# we have apparently-redundant -I args in PERL5OPT here because Docker
# only works with one and Fedora's build system only works with the other
.PHONY: test-with-database
test-with-database: node_modules
test -d $(TEST_PG_PATH) && (pg_ctl -D $(TEST_PG_PATH) -s status >&/dev/null || pg_ctl -D $(TEST_PG_PATH) -s start) || ./t/test_postgresql $(TEST_PG_PATH)
test-with-database: node_modules setup-database
$(MAKE) test-unit-and-integration TEST_PG="DBI:Pg:dbname=openqa_test;host=$(TEST_PG_PATH)"
-[ $(KEEP_DB) = 1 ] || pg_ctl -D $(TEST_PG_PATH) stop

Expand All @@ -232,6 +231,10 @@ test-unit-and-integration: node_modules
export PERL5OPT="$(COVEROPT)$(PERL5OPT) -It/lib -I$(PWD)/t/lib -I$(PWD)/external/os-autoinst-common/lib -MOpenQA::Test::PatchDeparse";\
RETRY=${RETRY} HOOK=./tools/delete-coverdb-folder timeout -s SIGINT -k 5 -v ${TIMEOUT_RETRIES} tools/retry prove ${PROVE_LIB_ARGS} ${PROVE_ARGS}

.PHONY: setup-database
setup-database:
test -d $(TEST_PG_PATH) && (pg_ctl -D $(TEST_PG_PATH) -s status >&/dev/null || pg_ctl -D $(TEST_PG_PATH) -s start) || ./t/test_postgresql $(TEST_PG_PATH)

# prepares running the tests within a container (eg. pulls os-autoinst) and then runs the tests considering
# the test matrix environment variables
# note: This is supposed to run within the container unlike `launch-container-to-run-tests-within`
Expand Down
44 changes: 44 additions & 0 deletions t/16-utils-git.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

use Test::Most;
use Test::Warnings ':report_warnings';
use Test::MockModule;
use Test::MockObject;
use FindBin;
use lib "$FindBin::Bin/lib", "$FindBin::Bin/../external/os-autoinst-common/lib";
use OpenQA::Test::TimeLimit '10';
use OpenQA::Git;

my $mock = Test::MockModule->new('OpenQA::Utils');
$mock->redefine('run_cmd_with_log_return_error', sub {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at t/16-utils-runcmd.t
run_cmd_with_log_return_error is an imported function, so it is already defined in OpenQA::Git when you create the OpenQA::Util mock.
Just do it like in t/16-utils-runcmd.t

Maybe the tests from there should be moved to your new test?
It would be a bit weird to have them in separate files.

my ($cmd) = @_;

if ($cmd->[1] eq 'push') {
# Simulate a failure when attempting to push
return { status => 0 };
}
# Simulate success for other git commands ('add', 'commit')
return { status => 1 };
});

my $mock_app = Test::MockObject->new();
$mock_app->set_always('config', {
'global' => { 'scm' => 'git' },
'scm git' => { 'do_push' => 'yes' }
});

my $mock_user = Test::MockObject->new();
$mock_user->set_always('fullname', 'Test User');
$mock_user->set_always('email', '[email protected]');

my $git = OpenQA::Git->new(app => $mock_app, dir => '.', user => $mock_user);

my $result = $git->commit({
add => ['t/16-utils-git.t'],
message => 'Test commit message',
});

is $result, 'Unable to push Git commit', 'Commit method correctly handles push failure';

done_testing;
Loading