Skip to content

Commit 7dcd269

Browse files
authored
tests(prost-build): Validate error texts (#1152)
Trigger all errors of `prost-build` and check the produced error text
1 parent d3fd54c commit 7dcd269

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

prost-build/src/config.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,3 +1249,100 @@ pub fn protoc_include_from_env() -> Option<PathBuf> {
12491249

12501250
Some(protoc_include)
12511251
}
1252+
1253+
#[cfg(test)]
1254+
mod tests {
1255+
use super::*;
1256+
1257+
macro_rules! assert_starts_with {
1258+
($left:expr, $right:expr) => {
1259+
match (&$left, &$right) {
1260+
(left_val, right_val) => {
1261+
if !(left_val.starts_with(right_val)) {
1262+
panic!(
1263+
"assertion 'starts_with` failed:\nleft: {}\nright: {}",
1264+
left_val, right_val
1265+
)
1266+
}
1267+
}
1268+
}
1269+
};
1270+
}
1271+
1272+
#[test]
1273+
fn test_error_protoc_not_found() {
1274+
let mut config = Config::new();
1275+
config.protoc_executable("path-does-not-exist");
1276+
1277+
let err = config.load_fds(&[""], &[""]).unwrap_err();
1278+
assert_eq!(err.to_string(), error_message_protoc_not_found())
1279+
}
1280+
1281+
#[test]
1282+
fn test_error_protoc_not_executable() {
1283+
let mut config = Config::new();
1284+
config.protoc_executable("src/lib.rs");
1285+
1286+
let err = config.load_fds(&[""], &[""]).unwrap_err();
1287+
assert_starts_with!(err.to_string(), "failed to invoke protoc (hint: https://docs.rs/prost-build/#sourcing-protoc): (path: \"src/lib.rs\"): ")
1288+
}
1289+
1290+
#[test]
1291+
fn test_error_incorrect_skip_protoc_run() {
1292+
let mut config = Config::new();
1293+
config.skip_protoc_run();
1294+
1295+
let err = config.load_fds(&[""], &[""]).unwrap_err();
1296+
assert_eq!(
1297+
err.to_string(),
1298+
"file_descriptor_set_path is required with skip_protoc_run"
1299+
)
1300+
}
1301+
1302+
#[test]
1303+
fn test_error_protoc_failed() {
1304+
let mut config = Config::new();
1305+
1306+
let err = config.load_fds(&[""], &[""]).unwrap_err();
1307+
assert_starts_with!(
1308+
err.to_string(),
1309+
"protoc failed: You seem to have passed an empty string as one of the arguments to "
1310+
)
1311+
}
1312+
1313+
#[test]
1314+
fn test_error_non_existing_file_descriptor_set() {
1315+
let mut config = Config::new();
1316+
config.skip_protoc_run();
1317+
config.file_descriptor_set_path("path-does-not-exist");
1318+
1319+
let err = config.load_fds(&[""], &[""]).unwrap_err();
1320+
assert_starts_with!(
1321+
err.to_string(),
1322+
"unable to open file_descriptor_set_path: \"path-does-not-exist\", OS: "
1323+
)
1324+
}
1325+
1326+
#[test]
1327+
fn test_error_text_incorrect_file_descriptor_set() {
1328+
let mut config = Config::new();
1329+
config.skip_protoc_run();
1330+
config.file_descriptor_set_path("src/lib.rs");
1331+
1332+
let err = config.load_fds(&[""], &[""]).unwrap_err();
1333+
assert_eq!(
1334+
err.to_string(),
1335+
"invalid FileDescriptorSet: failed to decode Protobuf message: unexpected end group tag"
1336+
)
1337+
}
1338+
1339+
#[test]
1340+
fn test_error_unset_out_dir() {
1341+
let mut config = Config::new();
1342+
1343+
let err = config
1344+
.compile_fds(FileDescriptorSet::default())
1345+
.unwrap_err();
1346+
assert_eq!(err.to_string(), "OUT_DIR environment variable is not set")
1347+
}
1348+
}

prost-build/src/extern_paths.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,34 @@ mod tests {
167167
case(".google.protobuf.Duration", "::prost_types::Duration");
168168
case(".google.protobuf.Empty", "()");
169169
}
170+
171+
#[test]
172+
fn test_error_fully_qualified() {
173+
let paths = [("foo".to_string(), "bar".to_string())];
174+
let err = ExternPaths::new(&paths, false).unwrap_err();
175+
assert_eq!(
176+
err.to_string(),
177+
"Protobuf paths must be fully qualified (begin with a leading '.'): foo"
178+
)
179+
}
180+
181+
#[test]
182+
fn test_error_invalid_path() {
183+
let paths = [(".foo.".to_string(), "bar".to_string())];
184+
let err = ExternPaths::new(&paths, false).unwrap_err();
185+
assert_eq!(
186+
err.to_string(),
187+
"invalid fully-qualified Protobuf path: .foo."
188+
)
189+
}
190+
191+
#[test]
192+
fn test_error_duplicate() {
193+
let paths = [
194+
(".foo".to_string(), "bar".to_string()),
195+
(".foo".to_string(), "bar".to_string()),
196+
];
197+
let err = ExternPaths::new(&paths, false).unwrap_err();
198+
assert_eq!(err.to_string(), "duplicate extern Protobuf path: .foo")
199+
}
170200
}

0 commit comments

Comments
 (0)