use super::*;
use crate::generated::descriptor::field_descriptor_proto::{Label, Type};
fn dep_file_with_message(file_name: &str, package: &str, msg_name: &str) -> FileDescriptorProto {
FileDescriptorProto {
name: Some(file_name.to_string()),
package: Some(package.to_string()),
syntax: Some("proto3".to_string()),
message_type: vec![DescriptorProto {
name: Some(msg_name.to_string()),
..Default::default()
}],
..Default::default()
}
}
fn field_referencing(name: &str, number: i32, type_fqn: &str) -> FieldDescriptorProto {
FieldDescriptorProto {
name: Some(name.to_string()),
number: Some(number),
label: Some(Label::LABEL_OPTIONAL),
r#type: Some(Type::TYPE_MESSAGE),
type_name: Some(type_fqn.to_string()),
..Default::default()
}
}
fn fixture() -> (FileDescriptorProto, FileDescriptorProto) {
let dep = dep_file_with_message("dep.proto", "dep", "Constraint");
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
kept.message_type.push(DescriptorProto {
name: Some("Request".to_string()),
field: vec![field_referencing("constraint", 1, ".dep.Constraint")],
..Default::default()
});
(dep, kept)
}
#[test]
fn test_excluded_package_ref_warns() {
let (dep, kept) = fixture();
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
assert!(
warnings.iter().any(|w| matches!(
w,
CodeGenWarning::ExcludedPackageFieldRef { ref_package, .. }
if ref_package == "dep"
)),
"must warn when a field references a type from an excluded package: {warnings:?}"
);
}
#[test]
fn test_excluded_package_ref_structured_fields() {
let (dep, kept) = fixture();
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
match warnings
.iter()
.find(|w| matches!(w, CodeGenWarning::ExcludedPackageFieldRef { .. }))
.expect("must have ExcludedPackageFieldRef warning")
{
CodeGenWarning::ExcludedPackageFieldRef {
file_name,
message_name,
field_name,
ref_package,
type_fqn,
..
} => {
assert_eq!(file_name, "kept.proto");
assert_eq!(message_name, "Request");
assert_eq!(field_name, "constraint");
assert_eq!(ref_package, "dep");
assert_eq!(type_fqn, ".dep.Constraint");
}
other => panic!("unexpected warning variant: {other:?}"),
}
}
#[test]
fn test_excluded_package_ref_display() {
let (dep, kept) = fixture();
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
let w = warnings
.iter()
.find(|w| matches!(w, CodeGenWarning::ExcludedPackageFieldRef { .. }))
.expect("must have ExcludedPackageFieldRef warning");
let s = w.to_string();
assert!(s.contains("kept.proto"), "display must name the file: {s}");
assert!(
s.contains(".dep.Constraint"),
"display must name the type: {s}"
);
assert!(s.contains("dep"), "display must name the package: {s}");
assert!(
s.contains("extern_path"),
"display must hint at extern_path fix: {s}"
);
assert!(
s.contains("buffa-build"),
"display must give build-tool-specific hint: {s}"
);
}
#[test]
fn test_no_warn_when_type_is_generated() {
let (dep, kept) = fixture();
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["dep.proto".to_string(), "kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
assert!(
!warnings
.iter()
.any(|w| matches!(w, CodeGenWarning::ExcludedPackageFieldRef { .. })),
"must not warn when the referenced type is being generated: {warnings:?}"
);
}
#[test]
fn test_no_warn_with_package_extern_path() {
let (dep, kept) = fixture();
let config = CodeGenConfig {
extern_paths: vec![(".dep".to_string(), "::dep_crate".to_string())],
..Default::default()
};
let (_, warnings) =
generate_with_diagnostics(&[dep, kept], &["kept.proto".to_string()], &config).unwrap();
assert!(
!warnings
.iter()
.any(|w| matches!(w, CodeGenWarning::ExcludedPackageFieldRef { .. })),
"must not warn when the referenced package has an extern_path: {warnings:?}"
);
}
#[test]
fn test_no_warn_with_per_type_extern_path() {
let (dep, kept) = fixture();
let config = CodeGenConfig {
extern_paths: vec![(
".dep.Constraint".to_string(),
"::dep_crate::Constraint".to_string(),
)],
..Default::default()
};
let (_, warnings) =
generate_with_diagnostics(&[dep, kept], &["kept.proto".to_string()], &config).unwrap();
assert!(
!warnings
.iter()
.any(|w| matches!(w, CodeGenWarning::ExcludedPackageFieldRef { .. })),
"must not warn when the type has a per-type extern_path: {warnings:?}"
);
}
#[test]
fn test_no_warn_for_wkt_auto_extern_path() {
let wkt_dep = dep_file_with_message(
"google/protobuf/timestamp.proto",
"google.protobuf",
"Timestamp",
);
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
kept.message_type.push(DescriptorProto {
name: Some("Event".to_string()),
field: vec![field_referencing("ts", 1, ".google.protobuf.Timestamp")],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[wkt_dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
assert!(
!warnings
.iter()
.any(|w| matches!(w, CodeGenWarning::ExcludedPackageFieldRef { .. })),
"must not warn for WKT references (auto extern_path): {warnings:?}"
);
}
#[test]
fn test_excluded_package_ref_in_nested_message() {
let dep = dep_file_with_message("dep.proto", "dep", "Constraint");
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
kept.message_type.push(DescriptorProto {
name: Some("Outer".to_string()),
nested_type: vec![DescriptorProto {
name: Some("Inner".to_string()),
field: vec![field_referencing("constraint", 1, ".dep.Constraint")],
..Default::default()
}],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
match warnings
.iter()
.find(|w| matches!(w, CodeGenWarning::ExcludedPackageFieldRef { .. }))
{
Some(CodeGenWarning::ExcludedPackageFieldRef {
message_name,
field_name,
..
}) => {
assert_eq!(
message_name, "Outer.Inner",
"message_name must include nested path"
);
assert_eq!(field_name, "constraint");
}
other => panic!("expected ExcludedPackageFieldRef, got: {other:?}"),
}
}
#[test]
fn test_excluded_package_ref_enum_type() {
let mut dep = proto3_file("dep.proto");
dep.package = Some("dep".to_string());
dep.enum_type
.push(crate::generated::descriptor::EnumDescriptorProto {
name: Some("Status".to_string()),
value: vec![enum_value("UNKNOWN", 0)],
..Default::default()
});
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
kept.message_type.push(DescriptorProto {
name: Some("Request".to_string()),
field: vec![FieldDescriptorProto {
name: Some("status".to_string()),
number: Some(1),
label: Some(Label::LABEL_OPTIONAL),
r#type: Some(Type::TYPE_ENUM),
type_name: Some(".dep.Status".to_string()),
..Default::default()
}],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
assert!(
warnings.iter().any(|w| matches!(
w,
CodeGenWarning::ExcludedPackageFieldRef { type_fqn, .. }
if type_fqn == ".dep.Status"
)),
"must warn for an enum-typed field: {warnings:?}"
);
}
#[test]
fn test_excluded_package_ref_file_level_extension() {
let dep = dep_file_with_message("dep.proto", "dep", "Constraint");
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
kept.extension
.push(field_referencing("constraint_ext", 1001, ".dep.Constraint"));
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
match warnings
.iter()
.find(|w| matches!(w, CodeGenWarning::ExcludedPackageFieldRef { .. }))
{
Some(CodeGenWarning::ExcludedPackageFieldRef {
message_name,
field_name,
..
}) => {
assert!(
message_name.is_empty(),
"file-level extension must have empty message_name, got: {message_name:?}"
);
assert_eq!(field_name, "constraint_ext");
}
other => panic!("expected ExcludedPackageFieldRef, got: {other:?}"),
}
}
#[test]
fn test_dedup_same_type_multiple_fields() {
let dep = dep_file_with_message("dep.proto", "dep", "Constraint");
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
kept.message_type.push(DescriptorProto {
name: Some("Request".to_string()),
field: vec![
field_referencing("c1", 1, ".dep.Constraint"),
field_referencing("c2", 2, ".dep.Constraint"),
],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
let count = warnings
.iter()
.filter(|w| {
matches!(w, CodeGenWarning::ExcludedPackageFieldRef { type_fqn, .. }
if type_fqn == ".dep.Constraint")
})
.count();
assert_eq!(
count, 1,
"must emit only one warning per unique (file, type_fqn) pair: {warnings:?}"
);
}
#[test]
fn test_cross_file_dedup_two_warnings() {
let dep = dep_file_with_message("dep.proto", "dep", "Constraint");
let mut kept1 = proto3_file("kept1.proto");
kept1.package = Some("svc".to_string());
kept1.message_type.push(DescriptorProto {
name: Some("Request".to_string()),
field: vec![field_referencing("constraint", 1, ".dep.Constraint")],
..Default::default()
});
let mut kept2 = proto3_file("kept2.proto");
kept2.package = Some("svc2".to_string());
kept2.message_type.push(DescriptorProto {
name: Some("Response".to_string()),
field: vec![field_referencing("constraint", 1, ".dep.Constraint")],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[dep, kept1, kept2],
&["kept1.proto".to_string(), "kept2.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
let count = warnings
.iter()
.filter(|w| {
matches!(w, CodeGenWarning::ExcludedPackageFieldRef { type_fqn, .. }
if type_fqn == ".dep.Constraint")
})
.count();
assert_eq!(
count, 2,
"must emit one warning per (file, type_fqn) pair across files: {warnings:?}"
);
}
#[test]
fn test_type_granular_partial_package() {
let mut dep1 = proto3_file("dep1.proto");
dep1.package = Some("dep".to_string());
dep1.message_type.push(DescriptorProto {
name: Some("TypeFromDep1".to_string()),
..Default::default()
});
let mut dep2 = proto3_file("dep2.proto");
dep2.package = Some("dep".to_string());
dep2.message_type.push(DescriptorProto {
name: Some("TypeFromDep2".to_string()),
..Default::default()
});
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
kept.message_type.push(DescriptorProto {
name: Some("Request".to_string()),
field: vec![
field_referencing("from_dep1", 1, ".dep.TypeFromDep1"),
field_referencing("from_dep2", 2, ".dep.TypeFromDep2"),
],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[dep1, dep2, kept],
&["dep1.proto".to_string(), "kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
assert!(
!warnings.iter().any(|w| matches!(
w,
CodeGenWarning::ExcludedPackageFieldRef { type_fqn, .. }
if type_fqn == ".dep.TypeFromDep1"
)),
"must not warn for a type that IS being generated: {warnings:?}"
);
assert!(
warnings.iter().any(|w| matches!(
w,
CodeGenWarning::ExcludedPackageFieldRef { type_fqn, .. }
if type_fqn == ".dep.TypeFromDep2"
)),
"must warn for a type that is NOT being generated (partial package): {warnings:?}"
);
}
#[test]
fn test_excluded_package_ref_in_map_value() {
let dep = dep_file_with_message("dep.proto", "dep", "Money");
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
let map_entry = DescriptorProto {
name: Some("PricesEntry".to_string()),
field: vec![
make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
field_referencing("value", 2, ".dep.Money"),
],
options: (MessageOptions {
map_entry: Some(true),
..Default::default()
})
.into(),
..Default::default()
};
kept.message_type.push(DescriptorProto {
name: Some("Order".to_string()),
field: vec![FieldDescriptorProto {
name: Some("prices".to_string()),
number: Some(1),
label: Some(Label::LABEL_REPEATED),
r#type: Some(Type::TYPE_MESSAGE),
type_name: Some(".svc.Order.PricesEntry".to_string()),
..Default::default()
}],
nested_type: vec![map_entry],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
match warnings
.iter()
.find(|w| matches!(w, CodeGenWarning::ExcludedPackageFieldRef { .. }))
.expect("must warn for excluded type in map value field")
{
CodeGenWarning::ExcludedPackageFieldRef {
file_name,
message_name,
field_name,
ref_package,
type_fqn,
..
} => {
assert_eq!(file_name, "kept.proto");
assert_eq!(message_name, "Order");
assert_eq!(field_name, "prices");
assert_eq!(ref_package, "dep");
assert_eq!(type_fqn, ".dep.Money");
}
other => panic!("unexpected warning variant: {other:?}"),
}
}
#[test]
fn test_no_warn_map_value_when_generated() {
let dep = dep_file_with_message("dep.proto", "dep", "Money");
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
let map_entry = DescriptorProto {
name: Some("PricesEntry".to_string()),
field: vec![
make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
field_referencing("value", 2, ".dep.Money"),
],
options: (MessageOptions {
map_entry: Some(true),
..Default::default()
})
.into(),
..Default::default()
};
kept.message_type.push(DescriptorProto {
name: Some("Order".to_string()),
field: vec![FieldDescriptorProto {
name: Some("prices".to_string()),
number: Some(1),
label: Some(Label::LABEL_REPEATED),
r#type: Some(Type::TYPE_MESSAGE),
type_name: Some(".svc.Order.PricesEntry".to_string()),
..Default::default()
}],
nested_type: vec![map_entry],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["dep.proto".to_string(), "kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
assert!(
!warnings
.iter()
.any(|w| matches!(w, CodeGenWarning::ExcludedPackageFieldRef { .. })),
"must not warn when the map value type is being generated: {warnings:?}"
);
}
#[test]
fn test_imported_message_named_like_map_entry_warns() {
let dep = dep_file_with_message("dep.proto", "dep", "PricesEntry");
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
let map_entry = DescriptorProto {
name: Some("PricesEntry".to_string()),
field: vec![
make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
make_field("value", 2, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
],
options: (MessageOptions {
map_entry: Some(true),
..Default::default()
})
.into(),
..Default::default()
};
kept.message_type.push(DescriptorProto {
name: Some("Order".to_string()),
field: vec![
FieldDescriptorProto {
name: Some("prices".to_string()),
number: Some(1),
label: Some(Label::LABEL_REPEATED),
r#type: Some(Type::TYPE_MESSAGE),
type_name: Some(".svc.Order.PricesEntry".to_string()),
..Default::default()
},
field_referencing("legacy", 2, ".dep.PricesEntry"),
],
nested_type: vec![map_entry],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
assert!(
warnings.iter().any(|w| matches!(
w,
CodeGenWarning::ExcludedPackageFieldRef { type_fqn, field_name, .. }
if type_fqn == ".dep.PricesEntry" && field_name == "legacy"
)),
"must warn for excluded imported type even when its simple name \
collides with a synthetic map-entry: {warnings:?}"
);
}
#[test]
fn test_repeated_imported_message_named_like_map_entry_warns_once() {
let dep = dep_file_with_message("dep.proto", "dep", "PricesEntry");
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
let map_entry = DescriptorProto {
name: Some("PricesEntry".to_string()),
field: vec![
make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
make_field("value", 2, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
],
options: (MessageOptions {
map_entry: Some(true),
..Default::default()
})
.into(),
..Default::default()
};
kept.message_type.push(DescriptorProto {
name: Some("Order".to_string()),
field: vec![
FieldDescriptorProto {
name: Some("prices".to_string()),
number: Some(1),
label: Some(Label::LABEL_REPEATED),
r#type: Some(Type::TYPE_MESSAGE),
type_name: Some(".svc.Order.PricesEntry".to_string()),
..Default::default()
},
FieldDescriptorProto {
name: Some("legacy".to_string()),
number: Some(2),
label: Some(Label::LABEL_REPEATED),
r#type: Some(Type::TYPE_MESSAGE),
type_name: Some(".dep.PricesEntry".to_string()),
..Default::default()
},
],
nested_type: vec![map_entry],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
let legacy_warnings = warnings
.iter()
.filter(|w| {
matches!(
w,
CodeGenWarning::ExcludedPackageFieldRef { type_fqn, field_name, .. }
if type_fqn == ".dep.PricesEntry" && field_name == "legacy"
)
})
.count();
assert_eq!(
legacy_warnings, 1,
"must warn exactly once for a repeated excluded type whose simple name \
collides with a synthetic map-entry: {warnings:?}"
);
}
#[test]
fn test_excluded_package_ref_in_oneof() {
let dep = dep_file_with_message("dep.proto", "dep", "Constraint");
let mut kept = proto3_file("kept.proto");
kept.package = Some("svc".to_string());
kept.message_type.push(DescriptorProto {
name: Some("Request".to_string()),
oneof_decl: vec![OneofDescriptorProto {
name: Some("body".to_string()),
..Default::default()
}],
field: vec![
FieldDescriptorProto {
name: Some("constraint".to_string()),
number: Some(1),
label: Some(Label::LABEL_OPTIONAL),
r#type: Some(Type::TYPE_MESSAGE),
type_name: Some(".dep.Constraint".to_string()),
oneof_index: Some(0),
..Default::default()
},
FieldDescriptorProto {
name: Some("note".to_string()),
number: Some(2),
label: Some(Label::LABEL_OPTIONAL),
r#type: Some(Type::TYPE_STRING),
oneof_index: Some(0),
..Default::default()
},
],
..Default::default()
});
let (_, warnings) = generate_with_diagnostics(
&[dep, kept],
&["kept.proto".to_string()],
&CodeGenConfig::default(),
)
.unwrap();
assert!(
warnings.iter().any(|w| matches!(
w,
CodeGenWarning::ExcludedPackageFieldRef { type_fqn, field_name, .. }
if type_fqn == ".dep.Constraint" && field_name == "constraint"
)),
"must warn for an excluded type referenced from a oneof variant: {warnings:?}"
);
}