mirror of
https://github.com/uutils/diffutils.git
synced 2026-05-06 14:17:17 -04:00
Handle the rewrite of "-" to "/dev/stdin" in main to leave the filenames unchanged (fixes #46)
This commit is contained in:
+10
-4
@@ -5,7 +5,7 @@
|
||||
|
||||
use crate::params::{parse_params, Format};
|
||||
use std::env;
|
||||
|
||||
use std::ffi::OsString;
|
||||
use std::fs;
|
||||
use std::io::{self, Write};
|
||||
use std::process::{exit, ExitCode};
|
||||
@@ -38,19 +38,25 @@ fn main() -> ExitCode {
|
||||
);
|
||||
}
|
||||
};
|
||||
if same_file::is_same_file(¶ms.from, ¶ms.to).unwrap_or(false) {
|
||||
if params.from == "-" && params.to == "-"
|
||||
|| same_file::is_same_file(¶ms.from, ¶ms.to).unwrap_or(false)
|
||||
{
|
||||
maybe_report_identical_files();
|
||||
return ExitCode::SUCCESS;
|
||||
}
|
||||
// read files
|
||||
let from_content = match fs::read(¶ms.from) {
|
||||
fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> {
|
||||
let stdin = OsString::from("/dev/stdin");
|
||||
fs::read(if filepath == "-" { &stdin } else { filepath })
|
||||
}
|
||||
let from_content = match read_file_contents(¶ms.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(¶ms.to) {
|
||||
let to_content = match read_file_contents(¶ms.to) {
|
||||
Ok(to_content) => to_content,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to read to-file: {e}");
|
||||
|
||||
+6
-6
@@ -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()));
|
||||
}
|
||||
@@ -461,14 +461,14 @@ 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()
|
||||
}),
|
||||
@@ -476,8 +476,8 @@ mod tests {
|
||||
);
|
||||
assert_eq!(
|
||||
Ok(Params {
|
||||
from: os("/dev/stdin"),
|
||||
to: os("/dev/stdin"),
|
||||
from: os("-"),
|
||||
to: os("-"),
|
||||
..Default::default()
|
||||
}),
|
||||
parse_params([os("diff"), os("-"), os("-")].iter().cloned())
|
||||
|
||||
+16
-11
@@ -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(())
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
)));
|
||||
|
||||
@@ -190,12 +195,12 @@ 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()
|
||||
|
||||
Reference in New Issue
Block a user