diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2020-11-02 15:57:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2020-11-02 15:57:20 +0000 |
commit | c0456228aa67dadc0b1d28712b4b94a7d1f40054 (patch) | |
tree | 95d184e36876ec4e1539fa6806409f458762b270 /tests | |
parent | Releasing debian version 0.10.10-1. (diff) | |
download | gita-c0456228aa67dadc0b1d28712b4b94a7d1f40054.tar.xz gita-c0456228aa67dadc0b1d28712b4b94a7d1f40054.zip |
Merging upstream version 0.11.9.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_main.py | 223 | ||||
-rw-r--r-- | tests/test_utils.py | 30 | ||||
-rw-r--r-- | tests/xx.context | 0 |
3 files changed, 204 insertions, 49 deletions
diff --git a/tests/test_main.py b/tests/test_main.py index ff28111..1c30eec 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,22 +1,28 @@ import pytest -from unittest.mock import patch +from unittest.mock import patch, mock_open +from pathlib import Path import argparse import shlex from gita import __main__ -from gita import utils +from gita import utils, info from conftest import ( PATH_FNAME, PATH_FNAME_EMPTY, PATH_FNAME_CLASH, GROUP_FNAME, - async_mock + async_mock, TEST_DIR, ) class TestLsLl: - @patch('gita.utils.get_config_fname') + @patch('gita.common.get_config_fname') def testLl(self, mock_path_fname, capfd, tmp_path): - """ functional test """ + """ + functional test + """ # avoid modifying the local configuration - mock_path_fname.return_value = tmp_path / 'path_config.txt' + def side_effect(input): + return tmp_path / f'{input}.txt' + #mock_path_fname.return_value = tmp_path / 'path_config.txt' + mock_path_fname.side_effect = side_effect __main__.main(['add', '.']) out, err = capfd.readouterr() assert err == '' @@ -34,6 +40,14 @@ class TestLsLl: out, err = capfd.readouterr() assert err == '' assert 'gita' in out + assert info.Color.end in out + + # no color on branch name + __main__.main(['ll', '-n']) + out, err = capfd.readouterr() + assert err == '' + assert 'gita' in out + assert info.Color.end not in out __main__.main(['ls', 'gita']) out, err = capfd.readouterr() @@ -65,10 +79,14 @@ class TestLsLl: @patch('gita.info.get_head', return_value="master") @patch('gita.info._get_repo_status', return_value=("d", "s", "u", "c")) @patch('gita.info.get_commit_msg', return_value="msg") - @patch('gita.utils.get_config_fname') + @patch('gita.common.get_config_fname') def testWithPathFiles(self, mock_path_fname, _0, _1, _2, _3, path_fname, expected, capfd): - mock_path_fname.return_value = path_fname + def side_effect(input): + if input == 'repo_path': + return path_fname + return f'/{input}' + mock_path_fname.side_effect = side_effect utils.get_repos.cache_clear() __main__.main(['ll']) out, err = capfd.readouterr() @@ -78,7 +96,7 @@ class TestLsLl: @patch('os.path.isfile', return_value=True) -@patch('gita.utils.get_config_fname', return_value='some path') +@patch('gita.common.get_config_fname', return_value='some path') @patch('gita.utils.get_repos', return_value={'repo1': '/a/', 'repo2': '/b/'}) @patch('gita.utils.write_to_repo_file') def test_rm(mock_write, *_): @@ -131,34 +149,133 @@ def test_superman(mock_run, _, input): mock_run.assert_called_once_with(expected_cmds, cwd='path7') -@pytest.mark.parametrize('input, expected', [ - ('a', {'xx': ['b'], 'yy': ['c', 'd']}), - ("c", {'xx': ['a', 'b'], 'yy': ['a', 'd']}), - ("a b", {'yy': ['c', 'd']}), -]) -@patch('gita.utils.get_repos', return_value={'a': '', 'b': '', 'c': '', 'd': ''}) -@patch('gita.utils.get_config_fname', return_value=GROUP_FNAME) -@patch('gita.utils.write_to_groups_file') -def test_ungroup(mock_write, _, __, input, expected): - utils.get_groups.cache_clear() - args = ['ungroup'] + shlex.split(input) - __main__.main(args) - mock_write.assert_called_once_with(expected, 'w') +class TestContext: + @patch('gita.utils.get_context', return_value=None) + def testDisplayNoContext(self, _, capfd): + __main__.main(['context']) + out, err = capfd.readouterr() + assert err == '' + assert 'Context is not set\n' == out -@patch('gita.utils.get_config_fname', return_value=GROUP_FNAME) -def test_group_display(_, capfd): - args = argparse.Namespace() - args.to_group = None - utils.get_groups.cache_clear() - __main__.f_group(args) - out, err = capfd.readouterr() - assert err == '' - assert 'xx: a b\nyy: a c d\n' == out + @patch('gita.utils.get_context', return_value=Path('gname.context')) + @patch('gita.utils.get_groups', return_value={'gname': ['a', 'b']}) + def testDisplayContext(self, _, __, capfd): + __main__.main(['context']) + out, err = capfd.readouterr() + assert err == '' + assert 'gname: a b\n' == out + + @patch('gita.utils.get_context') + def testReset(self, mock_ctx): + __main__.main(['context', 'none']) + mock_ctx.return_value.unlink.assert_called() + + @patch('gita.utils.get_context', return_value=None) + @patch('gita.common.get_config_dir', return_value=TEST_DIR) + @patch('gita.utils.get_groups', return_value={'lala': ['b'], 'kaka': []}) + def testSetFirstTime(self, *_): + ctx = TEST_DIR / 'lala.context' + assert not ctx.is_file() + __main__.main(['context', 'lala']) + assert ctx.is_file() + ctx.unlink() + + @patch('gita.common.get_config_dir', return_value=TEST_DIR) + @patch('gita.utils.get_groups', return_value={'lala': ['b'], 'kaka': []}) + @patch('gita.utils.get_context') + def testSetSecondTime(self, mock_ctx, *_): + __main__.main(['context', 'kaka']) + mock_ctx.return_value.rename.assert_called() + + +class TestGroupCmd: + + @patch('gita.common.get_config_fname', return_value=GROUP_FNAME) + def testLs(self, _, capfd): + args = argparse.Namespace() + args.to_group = None + args.group_cmd = 'ls' + utils.get_groups.cache_clear() + __main__.f_group(args) + out, err = capfd.readouterr() + assert err == '' + assert 'xx yy\n' == out + + @patch('gita.common.get_config_fname', return_value=GROUP_FNAME) + def testLl(self, _, capfd): + args = argparse.Namespace() + args.to_group = None + args.group_cmd = None + utils.get_groups.cache_clear() + __main__.f_group(args) + out, err = capfd.readouterr() + assert err == '' + assert 'xx: a b\nyy: a c d\n' == out + + @patch('gita.common.get_config_fname', return_value=GROUP_FNAME) + @patch('gita.utils.write_to_groups_file') + def testRename(self, mock_write, _): + args = argparse.Namespace() + args.gname = 'xx' + args.new_name = 'zz' + args.group_cmd = 'rename' + utils.get_groups.cache_clear() + __main__.f_group(args) + expected = {'yy': ['a', 'c', 'd'], 'zz': ['a', 'b']} + mock_write.assert_called_once_with(expected, 'w') + + @patch('gita.common.get_config_fname', return_value=GROUP_FNAME) + def testRenameError(self, *_): + args = argparse.Namespace() + args.gname = 'xx' + args.new_name = 'yy' + args.group_cmd = 'rename' + utils.get_groups.cache_clear() + with pytest.raises(SystemExit, match='yy already exists.'): + __main__.f_group(args) + + @pytest.mark.parametrize('input, expected', [ + ('xx', {'yy': ['a', 'c', 'd']}), + ("xx yy", {}), + ]) + @patch('gita.utils.get_repos', return_value={'a': '', 'b': '', 'c': '', 'd': ''}) + @patch('gita.common.get_config_fname', return_value=GROUP_FNAME) + @patch('gita.utils.write_to_groups_file') + def testRm(self, mock_write, _, __, input, expected): + utils.get_groups.cache_clear() + args = ['group', 'rm'] + shlex.split(input) + __main__.main(args) + mock_write.assert_called_once_with(expected, 'w') + + @patch('gita.utils.get_repos', return_value={'a': '', 'b': '', 'c': '', 'd': ''}) + @patch('gita.common.get_config_fname', return_value=GROUP_FNAME) + @patch('gita.utils.write_to_groups_file') + def testAdd(self, mock_write, *_): + args = argparse.Namespace() + args.to_group = ['a', 'c'] + args.group_cmd = 'add' + args.gname = 'zz' + utils.get_groups.cache_clear() + __main__.f_group(args) + mock_write.assert_called_once_with({'zz': ['a', 'c']}, 'a+') + + @patch('gita.utils.get_repos', return_value={'a': '', 'b': '', 'c': '', 'd': ''}) + @patch('gita.common.get_config_fname', return_value=GROUP_FNAME) + @patch('gita.utils.write_to_groups_file') + def testAddToExisting(self, mock_write, *_): + args = argparse.Namespace() + args.to_group = ['a', 'c'] + args.group_cmd = 'add' + args.gname = 'xx' + utils.get_groups.cache_clear() + __main__.f_group(args) + mock_write.assert_called_once_with( + {'xx': ['a', 'b', 'c'], 'yy': ['a', 'c', 'd']}, 'w') @patch('gita.utils.is_git', return_value=True) -@patch('gita.utils.get_config_fname', return_value=PATH_FNAME) +@patch('gita.common.get_config_fname', return_value=PATH_FNAME) @patch('gita.utils.rename_repo') def test_rename(mock_rename, _, __): utils.get_repos.cache_clear() @@ -170,9 +287,39 @@ def test_rename(mock_rename, _, __): 'repo1', 'abc') -@patch('os.path.isfile', return_value=False) -def test_info(mock_isfile, capfd): - __main__.f_info(None) - out, err = capfd.readouterr() - assert 'In use: branch,commit_msg\nUnused: path\n' == out - assert err == '' +class TestInfo: + + @patch('gita.common.get_config_fname', return_value='') + def testLl(self, _, capfd): + args = argparse.Namespace() + args.info_cmd = None + __main__.f_info(args) + out, err = capfd.readouterr() + assert 'In use: branch,commit_msg\nUnused: path\n' == out + assert err == '' + + @patch('gita.common.get_config_fname', return_value='') + @patch('yaml.dump') + def testAdd(self, mock_dump, _): + args = argparse.Namespace() + args.info_cmd = 'add' + args.info_item = 'path' + with patch('builtins.open', mock_open(), create=True): + __main__.f_info(args) + mock_dump.assert_called_once() + args, kwargs = mock_dump.call_args + assert args[0] == ['branch', 'commit_msg', 'path'] + assert kwargs == {'default_flow_style': None} + + @patch('gita.common.get_config_fname', return_value='') + @patch('yaml.dump') + def testRm(self, mock_dump, _): + args = argparse.Namespace() + args.info_cmd = 'rm' + args.info_item = 'commit_msg' + with patch('builtins.open', mock_open(), create=True): + __main__.f_info(args) + mock_dump.assert_called_once() + args, kwargs = mock_dump.call_args + assert args[0] == ['branch'] + assert kwargs == {'default_flow_style': None} diff --git a/tests/test_utils.py b/tests/test_utils.py index 3128041..886ddb9 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -4,17 +4,14 @@ from unittest.mock import patch, mock_open from gita import utils, info from conftest import ( - PATH_FNAME, PATH_FNAME_EMPTY, PATH_FNAME_CLASH, GROUP_FNAME, + PATH_FNAME, PATH_FNAME_EMPTY, PATH_FNAME_CLASH, GROUP_FNAME, TEST_DIR, ) @pytest.mark.parametrize('test_input, diff_return, expected', [ - ({ - 'abc': '/root/repo/' - }, True, 'abc \x1b[31mrepo *+_ \x1b[0m msg'), - ({ - 'repo': '/root/repo2/' - }, False, 'repo \x1b[32mrepo _ \x1b[0m msg'), + ([{'abc': '/root/repo/'}, False], True, 'abc \x1b[31mrepo *+_ \x1b[0m msg'), + ([{'abc': '/root/repo/'}, True], True, 'abc repo *+_ msg'), + ([{'repo': '/root/repo2/'}, False], False, 'repo \x1b[32mrepo _ \x1b[0m msg'), ]) def test_describe(test_input, diff_return, expected, monkeypatch): monkeypatch.setattr(info, 'get_head', lambda x: 'repo') @@ -23,8 +20,8 @@ def test_describe(test_input, diff_return, expected, monkeypatch): monkeypatch.setattr(info, 'has_untracked', lambda: True) monkeypatch.setattr('os.chdir', lambda x: None) print('expected: ', repr(expected)) - print('got: ', repr(next(utils.describe(test_input)))) - assert expected == next(utils.describe(test_input)) + print('got: ', repr(next(utils.describe(*test_input)))) + assert expected == next(utils.describe(*test_input)) @pytest.mark.parametrize('path_fname, expected', [ @@ -41,17 +38,28 @@ def test_describe(test_input, diff_return, expected, monkeypatch): }), ]) @patch('gita.utils.is_git', return_value=True) -@patch('gita.utils.get_config_fname') +@patch('gita.common.get_config_fname') def test_get_repos(mock_path_fname, _, path_fname, expected): mock_path_fname.return_value = path_fname utils.get_repos.cache_clear() assert utils.get_repos() == expected +@patch('gita.common.get_config_dir') +def test_get_context(mock_config_dir): + mock_config_dir.return_value = TEST_DIR + utils.get_context.cache_clear() + assert utils.get_context() == TEST_DIR / 'xx.context' + + mock_config_dir.return_value = '/' + utils.get_context.cache_clear() + assert utils.get_context() == None + + @pytest.mark.parametrize('group_fname, expected', [ (GROUP_FNAME, {'xx': ['a', 'b'], 'yy': ['a', 'c', 'd']}), ]) -@patch('gita.utils.get_config_fname') +@patch('gita.common.get_config_fname') def test_get_groups(mock_group_fname, group_fname, expected): mock_group_fname.return_value = group_fname utils.get_groups.cache_clear() diff --git a/tests/xx.context b/tests/xx.context new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/xx.context |