Debian Codegraph @

tests/test_builders/test_build_html_copyright.py

raw
1 from __future__ import annotations
2
3 import time
4 from typing import TYPE_CHECKING
5
6 import pytest
7
8 from sphinx.testing.util import SphinxTestApp
9
10 if TYPE_CHECKING:
11 from collections.abc import Iterator
12
13 from sphinx.testing.util import SphinxTestApp
14
15 LT = time.localtime()
16 LT_NEW = (2009, *LT[1:], LT.tm_zone, LT.tm_gmtoff)
17 LOCALTIME_2009 = type(LT)(LT_NEW)
18
19
20 @pytest.fixture
21 def no_source_date_year(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
22 """Explicitly clear SOURCE_DATE_EPOCH from the environment; this
23 fixture can be used to ensure that copyright substitution logic
24 does not occur during selected test cases.
25 """
26 with monkeypatch.context() as m:
27 m.delenv('SOURCE_DATE_EPOCH', raising=False)
28 yield
29
30
31 @pytest.fixture(
32 params=[
33 1199145600, # 2008-01-01 00:00:00
34 1199145599, # 2007-12-31 23:59:59
35 ]
36 )
37 def source_date_year(
38 request: pytest.FixtureRequest,
39 monkeypatch: pytest.MonkeyPatch,
40 ) -> Iterator[int]:
41 source_date_epoch = request.param
42 with monkeypatch.context() as m:
43 m.setattr(time, 'localtime', lambda *a: LOCALTIME_2009)
44 m.setenv('SOURCE_DATE_EPOCH', str(source_date_epoch))
45 yield time.gmtime(source_date_epoch).tm_year
46
47
48 @pytest.mark.sphinx('html', testroot='copyright-multiline')
49 def test_html_multi_line_copyright(
50 no_source_date_year: None, app: SphinxTestApp
51 ) -> None:
52 app.build(force_all=True)
53
54 content = (app.outdir / 'index.html').read_text(encoding='utf-8')
55
56 # check the copyright footer line by line (empty lines ignored)
57 assert ' &#169; Copyright 2006.<br/>\n' in content
58 assert ' &#169; Copyright 2006-2009, Alice.<br/>\n' in content
59 assert ' &#169; Copyright 2010-2013, Bob.<br/>\n' in content
60 assert ' &#169; Copyright 2014-2017, Charlie.<br/>\n' in content
61 assert ' &#169; Copyright 2018-2021, David.<br/>\n' in content
62 assert ' &#169; Copyright 2022-2025, Eve.' in content
63
64 # check the raw copyright footer block (empty lines included)
65 assert (
66 ' &#169; Copyright 2006.<br/>\n'
67 ' \n'
68 ' &#169; Copyright 2006-2009, Alice.<br/>\n'
69 ' \n'
70 ' &#169; Copyright 2010-2013, Bob.<br/>\n'
71 ' \n'
72 ' &#169; Copyright 2014-2017, Charlie.<br/>\n'
73 ' \n'
74 ' &#169; Copyright 2018-2021, David.<br/>\n'
75 ' \n'
76 ' &#169; Copyright 2022-2025, Eve.'
77 ) in content
78
79
80 @pytest.mark.sphinx('html', testroot='copyright-multiline')
81 def test_html_multi_line_copyright_sde(
82 source_date_year: None, app: SphinxTestApp
83 ) -> None:
84 app.build(force_all=True)
85
86 content = (app.outdir / 'index.html').read_text(encoding='utf-8')
87
88 # check the copyright footer line by line (empty lines ignored)
89 assert ' &#169; Copyright 2006.<br/>\n' in content
90 assert f' &#169; Copyright 2006-{source_date_year}, Alice.<br/>\n' in content
91 assert ' &#169; Copyright 2010-2013, Bob.<br/>\n' in content
92 assert ' &#169; Copyright 2014-2017, Charlie.<br/>\n' in content
93 assert ' &#169; Copyright 2018-2021, David.<br/>\n' in content
94 assert ' &#169; Copyright 2022-2025, Eve.' in content
95
96 # check the raw copyright footer block (empty lines included)
97 assert (
98 ' &#169; Copyright 2006.<br/>\n'
99 ' \n'
100 f' &#169; Copyright 2006-{source_date_year}, Alice.<br/>\n'
101 ' \n'
102 ' &#169; Copyright 2010-2013, Bob.<br/>\n'
103 ' \n'
104 ' &#169; Copyright 2014-2017, Charlie.<br/>\n'
105 ' \n'
106 ' &#169; Copyright 2018-2021, David.<br/>\n'
107 ' \n'
108 ' &#169; Copyright 2022-2025, Eve.'
109 ) in content