File: test_utils.py

package info (click to toggle)
python-trubar 0.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 716 kB
  • sloc: python: 2,968; sh: 375; makefile: 3; javascript: 1
file content (235 lines) | stat: -rw-r--r-- 9,569 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
from pathlib import PureWindowsPath

import unittest

from unittest.mock import patch, Mock

from trubar.utils import \
    walk_files, check_any_files, unique_name, dump_removed, make_list
from trubar.config import config
import trubar.tests.test_module


test_module_path = os.path.split(trubar.tests.test_module.__file__)[0]


class UtilsTest(unittest.TestCase):
    def test_walk_files(self):
        tmp = test_module_path
        old_pattern = config.exclude_pattern
        try:
            config.set_exclude_pattern("")
            self.assertEqual(
                set(walk_files(tmp, select=True)),
                {('bar_module/__init__.py',
                  f'{tmp}/bar_module/__init__.py'),
                 ('bar_module/foo_module/__init__.py',
                  f'{tmp}/bar_module/foo_module/__init__.py'),
                 ('baz_module/__init__.py',
                  f'{tmp}/baz_module/__init__.py'),
                 ('__init__.py',
                  f'{tmp}/__init__.py')}
            )

            old_path = os.getcwd()
            try:
                os.chdir(tmp)
                self.assertEqual(
                    set(walk_files(".", select=True)),
                    {('bar_module/__init__.py',
                      './bar_module/__init__.py'),
                     ('bar_module/foo_module/__init__.py',
                      './bar_module/foo_module/__init__.py'),
                     ('baz_module/__init__.py',
                      './baz_module/__init__.py'),
                     ('__init__.py',
                      './__init__.py')}
                )
                self.assertEqual(
                    set(walk_files(".", "bar", select=True)),
                    {('bar_module/__init__.py',
                      './bar_module/__init__.py'),
                     ('bar_module/foo_module/__init__.py',
                      './bar_module/foo_module/__init__.py')}
                )
            finally:
                os.chdir(old_path)

            self.assertEqual(
                set(walk_files(tmp, select=False)),
                {('bar_module/__init__.py',
                  f'{tmp}/bar_module/__init__.py'),
                 ('bar_module/foo_module/__init__.py',
                  f'{tmp}/bar_module/foo_module/__init__.py'),
                 ('baz_module/__init__.py',
                  f'{tmp}/baz_module/__init__.py'),
                 ('__init__.py',
                  f'{tmp}/__init__.py'),
                 ('baz_module/not-python.js',
                  f'{tmp}/baz_module/not-python.js')}
            )

            config.set_exclude_pattern("b?r_")
            self.assertEqual(
                set(walk_files(tmp, select=False)),
                {('bar_module/__init__.py',
                  f'{tmp}/bar_module/__init__.py'),
                 ('bar_module/foo_module/__init__.py',
                  f'{tmp}/bar_module/foo_module/__init__.py'),
                 ('baz_module/__init__.py',
                  f'{tmp}/baz_module/__init__.py'),
                 ('__init__.py',
                  f'{tmp}/__init__.py'),
                 ('baz_module/not-python.js',
                  f'{tmp}/baz_module/not-python.js')}
            )
            self.assertEqual(
                set(walk_files(tmp, select=True)),
                {('baz_module/__init__.py',
                  f'{tmp}/baz_module/__init__.py'),
                 ('__init__.py',
                  f'{tmp}/__init__.py')}
            )
        finally:
            config.set_exclude_pattern(old_pattern)

    @patch("os.walk",
           return_value=[(r"c:\foo\bar\ann\bert",
                          None,
                          ["cecil.py", "dan.txt", "emily.py"])]
           )
    @patch("os.path.join", new=lambda *c: "\\".join(c))
    @patch("trubar.utils.PurePath", new=PureWindowsPath)
    def test_walk_backslashes_on_windows(self, _):
        self.assertEqual(
            list(walk_files(r"c:\foo\bar", select=False)),
            [("ann/bert/cecil.py", r"c:\foo\bar\ann\bert\cecil.py"),
             ("ann/bert/dan.txt", r"c:\foo\bar\ann\bert\dan.txt"),
             ("ann/bert/emily.py", r"c:\foo\bar\ann\bert\emily.py")]
        )
        self.assertEqual(
            list(walk_files(r"c:\foo\bar", "l", select=False)),
            [("ann/bert/cecil.py", r"c:\foo\bar\ann\bert\cecil.py"),
             ("ann/bert/emily.py", r"c:\foo\bar\ann\bert\emily.py")])
        # Don't match pattern in path
        self.assertEqual(
            list(walk_files(r"c:\foo\bar", "o", select=False)),
            [])

    @patch("sys.exit")
    @patch("builtins.print")
    def test_check_any_files(self, print_, exit_):
        keys = "a/x.py a/y.py a/b/x.py a/b/z.py".split()
        translations = dict.fromkeys(keys)
        with patch("trubar.utils.walk_files",
                   Mock(return_value=[(k, k) for k in keys])):
            check_any_files(set(translations), "foo/bar")
            exit_.assert_not_called()
            print_.assert_not_called()

        with patch("trubar.utils.walk_files",
                   Mock(return_value=[("t/x/" + k, k) for k in keys])):
            check_any_files(set(translations), "foo/bar")
            exit_.assert_called()
            print_.assert_called()
            msg = print_.call_args[0][0]
            self.assertIn("-s foo/bar/t/x", msg)
            print_.reset_mock()
            exit_.reset_mock()

            home = os.path.expanduser("~/foo/bar")
            check_any_files(set(translations), home)
            exit_.assert_called()
            msg = print_.call_args[0][0]
            self.assertIn("-s ~/foo/bar/t/x", msg)
            print_.reset_mock()
            exit_.reset_mock()

            with patch("sys.platform", "win32"):
                check_any_files(set(translations), home)
                exit_.assert_called()
                print_.assert_called()
                msg = print_.call_args[0][0]
                print_.assert_called()
                self.assertIn(f"-s {home}/t/x", msg)
                print_.reset_mock()
                exit_.reset_mock()

        keys = "a/x.py a/y.py a/b/x.py a/b/z.py a/b/u.py".split()
        with patch("trubar.utils.walk_files",
                   Mock(return_value=[(k, k) for k in keys])):
            check_any_files({"x.py", "z.py", "u.py"}, "foo")
            exit_.assert_called()
            print_.assert_called()
            msg = print_.call_args[0][0]
            self.assertIn("-s foo/a/b", msg)
            print_.reset_mock()
            exit_.reset_mock()

        keys = "a/x.py a/y.py a/b/x.py a/b/z.py".split()
        with patch("trubar.utils.walk_files",
                   Mock(return_value=[(k, k) for k in keys])):
            check_any_files({"x.py", "z.py", "u.py"}, "foo")
            exit_.assert_called()
            print_.assert_called()
            msg = print_.call_args[0][0]
            self.assertNotIn("-s foo/a/b", msg)
            print_.reset_mock()
            exit_.reset_mock()

    def test_unique_name(self):
        with patch("os.path.exists", return_value=False):
            self.assertEqual(unique_name("some name.yaml"),
                             "some name.yaml")

        with patch("os.path.exists", return_value=True):
            with patch("os.listdir", return_value=["some name.yaml"]) as listdir:
                self.assertEqual(unique_name("some name.yaml"),
                                 "some name (1).yaml")
                listdir.assert_called_with(".")

                self.assertEqual(unique_name("abc/def/some name.yaml"),
                                 "abc/def/some name (1).yaml")
                listdir.assert_called_with("abc/def")

            with patch("os.listdir", return_value=["some name.yaml",
                                                   "some name (1).yaml",
                                                   "some name (2).yaml",
                                                   "non sequitur.yaml",
                                                   ]) as listdir:
                self.assertEqual(unique_name("some name.yaml"),
                                 "some name (3).yaml")
                listdir.assert_called_with(".")

            with patch("os.listdir", return_value=["some name.yaml",
                                                   "some name (1).yaml",
                                                   "non sequitur.yaml",
                                                   "some name (4).yaml",
                                                   ]) as listdir:
                self.assertEqual(unique_name("some name.yaml"),
                                 "some name (5).yaml")
                listdir.assert_called_with(".")

    @patch("trubar.utils.dump")
    def test_dump_removed(self, mock_dump):
        dump_removed({}, "removed.yaml", "abc/def/x.yaml")
        mock_dump.assert_not_called()

        msgs = Mock()
        dump_removed(msgs, "removed.yaml", "abc/def/x.yaml")
        mock_dump.assert_called_with(msgs, "removed.yaml")

        dump_removed(msgs, None, "abc/def/xyz.jaml")
        mock_dump.assert_called_with(msgs, "abc/def/removed-from-xyz.jaml")

    def test_make_list(self):
        self.assertEqual(make_list(["a"]), "a")
        self.assertEqual(make_list(["a", "b"]), "a and b")
        self.assertEqual(make_list(["a", "b", "c"]), "a, b and c")
        self.assertEqual(make_list(["a"], "use"), "a uses")
        self.assertEqual(make_list(["a", "b", "c"], "use"), "a, b and c use")


if __name__ == "__main__":
    unittest.main()