-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixtures.py
More file actions
80 lines (61 loc) · 2.36 KB
/
fixtures.py
File metadata and controls
80 lines (61 loc) · 2.36 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
from pathlib import Path
from appwrite_lab.labs import Labs
from appwrite_lab.models import Lab
import hashlib
import pytest
@pytest.fixture(scope="session")
def lab_svc():
"""Lab service instance for managing Appwrite labs."""
return Labs()
@pytest.fixture(scope="session")
def appwrite_file_path():
"""Override this fixture to specify appwrite.json location."""
return NotImplementedError(
"Override this fixture to specify appwrite.json location."
)
@pytest.fixture(scope="session")
def appwrite_file(appwrite_file_path):
if appwrite_file_path:
file = Path(appwrite_file_path)
else:
# Look in current working directory
file = Path.cwd() / "appwrite.json"
if not file.exists():
return None
return file
@pytest.fixture(scope="session")
def lab_config():
"""Default lab configuration. Override in your test files."""
return {"name": "test-lab", "version": "1.7.4", "port": 8080}
@pytest.fixture(scope="session")
def lab(lab_svc: Labs, appwrite_file: Path, lab_config: dict) -> Lab:
"""Create or get existing lab with optional appwrite.json sync."""
lab_name = lab_config["name"]
hash_file_path = Path.home() / ".config" / "appwrite-lab" / "json_hashes"
hash_file_path.touch()
if lab := lab_svc.get_lab(lab_name):
# Check if the file has changed before unnecessary sync
if appwrite_file and appwrite_file.exists():
hash = hash_file(appwrite_file)
data = hash_file_path.read_text()
if len(data) > 0 and data.strip() == hash:
print("Skipping sync because the file has not changed")
else:
lab_svc.sync_with_appwrite_config(
name=lab_name, appwrite_json=appwrite_file
)
hash_file_path.write_text(hash)
return lab
res = lab_svc.new(**lab_config)
if appwrite_file and appwrite_file.exists():
hash_file_path.write_text(hash_file(appwrite_file))
lab_svc.sync_with_appwrite_config(name=lab_name, appwrite_json=appwrite_file)
if not res.error:
return lab_svc.get_lab(lab_name)
raise ValueError(res.message)
def hash_file(path, algo="sha256"):
h = hashlib.new(algo)
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
h.update(chunk)
return h.hexdigest()