Do not create dummy files

Since we now returning SystemTime::now() for invalid file input,
there is no need to crate dummy files
This commit is contained in:
Tanmay Patil
2024-04-14 18:24:00 +05:30
parent 33783d094e
commit ba7cb0aef9
2 changed files with 28 additions and 57 deletions
+13 -27
View File
@@ -427,15 +427,13 @@ mod tests {
if f != 2 { if f != 2 {
bet.write_all(b"l\n").unwrap(); bet.write_all(b"l\n").unwrap();
} }
let _ = File::create(&format!("{target}/aalef")).unwrap();
let mut fa = File::create(&format!("{target}/alef")).unwrap();
// This test diff is intentionally reversed. // This test diff is intentionally reversed.
// We want it to turn the alef into bet. // We want it to turn the alef into bet.
let diff = diff( let diff = diff(
&alef, &alef,
&bet, &bet,
&Params { &Params {
from: (&format!("{target}/aalef")).into(), from: "a/alef".into(),
to: (&format!("{target}/alef")).into(), to: (&format!("{target}/alef")).into(),
context_count: 2, context_count: 2,
..Default::default() ..Default::default()
@@ -445,6 +443,7 @@ mod tests {
.unwrap() .unwrap()
.write_all(&diff) .write_all(&diff)
.unwrap(); .unwrap();
let mut fa = File::create(&format!("{target}/alef")).unwrap();
fa.write_all(&alef[..]).unwrap(); fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/bet")).unwrap(); let mut fb = File::create(&format!("{target}/bet")).unwrap();
fb.write_all(&bet[..]).unwrap(); fb.write_all(&bet[..]).unwrap();
@@ -509,15 +508,13 @@ mod tests {
if f != 2 { if f != 2 {
bet.write_all(b"l\n").unwrap(); bet.write_all(b"l\n").unwrap();
} }
let _ = File::create(&format!("{target}/aalef_")).unwrap();
let mut fa = File::create(&format!("{target}/alef_")).unwrap();
// This test diff is intentionally reversed. // This test diff is intentionally reversed.
// We want it to turn the alef into bet. // We want it to turn the alef into bet.
let diff = diff( let diff = diff(
&alef, &alef,
&bet, &bet,
&Params { &Params {
from: (&format!("{target}/aalef_")).into(), from: "a/alef_".into(),
to: (&format!("{target}/alef_")).into(), to: (&format!("{target}/alef_")).into(),
context_count: 2, context_count: 2,
..Default::default() ..Default::default()
@@ -527,6 +524,7 @@ mod tests {
.unwrap() .unwrap()
.write_all(&diff) .write_all(&diff)
.unwrap(); .unwrap();
let mut fa = File::create(&format!("{target}/alef_")).unwrap();
fa.write_all(&alef[..]).unwrap(); fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/bet_")).unwrap(); let mut fb = File::create(&format!("{target}/bet_")).unwrap();
fb.write_all(&bet[..]).unwrap(); fb.write_all(&bet[..]).unwrap();
@@ -594,15 +592,13 @@ mod tests {
if alef.is_empty() && bet.is_empty() { if alef.is_empty() && bet.is_empty() {
continue; continue;
}; };
let _ = File::create(&format!("{target}/aalefx")).unwrap();
let mut fa = File::create(&format!("{target}/alefx")).unwrap();
// This test diff is intentionally reversed. // This test diff is intentionally reversed.
// We want it to turn the alef into bet. // We want it to turn the alef into bet.
let diff = diff( let diff = diff(
&alef, &alef,
&bet, &bet,
&Params { &Params {
from: (&format!("{target}/aalefx")).into(), from: "a/alefx".into(),
to: (&format!("{target}/alefx")).into(), to: (&format!("{target}/alefx")).into(),
context_count: 2, context_count: 2,
..Default::default() ..Default::default()
@@ -612,6 +608,7 @@ mod tests {
.unwrap() .unwrap()
.write_all(&diff) .write_all(&diff)
.unwrap(); .unwrap();
let mut fa = File::create(&format!("{target}/alefx")).unwrap();
fa.write_all(&alef[..]).unwrap(); fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/betx")).unwrap(); let mut fb = File::create(&format!("{target}/betx")).unwrap();
fb.write_all(&bet[..]).unwrap(); fb.write_all(&bet[..]).unwrap();
@@ -682,15 +679,13 @@ mod tests {
if f != 2 { if f != 2 {
bet.write_all(b"f\n").unwrap(); bet.write_all(b"f\n").unwrap();
} }
let _ = File::create(&format!("{target}/aalefr")).unwrap();
let mut fa = File::create(&format!("{target}/alefr")).unwrap();
// This test diff is intentionally reversed. // This test diff is intentionally reversed.
// We want it to turn the alef into bet. // We want it to turn the alef into bet.
let diff = diff( let diff = diff(
&alef, &alef,
&bet, &bet,
&Params { &Params {
from: (&format!("{target}/aalefr")).into(), from: "a/alefr".into(),
to: (&format!("{target}/alefr")).into(), to: (&format!("{target}/alefr")).into(),
context_count: 2, context_count: 2,
..Default::default() ..Default::default()
@@ -700,6 +695,7 @@ mod tests {
.unwrap() .unwrap()
.write_all(&diff) .write_all(&diff)
.unwrap(); .unwrap();
let mut fa = File::create(&format!("{target}/alefr")).unwrap();
fa.write_all(&alef[..]).unwrap(); fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/betr")).unwrap(); let mut fb = File::create(&format!("{target}/betr")).unwrap();
fb.write_all(&bet[..]).unwrap(); fb.write_all(&bet[..]).unwrap();
@@ -727,15 +723,10 @@ mod tests {
#[test] #[test]
fn test_stop_early() { fn test_stop_early() {
use crate::assert_diff_eq; use crate::assert_diff_eq;
use std::fs::File;
let target = "target/context-diff"; let from_filename = "foo";
let _ = std::fs::create_dir(target);
let from_filename = &format!("{target}/foo");
let _ = File::create(from_filename).unwrap();
let from = ["a", "b", "c", ""].join("\n"); let from = ["a", "b", "c", ""].join("\n");
let to_filename = &format!("{target}/bar"); let to_filename = "bar";
let _ = File::create(to_filename).unwrap();
let to = ["a", "d", "c", ""].join("\n"); let to = ["a", "d", "c", ""].join("\n");
let diff_full = diff( let diff_full = diff(
@@ -749,8 +740,8 @@ mod tests {
); );
let expected_full = [ let expected_full = [
"*** target/context-diff/foo\tTIMESTAMP", "*** foo\tTIMESTAMP",
"--- target/context-diff/bar\tTIMESTAMP", "--- bar\tTIMESTAMP",
"***************", "***************",
"*** 1,3 ****", "*** 1,3 ****",
" a", " a",
@@ -776,12 +767,7 @@ mod tests {
}, },
); );
let expected_brief = [ let expected_brief = ["*** foo\tTIMESTAMP", "--- bar\tTIMESTAMP", ""].join("\n");
"*** target/context-diff/foo\tTIMESTAMP",
"--- target/context-diff/bar\tTIMESTAMP",
"",
]
.join("\n");
assert_diff_eq!(diff_brief, expected_brief); assert_diff_eq!(diff_brief, expected_brief);
let nodiff_full = diff( let nodiff_full = diff(
+15 -30
View File
@@ -454,15 +454,13 @@ mod tests {
if f != 2 { if f != 2 {
bet.write_all(b"l\n").unwrap(); bet.write_all(b"l\n").unwrap();
} }
let _ = File::create(&format!("{target}/aalef")).unwrap();
let mut fa = File::create(&format!("{target}/alef")).unwrap();
// This test diff is intentionally reversed. // This test diff is intentionally reversed.
// We want it to turn the alef into bet. // We want it to turn the alef into bet.
let diff = diff( let diff = diff(
&alef, &alef,
&bet, &bet,
&Params { &Params {
from: (&format!("{target}/aalef")).into(), from: "a/alef".into(),
to: (&format!("{target}/alef")).into(), to: (&format!("{target}/alef")).into(),
context_count: 2, context_count: 2,
..Default::default() ..Default::default()
@@ -472,6 +470,7 @@ mod tests {
.unwrap() .unwrap()
.write_all(&diff) .write_all(&diff)
.unwrap(); .unwrap();
let mut fa = File::create(&format!("{target}/alef")).unwrap();
fa.write_all(&alef[..]).unwrap(); fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/bet")).unwrap(); let mut fb = File::create(&format!("{target}/bet")).unwrap();
fb.write_all(&bet[..]).unwrap(); fb.write_all(&bet[..]).unwrap();
@@ -571,15 +570,13 @@ mod tests {
} }
_ => unreachable!(), _ => unreachable!(),
} }
let _ = File::create(&format!("{target}/aalefn")).unwrap();
let mut fa = File::create(&format!("{target}/alefn")).unwrap();
// This test diff is intentionally reversed. // This test diff is intentionally reversed.
// We want it to turn the alef into bet. // We want it to turn the alef into bet.
let diff = diff( let diff = diff(
&alef, &alef,
&bet, &bet,
&Params { &Params {
from: (&format!("{target}/aalefn")).into(), from: "a/alefn".into(),
to: (&format!("{target}/alefn")).into(), to: (&format!("{target}/alefn")).into(),
context_count: 2, context_count: 2,
..Default::default() ..Default::default()
@@ -589,6 +586,7 @@ mod tests {
.unwrap() .unwrap()
.write_all(&diff) .write_all(&diff)
.unwrap(); .unwrap();
let mut fa = File::create(&format!("{target}/alefn")).unwrap();
fa.write_all(&alef[..]).unwrap(); fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/betn")).unwrap(); let mut fb = File::create(&format!("{target}/betn")).unwrap();
fb.write_all(&bet[..]).unwrap(); fb.write_all(&bet[..]).unwrap();
@@ -668,15 +666,13 @@ mod tests {
3 => {} 3 => {}
_ => unreachable!(), _ => unreachable!(),
} }
let _ = File::create(&format!("{target}/aalef_")).unwrap();
let mut fa = File::create(&format!("{target}/alef_")).unwrap();
// This test diff is intentionally reversed. // This test diff is intentionally reversed.
// We want it to turn the alef into bet. // We want it to turn the alef into bet.
let diff = diff( let diff = diff(
&alef, &alef,
&bet, &bet,
&Params { &Params {
from: (&format!("{target}/aalef_")).into(), from: "a/alef_".into(),
to: (&format!("{target}/alef_")).into(), to: (&format!("{target}/alef_")).into(),
context_count: 2, context_count: 2,
..Default::default() ..Default::default()
@@ -686,6 +682,7 @@ mod tests {
.unwrap() .unwrap()
.write_all(&diff) .write_all(&diff)
.unwrap(); .unwrap();
let mut fa = File::create(&format!("{target}/alef_")).unwrap();
fa.write_all(&alef[..]).unwrap(); fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/bet_")).unwrap(); let mut fb = File::create(&format!("{target}/bet_")).unwrap();
fb.write_all(&bet[..]).unwrap(); fb.write_all(&bet[..]).unwrap();
@@ -750,15 +747,13 @@ mod tests {
if f != 2 { if f != 2 {
bet.write_all(b"l\n").unwrap(); bet.write_all(b"l\n").unwrap();
} }
let _ = File::create(&format!("{target}/aalefx")).unwrap();
let mut fa = File::create(&format!("{target}/alefx")).unwrap();
// This test diff is intentionally reversed. // This test diff is intentionally reversed.
// We want it to turn the alef into bet. // We want it to turn the alef into bet.
let diff = diff( let diff = diff(
&alef, &alef,
&bet, &bet,
&Params { &Params {
from: (&format!("{target}/aalefx")).into(), from: "a/alefx".into(),
to: (&format!("{target}/alefx")).into(), to: (&format!("{target}/alefx")).into(),
context_count: 2, context_count: 2,
..Default::default() ..Default::default()
@@ -768,6 +763,7 @@ mod tests {
.unwrap() .unwrap()
.write_all(&diff) .write_all(&diff)
.unwrap(); .unwrap();
let mut fa = File::create(&format!("{target}/alefx")).unwrap();
fa.write_all(&alef[..]).unwrap(); fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/betx")).unwrap(); let mut fb = File::create(&format!("{target}/betx")).unwrap();
fb.write_all(&bet[..]).unwrap(); fb.write_all(&bet[..]).unwrap();
@@ -837,15 +833,13 @@ mod tests {
if f != 2 { if f != 2 {
bet.write_all(b"f\n").unwrap(); bet.write_all(b"f\n").unwrap();
} }
let _ = File::create(&format!("{target}/aalefr")).unwrap();
let mut fa = File::create(&format!("{target}/alefr")).unwrap();
// This test diff is intentionally reversed. // This test diff is intentionally reversed.
// We want it to turn the alef into bet. // We want it to turn the alef into bet.
let diff = diff( let diff = diff(
&alef, &alef,
&bet, &bet,
&Params { &Params {
from: (&format!("{target}/aalefr")).into(), from: "a/alefr".into(),
to: (&format!("{target}/alefr")).into(), to: (&format!("{target}/alefr")).into(),
context_count: 2, context_count: 2,
..Default::default() ..Default::default()
@@ -855,6 +849,7 @@ mod tests {
.unwrap() .unwrap()
.write_all(&diff) .write_all(&diff)
.unwrap(); .unwrap();
let mut fa = File::create(&format!("{target}/alefr")).unwrap();
fa.write_all(&alef[..]).unwrap(); fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/betr")).unwrap(); let mut fb = File::create(&format!("{target}/betr")).unwrap();
fb.write_all(&bet[..]).unwrap(); fb.write_all(&bet[..]).unwrap();
@@ -881,15 +876,10 @@ mod tests {
#[test] #[test]
fn test_stop_early() { fn test_stop_early() {
use crate::assert_diff_eq; use crate::assert_diff_eq;
use std::fs::File;
let target = "target/context-diff"; let from_filename = "foo";
let _ = std::fs::create_dir(target);
let from_filename = &format!("{target}/foo");
let _ = File::create(from_filename).unwrap();
let from = ["a", "b", "c", ""].join("\n"); let from = ["a", "b", "c", ""].join("\n");
let to_filename = &format!("{target}/bar"); let to_filename = "bar";
let _ = File::create(to_filename).unwrap();
let to = ["a", "d", "c", ""].join("\n"); let to = ["a", "d", "c", ""].join("\n");
let diff_full = diff( let diff_full = diff(
@@ -903,8 +893,8 @@ mod tests {
); );
let expected_full = [ let expected_full = [
"--- target/context-diff/foo\tTIMESTAMP", "--- foo\tTIMESTAMP",
"+++ target/context-diff/bar\tTIMESTAMP", "+++ bar\tTIMESTAMP",
"@@ -1,3 +1,3 @@", "@@ -1,3 +1,3 @@",
" a", " a",
"-b", "-b",
@@ -926,12 +916,7 @@ mod tests {
}, },
); );
let expected_brief = [ let expected_brief = ["--- foo\tTIMESTAMP", "+++ bar\tTIMESTAMP", ""].join("\n");
"--- target/context-diff/foo\tTIMESTAMP",
"+++ target/context-diff/bar\tTIMESTAMP",
"",
]
.join("\n");
assert_diff_eq!(diff_brief, expected_brief); assert_diff_eq!(diff_brief, expected_brief);
let nodiff_full = diff( let nodiff_full = diff(