Rust: reorganize options integration tests

This commit is contained in:
Paolo Tranquilli
2024-11-11 13:11:10 +01:00
parent 07c59f7562
commit 34361b81eb
35 changed files with 101 additions and 85 deletions

View File

@@ -1 +0,0 @@
| src/arch.rs:7:1:8:13 | macos |

View File

@@ -1 +0,0 @@
| src/arch.rs:1:1:2:13 | linux |

View File

@@ -1 +0,0 @@
| src/arch.rs:4:1:5:15 | windows |

View File

@@ -1,5 +0,0 @@
import rust
from Function f
where f.getLocation().getFile().getBaseName() = "arch.rs" and f.hasExtendedCanonicalPath()
select f

View File

@@ -0,0 +1,5 @@
[workspace]
[package]
name = "cfg"
version = "0.1.0"
edition = "2021"

View File

@@ -0,0 +1,3 @@
| src/lib.rs:7:1:8:19 | cfg_no_flag |
| src/lib.rs:10:1:11:18 | cfg_no_key |
| src/lib.rs:16:1:17:24 | pointer_width_64 |

View File

@@ -0,0 +1,4 @@
| src/lib.rs:1:1:2:16 | cfg_flag |
| src/lib.rs:4:1:5:15 | cfg_key |
| src/lib.rs:13:1:14:12 | test |
| src/lib.rs:19:1:20:24 | pointer_width_32 |

View File

@@ -0,0 +1,5 @@
import rust
from Function f
where f.hasExtendedCanonicalPath()
select f

View File

@@ -13,5 +13,8 @@ fn cfg_no_key() {}
#[cfg(test)]
fn test() {}
#[cfg(target_pointer_width = "64")]
fn pointer_width_64() {}
#[cfg(target_pointer_width = "32")]
fn pointer_width_32() {}

View File

@@ -0,0 +1,19 @@
import pytest
import platform
import os
def test_default(codeql, rust):
codeql.database.create()
@pytest.mark.ql_test(expected=".override.expected")
def test_cfg_overrides(codeql, rust):
overrides = ",".join((
"cfg_flag",
"cfg_key=value",
"-target_pointer_width=64",
"target_pointer_width=32",
"test",
))
codeql.database.create(extractor_option=f"cargo_cfg_overrides={overrides}")

View File

@@ -1,2 +0,0 @@
| src/cfg.rs:7:1:8:19 | cfg_no_flag |
| src/cfg.rs:10:1:11:18 | cfg_no_key |

View File

@@ -1,4 +0,0 @@
| src/cfg.rs:1:1:2:16 | cfg_flag |
| src/cfg.rs:4:1:5:15 | cfg_key |
| src/cfg.rs:13:1:14:12 | test |
| src/cfg.rs:16:1:17:24 | pointer_width_32 |

View File

@@ -1,5 +0,0 @@
import rust
from Function f
where f.getLocation().getFile().getBaseName() = "cfg.rs" and f.hasExtendedCanonicalPath()
select f

View File

@@ -1,3 +0,0 @@
| src/features.rs:1:1:2:11 | foo |
| src/features.rs:4:1:5:11 | bar |
| src/features.rs:7:1:7:14 | always |

View File

@@ -1,2 +0,0 @@
| src/features.rs:4:1:5:11 | bar |
| src/features.rs:7:1:7:14 | always |

View File

@@ -1 +0,0 @@
| src/features.rs:7:1:7:14 | always |

View File

@@ -1,2 +0,0 @@
| src/features.rs:1:1:2:11 | foo |
| src/features.rs:7:1:7:14 | always |

View File

@@ -1,5 +0,0 @@
import rust
from Function f
where f.getLocation().getFile().getBaseName() = "features.rs" and f.hasExtendedCanonicalPath()
select f

View File

@@ -1,6 +1,6 @@
[workspace]
[package]
name = "options"
name = "features"
version = "0.1.0"
edition = "2021"

View File

@@ -0,0 +1,3 @@
| src/lib.rs:1:1:2:11 | foo |
| src/lib.rs:4:1:5:11 | bar |
| src/lib.rs:7:1:7:14 | always |

View File

@@ -0,0 +1,2 @@
| src/lib.rs:4:1:5:11 | bar |
| src/lib.rs:7:1:7:14 | always |

View File

@@ -0,0 +1 @@
| src/lib.rs:7:1:7:14 | always |

View File

@@ -0,0 +1,2 @@
| src/lib.rs:1:1:2:11 | foo |
| src/lib.rs:7:1:7:14 | always |

View File

@@ -0,0 +1,5 @@
import rust
from Function f
where f.hasExtendedCanonicalPath()
select f

View File

@@ -0,0 +1,16 @@
import pytest
@pytest.mark.parametrize("option",
[
pytest.param(p,
marks=pytest.mark.ql_test(expected=f".{e}.expected"))
for p, e in (
(None, "default"),
("cargo_features=foo", "foo"),
("cargo_features=bar", "bar"),
("cargo_features=*", "all"),
("cargo_features=foo,bar", "all"))
])
def test_features(codeql, rust, option):
codeql.database.create(extractor_option=option)

View File

@@ -1,3 +0,0 @@
mod arch;
mod features;
mod cfg;

View File

@@ -0,0 +1,5 @@
[workspace]
[package]
name = "target"
version = "0.1.0"
edition = "2021"

View File

@@ -0,0 +1 @@
| src/lib.rs:7:1:8:13 | macos |

View File

@@ -0,0 +1 @@
| src/lib.rs:1:1:2:13 | linux |

View File

@@ -0,0 +1 @@
| src/lib.rs:4:1:5:15 | windows |

View File

@@ -0,0 +1,5 @@
import rust
from Function f
where f.hasExtendedCanonicalPath()
select f

View File

@@ -0,0 +1,19 @@
import pytest
import platform
@pytest.mark.ql_test(expected=f".{platform.system()}.expected")
def test_default(codeql, rust):
codeql.database.create()
@pytest.mark.ql_test(expected=".Windows.expected")
def test_target_windows(codeql, rust):
codeql.database.create(extractor_option="cargo_target=x86_64-pc-windows-msvc")
@pytest.mark.ql_test(expected=".Darwin.expected")
def test_target_macos(codeql, rust):
codeql.database.create(extractor_option="cargo_target=aarch64-apple-darwin")
@pytest.mark.ql_test(expected=".Linux.expected")
def test_target_linux(codeql, rust):
codeql.database.create(extractor_option="cargo_target=x86_64-unknown-linux-gnu")

View File

@@ -1,49 +0,0 @@
import pytest
import platform
import os
@pytest.mark.ql_test("arch_functions.ql", expected=f".{platform.system()}.expected")
def test_default(codeql, rust):
codeql.database.create()
@pytest.mark.ql_test("arch_functions.ql", expected=".Windows.expected")
def test_target_windows(codeql, rust):
codeql.database.create(extractor_option="cargo_target=x86_64-pc-windows-msvc")
@pytest.mark.ql_test("arch_functions.ql", expected=".Darwin.expected")
def test_target_macos(codeql, rust):
codeql.database.create(extractor_option="cargo_target=aarch64-apple-darwin")
@pytest.mark.ql_test("arch_functions.ql", expected=".Linux.expected")
def test_target_linux(codeql, rust):
codeql.database.create(extractor_option="cargo_target=x86_64-unknown-linux-gnu")
@pytest.mark.ql_test("cfg_functions.ql", expected=".override.expected")
@pytest.mark.ql_test("arch_functions.ql", expected=f".{platform.system()}.expected")
def test_cfg_override(codeql, rust):
overrides = ",".join((
"cfg_flag",
"cfg_key=value",
"-target_pointer_width=64",
"target_pointer_width=32",
"test",
))
codeql.database.create(extractor_option=f"cargo_cfg_overrides={overrides}")
@pytest.mark.ql_test("arch_functions.ql", expected=f".{platform.system()}.expected")
@pytest.mark.parametrize("features",
[
pytest.param(p,
marks=pytest.mark.ql_test("feature_functions.ql", expected=f".{e}.expected"),
id=id)
for p, e, id in (
("foo", "foo", "foo"),
("bar", "bar", "bar"),
# as long as the integration test runner does not sanitize filenames we must
# replace `*` and `,` in the parameter id
("*", "all", "all"),
("foo,bar", "all", "foo+bar"))
])
def test_features(codeql, rust, features):
codeql.database.create(extractor_option=f"cargo_features={features}")