2Tests for `chives.javascript`.
5from pathlib import Path
10from chives import __version__, javascript
13@pytest.mark.parametrize(
14 "contents, expect_error",
16 ("", "did not find chives header in file"),
17 ("// javascript/chives-example.js\n", "did not find chives header in file"),
19 "// javascript/chives-wrongname.js\n// chives version: 1\n",
23 "// javascript/chives-example.js\n// bad version info\n",
24 "incorrect header: did not find 'chives version' line",
28def test_malformed_header(tmp_path: Path, contents: str, expect_error: str) -> None:
30 The version parser returns meaningful errors.
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:
45 Tests for the `chives-update-js` command.
48 def test_no_js_files(self, tmp_path: Path) -> None:
50 If there are no JS files in the folder, it's a trivial no-op.
52 javascript.update_js_components(root=tmp_path, js_assets=JS_DIR)
54 def test_updates_matching_js_file(self, tmp_path: Path) -> None:
56 If there's a JS file in the folder with a matching filename,
57 it's replaced with the current version.
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)
65 javascript.get_version(tmp_path / "static/chives-image.js") == __version__
68 def test_updates_outdated_js_file(self, tmp_path: Path) -> None:
70 If there's a JS file in the folder with a matching filename,
71 it's replaced with the current version.
73 (tmp_path / "static").mkdir()
74 (tmp_path / "static/chives-image.js").write_text(
75 "// javascript/chives-image.js\n// chives version: 1\n"
78 javascript.update_js_components(root=tmp_path, js_assets=JS_DIR)
81 javascript.get_version(tmp_path / "static/chives-image.js") == __version__
84 def test_ignores_unmatched_js_file(self, tmp_path: Path) -> None:
86 If there's a JS file that doesn't come from chives, it's left as-is.
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:
97 Once a JS file is up-to-date, it's not edited again.
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