Skip to content

Commit a3a372f

Browse files
Display modification times of input files in unified diff
1 parent 5b814f8 commit a3a372f

File tree

3 files changed

+68
-37
lines changed

3 files changed

+68
-37
lines changed

src/context_diff.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::io::Write;
88

99
use crate::params::Params;
1010
use crate::utils::do_write_line;
11+
use crate::utils::get_modification_time;
1112

1213
#[derive(Debug, PartialEq)]
1314
pub enum DiffLine {
@@ -265,24 +266,7 @@ fn make_diff(
265266
results
266267
}
267268

268-
fn get_modification_time(file_path: &str) -> String {
269-
use chrono::{DateTime, Local};
270-
use std::fs;
271-
272-
let metadata = fs::metadata(file_path).expect("Failed to get metadata");
273-
let modification_time = metadata
274-
.modified()
275-
.expect("Failed to get modification time");
276-
let modification_time: DateTime<Local> = modification_time.into();
277-
let modification_time: String = modification_time
278-
.format("%Y-%m-%d %H:%M:%S%.9f %z")
279-
.to_string();
280-
281-
modification_time
282-
}
283-
284269
#[must_use]
285-
#[allow(clippy::too_many_arguments)]
286270
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
287271
let from_modified_time = get_modification_time(&params.from.to_string_lossy());
288272
let to_modified_time = get_modification_time(&params.to.to_string_lossy());
@@ -747,7 +731,6 @@ mod tests {
747731
use std::str;
748732

749733
let target = "target/context-diff";
750-
// test all possible six-line files.
751734
let _ = std::fs::create_dir(target);
752735
let from_filename = &format!("{target}/foo");
753736
let _ = File::create(from_filename).unwrap();

src/unified_diff.rs

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::io::Write;
88

99
use crate::params::Params;
1010
use crate::utils::do_write_line;
11+
use crate::utils::get_modification_time;
1112

1213
#[derive(Debug, PartialEq)]
1314
pub enum DiffLine {
@@ -238,10 +239,14 @@ fn make_diff(
238239

239240
#[must_use]
240241
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
242+
let from_modified_time = get_modification_time(&params.from.to_string_lossy());
243+
let to_modified_time = get_modification_time(&params.to.to_string_lossy());
241244
let mut output = format!(
242-
"--- {0}\t\n+++ {1}\t\n",
245+
"--- {0}\t{1}\n+++ {2}\t{3}\n",
243246
params.from.to_string_lossy(),
244-
params.to.to_string_lossy()
247+
from_modified_time,
248+
params.to.to_string_lossy(),
249+
to_modified_time
245250
)
246251
.into_bytes();
247252
let diff_results = make_diff(expected, actual, params.context_count, params.brief);
@@ -449,13 +454,15 @@ mod tests {
449454
if f != 2 {
450455
bet.write_all(b"l\n").unwrap();
451456
}
457+
let _ = File::create(&format!("{target}/aalef")).unwrap();
458+
let mut fa = File::create(&format!("{target}/alef")).unwrap();
452459
// This test diff is intentionally reversed.
453460
// We want it to turn the alef into bet.
454461
let diff = diff(
455462
&alef,
456463
&bet,
457464
&Params {
458-
from: "a/alef".into(),
465+
from: (&format!("{target}/aalef")).into(),
459466
to: (&format!("{target}/alef")).into(),
460467
context_count: 2,
461468
..Default::default()
@@ -465,7 +472,6 @@ mod tests {
465472
.unwrap()
466473
.write_all(&diff)
467474
.unwrap();
468-
let mut fa = File::create(&format!("{target}/alef")).unwrap();
469475
fa.write_all(&alef[..]).unwrap();
470476
let mut fb = File::create(&format!("{target}/bet")).unwrap();
471477
fb.write_all(&bet[..]).unwrap();
@@ -565,13 +571,15 @@ mod tests {
565571
}
566572
_ => unreachable!(),
567573
}
574+
let _ = File::create(&format!("{target}/aalefn")).unwrap();
575+
let mut fa = File::create(&format!("{target}/alefn")).unwrap();
568576
// This test diff is intentionally reversed.
569577
// We want it to turn the alef into bet.
570578
let diff = diff(
571579
&alef,
572580
&bet,
573581
&Params {
574-
from: "a/alefn".into(),
582+
from: (&format!("{target}/aalefn")).into(),
575583
to: (&format!("{target}/alefn")).into(),
576584
context_count: 2,
577585
..Default::default()
@@ -581,7 +589,6 @@ mod tests {
581589
.unwrap()
582590
.write_all(&diff)
583591
.unwrap();
584-
let mut fa = File::create(&format!("{target}/alefn")).unwrap();
585592
fa.write_all(&alef[..]).unwrap();
586593
let mut fb = File::create(&format!("{target}/betn")).unwrap();
587594
fb.write_all(&bet[..]).unwrap();
@@ -661,13 +668,15 @@ mod tests {
661668
3 => {}
662669
_ => unreachable!(),
663670
}
671+
let _ = File::create(&format!("{target}/aalef_")).unwrap();
672+
let mut fa = File::create(&format!("{target}/alef_")).unwrap();
664673
// This test diff is intentionally reversed.
665674
// We want it to turn the alef into bet.
666675
let diff = diff(
667676
&alef,
668677
&bet,
669678
&Params {
670-
from: "a/alef_".into(),
679+
from: (&format!("{target}/aalef_")).into(),
671680
to: (&format!("{target}/alef_")).into(),
672681
context_count: 2,
673682
..Default::default()
@@ -677,7 +686,6 @@ mod tests {
677686
.unwrap()
678687
.write_all(&diff)
679688
.unwrap();
680-
let mut fa = File::create(&format!("{target}/alef_")).unwrap();
681689
fa.write_all(&alef[..]).unwrap();
682690
let mut fb = File::create(&format!("{target}/bet_")).unwrap();
683691
fb.write_all(&bet[..]).unwrap();
@@ -742,13 +750,15 @@ mod tests {
742750
if f != 2 {
743751
bet.write_all(b"l\n").unwrap();
744752
}
753+
let _ = File::create(&format!("{target}/aalefx")).unwrap();
754+
let mut fa = File::create(&format!("{target}/alefx")).unwrap();
745755
// This test diff is intentionally reversed.
746756
// We want it to turn the alef into bet.
747757
let diff = diff(
748758
&alef,
749759
&bet,
750760
&Params {
751-
from: "a/alefx".into(),
761+
from: (&format!("{target}/aalefx")).into(),
752762
to: (&format!("{target}/alefx")).into(),
753763
context_count: 2,
754764
..Default::default()
@@ -758,7 +768,6 @@ mod tests {
758768
.unwrap()
759769
.write_all(&diff)
760770
.unwrap();
761-
let mut fa = File::create(&format!("{target}/alefx")).unwrap();
762771
fa.write_all(&alef[..]).unwrap();
763772
let mut fb = File::create(&format!("{target}/betx")).unwrap();
764773
fb.write_all(&bet[..]).unwrap();
@@ -828,13 +837,15 @@ mod tests {
828837
if f != 2 {
829838
bet.write_all(b"f\n").unwrap();
830839
}
840+
let _ = File::create(&format!("{target}/aalefr")).unwrap();
841+
let mut fa = File::create(&format!("{target}/alefr")).unwrap();
831842
// This test diff is intentionally reversed.
832843
// We want it to turn the alef into bet.
833844
let diff = diff(
834845
&alef,
835846
&bet,
836847
&Params {
837-
from: "a/alefr".into(),
848+
from: (&format!("{target}/aalefr")).into(),
838849
to: (&format!("{target}/alefr")).into(),
839850
context_count: 2,
840851
..Default::default()
@@ -844,7 +855,6 @@ mod tests {
844855
.unwrap()
845856
.write_all(&diff)
846857
.unwrap();
847-
let mut fa = File::create(&format!("{target}/alefr")).unwrap();
848858
fa.write_all(&alef[..]).unwrap();
849859
let mut fb = File::create(&format!("{target}/betr")).unwrap();
850860
fb.write_all(&bet[..]).unwrap();
@@ -870,9 +880,17 @@ mod tests {
870880

871881
#[test]
872882
fn test_stop_early() {
873-
let from_filename = "foo";
883+
use regex::Regex;
884+
use std::fs::File;
885+
use std::str;
886+
887+
let target = "target/context-diff";
888+
let _ = std::fs::create_dir(target);
889+
let from_filename = &format!("{target}/foo");
890+
let _ = File::create(from_filename).unwrap();
874891
let from = ["a", "b", "c", ""].join("\n");
875-
let to_filename = "bar";
892+
let to_filename = &format!("{target}/bar");
893+
let _ = File::create(to_filename).unwrap();
876894
let to = ["a", "d", "c", ""].join("\n");
877895

878896
let diff_full = diff(
@@ -884,9 +902,14 @@ mod tests {
884902
..Default::default()
885903
},
886904
);
905+
906+
let diff_full_text = str::from_utf8(&diff_full).unwrap();
907+
let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap();
908+
let diff_full = re.replace_all(diff_full_text, "");
909+
887910
let expected_full = [
888-
"--- foo\t",
889-
"+++ bar\t",
911+
"--- target/context-diff/foo\t",
912+
"+++ target/context-diff/bar\t",
890913
"@@ -1,3 +1,3 @@",
891914
" a",
892915
"-b",
@@ -895,7 +918,7 @@ mod tests {
895918
"",
896919
]
897920
.join("\n");
898-
assert_eq!(diff_full, expected_full.as_bytes());
921+
assert_eq!(diff_full, expected_full);
899922

900923
let diff_brief = diff(
901924
from.as_bytes(),
@@ -907,8 +930,17 @@ mod tests {
907930
..Default::default()
908931
},
909932
);
910-
let expected_brief = ["--- foo\t", "+++ bar\t", ""].join("\n");
911-
assert_eq!(diff_brief, expected_brief.as_bytes());
933+
934+
let diff_brief_text = str::from_utf8(&diff_brief).unwrap();
935+
let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap();
936+
let diff_brief = re.replace_all(diff_brief_text, "");
937+
let expected_brief = [
938+
"--- target/context-diff/foo\t",
939+
"+++ target/context-diff/bar\t",
940+
"",
941+
]
942+
.join("\n");
943+
assert_eq!(diff_brief, expected_brief);
912944

913945
let nodiff_full = diff(
914946
from.as_bytes(),

src/utils.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ pub fn do_write_line(
5151
}
5252
}
5353

54+
pub fn get_modification_time(file_path: &str) -> String {
55+
use chrono::{DateTime, Local};
56+
use std::fs;
57+
58+
let metadata = fs::metadata(file_path).expect("Failed to get metadata");
59+
let modification_time = metadata
60+
.modified()
61+
.expect("Failed to get modification time");
62+
let modification_time: DateTime<Local> = modification_time.into();
63+
let modification_time: String = modification_time
64+
.format("%Y-%m-%d %H:%M:%S%.9f %z")
65+
.to_string();
66+
67+
modification_time
68+
}
69+
5470
#[cfg(test)]
5571
mod tests {
5672
use super::*;

0 commit comments

Comments
 (0)