@@ -1249,3 +1249,100 @@ pub fn protoc_include_from_env() -> Option<PathBuf> {
1249
1249
1250
1250
Some ( protoc_include)
1251
1251
}
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:\n left: {}\n right: {}" ,
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
+ }
0 commit comments