Skip to content

Commit

Permalink
Do not create dummy files
Browse files Browse the repository at this point in the history
Since we now returning SystemTime::now() for invalid file input,
there is no need to crate dummy files
  • Loading branch information
TanmayPatil105 committed Apr 14, 2024
1 parent 33783d0 commit aab1a13
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 49 deletions.
37 changes: 14 additions & 23 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,13 @@ mod tests {
if f != 2 {
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.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&bet,
&Params {
from: (&format!("{target}/aalef")).into(),
from: "a/alef".into(),
to: (&format!("{target}/alef")).into(),
context_count: 2,
..Default::default()
Expand All @@ -445,6 +443,7 @@ mod tests {
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{target}/alef")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/bet")).unwrap();
fb.write_all(&bet[..]).unwrap();
Expand Down Expand Up @@ -509,15 +508,13 @@ mod tests {
if f != 2 {
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.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&bet,
&Params {
from: (&format!("{target}/aalef_")).into(),
from: "a/alef_".into(),
to: (&format!("{target}/alef_")).into(),
context_count: 2,
..Default::default()
Expand All @@ -527,6 +524,7 @@ mod tests {
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{target}/alef_")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/bet_")).unwrap();
fb.write_all(&bet[..]).unwrap();
Expand Down Expand Up @@ -594,15 +592,13 @@ mod tests {
if alef.is_empty() && bet.is_empty() {
continue;
};
let _ = File::create(&format!("{target}/aalefx")).unwrap();
let mut fa = File::create(&format!("{target}/alefx")).unwrap();
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&bet,
&Params {
from: (&format!("{target}/aalefx")).into(),
from: "a/alefx".into(),
to: (&format!("{target}/alefx")).into(),
context_count: 2,
..Default::default()
Expand All @@ -612,6 +608,7 @@ mod tests {
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{target}/alefx")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/betx")).unwrap();
fb.write_all(&bet[..]).unwrap();
Expand Down Expand Up @@ -682,15 +679,13 @@ mod tests {
if f != 2 {
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.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&bet,
&Params {
from: (&format!("{target}/aalefr")).into(),
from: "a/alefr".into(),
to: (&format!("{target}/alefr")).into(),
context_count: 2,
..Default::default()
Expand All @@ -700,6 +695,7 @@ mod tests {
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{target}/alefr")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/betr")).unwrap();
fb.write_all(&bet[..]).unwrap();
Expand Down Expand Up @@ -727,15 +723,10 @@ mod tests {
#[test]
fn test_stop_early() {
use crate::assert_diff_eq;
use std::fs::File;

let target = "target/context-diff";
let _ = std::fs::create_dir(target);
let from_filename = &format!("{target}/foo");
let _ = File::create(from_filename).unwrap();
let from_filename = "foo";
let from = ["a", "b", "c", ""].join("\n");
let to_filename = &format!("{target}/bar");
let _ = File::create(to_filename).unwrap();
let to_filename = "bar";
let to = ["a", "d", "c", ""].join("\n");

let diff_full = diff(
Expand All @@ -749,8 +740,8 @@ mod tests {
);

let expected_full = [
"*** target/context-diff/foo\tTIMESTAMP",
"--- target/context-diff/bar\tTIMESTAMP",
"*** foo\tTIMESTAMP",
"--- bar\tTIMESTAMP",
"***************",
"*** 1,3 ****",
" a",
Expand All @@ -777,8 +768,8 @@ mod tests {
);

let expected_brief = [
"*** target/context-diff/foo\tTIMESTAMP",
"--- target/context-diff/bar\tTIMESTAMP",
"*** foo\tTIMESTAMP",
"--- bar\tTIMESTAMP",
"",
]
.join("\n");
Expand Down
42 changes: 16 additions & 26 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,13 @@ mod tests {
if f != 2 {
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.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&bet,
&Params {
from: (&format!("{target}/aalef")).into(),
from: "a/alef".into(),
to: (&format!("{target}/alef")).into(),
context_count: 2,
..Default::default()
Expand All @@ -472,6 +470,7 @@ mod tests {
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{target}/alef")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/bet")).unwrap();
fb.write_all(&bet[..]).unwrap();
Expand Down Expand Up @@ -571,15 +570,13 @@ mod tests {
}
_ => unreachable!(),
}
let _ = File::create(&format!("{target}/aalefn")).unwrap();
let mut fa = File::create(&format!("{target}/alefn")).unwrap();
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&bet,
&Params {
from: (&format!("{target}/aalefn")).into(),
from: "a/alefn".into(),
to: (&format!("{target}/alefn")).into(),
context_count: 2,
..Default::default()
Expand All @@ -589,6 +586,7 @@ mod tests {
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{target}/alefn")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/betn")).unwrap();
fb.write_all(&bet[..]).unwrap();
Expand Down Expand Up @@ -668,15 +666,13 @@ mod tests {
3 => {}
_ => unreachable!(),
}
let _ = File::create(&format!("{target}/aalef_")).unwrap();
let mut fa = File::create(&format!("{target}/alef_")).unwrap();
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&bet,
&Params {
from: (&format!("{target}/aalef_")).into(),
from: "a/alef_".into(),
to: (&format!("{target}/alef_")).into(),
context_count: 2,
..Default::default()
Expand All @@ -686,6 +682,7 @@ mod tests {
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{target}/alef_")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/bet_")).unwrap();
fb.write_all(&bet[..]).unwrap();
Expand Down Expand Up @@ -750,15 +747,13 @@ mod tests {
if f != 2 {
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.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&bet,
&Params {
from: (&format!("{target}/aalefx")).into(),
from: "a/alefx".into(),
to: (&format!("{target}/alefx")).into(),
context_count: 2,
..Default::default()
Expand All @@ -768,6 +763,7 @@ mod tests {
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{target}/alefx")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/betx")).unwrap();
fb.write_all(&bet[..]).unwrap();
Expand Down Expand Up @@ -837,15 +833,13 @@ mod tests {
if f != 2 {
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.
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&bet,
&Params {
from: (&format!("{target}/aalefr")).into(),
from: "a/alefr".into(),
to: (&format!("{target}/alefr")).into(),
context_count: 2,
..Default::default()
Expand All @@ -855,6 +849,7 @@ mod tests {
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{target}/alefr")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{target}/betr")).unwrap();
fb.write_all(&bet[..]).unwrap();
Expand All @@ -881,15 +876,10 @@ mod tests {
#[test]
fn test_stop_early() {
use crate::assert_diff_eq;
use std::fs::File;

let target = "target/context-diff";
let _ = std::fs::create_dir(target);
let from_filename = &format!("{target}/foo");
let _ = File::create(from_filename).unwrap();
let from_filename = "foo";
let from = ["a", "b", "c", ""].join("\n");
let to_filename = &format!("{target}/bar");
let _ = File::create(to_filename).unwrap();
let to_filename = "bar";
let to = ["a", "d", "c", ""].join("\n");

let diff_full = diff(
Expand All @@ -903,8 +893,8 @@ mod tests {
);

let expected_full = [
"--- target/context-diff/foo\tTIMESTAMP",
"+++ target/context-diff/bar\tTIMESTAMP",
"--- foo\tTIMESTAMP",
"+++ bar\tTIMESTAMP",
"@@ -1,3 +1,3 @@",
" a",
"-b",
Expand All @@ -927,8 +917,8 @@ mod tests {
);

let expected_brief = [
"--- target/context-diff/foo\tTIMESTAMP",
"+++ target/context-diff/bar\tTIMESTAMP",
"--- foo\tTIMESTAMP",
"+++ bar\tTIMESTAMP",
"",
]
.join("\n");
Expand Down

0 comments on commit aab1a13

Please sign in to comment.