|
1
|
|
|
# pylint: disable=no-self-use |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
from unittest.mock import patch, Mock |
|
4
|
|
|
|
|
5
|
|
|
import pytest |
|
6
|
|
|
|
|
7
|
|
|
from gitman import shell |
|
8
|
|
|
from gitman.exceptions import ShellError |
|
9
|
|
|
|
|
10
|
|
|
from . import assert_calls |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class TestCall: |
|
14
|
|
|
|
|
15
|
|
|
"""Tests for interacting with the shell.""" |
|
16
|
|
|
|
|
17
|
|
|
@patch('os.chdir') |
|
18
|
|
|
def test_cd(self, mock_chdir): |
|
19
|
|
|
"""Verify directories are changed correctly.""" |
|
20
|
|
|
shell.call('cd', 'mock/dir') |
|
21
|
|
|
mock_chdir.assert_called_once_with('mock/dir') |
|
22
|
|
|
|
|
23
|
|
|
def test_other_error_uncaught(self): |
|
24
|
|
|
"""Verify program errors raise exceptions.""" |
|
25
|
|
|
with pytest.raises(ShellError): |
|
26
|
|
|
shell.call('git', '--invalid-git-argument') |
|
27
|
|
|
|
|
28
|
|
|
def test_other_error_ignored(self): |
|
29
|
|
|
"""Verify program errors can be ignored.""" |
|
30
|
|
|
shell.call('git', '--invalid-git-argument', _ignore=True) |
|
31
|
|
|
|
|
32
|
|
|
def test_other_capture(self): |
|
33
|
|
|
"""Verify a program's output can be captured.""" |
|
34
|
|
|
stdout = shell.call('echo', 'Hello, world!\n') |
|
35
|
|
|
assert "Hello, world!" == stdout |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
@patch('gitman.shell.call') |
|
39
|
|
|
class TestPrograms: |
|
40
|
|
|
|
|
41
|
|
|
"""Tests for calls to shell programs.""" |
|
42
|
|
|
|
|
43
|
|
|
def test_mkdir(self, mock_call): |
|
44
|
|
|
"""Verify the commands to create directories.""" |
|
45
|
|
|
shell.mkdir('mock/dir/path') |
|
46
|
|
|
assert_calls(mock_call, ["mkdir -p mock/dir/path"]) |
|
47
|
|
|
|
|
48
|
|
|
def test_cd(self, mock_call): |
|
49
|
|
|
"""Verify the commands to change directories.""" |
|
50
|
|
|
shell.cd('mock/dir/path') |
|
51
|
|
|
assert_calls(mock_call, ["cd mock/dir/path"]) |
|
52
|
|
|
|
|
53
|
|
|
@patch('os.path.isdir', Mock(return_value=True)) |
|
54
|
|
|
def test_ln(self, mock_call): |
|
55
|
|
|
"""Verify the commands to create symbolic links.""" |
|
56
|
|
|
shell.ln('mock/target', 'mock/source') |
|
57
|
|
|
assert_calls(mock_call, ["ln -s mock/target mock/source"]) |
|
58
|
|
|
|
|
59
|
|
|
@patch('os.path.isdir', Mock(return_value=False)) |
|
60
|
|
|
def test_ln_missing_parent(self, mock_call): |
|
61
|
|
|
"""Verify the commands to create symbolic links (missing parent).""" |
|
62
|
|
|
shell.ln('mock/target', 'mock/source') |
|
63
|
|
|
assert_calls(mock_call, ["mkdir -p mock", |
|
64
|
|
|
"ln -s mock/target mock/source"]) |
|
65
|
|
|
|
|
66
|
|
|
def test_rm(self, mock_call): |
|
67
|
|
|
"""Verify the commands to delete files/folders.""" |
|
68
|
|
|
shell.rm('mock/dir/path') |
|
69
|
|
|
assert_calls(mock_call, ["rm -rf mock/dir/path"]) |
|
70
|
|
|
|
Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.