Completed
Pull Request — master (#68)
by Jace
03:27
created

TestFindConfigPath   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 34
rs 10
wmc 9
1
# pylint: disable=misplaced-comparison-constant,no-self-use
2
3
import os
4
from unittest.mock import patch, Mock
5
6
import pytest
7
8
from mine import services
9
10
from mine.tests.conftest import FILES
11
12
13
class TestFindRoot:
14
15
    @patch('os.listdir', Mock(return_value=[]))
16
    @patch('os.getenv', Mock(return_value=True))
17
    def test_ci_workaround_enabled(self):
18
        root = services.find_root(top="mock/top")
19
        assert "mock/top" == root
20
21
    @patch('os.listdir', Mock(return_value=[]))
22
    @patch('os.getenv', Mock(return_value=False))
23
    def test_ci_workaround_disabled(self):
24
        with pytest.raises(EnvironmentError):
25
            services.find_root(top="mock/top")
26
27
28
class TestFindConfigPath:
29
30
    def test_find_dropbox(self, tmpdir):
31
        """Verify a settings file can be found in Dropbox."""
32
        tmpdir.chdir()
33
        _touch('Dropbox', 'mine.yml')
34
35
        path = services.find_config_path(tmpdir.strpath)
36
37
        assert os.path.isfile(path)
38
39
    def test_find_dropbox_personal(self, tmpdir):
40
        """Verify a settings file can be found in Dropbox (Personal)."""
41
        tmpdir.chdir()
42
        _touch('Dropbox (Personal)', 'mine.yml')
43
44
        path = services.find_config_path(tmpdir.strpath)
45
46
        assert os.path.isfile(path)
47
48
    @patch('mine.services.DEPTH', 2)
49
    def test_find_depth(self, tmpdir):
50
        """Verify a settings file is not found below the maximum depth."""
51
        tmpdir.chdir()
52
        _touch('Dropbox', 'a', 'b', 'mine.yml')
53
54
        with pytest.raises(OSError):
55
            services.find_config_path(tmpdir.strpath)
56
57
    def test_find_no_share(self):
58
        """Verify an error occurs when no service directory is found."""
59
        with pytest.raises(EnvironmentError):
60
            with patch('os.getenv', Mock(return_value=False)):
61
                services.find_config_path(FILES)
62
63
64
@patch('os.remove')
65
class TestDeleteConflicts:
66
67
    @staticmethod
68
    def _create_conflicts(tmpdir, count=2):
69
        tmpdir.chdir()
70
        root = str(tmpdir)
71
72
        for index in range(count):
73
            fmt = "{} (Jace's conflicted copy 2015-03-11).fake"
74
            filename = fmt.format(index)
75
            _touch(root, filename)
76
77
        return root
78
79
    def test_fail_when_leftover_conflicts(self, _, tmpdir):
80
        root = self._create_conflicts(tmpdir)
81
82
        result = services.delete_conflicts(root)
83
84
        assert False is result
85
86
    def test_pass_when_no_conflicts(self, _, tmpdir):
87
        root = self._create_conflicts(tmpdir, count=0)
88
89
        result = services.delete_conflicts(root)
90
91
        assert True is result
92
93
    def test_no_deletion_without_force(self, mock_remove, tmpdir):
94
        root = self._create_conflicts(tmpdir)
95
96
        services.delete_conflicts(root, force=False)
97
98
        assert 0 == mock_remove.call_count
99
100
    def test_deletion_count_is_correct(self, mock_remove, tmpdir):
101
        root = self._create_conflicts(tmpdir, count=2)
102
103
        services.delete_conflicts(root, force=True)
104
105
        assert 2 == mock_remove.call_count
106
107
108
def _touch(*parts):
109
    """Create an empty file at the given path."""
110
    path = os.path.join(*parts)
111
    dirpath = os.path.dirname(path)
112
    if not os.path.isdir(dirpath):
113
        os.makedirs(dirpath)
114
    open(path, 'w').close()
115