@@ -111,21 +111,11 @@ fn execute() -> i32 {
111
111
}
112
112
113
113
let strategy = CargoFmtStrategy :: from_opts ( & opts) ;
114
- let mut rustfmt_args = opts. rustfmt_options ;
115
- if opts. check {
116
- let check_flag = String :: from ( "--check" ) ;
117
- if !rustfmt_args. contains ( & check_flag) {
118
- rustfmt_args. push ( check_flag) ;
119
- }
120
- }
121
- if let Some ( message_format) = opts. message_format {
122
- if let Err ( msg) = convert_message_format_to_rustfmt_args ( & message_format, & mut rustfmt_args)
123
- {
124
- print_usage_to_stderr ( & msg) ;
125
- return FAILURE ;
126
- }
114
+ let mut rustfmt_args = opts. rustfmt_options . to_owned ( ) ;
115
+ if let Err ( ref msg) = build_rustfmt_args ( & opts, & mut rustfmt_args) {
116
+ print_usage_to_stderr ( msg) ;
117
+ return FAILURE ;
127
118
}
128
-
129
119
let include_nested_test_files = opts. include_nested_test_files ;
130
120
131
121
if let Some ( specified_manifest_path) = opts. manifest_path {
@@ -152,13 +142,12 @@ fn execute() -> i32 {
152
142
}
153
143
}
154
144
155
- fn convert_message_format_to_rustfmt_args (
156
- message_format : & str ,
157
- rustfmt_args : & mut Vec < String > ,
158
- ) -> Result < ( ) , String > {
159
- let mut contains_emit_mode = false ;
145
+ fn build_rustfmt_args ( opts : & Opts , rustfmt_args : & mut Vec < String > ) -> Result < ( ) , String > {
160
146
let mut contains_check = false ;
147
+ let mut contains_emit_mode = false ;
161
148
let mut contains_list_files = false ;
149
+ let mut contains_recursive = false ;
150
+
162
151
for arg in rustfmt_args. iter ( ) {
163
152
if arg. starts_with ( "--emit" ) {
164
153
contains_emit_mode = true ;
@@ -169,37 +158,53 @@ fn convert_message_format_to_rustfmt_args(
169
158
if arg == "-l" || arg == "--files-with-diff" {
170
159
contains_list_files = true ;
171
160
}
161
+ if arg == "-r" || arg == "--recursive" {
162
+ contains_recursive = true ;
163
+ }
164
+ }
165
+
166
+ if opts. check && !contains_check {
167
+ rustfmt_args. push ( String :: from ( "--check" ) ) ;
172
168
}
173
- match message_format {
174
- "short" => {
175
- if !contains_list_files {
176
- rustfmt_args. push ( String :: from ( "-l" ) ) ;
169
+
170
+ if !contains_recursive {
171
+ rustfmt_args. push ( String :: from ( "--recursive" ) ) ;
172
+ }
173
+
174
+ if let Some ( ref format) = opts. message_format {
175
+ return match format. as_ref ( ) {
176
+ "short" => {
177
+ if !contains_list_files {
178
+ rustfmt_args. push ( String :: from ( "-l" ) ) ;
179
+ }
180
+ Ok ( ( ) )
177
181
}
178
- Ok ( ( ) )
179
- }
180
- "json" => {
181
- if contains_emit_mode {
182
- return Err ( String :: from (
183
- "cannot include --emit arg when --message-format is set to json" ,
184
- ) ) ;
182
+ "json" => {
183
+ if contains_emit_mode {
184
+ return Err ( String :: from (
185
+ "cannot include --emit arg when --message-format is set to json" ,
186
+ ) ) ;
187
+ }
188
+ if contains_check {
189
+ return Err ( String :: from (
190
+ "cannot include --check arg when --message-format is set to json" ,
191
+ ) ) ;
192
+ }
193
+ rustfmt_args. push ( String :: from ( "--emit" ) ) ;
194
+ rustfmt_args. push ( String :: from ( "json" ) ) ;
195
+ Ok ( ( ) )
185
196
}
186
- if contains_check {
187
- return Err ( String :: from (
188
- "cannot include --check arg when --message-format is set to json" ,
197
+ "human" => Ok ( ( ) ) ,
198
+ _ => {
199
+ return Err ( format ! (
200
+ "invalid --message-format value: {}. Allowed values are: short|json|human" ,
201
+ format
189
202
) ) ;
190
203
}
191
- rustfmt_args. push ( String :: from ( "--emit" ) ) ;
192
- rustfmt_args. push ( String :: from ( "json" ) ) ;
193
- Ok ( ( ) )
194
- }
195
- "human" => Ok ( ( ) ) ,
196
- _ => {
197
- return Err ( format ! (
198
- "invalid --message-format value: {}. Allowed values are: short|json|human" ,
199
- message_format
200
- ) ) ;
201
- }
204
+ } ;
202
205
}
206
+
207
+ Ok ( ( ) )
203
208
}
204
209
205
210
fn print_usage_to_stderr ( reason : & str ) {
@@ -801,13 +806,14 @@ mod cargo_fmt_tests {
801
806
) ;
802
807
}
803
808
804
- mod convert_message_format_to_rustfmt_args_tests {
809
+ mod build_rustfmt_args_tests {
805
810
use super :: * ;
806
811
807
812
#[ test]
808
813
fn invalid_message_format ( ) {
814
+ let cargo_fmt_opts = Opts :: from_iter ( & [ "test" , "--message-format" , "awesome" ] ) ;
809
815
assert_eq ! (
810
- convert_message_format_to_rustfmt_args ( "awesome" , & mut vec![ ] ) ,
816
+ build_rustfmt_args ( & cargo_fmt_opts , & mut vec![ ] ) ,
811
817
Err ( String :: from(
812
818
"invalid --message-format value: awesome. Allowed values are: short|json|human"
813
819
) ) ,
@@ -816,9 +822,10 @@ mod cargo_fmt_tests {
816
822
817
823
#[ test]
818
824
fn json_message_format_and_check_arg ( ) {
819
- let mut args = vec ! [ String :: from( "--check" ) ] ;
825
+ let mut rustfmt_args = vec ! [ String :: from( "--check" ) ] ;
826
+ let cargo_fmt_opts = Opts :: from_iter ( & [ "test" , "--message-format" , "json" ] ) ;
820
827
assert_eq ! (
821
- convert_message_format_to_rustfmt_args ( "json" , & mut args ) ,
828
+ build_rustfmt_args ( & cargo_fmt_opts , & mut rustfmt_args ) ,
822
829
Err ( String :: from(
823
830
"cannot include --check arg when --message-format is set to json"
824
831
) ) ,
@@ -827,9 +834,10 @@ mod cargo_fmt_tests {
827
834
828
835
#[ test]
829
836
fn json_message_format_and_emit_arg ( ) {
830
- let mut args = vec ! [ String :: from( "--emit" ) , String :: from( "checkstyle" ) ] ;
837
+ let cargo_fmt_opts = Opts :: from_iter ( & [ "test" , "--message-format" , "json" ] ) ;
838
+ let mut rustfmt_args = vec ! [ String :: from( "--emit" ) , String :: from( "checkstyle" ) ] ;
831
839
assert_eq ! (
832
- convert_message_format_to_rustfmt_args ( "json" , & mut args ) ,
840
+ build_rustfmt_args ( & cargo_fmt_opts , & mut rustfmt_args ) ,
833
841
Err ( String :: from(
834
842
"cannot include --emit arg when --message-format is set to json"
835
843
) ) ,
@@ -838,50 +846,155 @@ mod cargo_fmt_tests {
838
846
839
847
#[ test]
840
848
fn json_message_format ( ) {
841
- let mut args = vec ! [ String :: from( "--edition" ) , String :: from( "2018" ) ] ;
842
- assert ! ( convert_message_format_to_rustfmt_args( "json" , & mut args) . is_ok( ) ) ;
849
+ let mut rustfmt_args = vec ! [
850
+ String :: from( "--edition" ) ,
851
+ String :: from( "2018" ) ,
852
+ String :: from( "--recursive" ) ,
853
+ ] ;
854
+ let cargo_fmt_opts = Opts :: from_iter ( & [ "test" , "--message-format" , "json" ] ) ;
855
+ assert ! ( build_rustfmt_args( & cargo_fmt_opts, & mut rustfmt_args) . is_ok( ) ) ;
843
856
assert_eq ! (
844
- args ,
857
+ rustfmt_args ,
845
858
vec![
846
859
String :: from( "--edition" ) ,
847
860
String :: from( "2018" ) ,
861
+ String :: from( "--recursive" ) ,
848
862
String :: from( "--emit" ) ,
849
- String :: from( "json" )
863
+ String :: from( "json" ) ,
850
864
]
851
865
) ;
852
866
}
853
867
854
868
#[ test]
855
869
fn human_message_format ( ) {
856
- let exp_args = vec ! [ String :: from( "--emit" ) , String :: from( "json" ) ] ;
857
- let mut act_args = exp_args. clone ( ) ;
858
- assert ! ( convert_message_format_to_rustfmt_args( "human" , & mut act_args) . is_ok( ) ) ;
859
- assert_eq ! ( act_args, exp_args) ;
870
+ let exp_args = vec ! [
871
+ String :: from( "--emit" ) ,
872
+ String :: from( "json" ) ,
873
+ String :: from( "--recursive" ) ,
874
+ ] ;
875
+ let cargo_fmt_opts = Opts :: from_iter ( & [ "test" , "--message-format" , "human" ] ) ;
876
+ let mut rustfmt_args = exp_args. clone ( ) ;
877
+ assert ! ( build_rustfmt_args( & cargo_fmt_opts, & mut rustfmt_args) . is_ok( ) ) ;
878
+ assert_eq ! ( rustfmt_args, exp_args) ;
860
879
}
861
880
862
881
#[ test]
863
882
fn short_message_format ( ) {
864
- let mut args = vec ! [ String :: from( "--check" ) ] ;
865
- assert ! ( convert_message_format_to_rustfmt_args( "short" , & mut args) . is_ok( ) ) ;
866
- assert_eq ! ( args, vec![ String :: from( "--check" ) , String :: from( "-l" ) ] ) ;
883
+ let mut rustfmt_args = vec ! [ String :: from( "--check" ) , String :: from( "--recursive" ) ] ;
884
+ let cargo_fmt_opts = Opts :: from_iter ( & [ "test" , "--message-format" , "short" ] ) ;
885
+ assert ! ( build_rustfmt_args( & cargo_fmt_opts, & mut rustfmt_args) . is_ok( ) ) ;
886
+ assert_eq ! (
887
+ rustfmt_args,
888
+ vec![
889
+ String :: from( "--check" ) ,
890
+ String :: from( "--recursive" ) ,
891
+ String :: from( "-l" ) ,
892
+ ] ,
893
+ ) ;
867
894
}
868
895
869
896
#[ test]
870
897
fn short_message_format_included_short_list_files_flag ( ) {
871
- let mut args = vec ! [ String :: from( "--check" ) , String :: from( "-l" ) ] ;
872
- assert ! ( convert_message_format_to_rustfmt_args( "short" , & mut args) . is_ok( ) ) ;
873
- assert_eq ! ( args, vec![ String :: from( "--check" ) , String :: from( "-l" ) ] ) ;
898
+ let mut rustfmt_args = vec ! [
899
+ String :: from( "--check" ) ,
900
+ String :: from( "-l" ) ,
901
+ String :: from( "--recursive" ) ,
902
+ ] ;
903
+ let cargo_fmt_opts = Opts :: from_iter ( & [ "test" , "--message-format" , "short" ] ) ;
904
+ assert ! ( build_rustfmt_args( & cargo_fmt_opts, & mut rustfmt_args) . is_ok( ) ) ;
905
+ assert_eq ! (
906
+ rustfmt_args,
907
+ vec![
908
+ String :: from( "--check" ) ,
909
+ String :: from( "-l" ) ,
910
+ String :: from( "--recursive" ) ,
911
+ ] ,
912
+ ) ;
874
913
}
875
914
876
915
#[ test]
877
916
fn short_message_format_included_long_list_files_flag ( ) {
878
- let mut args = vec ! [ String :: from( "--check" ) , String :: from( "--files-with-diff" ) ] ;
879
- assert ! ( convert_message_format_to_rustfmt_args( "short" , & mut args) . is_ok( ) ) ;
917
+ let mut rustfmt_args = vec ! [
918
+ String :: from( "--check" ) ,
919
+ String :: from( "--files-with-diff" ) ,
920
+ String :: from( "--recursive" ) ,
921
+ ] ;
922
+ let cargo_fmt_opts = Opts :: from_iter ( & [ "test" , "--message-format" , "short" ] ) ;
923
+ assert ! ( build_rustfmt_args( & cargo_fmt_opts, & mut rustfmt_args) . is_ok( ) ) ;
924
+ assert_eq ! (
925
+ rustfmt_args,
926
+ vec![
927
+ String :: from( "--check" ) ,
928
+ String :: from( "--files-with-diff" ) ,
929
+ String :: from( "--recursive" ) ,
930
+ ]
931
+ ) ;
932
+ }
933
+
934
+ #[ test]
935
+ fn recursive_shorthand_not_duplicated ( ) {
936
+ let mut rustfmt_args = vec ! [ String :: from( "-r" ) ] ;
937
+ let empty: Vec < String > = vec ! [ ] ;
938
+ assert ! ( build_rustfmt_args( & Opts :: from_iter( & empty) , & mut rustfmt_args) . is_ok( ) ) ;
939
+ assert_eq ! ( rustfmt_args, vec![ String :: from( "-r" ) ] ) ;
940
+ }
941
+
942
+ #[ test]
943
+ fn recursive_long_not_duplicated ( ) {
944
+ let mut rustfmt_args = vec ! [ String :: from( "--recursive" ) ] ;
945
+ let empty: Vec < String > = vec ! [ ] ;
946
+ assert ! ( build_rustfmt_args( & Opts :: from_iter( & empty) , & mut rustfmt_args) . is_ok( ) ) ;
947
+ assert_eq ! ( rustfmt_args, vec![ String :: from( "--recursive" ) ] ) ;
948
+ }
949
+
950
+ #[ test]
951
+ fn recursive_added ( ) {
952
+ let mut rustfmt_args = vec ! [ ] ;
953
+ let empty: Vec < String > = vec ! [ ] ;
954
+ assert ! ( build_rustfmt_args( & Opts :: from_iter( & empty) , & mut rustfmt_args) . is_ok( ) ) ;
955
+ assert_eq ! ( rustfmt_args, vec![ String :: from( "--recursive" ) ] ) ;
956
+ }
957
+
958
+ #[ test]
959
+ fn check_not_duplicated_when_included_in_cargo_fmt ( ) {
960
+ let mut rustfmt_args = vec ! [ String :: from( "--check" ) , String :: from( "--recursive" ) ] ;
961
+ let cargo_fmt_opts = Opts :: from_iter ( & [ "test" , "--check" ] ) ;
962
+ assert ! ( build_rustfmt_args( & cargo_fmt_opts, & mut rustfmt_args) . is_ok( ) ) ;
963
+ assert_eq ! (
964
+ rustfmt_args,
965
+ vec![ String :: from( "--check" ) , String :: from( "--recursive" ) ] ,
966
+ ) ;
967
+ }
968
+
969
+ #[ test]
970
+ fn check_still_passed_through_when_not_included_in_cargo_fmt ( ) {
971
+ let mut rustfmt_args = vec ! [ String :: from( "--check" ) , String :: from( "--recursive" ) ] ;
972
+ let empty: Vec < String > = vec ! [ ] ;
973
+ assert ! ( build_rustfmt_args( & Opts :: from_iter( & empty) , & mut rustfmt_args) . is_ok( ) ) ;
880
974
assert_eq ! (
881
- args ,
882
- vec![ String :: from( "--check" ) , String :: from( "--files-with-diff " ) ]
975
+ rustfmt_args ,
976
+ vec![ String :: from( "--check" ) , String :: from( "--recursive " ) ] ,
883
977
) ;
884
978
}
979
+
980
+ #[ test]
981
+ fn check_added ( ) {
982
+ let mut rustfmt_args = vec ! [ String :: from( "--recursive" ) ] ;
983
+ let cargo_fmt_opts = Opts :: from_iter ( & [ "test" , "--check" ] ) ;
984
+ assert ! ( build_rustfmt_args( & cargo_fmt_opts, & mut rustfmt_args) . is_ok( ) ) ;
985
+ assert_eq ! (
986
+ rustfmt_args,
987
+ vec![ String :: from( "--recursive" ) , String :: from( "--check" ) ] ,
988
+ ) ;
989
+ }
990
+
991
+ #[ test]
992
+ fn check_not_added_when_flag_disabled ( ) {
993
+ let mut rustfmt_args = vec ! [ String :: from( "--recursive" ) ] ;
994
+ let empty: Vec < String > = vec ! [ ] ;
995
+ assert ! ( build_rustfmt_args( & Opts :: from_iter( & empty) , & mut rustfmt_args) . is_ok( ) ) ;
996
+ assert_eq ! ( rustfmt_args, vec![ String :: from( "--recursive" ) ] ) ;
997
+ }
885
998
}
886
999
887
1000
mod get_nested_integration_test_files_tests {
0 commit comments