Skip to content
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
15 changes: 7 additions & 8 deletions text/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,17 @@ fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
}
}

fn main() -> DiffExitStatus {
fn main() -> Result<DiffExitStatus, Box<dyn std::error::Error>> {
// parse command line arguments
let args = Args::parse();

let result = check_difference(args);

match result {
Ok(diff_exit_status) => diff_exit_status,
Err(error) => {
eprintln!("diff: {}", error);

DiffExitStatus::Trouble
}
if let Ok(diff_exit_status) = &result {
return Ok(*diff_exit_status);
} else if let Err(error) = &result {
eprintln!("diff: {}", error);
}

return Ok(DiffExitStatus::NotDifferent);
}
6 changes: 3 additions & 3 deletions text/diff_util/constants.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![allow(dead_code)]

pub const EXIT_STATUS_NO_DIFFERENCE: u8 = 0;
pub const EXIT_STATUS_DIFFERENCE: u8 = 1;
pub const EXIT_STATUS_TROUBLE: u8 = 2;
pub const EXIT_STATUS_NO_DIFFERENCE: i32 = 0;
pub const EXIT_STATUS_DIFFERENCE: i32 = 1;
pub const EXIT_STATUS_TROUBLE: i32 = 2;
pub const NO_NEW_LINE_AT_END_OF_FILE: &'static str = "\\ No newline at end of file";
pub const COULD_NOT_UNWRAP_FILENAME: &'static str = "Could not unwrap filename!";
pub const UTF8_NOT_ALLOWED_BYTES: [u8; 26] = [
Expand Down
8 changes: 6 additions & 2 deletions text/diff_util/diff_exit_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum DiffExitStatus {
}

impl DiffExitStatus {
pub fn status_code(&self) -> u8 {
pub fn status_code(&self) -> i32 {
match self {
DiffExitStatus::NotDifferent => EXIT_STATUS_NO_DIFFERENCE,
DiffExitStatus::Different => EXIT_STATUS_DIFFERENCE,
Expand All @@ -22,6 +22,10 @@ impl DiffExitStatus {

impl Termination for DiffExitStatus {
fn report(self) -> std::process::ExitCode {
std::process::ExitCode::from(self.status_code())
if self.status_code() == EXIT_STATUS_TROUBLE {
return std::process::ExitCode::FAILURE;
}

std::process::ExitCode::SUCCESS
}
}
47 changes: 6 additions & 41 deletions text/tests/diff-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@
// SPDX-License-Identifier: MIT
//

#[path = "../diff_util/mod.rs"]
mod diff_util;

use diff_util::constants::{EXIT_STATUS_DIFFERENCE, EXIT_STATUS_NO_DIFFERENCE};
use plib::{run_test, TestPlan};

fn diff_test(args: &[&str], expected_output: &str, expected_diff_exit_status: u8) {
fn diff_test(args: &[&str], expected_output: &str) {
let str_args: Vec<String> = args.iter().map(|s| String::from(*s)).collect();

run_test(TestPlan {
Expand All @@ -23,7 +19,7 @@ fn diff_test(args: &[&str], expected_output: &str, expected_diff_exit_status: u8
stdin_data: String::from(""),
expected_out: String::from(expected_output),
expected_err: String::from(""),
expected_exit_code: i32::from(expected_diff_exit_status),
expected_exit_code: 0,
});
}

Expand Down Expand Up @@ -218,11 +214,7 @@ fn diff_tests_setup() {
#[test]
fn test_diff_normal() {
let data = input_by_key("test_diff_normal");
diff_test(
&[data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
diff_test(&[data.file1_path(), data.file2_path()], data.content());
}

#[test]
Expand All @@ -232,7 +224,6 @@ fn test_diff_context3() {
diff_test(
&["-c", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -243,7 +234,6 @@ fn test_diff_context1() {
diff_test(
&["-C", "1", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -254,7 +244,6 @@ fn test_diff_context10() {
diff_test(
&["-C", "10", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -265,7 +254,6 @@ fn test_diff_edit_script() {
diff_test(
&["-e", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -276,7 +264,6 @@ fn test_diff_forward_edit_script() {
diff_test(
&["-f", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -287,7 +274,6 @@ fn test_diff_unified3() {
diff_test(
&["-u", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -298,7 +284,6 @@ fn test_diff_unified0() {
diff_test(
&["-U", "0", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -309,28 +294,19 @@ fn test_diff_unified10() {
diff_test(
&["-U", "10", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

#[test]
fn test_diff_file_directory() {
let data = input_by_key("test_diff_file_directory");
diff_test(
&[data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
diff_test(&[data.file1_path(), data.file2_path()], data.content());
}

#[test]
fn test_diff_directories() {
let data = input_by_key("test_diff_directories");
diff_test(
&[data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
diff_test(&[data.file1_path(), data.file2_path()], data.content());
}

#[test]
Expand All @@ -340,7 +316,6 @@ fn test_diff_directories_recursive() {
diff_test(
&["-r", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -351,7 +326,6 @@ fn test_diff_directories_recursive_context() {
diff_test(
&["-r", "-c", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -362,7 +336,6 @@ fn test_diff_directories_recursive_edit_script() {
diff_test(
&["-r", "-e", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -373,7 +346,6 @@ fn test_diff_directories_recursive_forward_edit_script() {
diff_test(
&["-r", "-f", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

Expand All @@ -384,18 +356,13 @@ fn test_diff_directories_recursive_unified() {
diff_test(
&["-r", "-u", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}

#[test]
fn test_diff_counting_eol_spaces() {
let data = input_by_key("test_diff_counting_eol_spaces");
diff_test(
&[data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
diff_test(&[data.file1_path(), data.file2_path()], data.content());
}

#[test]
Expand All @@ -405,7 +372,6 @@ fn test_diff_ignoring_eol_spaces() {
diff_test(
&["-b", data.file1_path(), data.file2_path()],
data.content(),
EXIT_STATUS_NO_DIFFERENCE,
);
}

Expand All @@ -424,6 +390,5 @@ fn test_diff_unified_two_labels() {
data.file2_path(),
],
data.content(),
EXIT_STATUS_DIFFERENCE,
);
}