-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_initializer.py
57 lines (45 loc) · 2.01 KB
/
test_initializer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import unittest
from unittest.mock import patch, mock_open, call
import subprocess
from not_gitmodules import initializer
from not_gitmodules.cli import cli
class TestInitializerFunction(unittest.TestCase):
@patch(
"builtins.open",
new_callable=mock_open,
read_data="file_manager: https://github.com/Armen-Jean-Andreasian/FileManager-Git-Module",
)
@patch("not_gitmodules.core.read_yaml")
def test_initializer_with_valid_yaml(self, mock_read_yaml, mock_file):
mock_read_yaml.return_value = {
"file_manager": "https://github.com/Armen-Jean-Andreasian/FileManager-Git-Module"
}
initializer("notgitmodules.yaml")
mock_read_yaml.assert_called_once_with("notgitmodules.yaml")
@patch("builtins.open", new_callable=mock_open, read_data="file_manager: invalid_repo")
@patch("not_gitmodules.core.read_yaml")
@patch("builtins.print")
@patch("subprocess.run")
def test_initializer_with_invalid_yaml(self, mock_subprocess, mock_print, mock_read_yaml, mock_file):
mock_read_yaml.return_value = {"file_manager": "invalid_repo"}
mock_subprocess.side_effect = subprocess.CalledProcessError(
returncode=1, cmd="git clone", stderr="error"
)
initializer("notgitmodules.yaml")
expected_calls = [
call("Directory 'my_gitmodules/file_manager' already exists. Skipping..."),
call("Failed to clone invalid_repo: error"),
]
self.assertTrue(any(call in mock_print.mock_calls for call in expected_calls))
@patch(
"builtins.open",
new_callable=mock_open,
read_data="file_manager: https://github.com/Armen-Jean-Andreasian/FileManager-Git-Module",
)
@patch("not_gitmodules.core.read_yaml")
def test_cli_with_default_input(self, mock_read_yaml, mock_file):
with patch("builtins.input", return_value=""):
cli()
mock_read_yaml.assert_called_once_with("notgitmodules.yaml")
if __name__ == "__main__":
unittest.main()