Skip to content

Commit

Permalink
Update runfiles::rlocation! to return Option instead of panicing (#…
Browse files Browse the repository at this point in the history
…2847)

closes #2653
  • Loading branch information
UebelAndre committed Sep 10, 2024
1 parent 1ade187 commit d07ac80
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 152 deletions.
2 changes: 1 addition & 1 deletion crate_universe/src/api/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ mod test {

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

let parsed = parse(&path).unwrap();
assert_eq!(parsed.workspace_members(), want_workspace_member_names);
Expand Down
3 changes: 2 additions & 1 deletion crate_universe/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,8 @@ mod test {
let path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/serialized_configs/config.json"
);
)
.unwrap();

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

Expand Down
15 changes: 10 additions & 5 deletions crate_universe/src/splicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ mod test {
let path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/serialized_configs/splicing_manifest.json"
);
)
.unwrap();

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

Expand Down Expand Up @@ -571,7 +572,8 @@ mod test {
let path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/serialized_configs/splicing_manifest.json"
);
)
.unwrap();

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

Expand Down Expand Up @@ -616,16 +618,19 @@ mod test {
let workspace_manifest_path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/metadata/workspace_path/Cargo.toml"
);
)
.unwrap();
let workspace_path = workspace_manifest_path.parent().unwrap().to_path_buf();
let child_a_manifest_path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/metadata/workspace_path/child_a/Cargo.toml"
);
)
.unwrap();
let child_b_manifest_path = runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/metadata/workspace_path/child_b/Cargo.toml"
);
)
.unwrap();
let manifest = SplicingManifest {
direct_packages: BTreeMap::new(),
manifests: BTreeMap::from([
Expand Down
5 changes: 3 additions & 2 deletions crate_universe/src/splicing/crate_index_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ mod test {
runfiles::rlocation!(
runfiles,
"rules_rust/crate_universe/test_data/crate_indexes/lazy_static/cargo_home"
),
)
.unwrap(),
);

let index = CrateIndexLookup::Http(
Expand Down Expand Up @@ -97,7 +98,7 @@ mod test {
}
{
let _e = EnvVarResetter::set("CARGO_HOME",
runfiles::rlocation!(runfiles, "rules_rust/crate_universe/test_data/crate_indexes/rewritten_lazy_static/cargo_home"));
runfiles::rlocation!(runfiles, "rules_rust/crate_universe/test_data/crate_indexes/rewritten_lazy_static/cargo_home").unwrap());

let index = CrateIndexLookup::Http(
crates_index::SparseIndex::from_url("sparse+https://index.crates.io/").unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions crate_universe/src/splicing/splicer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,8 @@ mod test {
#[cfg(not(feature = "cargo"))]
fn get_cargo_and_rustc_paths() -> (PathBuf, PathBuf) {
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")));
let cargo_path = runfiles::rlocation!(r, concat!("rules_rust/", env!("CARGO"))).unwrap();
let rustc_path = runfiles::rlocation!(r, concat!("rules_rust/", env!("RUSTC"))).unwrap();

(cargo_path, rustc_path)
}
Expand Down
8 changes: 7 additions & 1 deletion crate_universe/tests/cargo_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn run(repository_name: &str, manifests: HashMap<String, String>, lockfile: &str

splice(SpliceOptions {
splicing_manifest,
cargo_lockfile: Some(runfiles::rlocation!(runfiles, lockfile)),
cargo_lockfile: Some(runfiles::rlocation!(runfiles, lockfile).unwrap()),
repin: None,
workspace_dir: None,
output_dir: scratch.path().join("out"),
Expand Down Expand Up @@ -139,6 +139,7 @@ fn feature_generator() {
r,
"rules_rust/crate_universe/test_data/metadata/target_features/Cargo.toml"
)
.unwrap()
.to_string_lossy()
.to_string(),
"//:test_input".to_string(),
Expand Down Expand Up @@ -256,6 +257,7 @@ fn feature_generator_cfg_features() {
r,
"rules_rust/crate_universe/test_data/metadata/target_cfg_features/Cargo.toml"
)
.unwrap()
.to_string_lossy()
.to_string(),
"//:test_input".to_string(),
Expand Down Expand Up @@ -325,6 +327,7 @@ fn feature_generator_workspace() {
r,
"rules_rust/crate_universe/test_data/metadata/workspace/Cargo.toml"
)
.unwrap()
.to_string_lossy()
.to_string(),
"//:test_input".to_string(),
Expand All @@ -334,6 +337,7 @@ fn feature_generator_workspace() {
r,
"rules_rust/crate_universe/test_data/metadata/workspace/child/Cargo.toml"
)
.unwrap()
.to_string_lossy()
.to_string(),
"//crate_universe:test_data/metadata/workspace/child/Cargo.toml".to_string(),
Expand All @@ -360,6 +364,7 @@ fn feature_generator_crate_combined_features() {
r,
"rules_rust/crate_universe/test_data/metadata/crate_combined_features/Cargo.toml"
)
.unwrap()
.to_string_lossy()
.to_string(),
"//:test_input".to_string(),
Expand Down Expand Up @@ -400,6 +405,7 @@ fn resolver_2_deps() {
r,
"rules_rust/crate_universe/test_data/metadata/resolver_2_deps/Cargo.toml"
)
.unwrap()
.to_string_lossy()
.to_string(),
"//:test_input".to_string(),
Expand Down
8 changes: 3 additions & 5 deletions examples/hello_runfiles/hello_runfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ use runfiles::Runfiles;
fn main() {
let r = Runfiles::create().unwrap();

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

let mut buffer = String::new();
f.read_to_string(&mut buffer).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/proto/helloworld/helloworld_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl ServerInfo {
let mut c = Command::new(runfiles::rlocation!(
r,
"examples/proto/helloworld/greeter_server/greeter_server"
))
).unwrap())
.arg("0")
.stdout(Stdio::piped())
.spawn()
Expand Down Expand Up @@ -70,7 +70,7 @@ impl ServerInfo {
let mut cmd0 = Command::new(runfiles::rlocation!(
r,
"examples/proto/helloworld/greeter_client/greeter_client"
));
).unwrap());
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 = runfiles::rlocation!(r, std::env::var("MODULE_BAZEL").unwrap());
let path = runfiles::rlocation!(r, std::env::var("MODULE_BAZEL").unwrap()).unwrap();
std::fs::read_to_string(path).unwrap()
};

Expand Down
4 changes: 2 additions & 2 deletions test/bzlmod_repo_mapping/module_b/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ use runfiles::Runfiles;

pub fn read_file_from_module_c() -> String {
let r = Runfiles::create().unwrap();
std::fs::read_to_string(runfiles::rlocation!(r, "aliased_c/MODULE.bazel")).unwrap()
}
std::fs::read_to_string(runfiles::rlocation!(r, "aliased_c/MODULE.bazel").unwrap()).unwrap()
}
6 changes: 4 additions & 2 deletions test/process_wrapper/rustc_quit_on_rmeta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ mod test {
]
.iter()
.collect::<PathBuf>()
);
)
.unwrap();

let process_wrapper = runfiles::rlocation!(
r,
Expand All @@ -45,7 +46,8 @@ mod test {
]
.iter()
.collect::<PathBuf>()
);
)
.unwrap();

let output = Command::new(process_wrapper)
.args(process_wrapper_args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ fn test_shebang() {
let script = runfiles::rlocation!(
rfiles,
"rules_rust/util/process_wrapper/private/process_wrapper.sh"
);
)
.unwrap();

let content = read_to_string(script).unwrap();
assert!(
Expand Down
10 changes: 5 additions & 5 deletions test/rust/src/greeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ impl Greeter {
///
/// let greeter = Greeter::from_txt_file()?;
/// ```
pub fn from_txt_file() -> std::io::Result<Greeter> {
pub fn from_txt_file() -> runfiles::Result<Greeter> {
let r = runfiles::Runfiles::create()?;
Ok(Greeter {
greeting: std::fs::read_to_string(runfiles::rlocation!(
r,
"rules_rust/test/rust/greeting.txt"
))?,
greeting: std::fs::read_to_string(
runfiles::rlocation!(r, "rules_rust/test/rust/greeting.txt").unwrap(),
)
.map_err(runfiles::RunfilesError::RunfileIoError)?,
})
}

Expand Down
Loading

0 comments on commit d07ac80

Please sign in to comment.