-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathrender_default_test_env.py
More file actions
60 lines (44 loc) · 1.76 KB
/
render_default_test_env.py
File metadata and controls
60 lines (44 loc) · 1.76 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
from __future__ import annotations
import os
from ast import literal_eval
from functools import cache
import tomlkit
from markdown.preprocessors import Preprocessor
MARKER_DEPENDENCIES = "<HATCH_TEST_ENV_DEPENDENCIES>"
MARKER_MATRIX = "<HATCH_TEST_ENV_MATRIX>"
MARKER_SCRIPTS = "<HATCH_TEST_ENV_SCRIPTS>"
@cache
def test_env_config():
path = os.path.join(os.getcwd(), "src", "hatch", "env", "internal", "test.py")
with open(path, encoding="utf-8") as f:
contents = f.read()
value = "".join(contents.split(" return ")[1].strip().splitlines())
return literal_eval(value)
@cache
def get_dependencies_toml():
env_config = {"dependencies": test_env_config()["dependencies"]}
content = tomlkit.dumps({"tool": {"hatch": {"envs": {"hatch-test": env_config}}}}).strip()
# Reload to fix the long array
config = tomlkit.loads(content)
config["tool"]["hatch"]["envs"]["hatch-test"]["dependencies"].multiline(True)
# Reduce indentation
content = tomlkit.dumps(config).strip()
return content.replace(' "', ' "')
@cache
def get_matrix_toml():
env_config = {"matrix": test_env_config()["matrix"]}
return tomlkit.dumps({"tool": {"hatch": {"envs": {"hatch-test": env_config}}}}).strip()
@cache
def get_scripts_toml():
env_config = {"scripts": test_env_config()["scripts"]}
return tomlkit.dumps({"tool": {"hatch": {"envs": {"hatch-test": env_config}}}}).strip()
class TestEnvDefaultsPreprocessor(Preprocessor):
def run(self, lines): # noqa: PLR6301
return (
"\n"
.join(lines)
.replace(MARKER_DEPENDENCIES, get_dependencies_toml())
.replace(MARKER_MATRIX, get_matrix_toml())
.replace(MARKER_SCRIPTS, get_scripts_toml())
.splitlines()
)