Completed
Pull Request — develop (#141)
by Jace
02:42
created

TestCall.test_other_capture()   A

Complexity

Conditions 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 4
c 3
b 1
f 0
dl 0
loc 8
rs 9.2
1
# pylint: disable=no-self-use,misplaced-comparison-constant
2
3
import os
4
5
from unittest.mock import patch, Mock
6
7
import pytest
8
from gitman import shell
9
from gitman.exceptions import ShellError
10
11
from . import assert_calls
12
13
14
class TestCall:
15
    """Tests for interacting with the shell."""
16
17
    def test_other_error_uncaught(self):
18
        """Verify program errors raise exceptions."""
19
        with pytest.raises(ShellError):
20
            shell.call('git', '--invalid-git-argument')
21
22
    def test_other_error_ignored(self):
23
        """Verify program errors can be ignored."""
24
        shell.call('git', '--invalid-git-argument', _ignore=True)
25
26
    def test_other_capture(self):
27
        """Verify a program's output can be captured."""
28
        if os.name == 'nt':
29
            stdout = shell.call('echo', 'Hello, world!', _shell=True)
30
            assert '"Hello, world!"' == stdout
31
        else:
32
            stdout = shell.call('echo', 'Hello, world!\n')
33
            assert "Hello, world!" == stdout
34
35
36
@patch('gitman.shell.call')
37
class TestPrograms:
38
    """Tests for calls to shell programs."""
39
40
    @patch('os.makedirs')
41
    def test_mkdir(self, mock_makedirs, mock_call):
42
        """Verify the commands to create directories."""
43
        shell.mkdir('mock/dirpath')
44
        mock_makedirs.assert_called_once_with('mock/dirpath')
45
        assert_calls(mock_call, [])
46
47
    @patch('os.chdir')
48
    def test_cd(self, mock_chdir, mock_call):
49
        """Verify the commands to change directories."""
50
        shell.cd('mock/dirpath')
51
        mock_chdir.assert_called_once_with('mock/dirpath')
52
        assert_calls(mock_call, [])
53
54
    @patch('os.path.isdir', Mock(return_value=True))
55
    @pytest.mark.skipif(os.name == 'nt', reason="no symlink on Windows")
56
    def test_ln(self, mock_call):
57
        """Verify the commands to create symbolic links."""
58
        shell.ln('mock/target', 'mock/source')
59
        assert_calls(mock_call, ["ln -s mock/target mock/source"])
60
61
    @patch('os.path.isdir', Mock(return_value=False))
62
    @pytest.mark.skipif(os.name == 'nt', reason="no symlink on Windows")
63
    def test_ln_missing_parent(self, mock_call):
64
        """Verify the commands to create symbolic links (missing parent)."""
65
        shell.ln('mock/target', 'mock/source')
66
        assert_calls(mock_call, ["ln -s mock/target mock/source"])
67
68
    @patch('os.remove')
69
    @patch('os.path.exists', Mock(return_value=True))
70
    def test_rm_file(self, mock_remove, mock_call):
71
        """Verify the commands to delete files."""
72
        shell.rm('mock/path')
73
        mock_remove.assert_called_once_with('mock/path')
74
        assert_calls(mock_call, [])
75
76
    @patch('shutil.rmtree')
77
    @patch('os.path.exists', Mock(return_value=True))
78
    @patch('os.path.isdir', Mock(return_value=True))
79
    def test_rm_directory(self, mock_rmtree, mock_call):
80
        """Verify the commands to delete directories."""
81
        shell.rm('mock/dirpath')
82
        mock_rmtree.assert_called_once_with('mock/dirpath')
83
        assert_calls(mock_call, [])
84