-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathtest_copyright.py
More file actions
251 lines (220 loc) · 8.17 KB
/
test_copyright.py
File metadata and controls
251 lines (220 loc) · 8.17 KB
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
"""Test copyright year adjustment"""
from __future__ import annotations
import time
from typing import TYPE_CHECKING
import pytest
from sphinx.config import (
Config,
correct_copyright_year,
evaluate_copyright_placeholders,
)
from sphinx.testing.util import SphinxTestApp
if TYPE_CHECKING:
from collections.abc import Callable, Iterator
from pathlib import Path
from sphinx.testing.util import SphinxTestApp
LT = time.localtime()
LT_NEW = (2009, *LT[1:], LT.tm_zone, LT.tm_gmtoff)
LOCALTIME_2009 = type(LT)(LT_NEW)
@pytest.fixture(
params=[
# test with SOURCE_DATE_EPOCH unset: no modification
(None, None),
# test with post-2009 SOURCE_DATE_EPOCH set: copyright year should not be updated
('1293840000', 2011),
('1293839999', 2010),
# test with pre-2009 SOURCE_DATE_EPOCH set: copyright year should be updated
('1199145600', 2008),
('1199145599', 2007),
],
)
def expect_date(
request: pytest.FixtureRequest,
monkeypatch: pytest.MonkeyPatch,
) -> Iterator[int | None]:
sde, expect = request.param
with monkeypatch.context() as m:
m.setattr(time, 'localtime', lambda *a: LOCALTIME_2009)
if sde:
m.setenv('SOURCE_DATE_EPOCH', sde)
else:
m.delenv('SOURCE_DATE_EPOCH', raising=False)
yield expect
def test_correct_year(expect_date: int | None) -> None:
# test that copyright is substituted
copyright_date = '2006-2009, Alice'
cfg = Config({'copyright': copyright_date}, {})
assert cfg.copyright == copyright_date
correct_copyright_year(None, cfg) # type: ignore[arg-type]
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert cfg.copyright == f'2006-{expect_date}, Alice'
else:
assert cfg.copyright == copyright_date
def test_correct_year_space(expect_date: int | None) -> None:
# test that copyright is substituted
copyright_date = '2006-2009 Alice'
cfg = Config({'copyright': copyright_date}, {})
assert cfg.copyright == copyright_date
correct_copyright_year(None, cfg) # type: ignore[arg-type]
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert cfg.copyright == f'2006-{expect_date} Alice'
else:
assert cfg.copyright == copyright_date
def test_correct_year_no_author(expect_date: int | None) -> None:
# test that copyright is substituted
copyright_date = '2006-2009'
cfg = Config({'copyright': copyright_date}, {})
assert cfg.copyright == copyright_date
correct_copyright_year(None, cfg) # type: ignore[arg-type]
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert cfg.copyright == f'2006-{expect_date}'
else:
assert cfg.copyright == copyright_date
def test_correct_year_single(expect_date: int | None) -> None:
# test that copyright is substituted
copyright_date = '2009, Alice'
cfg = Config({'copyright': copyright_date}, {})
assert cfg.copyright == copyright_date
correct_copyright_year(None, cfg) # type: ignore[arg-type]
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert cfg.copyright == f'{expect_date}, Alice'
else:
assert cfg.copyright == copyright_date
def test_correct_year_single_space(expect_date: int | None) -> None:
# test that copyright is substituted
copyright_date = '2009 Alice'
cfg = Config({'copyright': copyright_date}, {})
assert cfg.copyright == copyright_date
correct_copyright_year(None, cfg) # type: ignore[arg-type]
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert cfg.copyright == f'{expect_date} Alice'
else:
assert cfg.copyright == copyright_date
def test_correct_year_single_no_author(expect_date: int | None) -> None:
# test that copyright is substituted
copyright_date = '2009'
cfg = Config({'copyright': copyright_date}, {})
assert cfg.copyright == copyright_date
correct_copyright_year(None, cfg) # type: ignore[arg-type]
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert cfg.copyright == f'{expect_date}'
else:
assert cfg.copyright == copyright_date
def test_correct_year_placeholder(expect_date: int | None) -> None:
# test that copyright is substituted
copyright_date = '2006-%Y, Alice'
cfg = Config({'copyright': copyright_date}, {})
assert cfg.copyright == copyright_date
evaluate_copyright_placeholders(None, cfg) # type: ignore[arg-type]
correct_copyright_year(None, cfg) # type: ignore[arg-type]
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert cfg.copyright == f'2006-{expect_date}, Alice'
else:
assert cfg.copyright == '2006-2009, Alice'
def test_correct_year_multi_line(expect_date: int | None) -> None:
# test that copyright is substituted
copyright_dates = (
'2009',
'2006-2009, Alice',
'2010-2013, Bob',
'2014-2017, Charlie',
'2018-2021, David',
'2022-2025, Eve',
)
cfg = Config({'copyright': copyright_dates}, {})
assert cfg.copyright == copyright_dates
correct_copyright_year(None, cfg) # type: ignore[arg-type]
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert cfg.copyright == (
f'{expect_date}',
f'2006-{expect_date}, Alice',
# post 2009-dates aren't substituted
'2010-2013, Bob',
'2014-2017, Charlie',
'2018-2021, David',
'2022-2025, Eve',
)
else:
assert cfg.copyright == copyright_dates
def test_correct_year_multi_line_all_formats(expect_date: int | None) -> None:
# test that copyright is substituted
copyright_dates = (
'2009',
'2009 Alice',
'2009, Bob',
'2006-2009',
'2006-2009 Charlie',
'2006-2009, David',
)
cfg = Config({'copyright': copyright_dates}, {})
assert cfg.copyright == copyright_dates
correct_copyright_year(None, cfg) # type: ignore[arg-type]
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert cfg.copyright == (
f'{expect_date}',
f'{expect_date} Alice',
f'{expect_date}, Bob',
f'2006-{expect_date}',
f'2006-{expect_date} Charlie',
f'2006-{expect_date}, David',
)
else:
assert cfg.copyright == copyright_dates
def test_correct_year_multi_line_all_formats_placeholder(
expect_date: int | None,
) -> None:
# test that copyright is substituted
copyright_dates = (
'%Y',
'%Y Alice',
'%Y, Bob',
'2006-%Y',
'2006-%Y Charlie',
'2006-%Y, David',
# other format codes are left as-is
'2006-%y, Eve',
'%Y-%m-%d %H:%M:S %z, Francis',
)
cfg = Config({'copyright': copyright_dates}, {})
assert cfg.copyright == copyright_dates
evaluate_copyright_placeholders(None, cfg) # type: ignore[arg-type]
correct_copyright_year(None, cfg) # type: ignore[arg-type]
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert cfg.copyright == (
f'{expect_date}',
f'{expect_date} Alice',
f'{expect_date}, Bob',
f'2006-{expect_date}',
f'2006-{expect_date} Charlie',
f'2006-{expect_date}, David',
'2006-%y, Eve',
'2009-%m-%d %H:%M:S %z, Francis',
)
else:
assert cfg.copyright == (
'2009',
'2009 Alice',
'2009, Bob',
'2006-2009',
'2006-2009 Charlie',
'2006-2009, David',
'2006-%y, Eve',
'2009-%m-%d %H:%M:S %z, Francis',
)
def test_correct_year_app(
expect_date: int | None,
tmp_path: Path,
make_app: Callable[..., SphinxTestApp],
) -> None:
# integration test
copyright_date = '2006-2009, Alice'
(tmp_path / 'conf.py').touch()
app = make_app(
'dummy',
srcdir=tmp_path,
confoverrides={'copyright': copyright_date},
)
if expect_date and expect_date <= LOCALTIME_2009.tm_year:
assert app.config.copyright == f'2006-{expect_date}, Alice'
else:
assert app.config.copyright == copyright_date