From 6c556df79380418667651b8011fc91dedc700a95 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 19:24:24 +0200 Subject: Adding upstream version 4.1. Signed-off-by: Daniel Baumann --- tests/test_jquery_installed.py | 93 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 tests/test_jquery_installed.py (limited to 'tests/test_jquery_installed.py') diff --git a/tests/test_jquery_installed.py b/tests/test_jquery_installed.py new file mode 100644 index 0000000..a402bd4 --- /dev/null +++ b/tests/test_jquery_installed.py @@ -0,0 +1,93 @@ +import base64 +import hashlib +from pathlib import Path + +import pytest +import sphinx +from sphinx.testing.path import path +from sphinx.testing.util import SphinxTestApp + +from sphinxcontrib.jquery import _FILES, _ROOT_DIR # NoQA + + +def run_blank_app(srcdir, **kwargs): + Path(srcdir, "conf.py").write_text("", encoding="ascii") + if sphinx.version_info[:2] >= (2, 0): + Path(srcdir, "index.rst").touch() + else: + Path(srcdir, "contents.rst").touch() + for _ in range(2): # build twice to test re-builds + app = SphinxTestApp(**kwargs, srcdir=srcdir) + app.builder.build_all() + app.cleanup() + return Path(srcdir, "_build", "html") + + +@pytest.fixture(scope="function") +def blank_app(tmpdir, monkeypatch): + def inner(**kwargs): + return run_blank_app(path(tmpdir), **kwargs) + + monkeypatch.setattr("sphinx.application.abspath", lambda x: x) + yield inner + + +@pytest.mark.skipif(sphinx.version_info[:2] < (6, 0), + reason="Requires Sphinx 6.0 or greater") +def test_jquery_installed_sphinx_ge_60_use_sri(blank_app): + out_dir = blank_app(confoverrides={"extensions": ["sphinxcontrib.jquery"], "jquery_use_sri": True}) + + text = out_dir.joinpath("index.html").read_text(encoding="utf-8") + assert ('') in text + assert ('') in text + + static_dir = out_dir / '_static' + assert static_dir.joinpath('jquery.js').is_file() + assert static_dir.joinpath('_sphinx_javascript_frameworks_compat.js').is_file() + + +@pytest.mark.skipif(sphinx.version_info[:2] < (6, 0), + reason="Requires Sphinx 6.0 or greater") +def test_jquery_installed_sphinx_ge_60(blank_app): + out_dir = blank_app(confoverrides={"extensions": ["sphinxcontrib.jquery"]}) + + text = out_dir.joinpath("index.html").read_text(encoding="utf-8") + assert ('') in text + assert ('') in text + + static_dir = out_dir / '_static' + assert static_dir.joinpath('jquery.js').is_file() + assert static_dir.joinpath('_sphinx_javascript_frameworks_compat.js').is_file() + + +@pytest.mark.skipif(sphinx.version_info[:2] >= (6, 0), + reason="Requires Sphinx older than 6.0") +def test_jquery_installed_sphinx_lt_60(blank_app): + out_dir = blank_app(confoverrides={"extensions": ["sphinxcontrib.jquery"]}) + + if sphinx.version_info[:2] >= (2, 0): + text = out_dir.joinpath("index.html").read_text(encoding="utf-8") + assert '' in text + else: + text = out_dir.joinpath("contents.html").read_text(encoding="utf-8") + assert '' in text + if sphinx.version_info[:1] == (5,): + assert '' in text + + static_dir = out_dir / '_static' + assert static_dir.joinpath('jquery.js').is_file() + if sphinx.version_info[:1] == (5,): + assert static_dir.joinpath('_sphinx_javascript_frameworks_compat.js').is_file() + + +@pytest.mark.parametrize(('filename', 'integrity'), _FILES, ids=[*dict(_FILES)]) +def test_integrity(filename, integrity): + checksum = hashlib.sha384(Path(_ROOT_DIR, filename).read_bytes()) + encoded = base64.b64encode(checksum.digest()).decode(encoding='ascii') + assert f"sha384-{encoded}" == integrity -- cgit v1.2.3