|
2 | 2 | import json
|
3 | 3 | import commands
|
4 | 4 | import pathlib
|
| 5 | +import tomllib |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(params=[2018, 2021, 2024]) |
| 9 | +def rust_edition(request): |
| 10 | + return request.param |
5 | 11 |
|
6 | 12 |
|
7 | 13 | @pytest.fixture
|
8 |
| -def cargo(cwd): |
9 |
| - assert (cwd / "Cargo.toml").exists() |
| 14 | +def cargo(cwd, rust_edition): |
| 15 | + manifest_file = cwd / "Cargo.toml" |
| 16 | + assert manifest_file.exists() |
10 | 17 | (cwd / "rust-project.json").unlink(missing_ok=True)
|
11 | 18 |
|
| 19 | + def update(file): |
| 20 | + contents = file.read_text() |
| 21 | + m = tomllib.loads(contents) |
| 22 | + if 'package' in m: |
| 23 | + # tomllib does not support writing, and we don't want to use further dependencies |
| 24 | + # so we just do a dumb search and replace |
| 25 | + contents = contents.replace(f'edition = "{m["package"]["edition"]}"', f'edition = "{rust_edition}"') |
| 26 | + file.write_text(contents) |
| 27 | + if 'members' in m.get('workspace', ()): |
| 28 | + for member in m['workspace']['members']: |
| 29 | + update(file.parent / member / "Cargo.toml") |
| 30 | + |
| 31 | + update(manifest_file) |
| 32 | + |
| 33 | + |
12 | 34 | @pytest.fixture(scope="session")
|
13 | 35 | def rust_sysroot_src() -> str:
|
14 | 36 | rust_sysroot = pathlib.Path(commands.run("rustc --print sysroot", _capture=True))
|
15 | 37 | ret = rust_sysroot.joinpath("lib", "rustlib", "src", "rust", "library")
|
16 | 38 | assert ret.exists()
|
17 | 39 | return str(ret)
|
18 | 40 |
|
| 41 | + |
19 | 42 | @pytest.fixture
|
20 |
| -def rust_project(cwd, rust_sysroot_src): |
| 43 | +def rust_project(cwd, rust_sysroot_src, rust_edition): |
21 | 44 | project_file = cwd / "rust-project.json"
|
22 | 45 | assert project_file.exists()
|
23 | 46 | project = json.loads(project_file.read_text())
|
24 | 47 | project["sysroot_src"] = rust_sysroot_src
|
| 48 | + for c in project["crates"]: |
| 49 | + c["edition"] = str(rust_edition) |
25 | 50 | project_file.write_text(json.dumps(project, indent=4))
|
26 | 51 | (cwd / "Cargo.toml").unlink(missing_ok=True)
|
27 | 52 |
|
| 53 | + |
28 | 54 | @pytest.fixture
|
29 | 55 | def rust_check_diagnostics(check_diagnostics):
|
30 | 56 | check_diagnostics.redact += [
|
|
0 commit comments