Skip to content

Commit 63e5f5a

Browse files
author
Paolo Tranquilli
committed
Rust: parametrize some integration tests on three editions
1 parent 868680f commit 63e5f5a

28 files changed

+32
-7
lines changed

rust/ql/integration-tests/conftest.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,55 @@
22
import json
33
import commands
44
import pathlib
5+
import tomllib
6+
7+
8+
@pytest.fixture(params=[2018, 2021, 2024])
9+
def rust_edition(request):
10+
return request.param
511

612

713
@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()
1017
(cwd / "rust-project.json").unlink(missing_ok=True)
1118

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+
1234
@pytest.fixture(scope="session")
1335
def rust_sysroot_src() -> str:
1436
rust_sysroot = pathlib.Path(commands.run("rustc --print sysroot", _capture=True))
1537
ret = rust_sysroot.joinpath("lib", "rustlib", "src", "rust", "library")
1638
assert ret.exists()
1739
return str(ret)
1840

41+
1942
@pytest.fixture
20-
def rust_project(cwd, rust_sysroot_src):
43+
def rust_project(cwd, rust_sysroot_src, rust_edition):
2144
project_file = cwd / "rust-project.json"
2245
assert project_file.exists()
2346
project = json.loads(project_file.read_text())
2447
project["sysroot_src"] = rust_sysroot_src
48+
for c in project["crates"]:
49+
c["edition"] = str(rust_edition)
2550
project_file.write_text(json.dumps(project, indent=4))
2651
(cwd / "Cargo.toml").unlink(missing_ok=True)
2752

53+
2854
@pytest.fixture
2955
def rust_check_diagnostics(check_diagnostics):
3056
check_diagnostics.redact += [

rust/ql/integration-tests/hello-project/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
[package]
33
name = "hello-cargo"
44
version = "0.1.0"
5-
edition = "2021"
5+
edition = "2021" # replaced in test
66

77
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "exe"
33
version = "0.1.0"
4-
edition = "2021"
4+
edition = "2021" # replaced in test
55

66
[dependencies]
77
lib = { path = "../lib" }
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lib"
33
version = "0.1.0"
4-
edition = "2021"
4+
edition = "2021" # replaced in test
55

66
[dependencies]

rust/ql/integration-tests/hello-[workspace]/test_workspace.py renamed to rust/ql/integration-tests/hello-workspace/test_workspace.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22

3-
43
@pytest.mark.ql_test("steps.ql", expected=".cargo.expected")
54
@pytest.mark.ql_test("summary.qlref", expected=".cargo.expected")
65
def test_cargo(codeql, rust, cargo, check_source_archive, rust_check_diagnostics):

0 commit comments

Comments
 (0)