Skip to content

Commit

Permalink
Add Coverage for GitCloner.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Niweera committed Jan 21, 2022
1 parent e52e1a4 commit d6e8914
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions tests/test_GitCloner.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import shutil
from unittest import TestCase
from unittest.mock import patch
from unittest.mock import patch, Mock
from git import Repo
from clocer.CustomExceptions import CloneError
from clocer.GitCloner import GitCloner
from os.path import exists, abspath, join, dirname, realpath


class TestGitCloner(TestCase):
def setUp(self) -> None:
self.url = "https://github.com/Niweera/cpu-meter"

def tearDown(self) -> None:
GitCloner.clean_up()

def test_clone_repo(self):
actual_url = "https://github.com/Niweera/cpu-meter"
actual_url = self.url
GitCloner.clone_repo(actual_url)

clone_path = abspath(
Expand All @@ -23,9 +27,25 @@ def test_clone_repo(self):
test_url = cloned_repo.remotes[0].config_reader.get("url")
self.assertEqual(test_url, actual_url)

def test_clone_repo_fail(self):
temp_path: str = abspath(
join(dirname(dirname(realpath(__file__))), "clocer", "temp")
)
clone_path: str = abspath(join(temp_path, "clone"))

with patch("clocer.GitCloner.exists", Mock(return_value=True)):
if not exists(clone_path):
with self.assertRaises(CloneError):
GitCloner.clone_repo(self.url)

with patch("clocer.GitCloner.exists", Mock(return_value=False)):
if exists(temp_path):
with self.assertRaises(CloneError):
GitCloner.clone_repo(self.url)

def test_clean_up_fail(self):
actual_error_msg = "Mock error occurred"
with patch.object(GitCloner, "clean_up", return_value=None) as get_mock:
with self.assertRaises(CloneError):
get_mock.side_effect = CloneError(actual_error_msg)
GitCloner.clean_up()
with patch("clocer.GitCloner.exists", Mock(return_value=True)), patch.object(
shutil, "rmtree"
) as get_mock, self.assertRaises(CloneError):
get_mock.side_effect = Exception("Mock error occurred in shutil.rmtree()")
GitCloner.clean_up()

0 comments on commit d6e8914

Please sign in to comment.