Skip to content

Commit 3ddf78b

Browse files
authored
Merge pull request #208 from andrewliebenow/diff-exit-status
diff: exit with correct status
2 parents fa8b38a + dc6f7e7 commit 3ddf78b

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

text/diff.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,18 @@ fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
152152
}
153153
}
154154

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

159159
let result = check_difference(args);
160160

161-
if let Ok(diff_exit_status) = &result {
162-
return Ok(*diff_exit_status);
163-
} else if let Err(error) = &result {
164-
eprintln!("diff: {}", error);
165-
}
161+
match result {
162+
Ok(diff_exit_status) => diff_exit_status,
163+
Err(error) => {
164+
eprintln!("diff: {}", error);
166165

167-
return Ok(DiffExitStatus::NotDifferent);
166+
DiffExitStatus::Trouble
167+
}
168+
}
168169
}

text/diff_util/constants.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![allow(dead_code)]
22

3-
pub const EXIT_STATUS_NO_DIFFERENCE: i32 = 0;
4-
pub const EXIT_STATUS_DIFFERENCE: i32 = 1;
5-
pub const EXIT_STATUS_TROUBLE: i32 = 2;
3+
pub const EXIT_STATUS_NO_DIFFERENCE: u8 = 0;
4+
pub const EXIT_STATUS_DIFFERENCE: u8 = 1;
5+
pub const EXIT_STATUS_TROUBLE: u8 = 2;
66
pub const NO_NEW_LINE_AT_END_OF_FILE: &'static str = "\\ No newline at end of file";
77
pub const COULD_NOT_UNWRAP_FILENAME: &'static str = "Could not unwrap filename!";
88
pub const UTF8_NOT_ALLOWED_BYTES: [u8; 26] = [

text/diff_util/diff_exit_status.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub enum DiffExitStatus {
1111
}
1212

1313
impl DiffExitStatus {
14-
pub fn status_code(&self) -> i32 {
14+
pub fn status_code(&self) -> u8 {
1515
match self {
1616
DiffExitStatus::NotDifferent => EXIT_STATUS_NO_DIFFERENCE,
1717
DiffExitStatus::Different => EXIT_STATUS_DIFFERENCE,
@@ -22,10 +22,6 @@ impl DiffExitStatus {
2222

2323
impl Termination for DiffExitStatus {
2424
fn report(self) -> std::process::ExitCode {
25-
if self.status_code() == EXIT_STATUS_TROUBLE {
26-
return std::process::ExitCode::FAILURE;
27-
}
28-
29-
std::process::ExitCode::SUCCESS
25+
std::process::ExitCode::from(self.status_code())
3026
}
3127
}

text/tests/diff-tests.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
// SPDX-License-Identifier: MIT
99
//
1010

11+
#[path = "../diff_util/mod.rs"]
12+
mod diff_util;
13+
14+
use diff_util::constants::{EXIT_STATUS_DIFFERENCE, EXIT_STATUS_NO_DIFFERENCE};
1115
use plib::{run_test, TestPlan};
1216

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

1620
run_test(TestPlan {
@@ -19,7 +23,7 @@ fn diff_test(args: &[&str], expected_output: &str) {
1923
stdin_data: String::from(""),
2024
expected_out: String::from(expected_output),
2125
expected_err: String::from(""),
22-
expected_exit_code: 0,
26+
expected_exit_code: i32::from(expected_diff_exit_status),
2327
});
2428
}
2529

@@ -214,7 +218,11 @@ fn diff_tests_setup() {
214218
#[test]
215219
fn test_diff_normal() {
216220
let data = input_by_key("test_diff_normal");
217-
diff_test(&[data.file1_path(), data.file2_path()], data.content());
221+
diff_test(
222+
&[data.file1_path(), data.file2_path()],
223+
data.content(),
224+
EXIT_STATUS_DIFFERENCE,
225+
);
218226
}
219227

220228
#[test]
@@ -224,6 +232,7 @@ fn test_diff_context3() {
224232
diff_test(
225233
&["-c", data.file1_path(), data.file2_path()],
226234
data.content(),
235+
EXIT_STATUS_DIFFERENCE,
227236
);
228237
}
229238

@@ -234,6 +243,7 @@ fn test_diff_context1() {
234243
diff_test(
235244
&["-C", "1", data.file1_path(), data.file2_path()],
236245
data.content(),
246+
EXIT_STATUS_DIFFERENCE,
237247
);
238248
}
239249

@@ -244,6 +254,7 @@ fn test_diff_context10() {
244254
diff_test(
245255
&["-C", "10", data.file1_path(), data.file2_path()],
246256
data.content(),
257+
EXIT_STATUS_DIFFERENCE,
247258
);
248259
}
249260

@@ -254,6 +265,7 @@ fn test_diff_edit_script() {
254265
diff_test(
255266
&["-e", data.file1_path(), data.file2_path()],
256267
data.content(),
268+
EXIT_STATUS_DIFFERENCE,
257269
);
258270
}
259271

@@ -264,6 +276,7 @@ fn test_diff_forward_edit_script() {
264276
diff_test(
265277
&["-f", data.file1_path(), data.file2_path()],
266278
data.content(),
279+
EXIT_STATUS_DIFFERENCE,
267280
);
268281
}
269282

@@ -274,6 +287,7 @@ fn test_diff_unified3() {
274287
diff_test(
275288
&["-u", data.file1_path(), data.file2_path()],
276289
data.content(),
290+
EXIT_STATUS_DIFFERENCE,
277291
);
278292
}
279293

@@ -284,6 +298,7 @@ fn test_diff_unified0() {
284298
diff_test(
285299
&["-U", "0", data.file1_path(), data.file2_path()],
286300
data.content(),
301+
EXIT_STATUS_DIFFERENCE,
287302
);
288303
}
289304

@@ -294,19 +309,28 @@ fn test_diff_unified10() {
294309
diff_test(
295310
&["-U", "10", data.file1_path(), data.file2_path()],
296311
data.content(),
312+
EXIT_STATUS_DIFFERENCE,
297313
);
298314
}
299315

300316
#[test]
301317
fn test_diff_file_directory() {
302318
let data = input_by_key("test_diff_file_directory");
303-
diff_test(&[data.file1_path(), data.file2_path()], data.content());
319+
diff_test(
320+
&[data.file1_path(), data.file2_path()],
321+
data.content(),
322+
EXIT_STATUS_DIFFERENCE,
323+
);
304324
}
305325

306326
#[test]
307327
fn test_diff_directories() {
308328
let data = input_by_key("test_diff_directories");
309-
diff_test(&[data.file1_path(), data.file2_path()], data.content());
329+
diff_test(
330+
&[data.file1_path(), data.file2_path()],
331+
data.content(),
332+
EXIT_STATUS_DIFFERENCE,
333+
);
310334
}
311335

312336
#[test]
@@ -316,6 +340,7 @@ fn test_diff_directories_recursive() {
316340
diff_test(
317341
&["-r", data.file1_path(), data.file2_path()],
318342
data.content(),
343+
EXIT_STATUS_DIFFERENCE,
319344
);
320345
}
321346

@@ -326,6 +351,7 @@ fn test_diff_directories_recursive_context() {
326351
diff_test(
327352
&["-r", "-c", data.file1_path(), data.file2_path()],
328353
data.content(),
354+
EXIT_STATUS_DIFFERENCE,
329355
);
330356
}
331357

@@ -336,6 +362,7 @@ fn test_diff_directories_recursive_edit_script() {
336362
diff_test(
337363
&["-r", "-e", data.file1_path(), data.file2_path()],
338364
data.content(),
365+
EXIT_STATUS_DIFFERENCE,
339366
);
340367
}
341368

@@ -346,6 +373,7 @@ fn test_diff_directories_recursive_forward_edit_script() {
346373
diff_test(
347374
&["-r", "-f", data.file1_path(), data.file2_path()],
348375
data.content(),
376+
EXIT_STATUS_DIFFERENCE,
349377
);
350378
}
351379

@@ -356,13 +384,18 @@ fn test_diff_directories_recursive_unified() {
356384
diff_test(
357385
&["-r", "-u", data.file1_path(), data.file2_path()],
358386
data.content(),
387+
EXIT_STATUS_DIFFERENCE,
359388
);
360389
}
361390

362391
#[test]
363392
fn test_diff_counting_eol_spaces() {
364393
let data = input_by_key("test_diff_counting_eol_spaces");
365-
diff_test(&[data.file1_path(), data.file2_path()], data.content());
394+
diff_test(
395+
&[data.file1_path(), data.file2_path()],
396+
data.content(),
397+
EXIT_STATUS_DIFFERENCE,
398+
);
366399
}
367400

368401
#[test]
@@ -372,6 +405,7 @@ fn test_diff_ignoring_eol_spaces() {
372405
diff_test(
373406
&["-b", data.file1_path(), data.file2_path()],
374407
data.content(),
408+
EXIT_STATUS_NO_DIFFERENCE,
375409
);
376410
}
377411

@@ -390,5 +424,6 @@ fn test_diff_unified_two_labels() {
390424
data.file2_path(),
391425
],
392426
data.content(),
427+
EXIT_STATUS_DIFFERENCE,
393428
);
394429
}

0 commit comments

Comments
 (0)