Skip to content

Commit 5a0ded8

Browse files
authored
Enable more tests on Windows (#3348)
1 parent bfb06e6 commit 5a0ded8

File tree

22 files changed

+287
-139
lines changed

22 files changed

+287
-139
lines changed

.bazelci/presubmit.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,6 @@ default_windows_targets: &default_windows_targets
7272
default_windows_no_runfiles_targets: &default_windows_no_runfiles_targets
7373
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
7474
- "//..."
75-
# TODO: https://github.com/bazelbuild/rules_rust/issues/1156
76-
- "-//crate_universe/..."
77-
- "-//test/chained_direct_deps:mod3_doc_test"
78-
- "-//test/out_dir_in_tests:demo_lib_doc_test"
79-
- "-//test/rustc_env_files:output_test"
80-
- "-//test/test_env_launcher:test"
81-
- "-//test/test_env:test_manifest_dir"
82-
- "-//test/test_env:test_run"
83-
- "-//test/unit/rustdoc/..."
84-
- "-//rust/runfiles/..."
85-
# Runfiles used by the test only.
86-
- "-//test/empty_suite:deps_test"
87-
- "-//cargo/cargo_toml_variable_extractor/testdata/..."
8875
crate_universe_vendor_example_targets: &crate_universe_vendor_example_targets
8976
- "//vendor_external:crates_vendor"
9077
- "//vendor_local_manifests:crates_vendor"

cargo/cargo_toml_variable_extractor/testdata/BUILD.bazel

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
21
load("//cargo:defs.bzl", "cargo_toml_env_vars")
3-
load("//rust:defs.bzl", "rust_binary")
2+
load("//rust:defs.bzl", "rust_binary", "rust_test")
3+
4+
# TODO: This target currently fails on windows.
5+
NOT_WINDOWS = select({
6+
"@platforms//os:windows": ["@platforms//:incompatible"],
7+
"//conditions:default": [],
8+
})
49

510
genrule(
611
name = "gen_empty_cargo_output",
7-
srcs = glob(["empty/**"]),
12+
srcs = glob([
13+
"empty/**/*.toml",
14+
"empty/**/*.rs",
15+
]),
816
outs = ["empty_cargo_output"],
9-
cmd = "RUSTC=$(location //tools/upstream_wrapper:rustc) $(location //tools/upstream_wrapper:cargo) run --manifest-path=cargo/cargo_toml_variable_extractor/testdata/empty/Cargo.toml > $@",
17+
cmd = "RUSTC=$(execpath //tools/upstream_wrapper:rustc) $(execpath //tools/upstream_wrapper:cargo) run --manifest-path=cargo/cargo_toml_variable_extractor/testdata/empty/Cargo.toml > $@",
18+
cmd_bat = "set RUSTC=$(execpath //tools/upstream_wrapper:rustc) && $(execpath //tools/upstream_wrapper:cargo) run --manifest-path=cargo\\cargo_toml_variable_extractor\\testdata\\empty\\Cargo.toml > $@",
19+
target_compatible_with = NOT_WINDOWS,
1020
tools = [
1121
"//tools/upstream_wrapper:cargo",
1222
"//tools/upstream_wrapper:rustc",
@@ -28,21 +38,38 @@ rust_binary(
2838
genrule(
2939
name = "gen_empty_bazel_output",
3040
outs = ["empty_bazel_output"],
31-
cmd = "$(location :empty_bin) > $@",
41+
cmd = "$(execpath :empty_bin) > $@",
42+
cmd_bat = "$(execpath :empty_bin) > $@",
3243
tools = [":empty_bin"],
3344
)
3445

35-
diff_test(
46+
rust_test(
3647
name = "empty_diff_test",
37-
file1 = ":empty_cargo_output",
38-
file2 = ":empty_bazel_output",
48+
srcs = ["diff_test.rs"],
49+
data = [
50+
":empty_bazel_output",
51+
":empty_cargo_output",
52+
],
53+
edition = "2021",
54+
env = {
55+
"FILE_1": "$(rlocationpath :empty_cargo_output)",
56+
"FILE_2": "$(rlocationpath :empty_bazel_output)",
57+
},
58+
deps = [
59+
"//rust/runfiles",
60+
],
3961
)
4062

4163
genrule(
4264
name = "gen_standalone_cargo_output",
43-
srcs = glob(["standalone/**"]),
65+
srcs = glob([
66+
"standalone/**/*.toml",
67+
"standalone/**/*.rs",
68+
]),
4469
outs = ["standalone_cargo_output"],
45-
cmd = "RUSTC=$(location //tools/upstream_wrapper:rustc) $(location //tools/upstream_wrapper:cargo) run --manifest-path=cargo/cargo_toml_variable_extractor/testdata/standalone/Cargo.toml > $@",
70+
cmd = "RUSTC=$(execpath //tools/upstream_wrapper:rustc) $(execpath //tools/upstream_wrapper:cargo) run --manifest-path=cargo/cargo_toml_variable_extractor/testdata/standalone/Cargo.toml > $@",
71+
cmd_bat = "set RUSTC=$(execpath //tools/upstream_wrapper:rustc) && $(execpath //tools/upstream_wrapper:cargo) run --manifest-path=cargo\\cargo_toml_variable_extractor\\testdata\\standalone\\Cargo.toml > $@",
72+
target_compatible_with = NOT_WINDOWS,
4673
tools = [
4774
"//rust/toolchain:current_rust_stdlib_files",
4875
"//tools/upstream_wrapper:cargo",
@@ -65,24 +92,42 @@ rust_binary(
6592
genrule(
6693
name = "gen_standalone_bazel_output",
6794
outs = ["standalone_bazel_output"],
68-
cmd = "$(location :standalone_bin) > $@",
95+
cmd = "$(execpath :standalone_bin) > $@",
96+
cmd_bat = "$(execpath :standalone_bin) > $@",
6997
tools = [":standalone_bin"],
7098
)
7199

72-
diff_test(
100+
rust_test(
73101
name = "standalone_diff_test",
74-
file1 = ":standalone_cargo_output",
75-
file2 = ":standalone_bazel_output",
102+
srcs = ["diff_test.rs"],
103+
data = [
104+
":standalone_bazel_output",
105+
":standalone_cargo_output",
106+
],
107+
edition = "2021",
108+
env = {
109+
"FILE_1": "$(rlocationpath :standalone_cargo_output)",
110+
"FILE_2": "$(rlocationpath :standalone_bazel_output)",
111+
},
112+
deps = [
113+
"//rust/runfiles",
114+
],
76115
)
77116

78117
genrule(
79118
name = "gen_workspace_subcrate_cargo_output",
80119
srcs = glob(
81-
["workspace/**"],
120+
include = [
121+
"workspace/**/*.toml",
122+
"workspace/**/*.rs",
123+
"workspace/**/.gitignore",
124+
],
82125
exclude = ["workspace/target/**"],
83126
),
84127
outs = ["workspace_subcrate_cargo_output"],
85-
cmd = "RUSTC=$(location //tools/upstream_wrapper:rustc) $(location //tools/upstream_wrapper:cargo) run --quiet --manifest-path=cargo/cargo_toml_variable_extractor/testdata/workspace/subcrate/Cargo.toml > $@",
128+
cmd = "RUSTC=$(execpath //tools/upstream_wrapper:rustc) $(execpath //tools/upstream_wrapper:cargo) run --quiet --manifest-path=cargo/cargo_toml_variable_extractor/testdata/workspace/subcrate/Cargo.toml > $@",
129+
cmd_bat = "set RUSTC=$(execpath //tools/upstream_wrapper:rustc) && $(execpath //tools/upstream_wrapper:cargo) run --quiet --manifest-path=cargo\\cargo_toml_variable_extractor\\testdata\\workspace\\subcrate\\Cargo.toml > $@",
130+
target_compatible_with = NOT_WINDOWS,
86131
tools = [
87132
"//rust/toolchain:current_rust_stdlib_files",
88133
"//tools/upstream_wrapper:cargo",
@@ -106,12 +151,24 @@ rust_binary(
106151
genrule(
107152
name = "gen_workspace_subcrate_bazel_output",
108153
outs = ["workspace_subcrate_bazel_output"],
109-
cmd = "$(location :workspace_subcrate_bin) > $@",
154+
cmd = "$(execpath :workspace_subcrate_bin) > $@",
155+
cmd_bat = "$(execpath :workspace_subcrate_bin) > $@",
110156
tools = [":workspace_subcrate_bin"],
111157
)
112158

113-
diff_test(
159+
rust_test(
114160
name = "workspace_subcrate_diff_test",
115-
file1 = ":workspace_subcrate_cargo_output",
116-
file2 = ":workspace_subcrate_bazel_output",
161+
srcs = ["diff_test.rs"],
162+
data = [
163+
":workspace_subcrate_bazel_output",
164+
":workspace_subcrate_cargo_output",
165+
],
166+
edition = "2021",
167+
env = {
168+
"FILE_1": "$(rlocationpath :workspace_subcrate_cargo_output)",
169+
"FILE_2": "$(rlocationpath :workspace_subcrate_bazel_output)",
170+
},
171+
deps = [
172+
"//rust/runfiles",
173+
],
117174
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[test]
2+
fn diff_test() {
3+
let runfiles = runfiles::Runfiles::create().unwrap();
4+
5+
let file_1_env = std::env::var("FILE_1").unwrap();
6+
let file_2_env = std::env::var("FILE_2").unwrap();
7+
8+
let file_1 = runfiles::rlocation!(runfiles, &file_1_env).unwrap();
9+
let file_2 = runfiles::rlocation!(runfiles, &file_2_env).unwrap();
10+
11+
let file_1_content = std::fs::read_to_string(&file_1).unwrap();
12+
let file_2_content = std::fs::read_to_string(&file_2).unwrap();
13+
14+
let file_1_lines = file_1_content.split_whitespace().collect::<Vec<&str>>();
15+
let file_2_lines = file_2_content.split_whitespace().collect::<Vec<&str>>();
16+
17+
assert_eq!(
18+
file_1_lines,
19+
file_2_lines,
20+
"Files `{}` and `{}` differ.",
21+
file_1.display(),
22+
file_2.display()
23+
);
24+
}

crate_universe/BUILD.bazel

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ rust_test(
109109
"CARGO": "$(rootpath @rules_rust//rust/toolchain:current_cargo_files)",
110110
"RUSTC": "$(rootpath @rules_rust//rust/toolchain:current_rustc_files)",
111111
},
112+
# TODO: `crate_universe` currently does not work on Windows.
113+
# https://github.com/bazelbuild/rules_rust/issues/1156
114+
target_compatible_with = select({
115+
"@platforms//os:windows": ["@platforms//:incompatible"],
116+
"//conditions:default": [],
117+
}),
112118
deps = [
113119
"@rules_rust//rust/runfiles",
114120
] + all_crate_deps(
@@ -136,6 +142,12 @@ rust_test(
136142
proc_macro_dev = True,
137143
),
138144
tags = ["requires-network"],
145+
# TODO: `crate_universe` currently does not work on Windows.
146+
# https://github.com/bazelbuild/rules_rust/issues/1156
147+
target_compatible_with = select({
148+
"@platforms//os:windows": ["@platforms//:incompatible"],
149+
"//conditions:default": [],
150+
}),
139151
deps = [
140152
":cargo_bazel",
141153
"@rules_rust//rust/runfiles",
@@ -153,9 +165,10 @@ rust_test(
153165
],
154166
edition = "2021",
155167
rustc_env = {
156-
"CARGO_TOML": "$(rootpath :Cargo.toml)",
157-
"VERSION_BZL": "$(rootpath :version.bzl)",
168+
"CARGO_TOML": "$(rlocationpath :Cargo.toml)",
169+
"VERSION_BZL": "$(rlocationpath :version.bzl)",
158170
},
171+
deps = ["//rust/runfiles"],
159172
)
160173

161174
rust_doc(
@@ -172,4 +185,10 @@ rust_doc(
172185
rust_doc_test(
173186
name = "rustdoc_test",
174187
crate = ":cargo_bazel",
188+
# TODO: `rust_doc_test` currently does not work on Windows.
189+
# https://github.com/bazelbuild/rules_rust/issues/1156
190+
target_compatible_with = select({
191+
"@platforms//os:windows": ["@platforms//:incompatible"],
192+
"//conditions:default": [],
193+
}),
175194
)

crate_universe/tests/version_test.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ use std::fs::File;
44
use std::io::{BufRead, BufReader};
55
use std::path::PathBuf;
66

7+
fn cargo_toml_path() -> PathBuf {
8+
let r = runfiles::Runfiles::create().unwrap();
9+
runfiles::rlocation!(r, env!("CARGO_TOML")).unwrap()
10+
}
11+
12+
fn version_bzl_path() -> PathBuf {
13+
let r = runfiles::Runfiles::create().unwrap();
14+
runfiles::rlocation!(r, env!("VERSION_BZL")).unwrap()
15+
}
16+
717
#[test]
818
fn test_cargo_and_bazel_versions() {
919
// Parse the version field from the `cargo-bazel` Cargo.toml file
1020
let cargo_version = {
11-
let cargo_path = PathBuf::from(env!("CARGO_TOML"));
21+
let cargo_path = cargo_toml_path();
1222
let file = File::open(cargo_path).expect("Failed to load Cargo.toml file");
1323
BufReader::new(file)
1424
.lines()
@@ -25,7 +35,7 @@ fn test_cargo_and_bazel_versions() {
2535

2636
// Parse the version global from the Bazel module
2737
let bazel_version = {
28-
let bazel_path = PathBuf::from(env!("VERSION_BZL"));
38+
let bazel_path = version_bzl_path();
2939
let file = File::open(bazel_path).expect("Failed to load versions.bzl file");
3040
BufReader::new(file)
3141
.lines()

rust/runfiles/runfiles.rs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,55 @@ mod test {
407407
assert_eq!(original_name, std::env::var("TEST_WORKSPACE").unwrap());
408408
}
409409

410+
/// Create a temp directory to act as a runfiles directory for testing
411+
/// [super::Mode::DirectoryBased] style runfiles.
412+
fn make_runfiles_like_dir(name: &str) -> String {
413+
with_mock_env([("FAKE", None::<&str>)], || {
414+
let r = Runfiles::create().unwrap();
415+
416+
let path = "rules_rust/rust/runfiles/data/sample.txt";
417+
let f = rlocation!(r, path).unwrap();
418+
419+
let temp_dir = PathBuf::from(std::env::var("TEST_TMPDIR").unwrap());
420+
let runfiles_dir = temp_dir.join(name);
421+
let test_path = runfiles_dir.join(path);
422+
if let Some(parent) = test_path.parent() {
423+
std::fs::create_dir_all(parent).expect("Failed to create test path parents.");
424+
}
425+
426+
std::fs::copy(f, test_path).expect("Failed to copy test file");
427+
428+
runfiles_dir.to_str().unwrap().to_string()
429+
})
430+
}
431+
432+
/// Test the general behavior of runfiles. The behavior of runfiles will change
433+
/// depending on the system but each mode is explicitly covered in other tests.
434+
#[test]
435+
fn test_standard_lookup() {
436+
let r = Runfiles::create().unwrap();
437+
438+
let f = rlocation!(r, "rules_rust/rust/runfiles/data/sample.txt").unwrap();
439+
440+
let mut f = File::open(&f)
441+
.unwrap_or_else(|e| panic!("Failed to open file: {}\n{:?}", f.display(), e));
442+
443+
let mut buffer = String::new();
444+
f.read_to_string(&mut buffer).unwrap();
445+
446+
assert_eq!("Example Text!", buffer);
447+
}
448+
410449
/// Only `RUNFILES_DIR` is set.
411450
#[test]
412451
fn test_env_only_runfiles_dir() {
452+
let runfiles_dir = make_runfiles_like_dir("test_env_only_runfiles_dir");
453+
413454
with_mock_env(
414455
[
415-
(TEST_SRCDIR_ENV_VAR, None::<&str>),
416456
(MANIFEST_FILE_ENV_VAR, None::<&str>),
457+
(RUNFILES_DIR_ENV_VAR, Some(runfiles_dir.as_str())),
458+
(TEST_SRCDIR_ENV_VAR, None::<&str>),
417459
],
418460
|| {
419461
let r = Runfiles::create().unwrap();
@@ -422,7 +464,8 @@ mod test {
422464
let f = rlocation!(r, "rules_rust/rust/runfiles/data/sample.txt").unwrap();
423465
assert_eq!(d.join("rust/runfiles/data/sample.txt"), f);
424466

425-
let mut f = File::open(f).unwrap();
467+
let mut f = File::open(&f)
468+
.unwrap_or_else(|e| panic!("Failed to open file: {}\n{:?}", f.display(), e));
426469

427470
let mut buffer = String::new();
428471
f.read_to_string(&mut buffer).unwrap();
@@ -435,17 +478,21 @@ mod test {
435478
/// Only `TEST_SRCDIR` is set.
436479
#[test]
437480
fn test_env_only_test_srcdir() {
481+
let runfiles_dir = make_runfiles_like_dir("test_env_only_test_srcdir");
482+
438483
with_mock_env(
439484
[
440-
(RUNFILES_DIR_ENV_VAR, None::<&str>),
441485
(MANIFEST_FILE_ENV_VAR, None::<&str>),
486+
(RUNFILES_DIR_ENV_VAR, None::<&str>),
487+
(TEST_SRCDIR_ENV_VAR, Some(runfiles_dir.as_str())),
442488
],
443489
|| {
444490
let r = Runfiles::create().unwrap();
445491

446-
let mut f =
447-
File::open(rlocation!(r, "rules_rust/rust/runfiles/data/sample.txt").unwrap())
448-
.unwrap();
492+
let runfile = rlocation!(r, "rules_rust/rust/runfiles/data/sample.txt").unwrap();
493+
494+
let mut f = File::open(&runfile)
495+
.unwrap_or_else(|e| panic!("Failed to open: {}\n{:?}", runfile.display(), e));
449496

450497
let mut buffer = String::new();
451498
f.read_to_string(&mut buffer).unwrap();
@@ -455,7 +502,12 @@ mod test {
455502
);
456503
}
457504

458-
/// Neither `RUNFILES_DIR` or `TEST_SRCDIR` are set
505+
/// `RUNFILES_DIR`, `TEST_SRCDIR`, and `MANIFEST_FILE_ENV_VAR` are not set. This
506+
/// will test the `.runfiles` directory lookup.
507+
///
508+
/// This test is skipped on windows as these directories are not guaranteed
509+
/// to have been created.
510+
#[cfg(not(target_family = "windows"))]
459511
#[test]
460512
fn test_env_nothing_set() {
461513
with_mock_env(

0 commit comments

Comments
 (0)