]> git.lizzy.rs Git - rust.git/blobdiff - src/rustfmt_diff.rs
Add test for trailing newline in diff.
[rust.git] / src / rustfmt_diff.rs
index c0aca68fe2ffebba94a00a4fafc5941385a79310..c2dac6c041689d3a6e3c36780c4b8056008a6cab 100644 (file)
@@ -8,11 +8,12 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use config::Color;
 use diff;
 use std::collections::VecDeque;
 use std::io;
 use term;
-use utils::isatty;
+use utils::use_colored_tty;
 
 #[derive(Debug, PartialEq)]
 pub enum DiffLine {
@@ -96,12 +97,12 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
     results
 }
 
-pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F)
+pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F, color: Color)
 where
     F: Fn(u32) -> String,
 {
     match term::stdout() {
-        Some(ref t) if isatty() && t.supports_color() => {
+        Some(ref t) if use_colored_tty(color) && t.supports_color() => {
             print_diff_fancy(diff, get_section_title, term::stdout().unwrap())
         }
         _ => print_diff_basic(diff, get_section_title),
@@ -233,4 +234,20 @@ fn diff_zerocontext() {
             ]
         );
     }
+
+    #[test]
+    fn diff_trailing_newline() {
+        let src = "one\ntwo\nthree\nfour\nfive";
+        let dest = "one\ntwo\nthree\nfour\nfive\n";
+        let diff = make_diff(src, dest, 1);
+        assert_eq!(
+            diff,
+            vec![
+                Mismatch {
+                    line_number: 5,
+                    lines: vec![Context("five".into()), Expected("".into())],
+                },
+            ]
+        );
+    }
 }