Skip to content

Commit

Permalink
Implement -q/--brief option
Browse files Browse the repository at this point in the history
  • Loading branch information
oSoMoN committed Feb 22, 2024
1 parent 0a67bf9 commit 9f64e5b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn main() -> ExitCode {
to,
context_count,
format,
brief,
} = parse_params(opts).unwrap_or_else(|error| {
eprintln!("{error}");
exit(2);
Expand Down Expand Up @@ -69,7 +70,15 @@ fn main() -> ExitCode {
exit(2);
}),
};
io::stdout().write_all(&result).unwrap();
if brief && !result.is_empty() {
println!(
"Files {} and {} differ",
from.to_string_lossy(),
to.to_string_lossy()
);
} else {
io::stdout().write_all(&result).unwrap();
}
if result.is_empty() {
ExitCode::SUCCESS
} else {
Expand Down
51 changes: 51 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Params {
pub to: OsString,
pub format: Format,
pub context_count: usize,
pub brief: bool,
}

pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params, String> {
Expand All @@ -38,6 +39,7 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
let mut to = None;
let mut format = None;
let mut context_count = 3;
let mut brief = false;
while let Some(param) = opts.next() {
if param == "--" {
break;
Expand All @@ -52,6 +54,10 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
}
continue;
}
if param == "-q" || param == "--brief" {
brief = true;
continue;
}
let p = osstr_bytes(&param);
if p.first() == Some(&b'-') && p.get(1) != Some(&b'-') {
let mut bit = p[1..].iter().copied().peekable();
Expand Down Expand Up @@ -133,6 +139,7 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
to,
format,
context_count,
brief,
})
}

Expand All @@ -150,6 +157,7 @@ mod tests {
to: os("bar"),
format: Format::Normal,
context_count: 3,
brief: false,
}),
parse_params([os("diff"), os("foo"), os("bar")].iter().cloned())
);
Expand All @@ -162,6 +170,7 @@ mod tests {
to: os("bar"),
format: Format::Ed,
context_count: 3,
brief: false,
}),
parse_params([os("diff"), os("-e"), os("foo"), os("bar")].iter().cloned())
);
Expand All @@ -174,6 +183,7 @@ mod tests {
to: os("bar"),
format: Format::Unified,
context_count: 54,
brief: false,
}),
parse_params(
[os("diff"), os("-u54"), os("foo"), os("bar")]
Expand All @@ -187,6 +197,7 @@ mod tests {
to: os("bar"),
format: Format::Unified,
context_count: 54,
brief: false,
}),
parse_params(
[os("diff"), os("-U54"), os("foo"), os("bar")]
Expand All @@ -200,6 +211,7 @@ mod tests {
to: os("bar"),
format: Format::Unified,
context_count: 54,
brief: false,
}),
parse_params(
[os("diff"), os("-U"), os("54"), os("foo"), os("bar")]
Expand All @@ -213,6 +225,7 @@ mod tests {
to: os("bar"),
format: Format::Context,
context_count: 54,
brief: false,
}),
parse_params(
[os("diff"), os("-c54"), os("foo"), os("bar")]
Expand All @@ -222,13 +235,51 @@ mod tests {
);
}
#[test]
fn brief() {
assert_eq!(
Ok(Params {
from: os("foo"),
to: os("bar"),
format: Format::Normal,
context_count: 3,
brief: false,
}),
parse_params([os("diff"), os("foo"), os("bar")].iter().cloned())
);
assert_eq!(
Ok(Params {
from: os("foo"),
to: os("bar"),
format: Format::Normal,
context_count: 3,
brief: true,
}),
parse_params([os("diff"), os("-q"), os("foo"), os("bar")].iter().cloned())
);
assert_eq!(
Ok(Params {
from: os("foo"),
to: os("bar"),
format: Format::Normal,
context_count: 3,
brief: true,
}),
parse_params(
[os("diff"), os("--brief"), os("foo"), os("bar")]
.iter()
.cloned()
)
);
}
#[test]
fn double_dash() {
assert_eq!(
Ok(Params {
from: os("-g"),
to: os("-h"),
format: Format::Normal,
context_count: 3,
brief: false,
}),
parse_params([os("diff"), os("--"), os("-g"), os("-h")].iter().cloned())
);
Expand Down
24 changes: 24 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ fn differences() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[test]
fn differences_brief() -> Result<(), Box<dyn std::error::Error>> {
let mut file1 = NamedTempFile::new()?;
file1.write_all("foo\n".as_bytes())?;
let mut file2 = NamedTempFile::new()?;
file2.write_all("bar\n".as_bytes())?;
for option in ["", "-u", "-c", "-e"] {
let mut cmd = Command::cargo_bin("diffutils")?;
if !option.is_empty() {
cmd.arg(option);
}
cmd.arg("-q").arg(file1.path()).arg(file2.path());
cmd.assert()
.code(predicate::eq(1))
.failure()
.stdout(predicate::eq(format!(
"Files {} and {} differ\n",
file1.path().to_string_lossy(),
file2.path().to_string_lossy()
)));
}
Ok(())
}

#[test]
fn missing_newline() -> Result<(), Box<dyn std::error::Error>> {
let mut file1 = NamedTempFile::new()?;
Expand Down

0 comments on commit 9f64e5b

Please sign in to comment.