Skip to content

Commit ab9eb40

Browse files
author
Tamas Vajk
committed
Add unit tests for crate_package_default_visibility
Two tests in rendering.rs: - render_crate_package_default_visibility: verifies custom visibility appears in the generated BUILD file instead of //visibility:public - render_default_visibility_when_not_configured: verifies the default behavior (//visibility:public) when the option is not set Use skip_serializing_if on the new field to preserve lockfile digest compatibility when the option is not set (None).
1 parent ee2b159 commit ab9eb40

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

crate_universe/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub(crate) struct RenderConfig {
111111
/// The default_visibility for individual crate BUILD packages. If None,
112112
/// defaults to ["//visibility:public"]. Set to restrict direct references
113113
/// to crate targets so that consumers must use the top-level aliases.
114+
#[serde(default, skip_serializing_if = "Option::is_none")]
114115
pub(crate) crate_package_default_visibility: Option<Vec<String>>,
115116
}
116117

crate_universe/src/rendering.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,4 +2366,97 @@ mod test {
23662366
"proc-macro lib must appear in proc_macro_deps:\n{binary_section}"
23672367
);
23682368
}
2369+
2370+
#[test]
2371+
fn render_crate_package_default_visibility() {
2372+
let mut context = Context::default();
2373+
let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO);
2374+
context.crates.insert(
2375+
crate_id.clone(),
2376+
CrateContext {
2377+
name: crate_id.name,
2378+
version: crate_id.version,
2379+
package_url: None,
2380+
repository: None,
2381+
targets: BTreeSet::from([Rule::Library(mock_target_attributes())]),
2382+
library_target_name: None,
2383+
common_attrs: CommonAttributes::default(),
2384+
build_script_attrs: None,
2385+
license: None,
2386+
license_ids: BTreeSet::default(),
2387+
license_file: None,
2388+
additive_build_file_content: None,
2389+
disable_pipelining: false,
2390+
extra_aliased_targets: BTreeMap::default(),
2391+
alias_rule: None,
2392+
override_targets: BTreeMap::default(),
2393+
},
2394+
);
2395+
2396+
let config = Arc::new(RenderConfig {
2397+
repository_name: "test_rendering".to_owned(),
2398+
regen_command: "cargo_bazel_regen_command".to_owned(),
2399+
crate_package_default_visibility: Some(vec![
2400+
"//third_party/rust:__subpackages__".to_owned()
2401+
]),
2402+
..RenderConfig::default()
2403+
});
2404+
let renderer = Renderer::new(config, mock_supported_platform_triples());
2405+
let output = renderer.render(&context, None).unwrap();
2406+
2407+
let build_file_content = output
2408+
.get(&PathBuf::from("BUILD.mock_crate-0.1.0.bazel"))
2409+
.unwrap();
2410+
2411+
assert!(
2412+
build_file_content.contains("//third_party/rust:__subpackages__"),
2413+
"Expected custom visibility in BUILD file:\n{}",
2414+
build_file_content
2415+
);
2416+
assert!(
2417+
!build_file_content.contains("//visibility:public"),
2418+
"Expected no public visibility in BUILD file:\n{}",
2419+
build_file_content
2420+
);
2421+
}
2422+
2423+
#[test]
2424+
fn render_default_visibility_when_not_configured() {
2425+
let mut context = Context::default();
2426+
let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO);
2427+
context.crates.insert(
2428+
crate_id.clone(),
2429+
CrateContext {
2430+
name: crate_id.name,
2431+
version: crate_id.version,
2432+
package_url: None,
2433+
repository: None,
2434+
targets: BTreeSet::from([Rule::Library(mock_target_attributes())]),
2435+
library_target_name: None,
2436+
common_attrs: CommonAttributes::default(),
2437+
build_script_attrs: None,
2438+
license: None,
2439+
license_ids: BTreeSet::default(),
2440+
license_file: None,
2441+
additive_build_file_content: None,
2442+
disable_pipelining: false,
2443+
extra_aliased_targets: BTreeMap::default(),
2444+
alias_rule: None,
2445+
override_targets: BTreeMap::default(),
2446+
},
2447+
);
2448+
2449+
let renderer = Renderer::new(mock_render_config(None), mock_supported_platform_triples());
2450+
let output = renderer.render(&context, None).unwrap();
2451+
2452+
let build_file_content = output
2453+
.get(&PathBuf::from("BUILD.mock_crate-0.1.0.bazel"))
2454+
.unwrap();
2455+
2456+
assert!(
2457+
build_file_content.contains("//visibility:public"),
2458+
"Expected public visibility in BUILD file:\n{}",
2459+
build_file_content
2460+
);
2461+
}
23692462
}

0 commit comments

Comments
 (0)