Skip to main content

tests/python/test_javascript.py

1"""
2Tests for `chives.javascript`.
3"""
5from pathlib import Path
6import subprocess
8import pytest
10from chives import __version__, javascript
13@pytest.mark.parametrize(
14 "contents, expect_error",
15 [
16 ("", "did not find chives header in file"),
17 ("// javascript/chives-example.js\n", "did not find chives header in file"),
18 (
19 "// javascript/chives-wrongname.js\n// chives version: 1\n",
20 "incorrect header",
21 ),
22 (
23 "// javascript/chives-example.js\n// bad version info\n",
24 "incorrect header: did not find 'chives version' line",
25 ),
26 ],
28def test_malformed_header(tmp_path: Path, contents: str, expect_error: str) -> None:
29 """
30 The version parser returns meaningful errors.
31 """
32 (tmp_path / "chives-example.js").write_text(contents)
34 with pytest.raises(ValueError, match=expect_error):
35 javascript.get_version(tmp_path / "chives-example.js")
38git_root_cmd = ["git", "rev-parse", "--show-toplevel"]
39GIT_ROOT = subprocess.check_output(git_root_cmd, text=True).strip()
40JS_DIR = Path(GIT_ROOT) / "javascript"
43class TestUpdateJsComponents:
44 """
45 Tests for the `chives-update-js` command.
46 """
48 def test_no_js_files(self, tmp_path: Path) -> None:
49 """
50 If there are no JS files in the folder, it's a trivial no-op.
51 """
52 javascript.update_js_components(root=tmp_path, js_assets=JS_DIR)
54 def test_updates_matching_js_file(self, tmp_path: Path) -> None:
55 """
56 If there's a JS file in the folder with a matching filename,
57 it's replaced with the current version.
58 """
59 (tmp_path / "static").mkdir()
60 (tmp_path / "static/chives-image.js").write_text("")
62 javascript.update_js_components(root=tmp_path, js_assets=JS_DIR)
64 assert (
65 javascript.get_version(tmp_path / "static/chives-image.js") == __version__
66 )
68 def test_updates_outdated_js_file(self, tmp_path: Path) -> None:
69 """
70 If there's a JS file in the folder with a matching filename,
71 it's replaced with the current version.
72 """
73 (tmp_path / "static").mkdir()
74 (tmp_path / "static/chives-image.js").write_text(
75 "// javascript/chives-image.js\n// chives version: 1\n"
76 )
78 javascript.update_js_components(root=tmp_path, js_assets=JS_DIR)
80 assert (
81 javascript.get_version(tmp_path / "static/chives-image.js") == __version__
82 )
84 def test_ignores_unmatched_js_file(self, tmp_path: Path) -> None:
85 """
86 If there's a JS file that doesn't come from chives, it's left as-is.
87 """
88 js = "function greet() { console.log('Hello world!'); }"
89 (tmp_path / "chives-example.js").write_text(js)
91 javascript.update_js_components(root=tmp_path, js_assets=JS_DIR)
93 assert (tmp_path / "chives-example.js").read_text() == js
95 def test_ignores_already_up_to_date_file(self, tmp_path: Path) -> None:
96 """
97 Once a JS file is up-to-date, it's not edited again.
98 """
99 (tmp_path / "static").mkdir()
100 (tmp_path / "static/chives-image.js").write_text("")
102 javascript.update_js_components(root=tmp_path, js_assets=JS_DIR)
103 mtime = (tmp_path / "static/chives-image.js").stat().st_mtime
105 javascript.update_js_components(root=tmp_path, js_assets=JS_DIR)
106 assert (tmp_path / "static/chives-image.js").stat().st_mtime == mtime