Skip to content

Handle the rewrite of "-" to "/dev/stdin" in main to leave the filenames unchanged (fixes #46) #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

use crate::params::{parse_params, Format};
use std::env;

use std::ffi::OsString;
use std::fs;
use std::io::{self, Write};
use std::io::{self, Read, Write};
use std::process::{exit, ExitCode};

mod context_diff;
Expand Down Expand Up @@ -38,19 +38,29 @@ fn main() -> ExitCode {
);
}
};
if same_file::is_same_file(&params.from, &params.to).unwrap_or(false) {
if params.from == "-" && params.to == "-"
|| same_file::is_same_file(&params.from, &params.to).unwrap_or(false)
{
maybe_report_identical_files();
return ExitCode::SUCCESS;
}
// read files
let from_content = match fs::read(&params.from) {
fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> {
if filepath == "-" {
let mut content = Vec::new();
io::stdin().read_to_end(&mut content).and(Ok(content))
} else {
fs::read(filepath)
}
}
let from_content = match read_file_contents(&params.from) {
Ok(from_content) => from_content,
Err(e) => {
eprintln!("Failed to read from-file: {e}");
return ExitCode::from(2);
}
};
let to_content = match fs::read(&params.to) {
let to_content = match read_file_contents(&params.to) {
Ok(to_content) => to_content,
Err(e) => {
eprintln!("Failed to read to-file: {e}");
Expand Down
12 changes: 6 additions & 6 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
}
if param == "-" {
if from.is_none() {
from = Some(OsString::from("/dev/stdin"));
from = Some(param);
} else if to.is_none() {
to = Some(OsString::from("/dev/stdin"));
to = Some(param);
} else {
return Err(format!("Usage: {} <from> <to>", exe.to_string_lossy()));
}
Expand Down Expand Up @@ -461,23 +461,23 @@ mod tests {
assert_eq!(
Ok(Params {
from: os("foo"),
to: os("/dev/stdin"),
to: os("-"),
..Default::default()
}),
parse_params([os("diff"), os("foo"), os("-")].iter().cloned())
);
assert_eq!(
Ok(Params {
from: os("/dev/stdin"),
from: os("-"),
to: os("bar"),
..Default::default()
}),
parse_params([os("diff"), os("-"), os("bar")].iter().cloned())
);
assert_eq!(
Ok(Params {
from: os("/dev/stdin"),
to: os("/dev/stdin"),
from: os("-"),
to: os("-"),
Comment on lines -479 to +480

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep at least one test that uses /dev/stdin. I think the right way to mark a test as "unix-only" is #[cfg(unix)] right before fn.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those tests do not actually exercise reading from stdin, only the parsing of command-line parameters. I don't see much added value to verifying that /dev/stdin doesn't get rewritten.

However I can add an integration test that exercises reading from /dev/stdin.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I got that wrong. And thank you, that's exactly what I was hoping for! :)

..Default::default()
}),
parse_params([os("diff"), os("-"), os("-")].iter().cloned())
Expand Down
43 changes: 32 additions & 11 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,30 @@ fn unknown_param() -> Result<(), Box<dyn std::error::Error>> {
}

#[test]
fn cannot_read_from_file() -> Result<(), Box<dyn std::error::Error>> {
fn cannot_read_files() -> Result<(), Box<dyn std::error::Error>> {
let file = NamedTempFile::new()?;

let mut cmd = Command::cargo_bin("diffutils")?;
cmd.arg("foo.txt").arg("bar.txt");
cmd.arg("foo.txt").arg(file.path());
cmd.assert()
.code(predicate::eq(2))
.failure()
.stderr(predicate::str::starts_with("Failed to read from-file"));
Ok(())
}

#[test]
fn cannot_read_to_file() -> Result<(), Box<dyn std::error::Error>> {
let file = NamedTempFile::new()?;
let mut cmd = Command::cargo_bin("diffutils")?;
cmd.arg(file.path()).arg("bar.txt");
cmd.arg(file.path()).arg("foo.txt");
cmd.assert()
.code(predicate::eq(2))
.failure()
.stderr(predicate::str::starts_with("Failed to read to-file"));

let mut cmd = Command::cargo_bin("diffutils")?;
cmd.arg("foo.txt").arg("foo.txt");
cmd.assert()
.code(predicate::eq(2))
.failure()
.stderr(predicate::str::starts_with("Failed to read from-file"));

Ok(())
}

Expand Down Expand Up @@ -177,7 +182,7 @@ fn read_from_stdin() -> Result<(), Box<dyn std::error::Error>> {
.code(predicate::eq(1))
.failure()
.stdout(predicate::eq(format!(
"--- {}\t\n+++ /dev/stdin\t\n@@ -1 +1 @@\n-foo\n+bar\n",
"--- {}\t\n+++ -\t\n@@ -1 +1 @@\n-foo\n+bar\n",
file1.path().to_string_lossy()
)));

Expand All @@ -190,16 +195,32 @@ fn read_from_stdin() -> Result<(), Box<dyn std::error::Error>> {
.code(predicate::eq(1))
.failure()
.stdout(predicate::eq(format!(
"--- /dev/stdin\t\n+++ {}\t\n@@ -1 +1 @@\n-foo\n+bar\n",
"--- -\t\n+++ {}\t\n@@ -1 +1 @@\n-foo\n+bar\n",
file2.path().to_string_lossy()
)));

let mut cmd = Command::cargo_bin("diffutils")?;
cmd.arg("-u").arg("-").arg("-").write_stdin("foo\n");
cmd.arg("-u").arg("-").arg("-");
cmd.assert()
.code(predicate::eq(0))
.success()
.stdout(predicate::str::is_empty());

#[cfg(unix)]
{
let mut cmd = Command::cargo_bin("diffutils")?;
cmd.arg("-u")
.arg(file1.path())
.arg("/dev/stdin")
.write_stdin("bar\n");
cmd.assert()
.code(predicate::eq(1))
.failure()
.stdout(predicate::eq(format!(
"--- {}\t\n+++ /dev/stdin\t\n@@ -1 +1 @@\n-foo\n+bar\n",
file1.path().to_string_lossy()
)));
}

Ok(())
}
Loading