| 1 |
"""Test copyright year adjustment""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import time |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from sphinx.config import ( |
| 11 |
Config, |
| 12 |
correct_copyright_year, |
| 13 |
evaluate_copyright_placeholders, |
| 14 |
) |
| 15 |
from sphinx.testing.util import SphinxTestApp |
| 16 |
|
| 17 |
if TYPE_CHECKING: |
| 18 |
from collections.abc import Callable, Iterator |
| 19 |
from pathlib import Path |
| 20 |
|
| 21 |
from sphinx.testing.util import SphinxTestApp |
| 22 |
|
| 23 |
LT = time.localtime() |
| 24 |
LT_NEW = (2009, *LT[1:], LT.tm_zone, LT.tm_gmtoff) |
| 25 |
LOCALTIME_2009 = type(LT)(LT_NEW) |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture( |
| 29 |
params=[ |
| 30 |
# test with SOURCE_DATE_EPOCH unset: no modification |
| 31 |
(None, None), |
| 32 |
# test with post-2009 SOURCE_DATE_EPOCH set: copyright year should not be updated |
| 33 |
('1293840000', 2011), |
| 34 |
('1293839999', 2010), |
| 35 |
# test with pre-2009 SOURCE_DATE_EPOCH set: copyright year should be updated |
| 36 |
('1199145600', 2008), |
| 37 |
('1199145599', 2007), |
| 38 |
], |
| 39 |
) |
| 40 |
def expect_date( |
| 41 |
request: pytest.FixtureRequest, |
| 42 |
monkeypatch: pytest.MonkeyPatch, |
| 43 |
) -> Iterator[int | None]: |
| 44 |
sde, expect = request.param |
| 45 |
with monkeypatch.context() as m: |
| 46 |
m.setattr(time, 'localtime', lambda *a: LOCALTIME_2009) |
| 47 |
if sde: |
| 48 |
m.setenv('SOURCE_DATE_EPOCH', sde) |
| 49 |
else: |
| 50 |
m.delenv('SOURCE_DATE_EPOCH', raising=False) |
| 51 |
yield expect |
| 52 |
|
| 53 |
|
| 54 |
def test_correct_year(expect_date: int | None) -> None: |
| 55 |
# test that copyright is substituted |
| 56 |
copyright_date = '2006-2009, Alice' |
| 57 |
cfg = Config({'copyright': copyright_date}, {}) |
| 58 |
assert cfg.copyright == copyright_date |
| 59 |
correct_copyright_year(None, cfg) # type: ignore[arg-type] |
| 60 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 61 |
assert cfg.copyright == f'2006-{expect_date}, Alice' |
| 62 |
else: |
| 63 |
assert cfg.copyright == copyright_date |
| 64 |
|
| 65 |
|
| 66 |
def test_correct_year_space(expect_date: int | None) -> None: |
| 67 |
# test that copyright is substituted |
| 68 |
copyright_date = '2006-2009 Alice' |
| 69 |
cfg = Config({'copyright': copyright_date}, {}) |
| 70 |
assert cfg.copyright == copyright_date |
| 71 |
correct_copyright_year(None, cfg) # type: ignore[arg-type] |
| 72 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 73 |
assert cfg.copyright == f'2006-{expect_date} Alice' |
| 74 |
else: |
| 75 |
assert cfg.copyright == copyright_date |
| 76 |
|
| 77 |
|
| 78 |
def test_correct_year_no_author(expect_date: int | None) -> None: |
| 79 |
# test that copyright is substituted |
| 80 |
copyright_date = '2006-2009' |
| 81 |
cfg = Config({'copyright': copyright_date}, {}) |
| 82 |
assert cfg.copyright == copyright_date |
| 83 |
correct_copyright_year(None, cfg) # type: ignore[arg-type] |
| 84 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 85 |
assert cfg.copyright == f'2006-{expect_date}' |
| 86 |
else: |
| 87 |
assert cfg.copyright == copyright_date |
| 88 |
|
| 89 |
|
| 90 |
def test_correct_year_single(expect_date: int | None) -> None: |
| 91 |
# test that copyright is substituted |
| 92 |
copyright_date = '2009, Alice' |
| 93 |
cfg = Config({'copyright': copyright_date}, {}) |
| 94 |
assert cfg.copyright == copyright_date |
| 95 |
correct_copyright_year(None, cfg) # type: ignore[arg-type] |
| 96 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 97 |
assert cfg.copyright == f'{expect_date}, Alice' |
| 98 |
else: |
| 99 |
assert cfg.copyright == copyright_date |
| 100 |
|
| 101 |
|
| 102 |
def test_correct_year_single_space(expect_date: int | None) -> None: |
| 103 |
# test that copyright is substituted |
| 104 |
copyright_date = '2009 Alice' |
| 105 |
cfg = Config({'copyright': copyright_date}, {}) |
| 106 |
assert cfg.copyright == copyright_date |
| 107 |
correct_copyright_year(None, cfg) # type: ignore[arg-type] |
| 108 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 109 |
assert cfg.copyright == f'{expect_date} Alice' |
| 110 |
else: |
| 111 |
assert cfg.copyright == copyright_date |
| 112 |
|
| 113 |
|
| 114 |
def test_correct_year_single_no_author(expect_date: int | None) -> None: |
| 115 |
# test that copyright is substituted |
| 116 |
copyright_date = '2009' |
| 117 |
cfg = Config({'copyright': copyright_date}, {}) |
| 118 |
assert cfg.copyright == copyright_date |
| 119 |
correct_copyright_year(None, cfg) # type: ignore[arg-type] |
| 120 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 121 |
assert cfg.copyright == f'{expect_date}' |
| 122 |
else: |
| 123 |
assert cfg.copyright == copyright_date |
| 124 |
|
| 125 |
|
| 126 |
def test_correct_year_placeholder(expect_date: int | None) -> None: |
| 127 |
# test that copyright is substituted |
| 128 |
copyright_date = '2006-%Y, Alice' |
| 129 |
cfg = Config({'copyright': copyright_date}, {}) |
| 130 |
assert cfg.copyright == copyright_date |
| 131 |
evaluate_copyright_placeholders(None, cfg) # type: ignore[arg-type] |
| 132 |
correct_copyright_year(None, cfg) # type: ignore[arg-type] |
| 133 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 134 |
assert cfg.copyright == f'2006-{expect_date}, Alice' |
| 135 |
else: |
| 136 |
assert cfg.copyright == '2006-2009, Alice' |
| 137 |
|
| 138 |
|
| 139 |
def test_correct_year_multi_line(expect_date: int | None) -> None: |
| 140 |
# test that copyright is substituted |
| 141 |
copyright_dates = ( |
| 142 |
'2009', |
| 143 |
'2006-2009, Alice', |
| 144 |
'2010-2013, Bob', |
| 145 |
'2014-2017, Charlie', |
| 146 |
'2018-2021, David', |
| 147 |
'2022-2025, Eve', |
| 148 |
) |
| 149 |
cfg = Config({'copyright': copyright_dates}, {}) |
| 150 |
assert cfg.copyright == copyright_dates |
| 151 |
correct_copyright_year(None, cfg) # type: ignore[arg-type] |
| 152 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 153 |
assert cfg.copyright == ( |
| 154 |
f'{expect_date}', |
| 155 |
f'2006-{expect_date}, Alice', |
| 156 |
# post 2009-dates aren't substituted |
| 157 |
'2010-2013, Bob', |
| 158 |
'2014-2017, Charlie', |
| 159 |
'2018-2021, David', |
| 160 |
'2022-2025, Eve', |
| 161 |
) |
| 162 |
else: |
| 163 |
assert cfg.copyright == copyright_dates |
| 164 |
|
| 165 |
|
| 166 |
def test_correct_year_multi_line_all_formats(expect_date: int | None) -> None: |
| 167 |
# test that copyright is substituted |
| 168 |
copyright_dates = ( |
| 169 |
'2009', |
| 170 |
'2009 Alice', |
| 171 |
'2009, Bob', |
| 172 |
'2006-2009', |
| 173 |
'2006-2009 Charlie', |
| 174 |
'2006-2009, David', |
| 175 |
) |
| 176 |
cfg = Config({'copyright': copyright_dates}, {}) |
| 177 |
assert cfg.copyright == copyright_dates |
| 178 |
correct_copyright_year(None, cfg) # type: ignore[arg-type] |
| 179 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 180 |
assert cfg.copyright == ( |
| 181 |
f'{expect_date}', |
| 182 |
f'{expect_date} Alice', |
| 183 |
f'{expect_date}, Bob', |
| 184 |
f'2006-{expect_date}', |
| 185 |
f'2006-{expect_date} Charlie', |
| 186 |
f'2006-{expect_date}, David', |
| 187 |
) |
| 188 |
else: |
| 189 |
assert cfg.copyright == copyright_dates |
| 190 |
|
| 191 |
|
| 192 |
def test_correct_year_multi_line_all_formats_placeholder( |
| 193 |
expect_date: int | None, |
| 194 |
) -> None: |
| 195 |
# test that copyright is substituted |
| 196 |
copyright_dates = ( |
| 197 |
'%Y', |
| 198 |
'%Y Alice', |
| 199 |
'%Y, Bob', |
| 200 |
'2006-%Y', |
| 201 |
'2006-%Y Charlie', |
| 202 |
'2006-%Y, David', |
| 203 |
# other format codes are left as-is |
| 204 |
'2006-%y, Eve', |
| 205 |
'%Y-%m-%d %H:%M:S %z, Francis', |
| 206 |
) |
| 207 |
cfg = Config({'copyright': copyright_dates}, {}) |
| 208 |
assert cfg.copyright == copyright_dates |
| 209 |
evaluate_copyright_placeholders(None, cfg) # type: ignore[arg-type] |
| 210 |
correct_copyright_year(None, cfg) # type: ignore[arg-type] |
| 211 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 212 |
assert cfg.copyright == ( |
| 213 |
f'{expect_date}', |
| 214 |
f'{expect_date} Alice', |
| 215 |
f'{expect_date}, Bob', |
| 216 |
f'2006-{expect_date}', |
| 217 |
f'2006-{expect_date} Charlie', |
| 218 |
f'2006-{expect_date}, David', |
| 219 |
'2006-%y, Eve', |
| 220 |
'2009-%m-%d %H:%M:S %z, Francis', |
| 221 |
) |
| 222 |
else: |
| 223 |
assert cfg.copyright == ( |
| 224 |
'2009', |
| 225 |
'2009 Alice', |
| 226 |
'2009, Bob', |
| 227 |
'2006-2009', |
| 228 |
'2006-2009 Charlie', |
| 229 |
'2006-2009, David', |
| 230 |
'2006-%y, Eve', |
| 231 |
'2009-%m-%d %H:%M:S %z, Francis', |
| 232 |
) |
| 233 |
|
| 234 |
|
| 235 |
def test_correct_year_app( |
| 236 |
expect_date: int | None, |
| 237 |
tmp_path: Path, |
| 238 |
make_app: Callable[..., SphinxTestApp], |
| 239 |
) -> None: |
| 240 |
# integration test |
| 241 |
copyright_date = '2006-2009, Alice' |
| 242 |
(tmp_path / 'conf.py').touch() |
| 243 |
app = make_app( |
| 244 |
'dummy', |
| 245 |
srcdir=tmp_path, |
| 246 |
confoverrides={'copyright': copyright_date}, |
| 247 |
) |
| 248 |
if expect_date and expect_date <= LOCALTIME_2009.tm_year: |
| 249 |
assert app.config.copyright == f'2006-{expect_date}, Alice' |
| 250 |
else: |
| 251 |
assert app.config.copyright == copyright_date |