Rust: parametrize some integration tests on three editions

This commit is contained in:
Paolo Tranquilli
2025-04-11 16:50:23 +02:00
parent 868680f078
commit 63e5f5a555
28 changed files with 32 additions and 7 deletions

View File

@@ -2,13 +2,35 @@ 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):
assert (cwd / "Cargo.toml").exists()
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))
@@ -16,15 +38,19 @@ def rust_sysroot_src() -> str:
assert ret.exists()
return str(ret)
@pytest.fixture
def rust_project(cwd, rust_sysroot_src):
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 += [

View File

@@ -2,6 +2,6 @@
[package]
name = "hello-cargo"
version = "0.1.0"
edition = "2021"
edition = "2021" # replaced in test
[dependencies]

View File

@@ -1,7 +1,7 @@
[package]
name = "exe"
version = "0.1.0"
edition = "2021"
edition = "2021" # replaced in test
[dependencies]
lib = { path = "../lib" }

View File

@@ -1,6 +1,6 @@
[package]
name = "lib"
version = "0.1.0"
edition = "2021"
edition = "2021" # replaced in test
[dependencies]

View File

@@ -1,6 +1,5 @@
import pytest
@pytest.mark.ql_test("steps.ql", expected=".cargo.expected")
@pytest.mark.ql_test("summary.qlref", expected=".cargo.expected")
def test_cargo(codeql, rust, cargo, check_source_archive, rust_check_diagnostics):