// This file is part of the uutils diffutils package. // // For the full copyright and license information, please view the LICENSE-* // files that was distributed with this source code. use assert_cmd::prelude::*; use predicates::prelude::*; use std::io::Write; use std::process::Command; use tempfile::NamedTempFile; // Integration tests for the diffutils command #[test] fn unknown_param() -> Result<(), Box> { let mut cmd = Command::cargo_bin("diffutils")?; cmd.arg("--foobar"); cmd.assert() .code(predicate::eq(2)) .failure() .stderr(predicate::str::starts_with("Usage: ")); Ok(()) } #[test] fn cannot_read_from_file() -> Result<(), Box> { let mut cmd = Command::cargo_bin("diffutils")?; cmd.arg("foo.txt").arg("bar.txt"); 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> { let file = NamedTempFile::new()?; let mut cmd = Command::cargo_bin("diffutils")?; cmd.arg(file.path()).arg("bar.txt"); cmd.assert() .code(predicate::eq(2)) .failure() .stderr(predicate::str::starts_with("Failed to read to-file")); Ok(()) } #[test] fn no_differences() -> Result<(), Box> { let file = NamedTempFile::new()?; for option in ["", "-u", "-c", "-e"] { let mut cmd = Command::cargo_bin("diffutils")?; if !option.is_empty() { cmd.arg(option); } cmd.arg(file.path()).arg(file.path()); cmd.assert() .code(predicate::eq(0)) .success() .stdout(predicate::str::is_empty()); } Ok(()) } #[test] fn no_differences_report_identical_files() -> Result<(), Box> { // same file let mut file1 = NamedTempFile::new()?; file1.write_all("foo\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("-s").arg(file1.path()).arg(file1.path()); cmd.assert() .code(predicate::eq(0)) .success() .stdout(predicate::eq(format!( "Files {} and {} are identical\n", file1.path().to_string_lossy(), file1.path().to_string_lossy(), ))); } // two files with the same content let mut file2 = NamedTempFile::new()?; file2.write_all("foo\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("-s").arg(file1.path()).arg(file2.path()); cmd.assert() .code(predicate::eq(0)) .success() .stdout(predicate::eq(format!( "Files {} and {} are identical\n", file1.path().to_string_lossy(), file2.path().to_string_lossy(), ))); } Ok(()) } #[test] fn differences() -> Result<(), Box> { 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(file1.path()).arg(file2.path()); cmd.assert() .code(predicate::eq(1)) .failure() .stdout(predicate::str::is_empty().not()); } Ok(()) } #[test] fn differences_brief() -> Result<(), Box> { 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> { let mut file1 = NamedTempFile::new()?; file1.write_all("foo".as_bytes())?; let mut file2 = NamedTempFile::new()?; file2.write_all("bar".as_bytes())?; let mut cmd = Command::cargo_bin("diffutils")?; cmd.arg("-e").arg(file1.path()).arg(file2.path()); cmd.assert() .code(predicate::eq(2)) .failure() .stderr(predicate::str::starts_with("No newline at end of file")); Ok(()) }