Skip to content

Commit 0356cdc

Browse files
authored
Merge pull request #18002 from github/redsun82/rust-ql-test-deps
Rust: add optional dependencies to ql tests
2 parents d178d84 + 75375be commit 0356cdc

File tree

7 files changed

+47
-9
lines changed

7 files changed

+47
-9
lines changed

rust/extractor/src/config.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct Config {
4343
pub inputs: Vec<PathBuf>,
4444
pub qltest: bool,
4545
pub qltest_cargo_check: bool,
46+
pub qltest_dependencies: Vec<String>,
4647
}
4748

4849
impl Config {
@@ -61,7 +62,13 @@ impl Config {
6162
.ancestors()
6263
// only travel up while we're within the test pack
6364
.take_while_inclusive(|p| !p.join("qlpack.yml").exists())
64-
.map(|p| p.join("options"))
65+
.flat_map(|p| {
66+
[
67+
p.join("options"),
68+
p.join("options.yml"),
69+
p.join("options.yaml"),
70+
]
71+
})
6572
.filter(|p| p.exists())
6673
.collect_vec();
6774
option_files.reverse();

rust/extractor/src/qltest.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn dump_lib() -> anyhow::Result<()> {
2121
fs::write("lib.rs", lib).context("writing lib.rs")
2222
}
2323

24-
fn dump_cargo_manifest() -> anyhow::Result<()> {
24+
fn dump_cargo_manifest(dependencies: &[String]) -> anyhow::Result<()> {
2525
let mut manifest = String::from(
2626
r#"[workspace]
2727
[package]
@@ -40,6 +40,13 @@ path = "main.rs"
4040
"#,
4141
);
4242
}
43+
if !dependencies.is_empty() {
44+
manifest.push_str("[dependencies]\n");
45+
for dep in dependencies {
46+
manifest.push_str(dep);
47+
manifest.push('\n');
48+
}
49+
}
4350
fs::write("Cargo.toml", manifest).context("writing Cargo.toml")
4451
}
4552

@@ -54,7 +61,7 @@ fn set_sources(config: &mut Config) -> anyhow::Result<()> {
5461
pub(crate) fn prepare(config: &mut Config) -> anyhow::Result<()> {
5562
dump_lib()?;
5663
set_sources(config)?;
57-
dump_cargo_manifest()?;
64+
dump_cargo_manifest(&config.qltest_dependencies)?;
5865
if config.qltest_cargo_check {
5966
let status = Command::new("cargo")
6067
.env("RUSTFLAGS", "-Awarnings")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.rs:4:1:7:1 | foo |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import rust
2+
3+
from Function f
4+
where exists(f.getLocation().getFile().getRelativePath())
5+
select f
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
qltest_cargo_check: true
2+
qltest_dependencies:
3+
- anyhow = "1.0.86"
4+
- log = "0.4.22"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use anyhow;
2+
use log::info;
3+
4+
fn foo() -> anyhow::Result<()> {
5+
info!("logging works");
6+
Ok(())
7+
}
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import runs_on
2+
import pytest
3+
24
# these tests are meant to exercise QL test running on multiple platforms
35
# therefore they don't rely on integration test built-in QL test running
46
# (which skips `qltest.{sh,cmd}`)
57

6-
def test_lib(codeql, rust, cwd):
7-
codeql.test.run("lib", threads=1)
8+
@pytest.fixture(autouse=True)
9+
def default_options(codeql):
10+
codeql.flags.update(
11+
threads = 1,
12+
show_extractor_output = True,
13+
check_databases = False,
14+
)
815

9-
def test_main(codeql, rust):
10-
codeql.test.run("main", threads=1)
16+
@pytest.mark.parametrize("dir", ["lib", "main", "dependencies"])
17+
def test(codeql, rust, dir):
18+
codeql.test.run(dir)
1119

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

0 commit comments

Comments
 (0)