Skip to content

Commit e7dc655

Browse files
authored
Merge pull request #56 from TanmayPatil105/handle-directory-input
Handle directory-file and file-directory comparisons in the diff
2 parents 8de0ca6 + 8c6a648 commit e7dc655

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/params.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::ffi::OsString;
2+
use std::path::PathBuf;
23

34
use regex::Regex;
45

@@ -171,6 +172,20 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
171172
} else {
172173
return Err(format!("Usage: {} <from> <to>", exe.to_string_lossy()));
173174
};
175+
176+
// diff DIRECTORY FILE => diff DIRECTORY/FILE FILE
177+
// diff FILE DIRECTORY => diff FILE DIRECTORY/FILE
178+
let mut from_path: PathBuf = PathBuf::from(&params.from);
179+
let mut to_path: PathBuf = PathBuf::from(&params.to);
180+
181+
if from_path.is_dir() && to_path.is_file() {
182+
from_path.push(to_path.file_name().unwrap());
183+
params.from = from_path.into_os_string();
184+
} else if from_path.is_file() && to_path.is_dir() {
185+
to_path.push(from_path.file_name().unwrap());
186+
params.to = to_path.into_os_string();
187+
}
188+
174189
params.format = format.unwrap_or(Format::default());
175190
if let Some(context_count) = context {
176191
params.context_count = context_count;

tests/integration.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use assert_cmd::cmd::Command;
77
use diffutilslib::assert_diff_eq;
88
use predicates::prelude::*;
9+
use std::fs::File;
910
use std::io::Write;
10-
use tempfile::NamedTempFile;
11+
use tempfile::{tempdir, NamedTempFile};
1112

1213
// Integration tests for the diffutils command
1314

@@ -238,3 +239,49 @@ fn read_from_stdin() -> Result<(), Box<dyn std::error::Error>> {
238239

239240
Ok(())
240241
}
242+
243+
#[test]
244+
fn compare_file_to_directory() -> Result<(), Box<dyn std::error::Error>> {
245+
let tmp_dir = tempdir()?;
246+
247+
let directory = tmp_dir.path().join("d");
248+
let _ = std::fs::create_dir(&directory);
249+
250+
let a_path = tmp_dir.path().join("a");
251+
let mut a = File::create(&a_path).unwrap();
252+
a.write_all(b"a\n").unwrap();
253+
254+
let da_path = directory.join("a");
255+
let mut da = File::create(&da_path).unwrap();
256+
da.write_all(b"da\n").unwrap();
257+
258+
let mut cmd = Command::cargo_bin("diffutils")?;
259+
cmd.arg("-u").arg(&directory).arg(&a_path);
260+
cmd.assert().code(predicate::eq(1)).failure();
261+
262+
let output = cmd.output().unwrap().stdout;
263+
assert_diff_eq!(
264+
output,
265+
format!(
266+
"--- {}\tTIMESTAMP\n+++ {}\tTIMESTAMP\n@@ -1 +1 @@\n-da\n+a\n",
267+
da_path.display(),
268+
a_path.display()
269+
)
270+
);
271+
272+
let mut cmd = Command::cargo_bin("diffutils")?;
273+
cmd.arg("-u").arg(&a_path).arg(&directory);
274+
cmd.assert().code(predicate::eq(1)).failure();
275+
276+
let output = cmd.output().unwrap().stdout;
277+
assert_diff_eq!(
278+
output,
279+
format!(
280+
"--- {}\tTIMESTAMP\n+++ {}\tTIMESTAMP\n@@ -1 +1 @@\n-a\n+da\n",
281+
a_path.display(),
282+
da_path.display()
283+
)
284+
);
285+
286+
Ok(())
287+
}

0 commit comments

Comments
 (0)