From a6413f5d88f12466b3daa833668d0f59fc65ece4 Mon Sep 17 00:00:00 2001 From: Jason Chang Date: Wed, 26 Jul 2023 13:23:40 -0700 Subject: Update errors to extend BaseRepoError In order to better analyze and track repo errors, repo command failures need to be tied to specific errors in repo source code. Additionally a new GitCommandError was added to differentiate between general git related errors to failed git commands. Git commands that opt into verification will raise a GitCommandError if the command failed. The first step in this process is a general error refactoring Bug: b/293344017 Change-Id: I46944b1825ce892757c8dd3f7e2fab7e460760c0 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/380994 Commit-Queue: Jason Chang Reviewed-by: Aravind Vasudevan Tested-by: Jason Chang Reviewed-by: Joanna Wang --- tests/test_git_command.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'tests/test_git_command.py') diff --git a/tests/test_git_command.py b/tests/test_git_command.py index c4c3a4c5..1e8beabc 100644 --- a/tests/test_git_command.py +++ b/tests/test_git_command.py @@ -16,6 +16,7 @@ import re import os +import subprocess import unittest try: @@ -65,6 +66,56 @@ class GitCommandTest(unittest.TestCase): ) +class GitCommandWaitTest(unittest.TestCase): + """Tests the GitCommand class .Wait()""" + + def setUp(self): + class MockPopen(object): + rc = 0 + + def communicate( + self, input: str = None, timeout: float = None + ) -> [str, str]: + """Mock communicate fn.""" + return ["", ""] + + def wait(self, timeout=None): + return self.rc + + self.popen = popen = MockPopen() + + def popen_mock(*args, **kwargs): + return popen + + def realpath_mock(val): + return val + + mock.patch.object(subprocess, "Popen", side_effect=popen_mock).start() + + mock.patch.object( + os.path, "realpath", side_effect=realpath_mock + ).start() + + def tearDown(self): + mock.patch.stopall() + + def test_raises_when_verify_non_zero_result(self): + self.popen.rc = 1 + r = git_command.GitCommand(None, ["status"], verify_command=True) + with self.assertRaises(git_command.GitCommandError): + r.Wait() + + def test_returns_when_no_verify_non_zero_result(self): + self.popen.rc = 1 + r = git_command.GitCommand(None, ["status"], verify_command=False) + self.assertEqual(1, r.Wait()) + + def test_default_returns_non_zero_result(self): + self.popen.rc = 1 + r = git_command.GitCommand(None, ["status"]) + self.assertEqual(1, r.Wait()) + + class GitCallUnitTest(unittest.TestCase): """Tests the _GitCall class (via git_command.git).""" -- cgit v1.2.3-54-g00ecf