-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathconftest.py
60 lines (48 loc) · 1.82 KB
/
conftest.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
import pytest
import json
import commands
import pathlib
import tomllib
@pytest.fixture(params=[2018, 2021, 2024])
def rust_edition(request):
return request.param
@pytest.fixture
def cargo(cwd, rust_edition):
manifest_file = cwd / "Cargo.toml"
assert manifest_file.exists()
(cwd / "rust-project.json").unlink(missing_ok=True)
def update(file):
contents = file.read_text()
m = tomllib.loads(contents)
if 'package' in m:
# tomllib does not support writing, and we don't want to use further dependencies
# so we just do a dumb search and replace
contents = contents.replace(f'edition = "{m["package"]["edition"]}"', f'edition = "{rust_edition}"')
file.write_text(contents)
if 'members' in m.get('workspace', ()):
for member in m['workspace']['members']:
update(file.parent / member / "Cargo.toml")
update(manifest_file)
@pytest.fixture(scope="session")
def rust_sysroot_src() -> str:
rust_sysroot = pathlib.Path(commands.run("rustc --print sysroot", _capture=True))
ret = rust_sysroot.joinpath("lib", "rustlib", "src", "rust", "library")
assert ret.exists()
return str(ret)
@pytest.fixture
def rust_project(cwd, rust_sysroot_src, rust_edition):
project_file = cwd / "rust-project.json"
assert project_file.exists()
project = json.loads(project_file.read_text())
project["sysroot_src"] = rust_sysroot_src
for c in project["crates"]:
c["edition"] = str(rust_edition)
project_file.write_text(json.dumps(project, indent=4))
(cwd / "Cargo.toml").unlink(missing_ok=True)
@pytest.fixture
def rust_check_diagnostics(check_diagnostics):
check_diagnostics.redact += [
"attributes.durations.*.ms",
"attributes.durations.*.pretty",
]
return check_diagnostics