-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_generate_index.py
More file actions
91 lines (76 loc) · 2.87 KB
/
Copy pathtest_generate_index.py
File metadata and controls
91 lines (76 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""Tests for generate_index.py."""
import hashlib
import json
import os
import shutil
import subprocess
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
def _make_repo(tmp_path, index):
"""Lay out a throwaway plugin repo and return its directory."""
shutil.copy(os.path.join(HERE, "generate_index.py"), tmp_path / "generate_index.py")
biome = os.path.join(HERE, "biome.json")
if os.path.exists(biome):
shutil.copy(biome, tmp_path / "biome.json")
for name in index:
(tmp_path / (name + ".py")).write_text(f"# {name}\n")
(tmp_path / "plugins_index.json").write_text(json.dumps(index))
return tmp_path
def _run(repo):
subprocess.run(
[sys.executable, str(repo / "generate_index.py")], cwd=str(repo), check=True
)
return json.loads((repo / "plugins_index.json").read_text())
def test_regenerates_hashes_url_and_drops_min_version(tmp_path):
repo = _make_repo(
tmp_path,
{
"plugin_foo": {
"url": "https://old/master/plugin_foo.py",
"desc": "Foo plugin",
"sha1sum": "stale",
"min_version": 0.0,
"_min_version_comment": "obsolete",
"requirements": {"dmenu-extended": ">=1.0.0", "python": ">=3.9"},
"verified": True,
}
},
)
entry = _run(repo)["plugin_foo"]
data = (repo / "plugin_foo.py").read_bytes()
assert entry["sha256"] == hashlib.sha256(data).hexdigest()
assert entry["sha1sum"] == hashlib.sha1(data).hexdigest()
assert entry["url"].endswith("/main/plugin_foo.py")
assert "min_version" not in entry
assert "_min_version_comment" not in entry
# hand-authored metadata is preserved
assert entry["desc"] == "Foo plugin"
assert entry["verified"] is True
assert entry["requirements"] == {"dmenu-extended": ">=1.0.0", "python": ">=3.9"}
def test_entries_sorted_by_name(tmp_path):
repo = _make_repo(
tmp_path,
{
"plugin_zebra": {"desc": "z", "requirements": {}},
"plugin_alpha": {"desc": "a", "requirements": {}},
},
)
assert list(_run(repo).keys()) == ["plugin_alpha", "plugin_zebra"]
def test_idempotent(tmp_path):
repo = _make_repo(tmp_path, {"plugin_foo": {"desc": "f", "requirements": {}}})
_run(repo)
first = (repo / "plugins_index.json").read_text()
_run(repo)
second = (repo / "plugins_index.json").read_text()
assert first == second
def test_missing_plugin_file_is_an_error(tmp_path):
repo = _make_repo(tmp_path, {"plugin_foo": {"desc": "f", "requirements": {}}})
(repo / "plugin_foo.py").unlink()
result = subprocess.run(
[sys.executable, str(repo / "generate_index.py")],
cwd=str(repo),
capture_output=True,
text=True,
)
assert result.returncode != 0
assert "no plugin file" in result.stderr