Skip to content

Commit

Permalink
Fix usage in examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Apr 2, 2024
1 parent e5a57b7 commit a6fb2ac
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 74 deletions.
4 changes: 2 additions & 2 deletions crate_universe/src/api/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ mod test {
};

let runfiles = runfiles::Runfiles::create().unwrap();
let path = runfiles
.rlocation("rules_rust/crate_universe/test_data/cargo_bazel_lockfile/multi_package-cargo-bazel-lock.json");
let path = runfiles::rlocation!(
runfiles, "rules_rust/crate_universe/test_data/cargo_bazel_lockfile/multi_package-cargo-bazel-lock.json");

let parsed = parse(&path).unwrap();
assert_eq!(parsed.workspace_members(), want_workspace_member_names);
Expand Down
6 changes: 4 additions & 2 deletions crate_universe/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,10 @@ mod test {
#[test]
fn deserialize_config() {
let runfiles = runfiles::Runfiles::create().unwrap();
let path = runfiles
.rlocation("rules_rust/crate_universe/test_data/serialized_configs/config.json");
let path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/serialized_configs/config.json"
);

let content = std::fs::read_to_string(path).unwrap();

Expand Down
26 changes: 16 additions & 10 deletions crate_universe/src/splicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,9 @@ mod test {
#[test]
fn deserialize_splicing_manifest() {
let runfiles = runfiles::Runfiles::create().unwrap();
let path = runfiles.rlocation(
"rules_rust/crate_universe/test_data/serialized_configs/splicing_manifest.json",
let path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/serialized_configs/splicing_manifest.json"
);

let content = std::fs::read_to_string(path).unwrap();
Expand Down Expand Up @@ -566,8 +567,9 @@ mod test {
#[test]
fn splicing_manifest_resolve() {
let runfiles = runfiles::Runfiles::create().unwrap();
let path = runfiles.rlocation(
"rules_rust/crate_universe/test_data/serialized_configs/splicing_manifest.json",
let path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/serialized_configs/splicing_manifest.json"
);

let content = std::fs::read_to_string(path).unwrap();
Expand Down Expand Up @@ -610,14 +612,18 @@ mod test {
#[test]
fn splicing_metadata_workspace_path() {
let runfiles = runfiles::Runfiles::create().unwrap();
let workspace_manifest_path = runfiles
.rlocation("rules_rust/crate_universe/test_data/metadata/workspace_path/Cargo.toml");
let workspace_manifest_path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/metadata/workspace_path/Cargo.toml"
);
let workspace_path = workspace_manifest_path.parent().unwrap().to_path_buf();
let child_a_manifest_path = runfiles.rlocation(
"rules_rust/crate_universe/test_data/metadata/workspace_path/child_a/Cargo.toml",
let child_a_manifest_path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/metadata/workspace_path/child_a/Cargo.toml"
);
let child_b_manifest_path = runfiles.rlocation(
"rules_rust/crate_universe/test_data/metadata/workspace_path/child_b/Cargo.toml",
let child_b_manifest_path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/metadata/workspace_path/child_b/Cargo.toml"
);
let manifest = SplicingManifest {
direct_packages: BTreeMap::new(),
Expand Down
8 changes: 5 additions & 3 deletions crate_universe/src/splicing/crate_index_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ mod test {
{
let _e = EnvVarResetter::set(
"CARGO_HOME",
runfiles.rlocation(
"rules_rust/crate_universe/test_data/crate_indexes/lazy_static/cargo_home",
runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/crate_indexes/lazy_static/cargo_home"
),
);

Expand Down Expand Up @@ -95,7 +96,8 @@ mod test {
);
}
{
let _e = EnvVarResetter::set("CARGO_HOME", runfiles.rlocation("rules_rust/crate_universe/test_data/crate_indexes/rewritten_lazy_static/cargo_home"));
let _e = EnvVarResetter::set("CARGO_HOME",
runfiles::rlocation!(runfiles, "rules_rust/crate_universe/test_data/crate_indexes/rewritten_lazy_static/cargo_home"));

let index = CrateIndexLookup::Http(
crates_index::SparseIndex::from_url("sparse+https://index.crates.io/").unwrap(),
Expand Down
6 changes: 3 additions & 3 deletions crate_universe/src/splicing/splicer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,9 @@ mod test {
/// Get cargo and rustc binaries the Bazel way
#[cfg(not(feature = "cargo"))]
fn get_cargo_and_rustc_paths() -> (PathBuf, PathBuf) {
let runfiles = runfiles::Runfiles::create().unwrap();
let cargo_path = runfiles.rlocation(concat!("rules_rust/", env!("CARGO")));
let rustc_path = runfiles.rlocation(concat!("rules_rust/", env!("RUSTC")));
let r = runfiles::Runfiles::create().unwrap();
let cargo_path = runfiles::rlocation!(r, concat!("rules_rust/", env!("CARGO")));
let rustc_path = runfiles::rlocation!(r, concat!("rules_rust/", env!("RUSTC")));

(cargo_path, rustc_path)
}
Expand Down
74 changes: 38 additions & 36 deletions crate_universe/tests/cargo_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn run(repository_name: &str, manifests: HashMap<String, String>, lockfile: &str
/*
let manifest_path = scratch.path().join("Cargo.toml");
fs::copy(
runfiles.rlocation(manifest),
runfiles::rlocation!(runfiles, manifest),
manifest_path,
)
.unwrap();
Expand Down Expand Up @@ -94,7 +94,7 @@ fn run(repository_name: &str, manifests: HashMap<String, String>, lockfile: &str

splice(SpliceOptions {
splicing_manifest,
cargo_lockfile: Some(runfiles.rlocation(lockfile)),
cargo_lockfile: Some(runfiles::rlocation!(runfiles, lockfile)),
repin: None,
workspace_dir: None,
output_dir: scratch.path().join("out"),
Expand Down Expand Up @@ -127,16 +127,16 @@ fn feature_generator() {
return;
}

let runfiles = runfiles::Runfiles::create().unwrap();
let r = runfiles::Runfiles::create().unwrap();
let metadata = run(
"target_feature_test",
HashMap::from([(
runfiles
.rlocation(
"rules_rust/crate_universe/test_data/metadata/target_features/Cargo.toml",
)
.to_string_lossy()
.to_string(),
runfiles::rlocation!(
r,
"rules_rust/crate_universe/test_data/metadata/target_features/Cargo.toml"
)
.to_string_lossy()
.to_string(),
"//:test_input".to_string(),
)]),
"rules_rust/crate_universe/test_data/metadata/target_features/Cargo.lock",
Expand Down Expand Up @@ -180,16 +180,16 @@ fn feature_generator_cfg_features() {
return;
}

let runfiles = runfiles::Runfiles::create().unwrap();
let r = runfiles::Runfiles::create().unwrap();
let metadata = run(
"target_cfg_features_test",
HashMap::from([(
runfiles
.rlocation(
"rules_rust/crate_universe/test_data/metadata/target_cfg_features/Cargo.toml",
)
.to_string_lossy()
.to_string(),
runfiles::rlocation!(
r,
"rules_rust/crate_universe/test_data/metadata/target_cfg_features/Cargo.toml"
)
.to_string_lossy()
.to_string(),
"//:test_input".to_string(),
)]),
"rules_rust/crate_universe/test_data/metadata/target_cfg_features/Cargo.lock",
Expand Down Expand Up @@ -235,24 +235,26 @@ fn feature_generator_workspace() {
return;
}

let runfiles = runfiles::Runfiles::create().unwrap();
let r = runfiles::Runfiles::create().unwrap();
let metadata = run(
"workspace_test",
HashMap::from([
(
runfiles
.rlocation("rules_rust/crate_universe/test_data/metadata/workspace/Cargo.toml")
.to_string_lossy()
.to_string(),
runfiles::rlocation!(
r,
"rules_rust/crate_universe/test_data/metadata/workspace/Cargo.toml"
)
.to_string_lossy()
.to_string(),
"//:test_input".to_string(),
),
(
runfiles
.rlocation(
"rules_rust/crate_universe/test_data/metadata/workspace/child/Cargo.toml",
)
.to_string_lossy()
.to_string(),
runfiles::rlocation!(
r,
"rules_rust/crate_universe/test_data/metadata/workspace/child/Cargo.toml"
)
.to_string_lossy()
.to_string(),
"//crate_universe:test_data/metadata/workspace/child/Cargo.toml".to_string(),
),
]),
Expand All @@ -274,18 +276,18 @@ fn feature_generator_crate_combined_features() {
return;
}

let runfiles = runfiles::Runfiles::create().unwrap();
let r = runfiles::Runfiles::create().unwrap();
let metadata = run(
"crate_combined_features",
HashMap::from([
(
runfiles
.rlocation("rules_rust/crate_universe/test_data/metadata/crate_combined_features/Cargo.toml")
.to_string_lossy()
.to_string(),
"//:test_input".to_string(),
HashMap::from([(
runfiles::rlocation!(
r,
"rules_rust/crate_universe/test_data/metadata/crate_combined_features/Cargo.toml"
)
]),
.to_string_lossy()
.to_string(),
"//:test_input".to_string(),
)]),
"rules_rust/crate_universe/test_data/metadata/crate_combined_features/Cargo.lock",
);

Expand Down
6 changes: 5 additions & 1 deletion examples/hello_runfiles/hello_runfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use runfiles::Runfiles;
fn main() {
let r = Runfiles::create().unwrap();

let mut f = File::open(r.rlocation("examples/hello_runfiles/hello_runfiles.rs")).unwrap();
let mut f = File::open(runfiles::rlocation!(
r,
"examples/hello_runfiles/hello_runfiles.rs"
))
.unwrap();

let mut buffer = String::new();
f.read_to_string(&mut buffer).unwrap();
Expand Down
20 changes: 12 additions & 8 deletions examples/proto/helloworld/helloworld_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ macro_rules! assert_contains {
impl ServerInfo {
fn new() -> ServerInfo {
let r = Runfiles::create().unwrap();
let mut c =
Command::new(r.rlocation("examples/proto/helloworld/greeter_server/greeter_server"))
.arg("0")
.stdout(Stdio::piped())
.spawn()
.expect("Unable to start server");
let mut c = Command::new(runfiles::rlocation!(
r,
"examples/proto/helloworld/greeter_server/greeter_server"
))
.arg("0")
.stdout(Stdio::piped())
.spawn()
.expect("Unable to start server");
let mut port: u16 = 0;
{
let mut stdout = BufReader::new(c.stdout.as_mut().expect("Failed to open stdout"));
Expand All @@ -65,8 +67,10 @@ impl ServerInfo {
fn run_client_impl(&self, arg: Option<String>) -> String {
let r = Runfiles::create().unwrap();

let mut cmd0 =
Command::new(r.rlocation("examples/proto/helloworld/greeter_client/greeter_client"));
let mut cmd0 = Command::new(runfiles::rlocation!(
r,
"examples/proto/helloworld/greeter_client/greeter_client"
));
let cmd = cmd0.arg(format!("-p={}", self.port));

let output = if let Some(s) = arg { cmd.arg(s) } else { cmd }
Expand Down
2 changes: 1 addition & 1 deletion test/bzl_version/bzl_version_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn module_bzl_has_correct_version() {
let version = std::env::var("VERSION").unwrap();
let module_bazel_text = {
let r = Runfiles::create().unwrap();
let path = r.rlocation(std::env::var("MODULE_BAZEL").unwrap());
let path = runfiles::rlocation!(r, std::env::var("MODULE_BAZEL").unwrap());
std::fs::read_to_string(path).unwrap()
};

Expand Down
1 change: 0 additions & 1 deletion test/no_std/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ fn panic(_info: &PanicInfo) -> ! {
#[allow(clippy::panic)]
fn default_handler(layout: core::alloc::Layout) -> ! {
panic!("memory allocation of {} bytes failed", layout.size())

}

#[lang = "eh_personality"]
Expand Down
10 changes: 6 additions & 4 deletions test/process_wrapper/rustc_quit_on_rmeta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ mod test {
should_succeed: bool,
) -> String {
let r = Runfiles::create().unwrap();
let fake_rustc = r.rlocation(
let fake_rustc = runfiles::rlocation!(
r,
[
"rules_rust",
"test",
Expand All @@ -27,10 +28,11 @@ mod test {
},
]
.iter()
.collect::<PathBuf>(),
.collect::<PathBuf>()
);

let process_wrapper = r.rlocation(
let process_wrapper = runfiles::rlocation!(
r,
[
"rules_rust",
"util",
Expand All @@ -42,7 +44,7 @@ mod test {
},
]
.iter()
.collect::<PathBuf>(),
.collect::<PathBuf>()
);

let output = Command::new(process_wrapper)
Expand Down
8 changes: 5 additions & 3 deletions test/rust/src/greeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ impl Greeter {
/// let greeter = Greeter::from_txt_file()?;
/// ```
pub fn from_txt_file() -> std::io::Result<Greeter> {
let r = runfiles::Runfiles::create()?;
Ok(Greeter {
greeting: std::fs::read_to_string(
runfiles::Runfiles::create()?.rlocation("rules_rust/test/rust/greeting.txt"),
)?,
greeting: std::fs::read_to_string(runfiles::rlocation!(
r,
"rules_rust/test/rust/greeting.txt"
))?,
})
}

Expand Down

0 comments on commit a6fb2ac

Please sign in to comment.