From aae33db13711304b33ec702c4517711193a59d56 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Tue, 17 Sep 2024 10:43:59 +0200 Subject: [PATCH] Rust: add basic integration tests This adds testing of well-formed rust projects and workspaces, using both `Cargo.toml` and `rust-project.json` manifests. --- rust/integration-tests/conftest.py | 15 +++++++++++++++ .../hello-project/functions.expected | 1 + rust/integration-tests/hello-project/functions.ql | 5 +++++ .../hello-project/manifests/Cargo.toml | 7 +++++++ .../hello-project/manifests/rust-project.json | 7 +++++++ .../hello-project/source_archive.expected | 4 ++++ .../hello-project/src/directory_module/ignored.rs | 0 .../hello-project/src/directory_module/mod.rs | 1 + .../src/directory_module/nested_module.rs | 0 .../hello-project/src/file_module.rs | 0 rust/integration-tests/hello-project/src/main.rs | 6 ++++++ .../hello-project/test_project.py | 7 +++++++ .../hello-workspace/exe/Cargo.toml | 7 +++++++ .../hello-workspace/exe/src/a_module.rs | 0 .../hello-workspace/exe/src/main.rs | 7 +++++++ .../hello-workspace/functions.expected | 2 ++ .../hello-workspace/functions.ql | 5 +++++ .../hello-workspace/lib/Cargo.toml | 6 ++++++ .../hello-workspace/lib/src/a_module/mod.rs | 3 +++ .../hello-workspace/lib/src/lib.rs | 1 + .../hello-workspace/manifests/Cargo.toml | 3 +++ .../hello-workspace/manifests/rust-project.json | 14 ++++++++++++++ .../hello-workspace/source_archive.expected | 4 ++++ .../hello-workspace/test_workspace.py | 7 +++++++ rust/integration-tests/qlpack.lock.yml | 4 ++++ rust/integration-tests/qlpack.yml | 8 ++++++++ rust/integration-tests/test.py | 4 ---- 27 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 rust/integration-tests/conftest.py create mode 100644 rust/integration-tests/hello-project/functions.expected create mode 100644 rust/integration-tests/hello-project/functions.ql create mode 100644 rust/integration-tests/hello-project/manifests/Cargo.toml create mode 100644 rust/integration-tests/hello-project/manifests/rust-project.json create mode 100644 rust/integration-tests/hello-project/source_archive.expected create mode 100644 rust/integration-tests/hello-project/src/directory_module/ignored.rs create mode 100644 rust/integration-tests/hello-project/src/directory_module/mod.rs create mode 100644 rust/integration-tests/hello-project/src/directory_module/nested_module.rs create mode 100644 rust/integration-tests/hello-project/src/file_module.rs create mode 100644 rust/integration-tests/hello-project/src/main.rs create mode 100644 rust/integration-tests/hello-project/test_project.py create mode 100644 rust/integration-tests/hello-workspace/exe/Cargo.toml create mode 100644 rust/integration-tests/hello-workspace/exe/src/a_module.rs create mode 100644 rust/integration-tests/hello-workspace/exe/src/main.rs create mode 100644 rust/integration-tests/hello-workspace/functions.expected create mode 100644 rust/integration-tests/hello-workspace/functions.ql create mode 100644 rust/integration-tests/hello-workspace/lib/Cargo.toml create mode 100644 rust/integration-tests/hello-workspace/lib/src/a_module/mod.rs create mode 100644 rust/integration-tests/hello-workspace/lib/src/lib.rs create mode 100644 rust/integration-tests/hello-workspace/manifests/Cargo.toml create mode 100644 rust/integration-tests/hello-workspace/manifests/rust-project.json create mode 100644 rust/integration-tests/hello-workspace/source_archive.expected create mode 100644 rust/integration-tests/hello-workspace/test_workspace.py create mode 100644 rust/integration-tests/qlpack.lock.yml create mode 100644 rust/integration-tests/qlpack.yml delete mode 100644 rust/integration-tests/test.py diff --git a/rust/integration-tests/conftest.py b/rust/integration-tests/conftest.py new file mode 100644 index 00000000000..08b17f106f8 --- /dev/null +++ b/rust/integration-tests/conftest.py @@ -0,0 +1,15 @@ +import pytest +import shutil + +class _Manifests: + def __init__(self, cwd): + self.dir = cwd / "manifests" + + def select(self, name: str): + (self.dir / name).rename(name) + shutil.rmtree(self.dir) + + +@pytest.fixture +def manifests(cwd): + return _Manifests(cwd) diff --git a/rust/integration-tests/hello-project/functions.expected b/rust/integration-tests/hello-project/functions.expected new file mode 100644 index 00000000000..fc49564084e --- /dev/null +++ b/rust/integration-tests/hello-project/functions.expected @@ -0,0 +1 @@ +| src/main.rs:4:1:6:1 | main | diff --git a/rust/integration-tests/hello-project/functions.ql b/rust/integration-tests/hello-project/functions.ql new file mode 100644 index 00000000000..8d9a3213dad --- /dev/null +++ b/rust/integration-tests/hello-project/functions.ql @@ -0,0 +1,5 @@ +import rust + +from Function f +where exists(f.getLocation().getFile().getRelativePath()) +select f diff --git a/rust/integration-tests/hello-project/manifests/Cargo.toml b/rust/integration-tests/hello-project/manifests/Cargo.toml new file mode 100644 index 00000000000..909b5b525ff --- /dev/null +++ b/rust/integration-tests/hello-project/manifests/Cargo.toml @@ -0,0 +1,7 @@ +[workspace] +[package] +name = "hello-cargo" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/rust/integration-tests/hello-project/manifests/rust-project.json b/rust/integration-tests/hello-project/manifests/rust-project.json new file mode 100644 index 00000000000..03d9a5ce2c0 --- /dev/null +++ b/rust/integration-tests/hello-project/manifests/rust-project.json @@ -0,0 +1,7 @@ +{ + "crates": [{ + "root_module": "src/main.rs", + "edition": "2021", + "deps": [] + }] +} diff --git a/rust/integration-tests/hello-project/source_archive.expected b/rust/integration-tests/hello-project/source_archive.expected new file mode 100644 index 00000000000..1e88c9d0e7a --- /dev/null +++ b/rust/integration-tests/hello-project/source_archive.expected @@ -0,0 +1,4 @@ +src/directory_module/mod.rs +src/directory_module/nested_module.rs +src/file_module.rs +src/main.rs diff --git a/rust/integration-tests/hello-project/src/directory_module/ignored.rs b/rust/integration-tests/hello-project/src/directory_module/ignored.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/rust/integration-tests/hello-project/src/directory_module/mod.rs b/rust/integration-tests/hello-project/src/directory_module/mod.rs new file mode 100644 index 00000000000..cd90d98055c --- /dev/null +++ b/rust/integration-tests/hello-project/src/directory_module/mod.rs @@ -0,0 +1 @@ +mod nested_module; diff --git a/rust/integration-tests/hello-project/src/directory_module/nested_module.rs b/rust/integration-tests/hello-project/src/directory_module/nested_module.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/rust/integration-tests/hello-project/src/file_module.rs b/rust/integration-tests/hello-project/src/file_module.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/rust/integration-tests/hello-project/src/main.rs b/rust/integration-tests/hello-project/src/main.rs new file mode 100644 index 00000000000..c055220a51d --- /dev/null +++ b/rust/integration-tests/hello-project/src/main.rs @@ -0,0 +1,6 @@ +mod file_module; +mod directory_module; + +fn main() { + println!("Hello, world!"); +} diff --git a/rust/integration-tests/hello-project/test_project.py b/rust/integration-tests/hello-project/test_project.py new file mode 100644 index 00000000000..d03c4f67e26 --- /dev/null +++ b/rust/integration-tests/hello-project/test_project.py @@ -0,0 +1,7 @@ +def test_cargo(codeql, rust, manifests, check_source_archive): + manifests.select("Cargo.toml") + codeql.database.create() + +def test_rust_project(codeql, rust, manifests, check_source_archive): + manifests.select("rust-project.json") + codeql.database.create() diff --git a/rust/integration-tests/hello-workspace/exe/Cargo.toml b/rust/integration-tests/hello-workspace/exe/Cargo.toml new file mode 100644 index 00000000000..b63ccfd5da5 --- /dev/null +++ b/rust/integration-tests/hello-workspace/exe/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "exe" +version = "0.1.0" +edition = "2021" + +[dependencies] +lib = { path = "../lib" } diff --git a/rust/integration-tests/hello-workspace/exe/src/a_module.rs b/rust/integration-tests/hello-workspace/exe/src/a_module.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/rust/integration-tests/hello-workspace/exe/src/main.rs b/rust/integration-tests/hello-workspace/exe/src/main.rs new file mode 100644 index 00000000000..3c46784465a --- /dev/null +++ b/rust/integration-tests/hello-workspace/exe/src/main.rs @@ -0,0 +1,7 @@ +use lib::a_module::hello; + +mod a_module; + +fn main() { + hello(); +} diff --git a/rust/integration-tests/hello-workspace/functions.expected b/rust/integration-tests/hello-workspace/functions.expected new file mode 100644 index 00000000000..0ec9937972e --- /dev/null +++ b/rust/integration-tests/hello-workspace/functions.expected @@ -0,0 +1,2 @@ +| exe/src/main.rs:5:1:7:1 | main | +| lib/src/a_module/mod.rs:1:1:3:1 | hello | diff --git a/rust/integration-tests/hello-workspace/functions.ql b/rust/integration-tests/hello-workspace/functions.ql new file mode 100644 index 00000000000..8d9a3213dad --- /dev/null +++ b/rust/integration-tests/hello-workspace/functions.ql @@ -0,0 +1,5 @@ +import rust + +from Function f +where exists(f.getLocation().getFile().getRelativePath()) +select f diff --git a/rust/integration-tests/hello-workspace/lib/Cargo.toml b/rust/integration-tests/hello-workspace/lib/Cargo.toml new file mode 100644 index 00000000000..e8fc5405b71 --- /dev/null +++ b/rust/integration-tests/hello-workspace/lib/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "lib" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/rust/integration-tests/hello-workspace/lib/src/a_module/mod.rs b/rust/integration-tests/hello-workspace/lib/src/a_module/mod.rs new file mode 100644 index 00000000000..fda8464098e --- /dev/null +++ b/rust/integration-tests/hello-workspace/lib/src/a_module/mod.rs @@ -0,0 +1,3 @@ +pub fn hello() { + println!("Hello, world!"); +} diff --git a/rust/integration-tests/hello-workspace/lib/src/lib.rs b/rust/integration-tests/hello-workspace/lib/src/lib.rs new file mode 100644 index 00000000000..e5b38ec20f6 --- /dev/null +++ b/rust/integration-tests/hello-workspace/lib/src/lib.rs @@ -0,0 +1 @@ +pub mod a_module; diff --git a/rust/integration-tests/hello-workspace/manifests/Cargo.toml b/rust/integration-tests/hello-workspace/manifests/Cargo.toml new file mode 100644 index 00000000000..6ddb463a27e --- /dev/null +++ b/rust/integration-tests/hello-workspace/manifests/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +members = [ "exe", "lib" ] +resolver = "2" diff --git a/rust/integration-tests/hello-workspace/manifests/rust-project.json b/rust/integration-tests/hello-workspace/manifests/rust-project.json new file mode 100644 index 00000000000..38e16980196 --- /dev/null +++ b/rust/integration-tests/hello-workspace/manifests/rust-project.json @@ -0,0 +1,14 @@ +{ + "crates": [ + { + "root_module": "exe/src/main.rs", + "edition": "2021", + "deps": [{"crate": 1, "name": "lib"}] + }, + { + "root_module": "lib/src/lib.rs", + "edition": "2021", + "deps": [] + } + ] +} diff --git a/rust/integration-tests/hello-workspace/source_archive.expected b/rust/integration-tests/hello-workspace/source_archive.expected new file mode 100644 index 00000000000..85650c2b512 --- /dev/null +++ b/rust/integration-tests/hello-workspace/source_archive.expected @@ -0,0 +1,4 @@ +exe/src/a_module.rs +exe/src/main.rs +lib/src/a_module/mod.rs +lib/src/lib.rs diff --git a/rust/integration-tests/hello-workspace/test_workspace.py b/rust/integration-tests/hello-workspace/test_workspace.py new file mode 100644 index 00000000000..d03c4f67e26 --- /dev/null +++ b/rust/integration-tests/hello-workspace/test_workspace.py @@ -0,0 +1,7 @@ +def test_cargo(codeql, rust, manifests, check_source_archive): + manifests.select("Cargo.toml") + codeql.database.create() + +def test_rust_project(codeql, rust, manifests, check_source_archive): + manifests.select("rust-project.json") + codeql.database.create() diff --git a/rust/integration-tests/qlpack.lock.yml b/rust/integration-tests/qlpack.lock.yml new file mode 100644 index 00000000000..06dd07fc7dc --- /dev/null +++ b/rust/integration-tests/qlpack.lock.yml @@ -0,0 +1,4 @@ +--- +dependencies: {} +compiled: false +lockVersion: 1.0.0 diff --git a/rust/integration-tests/qlpack.yml b/rust/integration-tests/qlpack.yml new file mode 100644 index 00000000000..e81e4c646c3 --- /dev/null +++ b/rust/integration-tests/qlpack.yml @@ -0,0 +1,8 @@ +name: codeql/rust-integration-tests +groups: [rust, test] +dependencies: + codeql/rust-queries: ${workspace} + codeql/rust-all: ${workspace} +extractor: rust +tests: . +warnOnImplicitThis: true diff --git a/rust/integration-tests/test.py b/rust/integration-tests/test.py deleted file mode 100644 index b2d446decd2..00000000000 --- a/rust/integration-tests/test.py +++ /dev/null @@ -1,4 +0,0 @@ -# dummy test to get CI going - -def test(codeql, rust): - pass