Skip to content

Commit 8d9792a

Browse files
committed
Unified: Make test runnable under Bazel
1 parent 4e2ac7c commit 8d9792a

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

unified/extractor/BUILD.bazel

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_test")
12
load("//misc/bazel:rust.bzl", "codeql_rust_binary")
23
load("//misc/bazel/3rdparty/tree_sitter_extractors_deps:defs.bzl", "aliases", "all_crate_deps")
34
load("//unified:platforms.bzl", "UNIFIED_SUPPORTED_PLATFORMS")
@@ -29,3 +30,54 @@ codeql_rust_binary(
2930
"//unified/swift-syntax-rs:swift_syntax_rs",
3031
],
3132
)
33+
34+
_TESTS = {
35+
"corpus_tests": {
36+
"data": glob(["tests/corpus/**"]),
37+
"compile_data": [],
38+
"size": "medium",
39+
},
40+
# `include_str!`s a checked-in `parse_to_json` dump.
41+
"swift_syntax_pipeline": {
42+
"data": [],
43+
"compile_data": glob(["tests/fixtures/**"]),
44+
"size": "small",
45+
},
46+
}
47+
48+
[
49+
rust_test(
50+
name = test_name,
51+
size = spec["size"],
52+
srcs = ["tests/%s.rs" % test_name] + glob(["src/**/*.rs"]),
53+
aliases = aliases(),
54+
compile_data = [
55+
"ast_types.yml",
56+
"swift_node_types.yml",
57+
] + spec["compile_data"],
58+
crate_root = "tests/%s.rs" % test_name,
59+
data = spec["data"] + select({
60+
"@platforms//os:linux": ["//unified/swift-syntax-rs:swift_runtime_libs"],
61+
"//conditions:default": [],
62+
}),
63+
edition = "2024",
64+
proc_macro_deps = all_crate_deps(
65+
proc_macro = True,
66+
),
67+
rustc_flags = ["--cfg=bazel"],
68+
target_compatible_with = UNIFIED_SUPPORTED_PLATFORMS,
69+
deps = all_crate_deps(
70+
normal = True,
71+
) + [
72+
"//shared/tree-sitter-extractor",
73+
"//shared/yeast",
74+
"//unified/swift-syntax-rs:swift_syntax_rs",
75+
],
76+
)
77+
for test_name, spec in _TESTS.items()
78+
]
79+
80+
test_suite(
81+
name = "all_tests",
82+
tests = [":%s" % test_name for test_name in _TESTS],
83+
)

unified/extractor/tests/corpus_tests.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,30 @@ fn collect_corpus_stems(dir: &Path, out: &mut Vec<std::path::PathBuf>) {
9696
}
9797
}
9898

99+
#[cfg(bazel)]
100+
fn corpus_dir() -> std::path::PathBuf {
101+
let base = std::path::PathBuf::from(
102+
std::env::var("RUNFILES_DIR").expect("RUNFILES_DIR not set"),
103+
);
104+
std::fs::read_dir(&base)
105+
.expect("failed to read RUNFILES_DIR")
106+
.filter_map(Result::ok)
107+
.map(|entry| entry.path().join("unified/extractor/tests/corpus"))
108+
.find(|path| path.exists())
109+
.expect("corpus not found under any runfiles repo root")
110+
}
111+
112+
#[cfg(not(bazel))]
113+
fn corpus_dir() -> std::path::PathBuf {
114+
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/corpus")
115+
}
116+
117+
99118
#[test]
100119
fn test_corpus() {
101120
let update_mode = update_mode_enabled();
102121
let all_languages = languages::all_language_specs();
103-
let corpus_dir = Path::new("tests/corpus");
122+
let corpus_dir = corpus_dir();
104123

105124
for lang in all_languages {
106125
let output_schema = yeast::node_types_yaml::schema_from_yaml(languages::OUTPUT_AST_SCHEMA)

0 commit comments

Comments
 (0)