-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtest_config.py
177 lines (145 loc) · 6.41 KB
/
test_config.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
from pathlib import Path
import pydantic
import pytest
from wake.config import WakeConfig
from wake.config.data_model import SolcRemapping
from wake.core.enums import EvmVersionEnum
from wake.core.solidity_version import SolidityVersion
sources_path = (Path(__file__).parent / "config_sources").resolve()
@pytest.mark.platform_dependent
def test_config_empty():
config = WakeConfig()
assert len(config.loaded_files) == 0
file_path = (sources_path / "empty.toml").resolve()
config.load(file_path)
assert len(config.loaded_files) == 1
assert file_path in config.loaded_files
assert len(config.compiler.solc.remappings) == 0
@pytest.mark.platform_dependent
def test_config_simple():
os.environ["XDG_CONFIG_HOME"] = str(sources_path)
config = WakeConfig()
config.load_configs() # should not have any effect
assert len(config.loaded_files) == 0
assert config.global_config_path.resolve() == sources_path / "wake" / "config.toml"
assert len(config.compiler.solc.remappings) == 0
config.load(sources_path / "a.toml")
assert config.global_config_path.resolve() == sources_path / "wake" / "config.toml"
assert len(config.loaded_files) == 4
assert (sources_path / "a.toml").resolve() in config.loaded_files
assert (sources_path / "b.toml").resolve() in config.loaded_files
assert (sources_path / "c.toml").resolve() in config.loaded_files
assert (sources_path / "empty.toml").resolve() in config.loaded_files
assert len(config.compiler.solc.remappings) == 1
assert config.compiler.solc.remappings[0] == SolcRemapping(
context=None, prefix="xyz", target=None
)
assert str(config.compiler.solc.remappings[0]) == "xyz="
assert len(config.compiler.solc.include_paths) == 1
assert sources_path in config.compiler.solc.include_paths
assert len(config.compiler.solc.allow_paths) == 1
assert (sources_path / "../").resolve() in config.compiler.solc.allow_paths
def test_config_from_dict():
os.environ["XDG_CONFIG_HOME"] = str(sources_path)
config_dict = {
"compiler": {
"solc": {
"allow_paths": ["."],
"evm_version": "london",
"include_paths": ["../"],
"remappings": ["hardhat/=node_modules/hardhat/"],
"target_version": "0.8.12",
}
}
}
config = WakeConfig.fromdict(
config_dict,
project_root_path=(sources_path / "containing_global_conf"),
)
assert len(config.compiler.solc.allow_paths) == 1
assert sources_path / "containing_global_conf" in config.compiler.solc.allow_paths
assert config.compiler.solc.evm_version == EvmVersionEnum.LONDON
assert len(config.compiler.solc.include_paths) == 1
assert sources_path in config.compiler.solc.include_paths
assert len(config.compiler.solc.remappings) == 1
assert config.compiler.solc.remappings[0] == SolcRemapping(
context=None, prefix="hardhat/", target="node_modules/hardhat/"
)
assert config.compiler.solc.target_version == SolidityVersion.fromstring("0.8.12")
@pytest.mark.platform_dependent
def test_config_global():
os.environ["XDG_CONFIG_HOME"] = str(sources_path / "containing_global_conf")
config = WakeConfig()
assert len(config.loaded_files) == 0
config.load_configs()
assert len(config.loaded_files) == 2
assert (
sources_path / "containing_global_conf" / "wake" / "config.toml"
).resolve() in config.loaded_files
assert (sources_path / "empty.toml").resolve() in config.loaded_files
assert len(config.compiler.solc.remappings) == 2
assert config.compiler.solc.remappings[0] == SolcRemapping(
context=None, prefix="https://url.com", target=None
)
assert config.compiler.solc.remappings[1] == SolcRemapping(
context=None, prefix="123", target="xyz"
)
@pytest.mark.platform_dependent
def test_config_project():
os.environ["XDG_CONFIG_HOME"] = str(sources_path / "containing_global_conf")
config = WakeConfig(
project_root_path=(sources_path / "containing_project_conf"),
)
config.load_configs()
assert len(config.compiler.solc.remappings) == 1
assert config.compiler.solc.remappings[0] == SolcRemapping(
context=None, prefix="wake", target="test-target"
)
@pytest.mark.platform_dependent
def test_config_project_path_not_dir():
with pytest.raises(ValueError):
config = WakeConfig(project_root_path=(sources_path / "a,toml"))
@pytest.mark.platform_dependent
def test_config_import_abs_path():
os.environ["XDG_CONFIG_HOME"] = str(sources_path)
tmp_file = sources_path / "tmp_bHtvhGrDp6.toml"
abs_path = (sources_path / "a.toml").resolve()
content = 'subconfigs = ["{path}"]'.format(path=str(abs_path).replace("\\", "\\\\"))
tmp_file.write_text(content)
config = WakeConfig()
# this one should load: a.toml -> b.toml -> c.toml
config.load(tmp_file)
assert len(config.loaded_files) == 5
assert tmp_file.resolve() in config.loaded_files
assert (sources_path / "a.toml").resolve() in config.loaded_files
assert (sources_path / "b.toml").resolve() in config.loaded_files
assert (sources_path / "c.toml").resolve() in config.loaded_files
assert (sources_path / "empty.toml").resolve() in config.loaded_files
assert len(config.compiler.solc.remappings) == 1
assert config.compiler.solc.remappings[0] == SolcRemapping(
context=None, prefix="xyz", target=None
)
assert len(config.compiler.solc.include_paths) == 1
assert sources_path in config.compiler.solc.include_paths
tmp_file.unlink()
@pytest.mark.platform_dependent
def test_config_invalid_format():
os.environ["XDG_CONFIG_HOME"] = str(sources_path)
config = WakeConfig()
with pytest.raises(pydantic.ValidationError):
config.load(sources_path / "invalid_1.toml")
with pytest.raises(pydantic.ValidationError):
config.load(sources_path / "invalid_2.toml")
with pytest.raises(pydantic.ValidationError):
config.load(sources_path / "invalid_3.toml")
@pytest.mark.platform_dependent
def test_config_cyclic_import():
os.environ["XDG_CONFIG_HOME"] = str(sources_path)
config = WakeConfig()
with pytest.raises(ValueError):
config.load(sources_path / "cyclic_x.toml")
with pytest.raises(ValueError):
config.load(sources_path / "cyclic_1.toml")
with pytest.raises(ValueError):
config.load(sources_path / "cyclic_2.toml")