Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Apr 2, 2024
1 parent c437c0f commit e5a57b7
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,12 @@ tasks:
test_flags:
- "--@rules_rust//rust/toolchain/channel=nightly"
- "--@rules_rust//:no_std=alloc"
bzlmod_repo_mapping_runfiles:
name: bzlmod repo mapping test
platform: ubuntu2004
working_directory: test/bzlmod_repo_mapping/module_a
test_targets:
- "//..."
android_examples_ubuntu2004:
name: Android Examples
platform: ubuntu2004
Expand Down
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ bzlmod
docs
examples
crate_universe/private/bootstrap
test/bzlmod_repo_mapping
test/cc_common_link
test/no_std
4 changes: 4 additions & 0 deletions test/bzlmod_repo_mapping/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Bzlmod allow modules to rename their dependencies and use the aliased names in runfiles lookups.
This test defines the following dependency tree: A -> B -> C. The test if whether a binary built in A
that calls code in B that has an `rlocation!` call to lookup a runfile from C is able to correctly use
B's repo_mapping. This is accomplished by having B use a custom `repo_name` for C.
1 change: 1 addition & 0 deletions test/bzlmod_repo_mapping/module_a/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
12 changes: 12 additions & 0 deletions test/bzlmod_repo_mapping/module_a/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

rust_library(
name = "lib",
srcs = ["lib.rs"],
deps = ["@module_b//:lib_b"],
)

rust_test(
name = "lib_test",
crate = ":lib",
)
15 changes: 15 additions & 0 deletions test/bzlmod_repo_mapping/module_a/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module(name = "module_a")

local_path_override(module_name = "module_b", path = "../module_b")
local_path_override(module_name = "module_c", path = "../module_c")

bazel_dep(name = "module_b", version = "0.0.0")

bazel_dep(name = "rules_rust", version = "0.0.0")
local_path_override(module_name = "rules_rust", path = "../../..")

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021")
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")
8 changes: 8 additions & 0 deletions test/bzlmod_repo_mapping/module_a/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(test)]
mod test {
#[test]
fn test_repo_remapping() {
let data = lib_b::read_file_from_module_c();
assert_eq!(data, "module(name = \"module_c\")");
}
}
9 changes: 9 additions & 0 deletions test/bzlmod_repo_mapping/module_b/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_rust//rust:defs.bzl", "rust_library")

rust_library(
name = "lib_b",
srcs = ["lib.rs"],
data = ["@aliased_c//:MODULE.bazel"],
deps = ["@rules_rust//tools/runfiles"],
visibility = ["//visibility:public"],
)
9 changes: 9 additions & 0 deletions test/bzlmod_repo_mapping/module_b/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module(name = "module_b")

bazel_dep(name = "module_c", version = "0.0.0", repo_name = "aliased_c")
bazel_dep(name = "rules_rust", version = "0.0.0")
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021")
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")
6 changes: 6 additions & 0 deletions test/bzlmod_repo_mapping/module_b/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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()
}
1 change: 1 addition & 0 deletions test/bzlmod_repo_mapping/module_c/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files(["MODULE.bazel"])
1 change: 1 addition & 0 deletions test/bzlmod_repo_mapping/module_c/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module(name = "module_c")
2 changes: 1 addition & 1 deletion tools/runfiles/runfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Runfiles {
/// or a directory based Runfiles object otherwise.
pub fn create() -> io::Result<Self> {
let mode = if let Some(manifest_file) = std::env::var_os(MANIFEST_FILE_ENV_VAR) {
Self::create_manifest_based(&Path::new(&manifest_file))?
Self::create_manifest_based(Path::new(&manifest_file))?
} else {
Mode::DirectoryBased(find_runfiles_dir()?)
};
Expand Down

0 comments on commit e5a57b7

Please sign in to comment.