1
|
|
|
# pylint: disable=no-self-use |
|
|
|
|
2
|
|
|
|
3
|
|
|
from unittest.mock import patch, Mock |
4
|
|
|
|
5
|
|
|
import pytest |
6
|
|
|
|
7
|
|
|
from gdm import shell |
8
|
|
|
from gdm.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
|
|
|
@patch('gdm.shell.Command') |
24
|
|
|
def test_other(self, mock_command): |
25
|
|
|
"""Verify directories are changed correctly.""" |
26
|
|
|
shell.call('mock_program') |
27
|
|
|
mock_command.assert_called_once_with('mock_program') |
28
|
|
|
|
29
|
|
|
def test_other_error_uncaught(self): |
30
|
|
|
"""Verify program errors raise exceptions.""" |
31
|
|
|
with pytest.raises(ShellError): |
32
|
|
|
shell.call('git', '--invalid-git-argument') |
33
|
|
|
|
34
|
|
|
def test_other_error_ignored(self): |
35
|
|
|
"""Verify program errors can be ignored.""" |
36
|
|
|
shell.call('git', '--invalid-git-argument', _ignore=True) |
37
|
|
|
|
38
|
|
|
def test_other_capture(self): |
39
|
|
|
"""Verify a program's output can be captured.""" |
40
|
|
|
stdout = shell.call('echo', 'Hello, world!\n', _capture=True) |
41
|
|
|
assert "Hello, world!" == stdout |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
@patch('gdm.shell.call') |
45
|
|
|
class TestPrograms: |
46
|
|
|
|
47
|
|
|
"""Tests for calls to shell programs.""" |
48
|
|
|
|
49
|
|
|
def test_mkdir(self, mock_call): |
50
|
|
|
"""Verify the commands to create directories.""" |
51
|
|
|
shell.mkdir('mock/dir/path') |
52
|
|
|
assert_calls(mock_call, ["mkdir -p mock/dir/path"]) |
53
|
|
|
|
54
|
|
|
def test_cd(self, mock_call): |
55
|
|
|
"""Verify the commands to change directories.""" |
56
|
|
|
shell.cd('mock/dir/path') |
57
|
|
|
assert_calls(mock_call, ["cd mock/dir/path"]) |
58
|
|
|
|
59
|
|
|
@patch('os.path.isdir', Mock(return_value=True)) |
60
|
|
|
def test_ln(self, mock_call): |
61
|
|
|
"""Verify the commands to create symbolic links.""" |
62
|
|
|
shell.ln('mock/target', 'mock/source') |
63
|
|
|
assert_calls(mock_call, ["ln -s mock/target mock/source"]) |
64
|
|
|
|
65
|
|
|
@patch('os.path.isdir', Mock(return_value=False)) |
66
|
|
|
def test_ln_missing_parent(self, mock_call): |
67
|
|
|
"""Verify the commands to create symbolic links (missing parent).""" |
68
|
|
|
shell.ln('mock/target', 'mock/source') |
69
|
|
|
assert_calls(mock_call, ["mkdir -p mock", |
70
|
|
|
"ln -s mock/target mock/source"]) |
71
|
|
|
|
72
|
|
|
def test_rm(self, mock_call): |
73
|
|
|
"""Verify the commands to delete files/folders.""" |
74
|
|
|
shell.rm('mock/dir/path') |
75
|
|
|
assert_calls(mock_call, ["rm -rf mock/dir/path"]) |
76
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.