File: util_test.py

package info (click to toggle)
dupeguru 4.3.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,604 kB
  • sloc: python: 16,846; ansic: 424; makefile: 123
file content (309 lines) | stat: -rw-r--r-- 9,695 bytes parent folder | download | duplicates (3)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Created By: Virgil Dupras
# Created On: 2011-01-11
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.gnu.org/licenses/gpl-3.0.html

from io import StringIO

from pytest import raises

from hscommon.testutil import eq_
from pathlib import Path
from hscommon.util import (
    nonone,
    tryint,
    first,
    flatten,
    dedupe,
    extract,
    allsame,
    format_time,
    format_time_decimal,
    format_size,
    multi_replace,
    delete_if_empty,
    open_if_filename,
    FileOrPath,
    iterconsume,
    escape,
    get_file_ext,
    rem_file_ext,
    pluralize,
)


def test_nonone():
    eq_("foo", nonone("foo", "bar"))
    eq_("bar", nonone(None, "bar"))


def test_tryint():
    eq_(42, tryint("42"))
    eq_(0, tryint("abc"))
    eq_(0, tryint(None))
    eq_(42, tryint(None, 42))


# --- Sequence


def test_first():
    eq_(first([3, 2, 1]), 3)
    eq_(first(i for i in [3, 2, 1] if i < 3), 2)


def test_flatten():
    eq_([1, 2, 3, 4], flatten([[1, 2], [3, 4]]))
    eq_([], flatten([]))


def test_dedupe():
    reflist = [0, 7, 1, 2, 3, 4, 4, 5, 6, 7, 1, 2, 3]
    eq_(dedupe(reflist), [0, 7, 1, 2, 3, 4, 5, 6])


def test_extract():
    wheat, shaft = extract(lambda n: n % 2 == 0, list(range(10)))
    eq_(wheat, [0, 2, 4, 6, 8])
    eq_(shaft, [1, 3, 5, 7, 9])


def test_allsame():
    assert allsame([42, 42, 42])
    assert not allsame([42, 43, 42])
    assert not allsame([43, 42, 42])
    # Works on non-sequence as well
    assert allsame(iter([42, 42, 42]))


def test_iterconsume():
    # We just want to make sure that we return *all* items and that we're not mistakenly skipping
    # one.
    eq_(list(range(2500)), list(iterconsume(list(range(2500)))))
    eq_(list(reversed(range(2500))), list(iterconsume(list(range(2500)), reverse=False)))


# --- String


def test_escape():
    eq_("f\\o\\ob\\ar", escape("foobar", "oa"))
    eq_("f*o*ob*ar", escape("foobar", "oa", "*"))
    eq_("f*o*ob*ar", escape("foobar", set("oa"), "*"))


def test_get_file_ext():
    eq_(get_file_ext("foobar"), "")
    eq_(get_file_ext("foo.bar"), "bar")
    eq_(get_file_ext("foobar."), "")
    eq_(get_file_ext(".foobar"), "foobar")


def test_rem_file_ext():
    eq_(rem_file_ext("foobar"), "foobar")
    eq_(rem_file_ext("foo.bar"), "foo")
    eq_(rem_file_ext("foobar."), "foobar")
    eq_(rem_file_ext(".foobar"), "")


def test_pluralize():
    eq_("0 song", pluralize(0, "song"))
    eq_("1 song", pluralize(1, "song"))
    eq_("2 songs", pluralize(2, "song"))
    eq_("1 song", pluralize(1.1, "song"))
    eq_("2 songs", pluralize(1.5, "song"))
    eq_("1.1 songs", pluralize(1.1, "song", 1))
    eq_("1.5 songs", pluralize(1.5, "song", 1))
    eq_("2 entries", pluralize(2, "entry", plural_word="entries"))


def test_format_time():
    eq_(format_time(0), "00:00:00")
    eq_(format_time(1), "00:00:01")
    eq_(format_time(23), "00:00:23")
    eq_(format_time(60), "00:01:00")
    eq_(format_time(101), "00:01:41")
    eq_(format_time(683), "00:11:23")
    eq_(format_time(3600), "01:00:00")
    eq_(format_time(3754), "01:02:34")
    eq_(format_time(36000), "10:00:00")
    eq_(format_time(366666), "101:51:06")
    eq_(format_time(0, with_hours=False), "00:00")
    eq_(format_time(1, with_hours=False), "00:01")
    eq_(format_time(23, with_hours=False), "00:23")
    eq_(format_time(60, with_hours=False), "01:00")
    eq_(format_time(101, with_hours=False), "01:41")
    eq_(format_time(683, with_hours=False), "11:23")
    eq_(format_time(3600, with_hours=False), "60:00")
    eq_(format_time(6036, with_hours=False), "100:36")
    eq_(format_time(60360, with_hours=False), "1006:00")


def test_format_time_decimal():
    eq_(format_time_decimal(0), "0.0 second")
    eq_(format_time_decimal(1), "1.0 second")
    eq_(format_time_decimal(23), "23.0 seconds")
    eq_(format_time_decimal(60), "1.0 minute")
    eq_(format_time_decimal(101), "1.7 minutes")
    eq_(format_time_decimal(683), "11.4 minutes")
    eq_(format_time_decimal(3600), "1.0 hour")
    eq_(format_time_decimal(6036), "1.7 hours")
    eq_(format_time_decimal(86400), "1.0 day")
    eq_(format_time_decimal(160360), "1.9 days")


def test_format_size():
    eq_(format_size(1024), "1 KB")
    eq_(format_size(1024, 2), "1.00 KB")
    eq_(format_size(1024, 0, 2), "1 MB")
    eq_(format_size(1024, 2, 2), "0.01 MB")
    eq_(format_size(1024, 3, 2), "0.001 MB")
    eq_(format_size(1024, 3, 2, False), "0.001")
    eq_(format_size(1023), "1023 B")
    eq_(format_size(1023, 0, 1), "1 KB")
    eq_(format_size(511, 0, 1), "1 KB")
    eq_(format_size(9), "9 B")
    eq_(format_size(99), "99 B")
    eq_(format_size(999), "999 B")
    eq_(format_size(9999), "10 KB")
    eq_(format_size(99999), "98 KB")
    eq_(format_size(999999), "977 KB")
    eq_(format_size(9999999), "10 MB")
    eq_(format_size(99999999), "96 MB")
    eq_(format_size(999999999), "954 MB")
    eq_(format_size(9999999999), "10 GB")
    eq_(format_size(99999999999), "94 GB")
    eq_(format_size(999999999999), "932 GB")
    eq_(format_size(9999999999999), "10 TB")
    eq_(format_size(99999999999999), "91 TB")
    eq_(format_size(999999999999999), "910 TB")
    eq_(format_size(9999999999999999), "9 PB")
    eq_(format_size(99999999999999999), "89 PB")
    eq_(format_size(999999999999999999), "889 PB")
    eq_(format_size(9999999999999999999), "9 EB")
    eq_(format_size(99999999999999999999), "87 EB")
    eq_(format_size(999999999999999999999), "868 EB")
    eq_(format_size(9999999999999999999999), "9 ZB")
    eq_(format_size(99999999999999999999999), "85 ZB")
    eq_(format_size(999999999999999999999999), "848 ZB")


def test_multi_replace():
    eq_("136", multi_replace("123456", ("2", "45")))
    eq_("1 3 6", multi_replace("123456", ("2", "45"), " "))
    eq_("1 3  6", multi_replace("123456", "245", " "))
    eq_("173896", multi_replace("123456", "245", "789"))
    eq_("173896", multi_replace("123456", "245", ("7", "8", "9")))
    eq_("17386", multi_replace("123456", ("2", "45"), "78"))
    eq_("17386", multi_replace("123456", ("2", "45"), ("7", "8")))
    with raises(ValueError):
        multi_replace("123456", ("2", "45"), ("7", "8", "9"))
    eq_("17346", multi_replace("12346", ("2", "45"), "78"))


# --- Files


class TestCaseDeleteIfEmpty:
    def test_is_empty(self, tmpdir):
        testpath = Path(str(tmpdir))
        assert delete_if_empty(testpath)
        assert not testpath.exists()

    def test_not_empty(self, tmpdir):
        testpath = Path(str(tmpdir))
        testpath.joinpath("foo").mkdir()
        assert not delete_if_empty(testpath)
        assert testpath.exists()

    def test_with_files_to_delete(self, tmpdir):
        testpath = Path(str(tmpdir))
        testpath.joinpath("foo").touch()
        testpath.joinpath("bar").touch()
        assert delete_if_empty(testpath, ["foo", "bar"])
        assert not testpath.exists()

    def test_directory_in_files_to_delete(self, tmpdir):
        testpath = Path(str(tmpdir))
        testpath.joinpath("foo").mkdir()
        assert not delete_if_empty(testpath, ["foo"])
        assert testpath.exists()

    def test_delete_files_to_delete_only_if_dir_is_empty(self, tmpdir):
        testpath = Path(str(tmpdir))
        testpath.joinpath("foo").touch()
        testpath.joinpath("bar").touch()
        assert not delete_if_empty(testpath, ["foo"])
        assert testpath.exists()
        assert testpath.joinpath("foo").exists()

    def test_doesnt_exist(self):
        # When the 'path' doesn't exist, just do nothing.
        delete_if_empty(Path("does_not_exist"))  # no crash

    def test_is_file(self, tmpdir):
        # When 'path' is a file, do nothing.
        p = Path(str(tmpdir)).joinpath("filename")
        p.touch()
        delete_if_empty(p)  # no crash

    def test_ioerror(self, tmpdir, monkeypatch):
        # if an IO error happens during the operation, ignore it.
        def do_raise(*args, **kw):
            raise OSError()

        monkeypatch.setattr(Path, "rmdir", do_raise)
        delete_if_empty(Path(str(tmpdir)))  # no crash


class TestCaseOpenIfFilename:
    FILE_NAME = "test.txt"

    def test_file_name(self, tmpdir):
        filepath = str(tmpdir.join(self.FILE_NAME))
        open(filepath, "wb").write(b"test_data")
        file, close = open_if_filename(filepath)
        assert close
        eq_(b"test_data", file.read())
        file.close()

    def test_opened_file(self):
        sio = StringIO()
        sio.write("test_data")
        sio.seek(0)
        file, close = open_if_filename(sio)
        assert not close
        eq_("test_data", file.read())

    def test_mode_is_passed_to_open(self, tmpdir):
        filepath = str(tmpdir.join(self.FILE_NAME))
        open(filepath, "w").close()
        file, close = open_if_filename(filepath, "a")
        eq_("a", file.mode)
        file.close()


class TestCaseFileOrPath:
    FILE_NAME = "test.txt"

    def test_path(self, tmpdir):
        filepath = str(tmpdir.join(self.FILE_NAME))
        open(filepath, "wb").write(b"test_data")
        with FileOrPath(filepath) as fp:
            eq_(b"test_data", fp.read())

    def test_opened_file(self):
        sio = StringIO()
        sio.write("test_data")
        sio.seek(0)
        with FileOrPath(sio) as fp:
            eq_("test_data", fp.read())

    def test_mode_is_passed_to_open(self, tmpdir):
        filepath = str(tmpdir.join(self.FILE_NAME))
        open(filepath, "w").close()
        with FileOrPath(filepath, "a") as fp:
            eq_("a", fp.mode)