@@ -193,18 +193,21 @@ fn mod_resolution_error_path_attribute_does_not_exist() {
193193
194194mod rustfmt_stdin_formatting {
195195 use super :: rustfmt_std_input;
196+ use rustfmt_config_proc_macro:: { nightly_only_test, stable_only_test} ;
196197
197198 #[ rustfmt:: skip]
198199 #[ test]
199200 fn changes_are_output_to_stdout ( ) {
200- let args = [ ] ;
201+ // line endings are normalized to '\n' to avoid platform differences
202+ let args = [ "--config" , "newline_style=Unix" ] ;
201203 let source = "fn main () { println!(\" hello world!\" ); }" ;
202204 let ( stdout, _stderr) = rustfmt_std_input ( & args, source) ;
203205 let expected_output =
204206r#"fn main() {
205207 println!("hello world!");
206- }"# ;
207- assert ! ( stdout. contains( expected_output) )
208+ }
209+ "# ;
210+ assert ! ( stdout == expected_output) ;
208211 }
209212
210213 #[ test]
@@ -215,4 +218,99 @@ r#"fn main() {
215218 let ( stdout, _stderr) = rustfmt_std_input ( & args, source) ;
216219 assert ! ( stdout. trim_end( ) == source)
217220 }
221+
222+ #[ nightly_only_test]
223+ #[ test]
224+ fn input_ignored_when_stdin_file_hint_is_ignored ( ) {
225+ // NOTE: the source is not properly formatted, but we're giving rustfmt a hint that
226+ // the input actually corresponds to `src/lib.rs`, which is ignored in the given config file
227+ let args = [
228+ "--stdin-file-hint" ,
229+ "src/lib.rs" ,
230+ "--config-path" ,
231+ "tests/config/stdin-file-hint-ignore.toml" ,
232+ ] ;
233+ let source = "fn main () { println!(\" hello world!\" ); }" ;
234+ let ( stdout, _stderr) = rustfmt_std_input ( & args, source) ;
235+ assert ! ( stdout. trim_end( ) == source)
236+ }
237+
238+ #[ rustfmt:: skip]
239+ #[ nightly_only_test]
240+ #[ test]
241+ fn input_formatted_when_stdin_file_hint_is_not_ignored ( ) {
242+ // NOTE: `src/bin/main.rs` is not ignored in the config file so the input is formatted.
243+ // line endings are normalized to '\n' to avoid platform differences
244+ let args = [
245+ "--stdin-file-hint" ,
246+ "src/bin/main.rs" ,
247+ "--config-path" ,
248+ "tests/config/stdin-file-hint-ignore.toml" ,
249+ "--config" ,
250+ "newline_style=Unix" ,
251+ ] ;
252+ let source = "fn main () { println!(\" hello world!\" ); }" ;
253+ let ( stdout, _stderr) = rustfmt_std_input ( & args, source) ;
254+ let expected_output =
255+ r#"fn main() {
256+ println!("hello world!");
257+ }
258+ "# ;
259+ assert ! ( stdout == expected_output) ;
260+ }
261+
262+ #[ rustfmt:: skip]
263+ #[ stable_only_test]
264+ #[ test]
265+ fn ignore_list_is_not_set_on_stable_channel_and_therefore_stdin_file_hint_does_nothing ( ) {
266+ // NOTE: the source is not properly formatted, and although the file is `ignored` we
267+ // can't set the `ignore` list on the stable channel so the input is formatted
268+ // line endings are normalized to '\n' to avoid platform differences
269+ let args = [
270+ "--stdin-file-hint" ,
271+ "src/lib.rs" ,
272+ "--config-path" ,
273+ "tests/config/stdin-file-hint-ignore.toml" ,
274+ "--config" ,
275+ "newline_style=Unix" ,
276+ ] ;
277+ let source = "fn main () { println!(\" hello world!\" ); }" ;
278+ let ( stdout, _stderr) = rustfmt_std_input ( & args, source) ;
279+ let expected_output =
280+ r#"fn main() {
281+ println!("hello world!");
282+ }
283+ "# ;
284+ assert ! ( stdout == expected_output) ;
285+
286+ }
287+ }
288+
289+ mod stdin_file_hint {
290+ use super :: { rustfmt, rustfmt_std_input} ;
291+
292+ #[ test]
293+ fn error_not_a_rust_file ( ) {
294+ let args = [ "--stdin-file-hint" , "README.md" ] ;
295+ let source = "fn main() {}" ;
296+ let ( _stdout, stderr) = rustfmt_std_input ( & args, source) ;
297+ assert ! ( stderr. contains( "`--std-file-hint=\" README.md\" ` is not a rust file" ) )
298+ }
299+
300+ #[ test]
301+ fn error_file_not_found ( ) {
302+ let args = [ "--stdin-file-hint" , "does_not_exist.rs" ] ;
303+ let source = "fn main() {}" ;
304+ let ( _stdout, stderr) = rustfmt_std_input ( & args, source) ;
305+ assert ! ( stderr. contains( "`--std-file-hint=\" does_not_exist.rs\" ` could not be found" ) )
306+ }
307+
308+ #[ test]
309+ fn cant_use_stdin_file_hint_if_input_not_passed_to_rustfmt_via_stdin ( ) {
310+ let args = [ "--stdin-file-hint" , "src/lib.rs" , "src/lib.rs" ] ;
311+ let ( _stdout, stderr) = rustfmt ( & args) ;
312+ assert ! (
313+ stderr. contains( "Cannot use `--std-file-hint` without formatting input from stdin." )
314+ ) ;
315+ }
218316}
0 commit comments