Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 29bcae3

Browse files
authored
Don't include static files twice (#80)
Only include the type that was requested.
1 parent da9f0c6 commit 29bcae3

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

sphinx_search/extension.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from sphinx.errors import ExtensionError
44
from sphinx.util.fileutil import copy_asset
55

6-
76
ASSETS_FILES = {
87
'minified': [
98
os.path.join('js', 'rtd_sphinx_search.min.js'),
@@ -16,26 +15,24 @@
1615
}
1716

1817

18+
def _get_static_files(config):
19+
file_type = config.rtd_sphinx_search_file_type
20+
if file_type not in ASSETS_FILES:
21+
raise ExtensionError(f'"{file_type}" file type is not supported')
22+
23+
return ASSETS_FILES[file_type]
24+
25+
1926
def copy_asset_files(app, exception):
2027
if exception is None: # build succeeded
21-
files = ASSETS_FILES['minified'] + ASSETS_FILES['un-minified']
22-
for file in files:
28+
for file in _get_static_files(app.config):
2329
path = os.path.join(os.path.dirname(__file__), 'static', file)
2430
copy_asset(path, os.path.join(app.outdir, '_static', file.split('.')[-1]))
2531

2632

2733
def inject_static_files(app):
2834
"""Inject correct CSS and JS files based on the value of ``rtd_sphinx_search_file_type``."""
29-
30-
file_type = app.config.rtd_sphinx_search_file_type
31-
expected_file_type = ASSETS_FILES.keys()
32-
33-
if file_type not in expected_file_type:
34-
raise ExtensionError(f'"{file_type}" file type is not supported')
35-
36-
files = ASSETS_FILES[file_type]
37-
38-
for file in files:
35+
for file in _get_static_files(app.config):
3936
if file.endswith('.js'):
4037
app.add_js_file(file)
4138
elif file.endswith('.css'):

tests/test_extension.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
11
"""Test working of extension."""
22

33
import os
4+
45
import pytest
56

6-
from tests import TEST_DOCS_SRC
77
from sphinx_search.extension import ASSETS_FILES
8-
9-
10-
@pytest.mark.sphinx(srcdir=TEST_DOCS_SRC)
11-
def test_static_files_exists(app, status, warning):
12-
"""Test if the static files are present in the _build folder."""
13-
app.build()
14-
path = app.outdir
15-
16-
static_files = ASSETS_FILES['minified'] + ASSETS_FILES['un-minified']
17-
18-
for file in static_files:
19-
file_path = os.path.join(path, '_static', file)
20-
assert (
21-
os.path.exists(file_path)
22-
), f'{file_path} should be present in the _build folder'
8+
from tests import TEST_DOCS_SRC
239

2410

2511
@pytest.mark.sphinx(
@@ -36,12 +22,15 @@ def test_minified_static_files_injected_in_html(selenium, app, status, warning):
3622
selenium.get(f'file://{path}')
3723
page_source = selenium.page_source
3824

39-
assert app.config.rtd_sphinx_search_file_type == 'minified'
40-
4125
file_type = app.config.rtd_sphinx_search_file_type
42-
files = ASSETS_FILES[file_type]
26+
assert file_type == 'minified'
27+
28+
for file in ASSETS_FILES[file_type]:
29+
file_path = os.path.join(app.outdir, '_static', file)
30+
assert (
31+
os.path.exists(file_path)
32+
), f'{file_path} should be present in the _build folder'
4333

44-
for file in files:
4534
file_name = file.split('/')[-1]
4635
assert (
4736
page_source.count(file_name) == 1
@@ -62,13 +51,16 @@ def test_un_minified_static_files_injected_in_html(selenium, app, status, warnin
6251
selenium.get(f'file://{path}')
6352
page_source = selenium.page_source
6453

65-
assert app.config.rtd_sphinx_search_file_type == 'un-minified'
66-
6754
file_type = app.config.rtd_sphinx_search_file_type
68-
files = ASSETS_FILES[file_type]
55+
assert file_type == 'un-minified'
56+
57+
for file in ASSETS_FILES[file_type]:
58+
file_path = os.path.join(app.outdir, '_static', file)
59+
assert (
60+
os.path.exists(file_path)
61+
), f'{file_path} should be present in the _build folder'
6962

70-
for file in files:
7163
file_name = file.split('/')[-1]
7264
assert (
7365
page_source.count(file_name) == 1
74-
), f'{file_name} should be present in the page source'
66+
), f'{file} should be present in the page source'

0 commit comments

Comments
 (0)