@@ -16,6 +16,7 @@ use std::process::{Command, Output};
16
16
use std:: str;
17
17
use std:: time:: { self , Duration } ;
18
18
19
+ use anyhow:: { bail, format_err, Result } ;
19
20
use cargo_util:: { is_ci, ProcessBuilder , ProcessError } ;
20
21
use serde_json:: { self , Value } ;
21
22
use url:: Url ;
@@ -413,19 +414,6 @@ pub fn main_file(println: &str, deps: &[&str]) -> String {
413
414
buf
414
415
}
415
416
416
- trait ErrMsg < T > {
417
- fn with_err_msg ( self , val : String ) -> Result < T , String > ;
418
- }
419
-
420
- impl < T , E : fmt:: Display > ErrMsg < T > for Result < T , E > {
421
- fn with_err_msg ( self , val : String ) -> Result < T , String > {
422
- match self {
423
- Ok ( val) => Ok ( val) ,
424
- Err ( err) => Err ( format ! ( "{}; original={}" , val, err) ) ,
425
- }
426
- }
427
- }
428
-
429
417
// Path to cargo executables
430
418
pub fn cargo_dir ( ) -> PathBuf {
431
419
env:: var_os ( "CARGO_BIN_PATH" )
@@ -452,8 +440,6 @@ pub fn cargo_exe() -> PathBuf {
452
440
*
453
441
*/
454
442
455
- pub type MatchResult = Result < ( ) , String > ;
456
-
457
443
#[ must_use]
458
444
#[ derive( Clone ) ]
459
445
pub struct Execs {
@@ -703,7 +689,7 @@ impl Execs {
703
689
self
704
690
}
705
691
706
- pub fn exec_with_output ( & mut self ) -> anyhow :: Result < Output > {
692
+ pub fn exec_with_output ( & mut self ) -> Result < Output > {
707
693
self . ran = true ;
708
694
// TODO avoid unwrap
709
695
let p = ( & self . process_builder ) . clone ( ) . unwrap ( ) ;
@@ -778,7 +764,7 @@ impl Execs {
778
764
}
779
765
}
780
766
781
- fn match_process ( & self , process : & ProcessBuilder ) -> MatchResult {
767
+ fn match_process ( & self , process : & ProcessBuilder ) -> Result < ( ) > {
782
768
println ! ( "running {}" , process) ;
783
769
let res = if self . stream_output {
784
770
if is_ci ( ) {
@@ -814,32 +800,32 @@ impl Execs {
814
800
. and ( self . match_stdout ( stdout, stderr) )
815
801
. and ( self . match_stderr ( stdout, stderr) ) ;
816
802
}
817
- Err ( format ! ( "could not exec process {}: {:?}" , process, e) )
803
+ bail ! ( "could not exec process {}: {:?}" , process, e)
818
804
}
819
805
}
820
806
}
821
807
822
- fn match_output ( & self , actual : & Output ) -> MatchResult {
808
+ fn match_output ( & self , actual : & Output ) -> Result < ( ) > {
823
809
self . verify_checks_output ( actual) ;
824
810
self . match_status ( actual. status . code ( ) , & actual. stdout , & actual. stderr )
825
811
. and ( self . match_stdout ( & actual. stdout , & actual. stderr ) )
826
812
. and ( self . match_stderr ( & actual. stdout , & actual. stderr ) )
827
813
}
828
814
829
- fn match_status ( & self , code : Option < i32 > , stdout : & [ u8 ] , stderr : & [ u8 ] ) -> MatchResult {
815
+ fn match_status ( & self , code : Option < i32 > , stdout : & [ u8 ] , stderr : & [ u8 ] ) -> Result < ( ) > {
830
816
match self . expect_exit_code {
831
817
None => Ok ( ( ) ) ,
832
818
Some ( expected) if code == Some ( expected) => Ok ( ( ) ) ,
833
- Some ( _) => Err ( format ! (
819
+ Some ( _) => bail ! (
834
820
"exited with {:?}\n --- stdout\n {}\n --- stderr\n {}" ,
835
821
code,
836
822
String :: from_utf8_lossy( stdout) ,
837
823
String :: from_utf8_lossy( stderr)
838
- ) ) ,
824
+ ) ,
839
825
}
840
826
}
841
827
842
- fn match_stdout ( & self , stdout : & [ u8 ] , stderr : & [ u8 ] ) -> MatchResult {
828
+ fn match_stdout ( & self , stdout : & [ u8 ] , stderr : & [ u8 ] ) -> Result < ( ) > {
843
829
self . match_std (
844
830
self . expect_stdout . as_ref ( ) ,
845
831
stdout,
@@ -908,12 +894,12 @@ impl Execs {
908
894
self . match_std ( Some ( expect) , stderr, "stderr" , stderr, MatchKind :: Partial ) ;
909
895
910
896
if let ( Err ( _) , Err ( _) ) = ( match_std, match_err) {
911
- return Err ( format ! (
897
+ bail ! (
912
898
"expected to find:\n \
913
899
{}\n \n \
914
900
did not find in either output.",
915
901
expect
916
- ) ) ;
902
+ ) ;
917
903
}
918
904
}
919
905
@@ -923,18 +909,18 @@ impl Execs {
923
909
924
910
if let Some ( ref objects) = self . expect_json {
925
911
let stdout =
926
- str:: from_utf8 ( stdout) . map_err ( |_| "stdout was not utf8 encoded" . to_owned ( ) ) ?;
912
+ str:: from_utf8 ( stdout) . map_err ( |_| format_err ! ( "stdout was not utf8 encoded" ) ) ?;
927
913
let lines = stdout
928
914
. lines ( )
929
915
. filter ( |line| line. starts_with ( '{' ) )
930
916
. collect :: < Vec < _ > > ( ) ;
931
917
if lines. len ( ) != objects. len ( ) {
932
- return Err ( format ! (
918
+ bail ! (
933
919
"expected {} json lines, got {}, stdout:\n {}" ,
934
920
objects. len( ) ,
935
921
lines. len( ) ,
936
922
stdout
937
- ) ) ;
923
+ ) ;
938
924
}
939
925
for ( obj, line) in objects. iter ( ) . zip ( lines) {
940
926
self . match_json ( obj, line) ?;
@@ -943,7 +929,7 @@ impl Execs {
943
929
944
930
if !self . expect_json_contains_unordered . is_empty ( ) {
945
931
let stdout =
946
- str:: from_utf8 ( stdout) . map_err ( |_| "stdout was not utf8 encoded" . to_owned ( ) ) ?;
932
+ str:: from_utf8 ( stdout) . map_err ( |_| format_err ! ( "stdout was not utf8 encoded" ) ) ?;
947
933
let mut lines = stdout
948
934
. lines ( )
949
935
. filter ( |line| line. starts_with ( '{' ) )
@@ -955,22 +941,22 @@ impl Execs {
955
941
{
956
942
Some ( index) => lines. remove ( index) ,
957
943
None => {
958
- return Err ( format ! (
944
+ bail ! (
959
945
"Did not find expected JSON:\n \
960
946
{}\n \
961
947
Remaining available output:\n \
962
948
{}\n ",
963
949
serde_json:: to_string_pretty( obj) . unwrap( ) ,
964
950
lines. join( "\n " )
965
- ) ) ;
951
+ ) ;
966
952
}
967
953
} ;
968
954
}
969
955
}
970
956
Ok ( ( ) )
971
957
}
972
958
973
- fn match_stderr ( & self , stdout : & [ u8 ] , stderr : & [ u8 ] ) -> MatchResult {
959
+ fn match_stderr ( & self , stdout : & [ u8 ] , stderr : & [ u8 ] ) -> Result < ( ) > {
974
960
self . match_std (
975
961
self . expect_stderr . as_ref ( ) ,
976
962
stderr,
@@ -980,9 +966,9 @@ impl Execs {
980
966
)
981
967
}
982
968
983
- fn normalize_actual ( & self , description : & str , actual : & [ u8 ] ) -> Result < String , String > {
969
+ fn normalize_actual ( & self , description : & str , actual : & [ u8 ] ) -> Result < String > {
984
970
let actual = match str:: from_utf8 ( actual) {
985
- Err ( ..) => return Err ( format ! ( "{} was not utf8 encoded" , description) ) ,
971
+ Err ( ..) => bail ! ( "{} was not utf8 encoded" , description) ,
986
972
Ok ( actual) => actual,
987
973
} ;
988
974
Ok ( self . normalize_matcher ( actual) )
@@ -1002,7 +988,7 @@ impl Execs {
1002
988
description : & str ,
1003
989
extra : & [ u8 ] ,
1004
990
kind : MatchKind ,
1005
- ) -> MatchResult {
991
+ ) -> Result < ( ) > {
1006
992
let out = match expected {
1007
993
Some ( out) => self . normalize_matcher ( out) ,
1008
994
None => return Ok ( ( ) ) ,
@@ -1019,14 +1005,14 @@ impl Execs {
1019
1005
if diffs. is_empty ( ) {
1020
1006
Ok ( ( ) )
1021
1007
} else {
1022
- Err ( format ! (
1008
+ bail ! (
1023
1009
"differences:\n \
1024
1010
{}\n \n \
1025
1011
other output:\n \
1026
1012
`{}`",
1027
1013
diffs. join( "\n " ) ,
1028
1014
String :: from_utf8_lossy( extra)
1029
- ) )
1015
+ )
1030
1016
}
1031
1017
}
1032
1018
MatchKind :: Partial => {
@@ -1043,13 +1029,14 @@ impl Execs {
1043
1029
if diffs. is_empty ( ) {
1044
1030
Ok ( ( ) )
1045
1031
} else {
1046
- Err ( format ! (
1032
+ bail ! (
1047
1033
"expected to find:\n \
1048
1034
{}\n \n \
1049
1035
did not find in output:\n \
1050
1036
{}",
1051
- out, actual
1052
- ) )
1037
+ out,
1038
+ actual
1039
+ )
1053
1040
}
1054
1041
}
1055
1042
MatchKind :: PartialN ( number) => {
@@ -1068,13 +1055,15 @@ impl Execs {
1068
1055
if matches == number {
1069
1056
Ok ( ( ) )
1070
1057
} else {
1071
- Err ( format ! (
1058
+ bail ! (
1072
1059
"expected to find {} occurrences:\n \
1073
1060
{}\n \n \
1074
1061
did not find in output:\n \
1075
1062
{}",
1076
- number, out, actual
1077
- ) )
1063
+ number,
1064
+ out,
1065
+ actual
1066
+ )
1078
1067
}
1079
1068
}
1080
1069
MatchKind :: NotPresent => {
@@ -1089,13 +1078,14 @@ impl Execs {
1089
1078
}
1090
1079
}
1091
1080
if diffs. is_empty ( ) {
1092
- Err ( format ! (
1081
+ bail ! (
1093
1082
"expected not to find:\n \
1094
1083
{}\n \n \
1095
1084
but found in output:\n \
1096
1085
{}",
1097
- out, actual
1098
- ) )
1086
+ out,
1087
+ actual
1088
+ )
1099
1089
} else {
1100
1090
Ok ( ( ) )
1101
1091
}
@@ -1104,12 +1094,7 @@ impl Execs {
1104
1094
}
1105
1095
}
1106
1096
1107
- fn match_with_without (
1108
- & self ,
1109
- actual : & [ u8 ] ,
1110
- with : & [ String ] ,
1111
- without : & [ String ] ,
1112
- ) -> MatchResult {
1097
+ fn match_with_without ( & self , actual : & [ u8 ] , with : & [ String ] , without : & [ String ] ) -> Result < ( ) > {
1113
1098
let actual = self . normalize_actual ( "stderr" , actual) ?;
1114
1099
let contains = |s, line| {
1115
1100
let mut s = self . normalize_matcher ( s) ;
@@ -1123,16 +1108,18 @@ impl Execs {
1123
1108
. filter ( |line| !without. iter ( ) . any ( |without| contains ( without, line) ) )
1124
1109
. collect ( ) ;
1125
1110
match matches. len ( ) {
1126
- 0 => Err ( format ! (
1111
+ 0 => bail ! (
1127
1112
"Could not find expected line in output.\n \
1128
1113
With contents: {:?}\n \
1129
1114
Without contents: {:?}\n \
1130
1115
Actual stderr:\n \
1131
1116
{}\n ",
1132
- with, without, actual
1133
- ) ) ,
1117
+ with,
1118
+ without,
1119
+ actual
1120
+ ) ,
1134
1121
1 => Ok ( ( ) ) ,
1135
- _ => Err ( format ! (
1122
+ _ => bail ! (
1136
1123
"Found multiple matching lines, but only expected one.\n \
1137
1124
With contents: {:?}\n \
1138
1125
Without contents: {:?}\n \
@@ -1141,17 +1128,17 @@ impl Execs {
1141
1128
with,
1142
1129
without,
1143
1130
matches. join( "\n " )
1144
- ) ) ,
1131
+ ) ,
1145
1132
}
1146
1133
}
1147
1134
1148
- fn match_json ( & self , expected : & str , line : & str ) -> MatchResult {
1135
+ fn match_json ( & self , expected : & str , line : & str ) -> Result < ( ) > {
1149
1136
let actual = match line. parse ( ) {
1150
- Err ( e) => return Err ( format ! ( "invalid json, {}:\n `{}`" , e, line) ) ,
1137
+ Err ( e) => bail ! ( "invalid json, {}:\n `{}`" , e, line) ,
1151
1138
Ok ( actual) => actual,
1152
1139
} ;
1153
1140
let expected = match expected. parse ( ) {
1154
- Err ( e) => return Err ( format ! ( "invalid json, {}:\n `{}`" , e, line) ) ,
1141
+ Err ( e) => bail ! ( "invalid json, {}:\n `{}`" , e, line) ,
1155
1142
Ok ( expected) => expected,
1156
1143
} ;
1157
1144
@@ -1231,7 +1218,7 @@ pub fn lines_match(expected: &str, mut actual: &str) -> bool {
1231
1218
actual. is_empty ( ) || expected. ends_with ( "[..]" )
1232
1219
}
1233
1220
1234
- pub fn lines_match_unordered ( expected : & str , actual : & str ) -> Result < ( ) , String > {
1221
+ pub fn lines_match_unordered ( expected : & str , actual : & str ) -> Result < ( ) > {
1235
1222
let mut a = actual. lines ( ) . collect :: < Vec < _ > > ( ) ;
1236
1223
// match more-constrained lines first, although in theory we'll
1237
1224
// need some sort of recursive match here. This handles the case
@@ -1252,19 +1239,19 @@ pub fn lines_match_unordered(expected: &str, actual: &str) -> Result<(), String>
1252
1239
}
1253
1240
}
1254
1241
if !failures. is_empty ( ) {
1255
- return Err ( format ! (
1242
+ bail ! (
1256
1243
"Did not find expected line(s):\n {}\n \
1257
1244
Remaining available output:\n {}\n ",
1258
1245
failures. join( "\n " ) ,
1259
1246
a. join( "\n " )
1260
- ) ) ;
1247
+ ) ;
1261
1248
}
1262
1249
if !a. is_empty ( ) {
1263
- Err ( format ! (
1250
+ bail ! (
1264
1251
"Output included extra lines:\n \
1265
1252
{}\n ",
1266
1253
a. join( "\n " )
1267
- ) )
1254
+ )
1268
1255
} else {
1269
1256
Ok ( ( ) )
1270
1257
}
@@ -1334,19 +1321,15 @@ fn lines_match_works() {
1334
1321
/// as paths). You can use a `"{...}"` string literal as a wildcard for
1335
1322
/// arbitrary nested JSON (useful for parts of object emitted by other programs
1336
1323
/// (e.g., rustc) rather than Cargo itself).
1337
- pub fn find_json_mismatch (
1338
- expected : & Value ,
1339
- actual : & Value ,
1340
- cwd : Option < & Path > ,
1341
- ) -> Result < ( ) , String > {
1324
+ pub fn find_json_mismatch ( expected : & Value , actual : & Value , cwd : Option < & Path > ) -> Result < ( ) > {
1342
1325
match find_json_mismatch_r ( expected, actual, cwd) {
1343
- Some ( ( expected_part, actual_part) ) => Err ( format ! (
1326
+ Some ( ( expected_part, actual_part) ) => bail ! (
1344
1327
"JSON mismatch\n Expected:\n {}\n Was:\n {}\n Expected part:\n {}\n Actual part:\n {}\n " ,
1345
1328
serde_json:: to_string_pretty( expected) . unwrap( ) ,
1346
1329
serde_json:: to_string_pretty( & actual) . unwrap( ) ,
1347
1330
serde_json:: to_string_pretty( expected_part) . unwrap( ) ,
1348
1331
serde_json:: to_string_pretty( actual_part) . unwrap( ) ,
1349
- ) ) ,
1332
+ ) ,
1350
1333
None => Ok ( ( ) ) ,
1351
1334
}
1352
1335
}
0 commit comments