forked from Delgan/loguru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_filesink_rotation.py
147 lines (121 loc) · 5.12 KB
/
test_filesink_rotation.py
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
import pytest
import pendulum
import datetime
import os
import re
from loguru import logger
@pytest.mark.parametrize('name, regex', [
('test.log', r'test(\.[0-9\-_]+)?\.log'),
('{time}.log', r'[0-9\-_]+\.log'),
('test.log.1', r'test\.log(\.[0-9\-_]+)?\.1'),
('test.{time}.log', r'test\.[0-9\-_]+\.log')
])
def test_renaming(monkeypatch_now, tmpdir, name, regex):
now = pendulum.parse("2017-06-18 12:00:00")
def patched(*a, **k):
nonlocal now
now = now.add(seconds=1)
return now
monkeypatch_now(patched)
file = tmpdir.join(name)
i = logger.start(file, format="{message}", rotation="10 B")
logger.debug("aaaaaaa")
logger.debug("bbbbbbb")
logger.debug("ccccccc")
logger.stop()
print(tmpdir.listdir())
assert all([re.match(regex, f.basename) for f in tmpdir.listdir()])
assert {f.read() for f in tmpdir.listdir()} == {'aaaaaaa\n', 'bbbbbbb\n', 'ccccccc\n'}
@pytest.mark.parametrize('size', [
8, 8.0, 7.99, "8 B", "8e-6MB", "0.008 kiB", "64b"
])
def test_size_rotation(tmpdir, size):
file = tmpdir.join("test.log")
i = logger.start(file.realpath(), format='{message}', rotation=size, mode='w')
logger.debug("abcde")
logger.debug("fghij")
logger.debug("klmno")
logger.stop(i)
assert len(tmpdir.listdir()) == 3
assert {f.read() for f in tmpdir.listdir()} == {'abcde\n', 'fghij\n', 'klmno\n'}
# TODO: Decomment test cases once pendulum/#211 is fixed
@pytest.mark.parametrize('when, hours', [
# hours = [Should not trigger, should trigger, should not trigger, should trigger, should trigger]
#('13', [0, 1, 20, 4, 24]),
#('13:00', [0.2, 0.9, 23, 1, 48]),
('13:00:00', [0.5, 1.5, 10, 15, 72]),
('13:00:00.123456', [0.9, 2, 10, 15, 256]),
#('11:00', [22.9, 0.2, 23, 1, 24]),
('w0', [11, 1, 24 * 7 - 1, 1, 24 * 7]),
#('W0 at 00:00', [10, 24 * 7 - 5, 0.1, 24 * 30, 24 * 14]),
('W6', [24, 24 * 28, 24 * 5, 24, 364 * 24]),
('saturday', [25, 25 * 12, 0, 25 * 12, 24 * 8]),
#('w6 at 00', [8, 24 * 7, 24 * 6, 24, 24 * 8]),
#(' W6 at 13 ', [0.5, 1, 24 * 6, 24 * 6, 365 * 24]),
('w2 at 11:00:00', [48 + 22, 3, 24 * 6, 24, 366 * 24]),
('MoNdAy at 11:00:30.123', [22, 24, 24, 24 * 7, 24 * 7]),
('sunday', [0.1, 24 * 7 - 10, 24, 24 * 6, 24 * 7]),
#('SUNDAY at 11:00', [1, 24 * 7, 2, 24*7, 30*12]),
('sunDAY at 13:00:00', [0.9, 0.2, 24 * 7 - 2, 3, 24 * 8]),
(datetime.time(15), [2, 3, 19, 5, 24]),
(pendulum.Time(18, 30, 11, 123), [1, 5.51, 20, 24, 40]),
("2 h", [1, 2, 0.9, 0.5, 10]),
("1 hour", [0.5, 1, 0.1, 100, 1000]),
("7 days", [24 * 7 - 1, 1, 48, 24 * 10, 24 * 365]),
("1h 30 minutes", [1.4, 0.2, 1, 2, 10]),
("1 w, 2D", [24 * 8, 24 * 2, 24, 24 * 9, 24 * 9]),
("1.5d", [30, 10, 0.9, 48, 35]),
("1.222 hours, 3.44s", [1.222, 0.1, 1, 1.2, 2]),
(datetime.timedelta(hours=1), [0.9, 0.2, 0.7, 0.5, 3]),
(pendulum.Duration(minutes=30), [0.48, 0.04, 0.07, 0.44, 0.5]),
('hourly', [0.9, 0.2, 0.8, 3, 1]),
('daily', [11, 1, 23, 1, 24]),
('WEEKLY', [11, 2, 24 * 6, 24, 24 * 7]),
(' mOnthLY', [0, 24 * 13, 29 * 24, 60 * 24, 24 * 35]),
('yearly ', [100, 24 * 7 * 30, 24 * 300, 24 * 100, 24 * 400]),
])
def test_time_rotation(monkeypatch_now, tmpdir, when, hours):
now = pendulum.parse("2017-06-18 12:00:00") # Sunday
monkeypatch_now(lambda *a, **k: now)
i = logger.start(tmpdir.join('test.log').realpath(), format='{message}', rotation=when, mode='w')
for h, m in zip(hours, ['a', 'b', 'c', 'd', 'e']):
now = now.add(hours=h)
logger.debug(m)
logger.stop(i)
assert len(tmpdir.listdir()) == 4
assert {f.read() for f in tmpdir.listdir()} == {'a\n', 'b\nc\n', 'd\n', 'e\n'}
def test_function_rotation(tmpdir):
x = iter([False, True, False])
i = logger.start(tmpdir.join("test.log"), rotation=lambda *_: next(x))
logger.debug("a")
assert len(tmpdir.listdir()) == 1
logger.debug("b")
assert len(tmpdir.listdir()) == 2
logger.debug("c")
assert len(tmpdir.listdir()) == 2
@pytest.mark.parametrize('mode', ['w', 'x'])
def test_rotation_at_stop(monkeypatch_now, tmpdir, mode):
monkeypatch_now(lambda *a, **k: pendulum.parse("2018-01-01 00:00:00"))
i = logger.start(tmpdir.join("test_{time:YYYY}.log"), rotation="10 MB", mode=mode)
logger.debug("test")
logger.stop(i)
assert len(tmpdir.listdir()) == 1
assert tmpdir.join("test_2018.log").check(exists=1)
@pytest.mark.parametrize('mode', ['a', 'a+'])
def test_no_rotation_at_stop(tmpdir, mode):
i = logger.start(tmpdir.join("test.log"), rotation="10 MB", mode=mode)
logger.debug("test")
logger.stop(i)
assert len(tmpdir.listdir()) == 1
assert tmpdir.join("test.log").check(exists=1)
@pytest.mark.parametrize('rotation', [
"w7", "w10", "w-1", "h", "M",
"w1at13", "www", "13 at w2", "w",
"K", "tufy MB", "111.111.111 kb", "3 Ki",
"2017.11.12", "11:99", "monday at 2017",
"e days", "2 days 8 pouooi", "foobar",
object(), os, pendulum.Date(2017, 11, 11), pendulum.now(), 1j,
])
def test_invalid_rotation(rotation):
with pytest.raises(ValueError):
logger.start('test.log', rotation=rotation)