Skip to content

Commit

Permalink
Merge pull request #56 from TanmayPatil105/handle-directory-input
Browse files Browse the repository at this point in the history
Handle directory-file and file-directory comparisons in the diff
  • Loading branch information
oSoMoN authored Apr 23, 2024
2 parents 8de0ca6 + 8c6a648 commit e7dc655
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ffi::OsString;
use std::path::PathBuf;

use regex::Regex;

Expand Down Expand Up @@ -171,6 +172,20 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
} else {
return Err(format!("Usage: {} <from> <to>", exe.to_string_lossy()));
};

// diff DIRECTORY FILE => diff DIRECTORY/FILE FILE
// diff FILE DIRECTORY => diff FILE DIRECTORY/FILE
let mut from_path: PathBuf = PathBuf::from(&params.from);
let mut to_path: PathBuf = PathBuf::from(&params.to);

if from_path.is_dir() && to_path.is_file() {
from_path.push(to_path.file_name().unwrap());
params.from = from_path.into_os_string();
} else if from_path.is_file() && to_path.is_dir() {
to_path.push(from_path.file_name().unwrap());
params.to = to_path.into_os_string();
}

params.format = format.unwrap_or(Format::default());
if let Some(context_count) = context {
params.context_count = context_count;
Expand Down
49 changes: 48 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use assert_cmd::cmd::Command;
use diffutilslib::assert_diff_eq;
use predicates::prelude::*;
use std::fs::File;
use std::io::Write;
use tempfile::NamedTempFile;
use tempfile::{tempdir, NamedTempFile};

// Integration tests for the diffutils command

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

Ok(())
}

#[test]
fn compare_file_to_directory() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;

let directory = tmp_dir.path().join("d");
let _ = std::fs::create_dir(&directory);

let a_path = tmp_dir.path().join("a");
let mut a = File::create(&a_path).unwrap();
a.write_all(b"a\n").unwrap();

let da_path = directory.join("a");
let mut da = File::create(&da_path).unwrap();
da.write_all(b"da\n").unwrap();

let mut cmd = Command::cargo_bin("diffutils")?;
cmd.arg("-u").arg(&directory).arg(&a_path);
cmd.assert().code(predicate::eq(1)).failure();

let output = cmd.output().unwrap().stdout;
assert_diff_eq!(
output,
format!(
"--- {}\tTIMESTAMP\n+++ {}\tTIMESTAMP\n@@ -1 +1 @@\n-da\n+a\n",
da_path.display(),
a_path.display()
)
);

let mut cmd = Command::cargo_bin("diffutils")?;
cmd.arg("-u").arg(&a_path).arg(&directory);
cmd.assert().code(predicate::eq(1)).failure();

let output = cmd.output().unwrap().stdout;
assert_diff_eq!(
output,
format!(
"--- {}\tTIMESTAMP\n+++ {}\tTIMESTAMP\n@@ -1 +1 @@\n-a\n+da\n",
a_path.display(),
da_path.display()
)
);

Ok(())
}

0 comments on commit e7dc655

Please sign in to comment.