Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: add optional dependencies to ql tests #18002

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion rust/extractor/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct Config {
pub inputs: Vec<PathBuf>,
pub qltest: bool,
pub qltest_cargo_check: bool,
pub qltest_dependencies: Vec<String>,
}

impl Config {
Expand All @@ -61,7 +62,13 @@ impl Config {
.ancestors()
// only travel up while we're within the test pack
.take_while_inclusive(|p| !p.join("qlpack.yml").exists())
.map(|p| p.join("options"))
.flat_map(|p| {
[
p.join("options"),
p.join("options.yml"),
p.join("options.yaml"),
]
})
.filter(|p| p.exists())
.collect_vec();
option_files.reverse();
Expand Down
11 changes: 9 additions & 2 deletions rust/extractor/src/qltest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn dump_lib() -> anyhow::Result<()> {
fs::write("lib.rs", lib).context("writing lib.rs")
}

fn dump_cargo_manifest() -> anyhow::Result<()> {
fn dump_cargo_manifest(dependencies: &[String]) -> anyhow::Result<()> {
let mut manifest = String::from(
r#"[workspace]
[package]
Expand All @@ -40,6 +40,13 @@ path = "main.rs"
"#,
);
}
if !dependencies.is_empty() {
manifest.push_str("[dependencies]\n");
for dep in dependencies {
manifest.push_str(dep);
manifest.push('\n');
}
}
fs::write("Cargo.toml", manifest).context("writing Cargo.toml")
}

Expand All @@ -54,7 +61,7 @@ fn set_sources(config: &mut Config) -> anyhow::Result<()> {
pub(crate) fn prepare(config: &mut Config) -> anyhow::Result<()> {
dump_lib()?;
set_sources(config)?;
dump_cargo_manifest()?;
dump_cargo_manifest(&config.qltest_dependencies)?;
if config.qltest_cargo_check {
let status = Command::new("cargo")
.env("RUSTFLAGS", "-Awarnings")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| test.rs:4:1:7:1 | foo |
5 changes: 5 additions & 0 deletions rust/ql/integration-tests/qltest/dependencies/functions.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import rust

from Function f
where exists(f.getLocation().getFile().getRelativePath())
select f
4 changes: 4 additions & 0 deletions rust/ql/integration-tests/qltest/dependencies/options.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
qltest_cargo_check: true
qltest_dependencies:
- anyhow = "1.0.86"
- log = "0.4.22"
7 changes: 7 additions & 0 deletions rust/ql/integration-tests/qltest/dependencies/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use anyhow;
use log::info;

fn foo() -> anyhow::Result<()> {
info!("logging works");
Ok(())
}
19 changes: 13 additions & 6 deletions rust/ql/integration-tests/qltest/test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import runs_on
import pytest

# these tests are meant to exercise QL test running on multiple platforms
# therefore they don't rely on integration test built-in QL test running
# (which skips `qltest.{sh,cmd}`)

def test_lib(codeql, rust, cwd):
codeql.test.run("lib", threads=1)
@pytest.fixture(autouse=True)
def default_options(codeql):
codeql.flags.update(
threads = 1,
show_extractor_output = True,
check_databases = False,
)

def test_main(codeql, rust):
codeql.test.run("main", threads=1)
@pytest.mark.parametrize("dir", ["lib", "main", "dependencies"])
def test(codeql, rust, dir):
codeql.test.run(dir)

def test_failing_cargo_check(codeql, rust):
out = codeql.test.run("failing_cargo_check", threads=1, show_extractor_output=True,
_assert_failure=True, _capture="stderr")
out = codeql.test.run("failing_cargo_check", _assert_failure=True, _capture="stderr")
# TODO: QL test output redirection is currently broken on windows, leaving it up for follow-up work
if not runs_on.windows:
assert "requested cargo check failed" in out