diff --git a/src/uu/tsort/locales/en-US.ftl b/src/uu/tsort/locales/en-US.ftl index a4b4218c3f6..2b2f90a1e01 100644 --- a/src/uu/tsort/locales/en-US.ftl +++ b/src/uu/tsort/locales/en-US.ftl @@ -6,3 +6,6 @@ tsort-usage = tsort [OPTIONS] FILE tsort-error-is-dir = read error: Is a directory tsort-error-odd = input contains an odd number of tokens tsort-error-loop = input contains a loop: +tsort-error-extra-operand = extra operand { $operand } + Try '{ $util } --help' for more information. +tsort-error-at-least-one-input = at least one input diff --git a/src/uu/tsort/locales/fr-FR.ftl b/src/uu/tsort/locales/fr-FR.ftl index 18349b978ca..c3594e7a212 100644 --- a/src/uu/tsort/locales/fr-FR.ftl +++ b/src/uu/tsort/locales/fr-FR.ftl @@ -6,3 +6,6 @@ tsort-usage = tsort [OPTIONS] FILE tsort-error-is-dir = erreur de lecture : c'est un répertoire tsort-error-odd = l'entrée contient un nombre impair de jetons tsort-error-loop = l'entrée contient une boucle : +tsort-error-extra-operand = opérande supplémentaire { $operand } + Essayez '{ $util } --help' pour plus d'informations. +tsort-error-at-least-one-input = au moins une entrée diff --git a/src/uu/tsort/src/tsort.rs b/src/uu/tsort/src/tsort.rs index 4b52e1e4534..67d8cca2630 100644 --- a/src/uu/tsort/src/tsort.rs +++ b/src/uu/tsort/src/tsort.rs @@ -3,14 +3,14 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. //spell-checker:ignore TAOCP indegree -use clap::{Arg, Command}; +use clap::{Arg, ArgAction, Command}; use std::collections::hash_map::Entry; use std::collections::{HashMap, VecDeque}; use std::ffi::OsString; use std::path::Path; use thiserror::Error; use uucore::display::Quotable; -use uucore::error::{UError, UResult}; +use uucore::error::{UError, UResult, USimpleError}; use uucore::{format_usage, show}; use uucore::translate; @@ -49,15 +49,36 @@ impl UError for LoopNode<'_> {} pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?; - let input = matches - .get_one::(options::FILE) - .expect("Value is required by clap"); + let mut inputs: Vec = matches + .get_many::(options::FILE) + .map(|vals| vals.cloned().collect()) + .unwrap_or_default(); + + if inputs.is_empty() { + inputs.push(OsString::from("-")); + } + + if inputs.len() > 1 { + return Err(USimpleError::new( + 1, + translate!( + "tsort-error-extra-operand", + "operand" => inputs[1].quote(), + "util" => uucore::util_name() + ), + )); + } + + let input = inputs + .into_iter() + .next() + .expect(translate!("tsort-error-at-least-one-input").as_str()); let data = if input == "-" { let stdin = std::io::stdin(); std::io::read_to_string(stdin)? } else { - let path = Path::new(input); + let path = Path::new(&input); if path.is_dir() { return Err(TsortError::IsDir(input.to_string_lossy().to_string()).into()); } @@ -96,12 +117,19 @@ pub fn uu_app() -> Command { .override_usage(format_usage(&translate!("tsort-usage"))) .about(translate!("tsort-about")) .infer_long_args(true) + .arg( + Arg::new("warn") + .short('w') + .action(ArgAction::SetTrue) + .hide(true), + ) .arg( Arg::new(options::FILE) - .default_value("-") .hide(true) .value_parser(clap::value_parser!(OsString)) - .value_hint(clap::ValueHint::FilePath), + .value_hint(clap::ValueHint::FilePath) + .num_args(0..) + .action(ArgAction::Append), ) } @@ -190,7 +218,7 @@ impl<'input> Graph<'input> { let v = self.find_next_node(&mut independent_nodes_queue); println!("{v}"); if let Some(node_to_process) = self.nodes.remove(v) { - for successor_name in node_to_process.successor_names { + for successor_name in node_to_process.successor_names.into_iter().rev() { let successor_node = self.nodes.get_mut(successor_name).unwrap(); successor_node.predecessor_count -= 1; if successor_node.predecessor_count == 0 { diff --git a/src/uucore/src/lib/mods/clap_localization.rs b/src/uucore/src/lib/mods/clap_localization.rs index e0a0ce84e8c..cfc30ab22fd 100644 --- a/src/uucore/src/lib/mods/clap_localization.rs +++ b/src/uucore/src/lib/mods/clap_localization.rs @@ -205,7 +205,6 @@ impl<'a> ErrorFormatter<'a> { "value" => self.color_mgr.colorize(&value, Color::Yellow), "option" => self.color_mgr.colorize(&option, Color::Green) ); - // Include validation error if present match err.source() { Some(source) if matches!(err.kind(), ErrorKind::ValueValidation) => { diff --git a/tests/by-util/test_tsort.rs b/tests/by-util/test_tsort.rs index 077fd26b772..64fe97385df 100644 --- a/tests/by-util/test_tsort.rs +++ b/tests/by-util/test_tsort.rs @@ -77,7 +77,7 @@ fn test_multiple_arguments() { .arg("call_graph.txt") .arg("invalid_file") .fails() - .stderr_contains("unexpected argument 'invalid_file' found"); + .stderr_contains("extra operand 'invalid_file'"); } #[test] @@ -119,7 +119,7 @@ fn test_two_cycles() { new_ucmd!() .pipe_in("a b b c c b b d d b") .fails_with_code(1) - .stdout_is("a\nb\nc\nd\n") + .stdout_is("a\nb\nd\nc\n") .stderr_is("tsort: -: input contains a loop:\ntsort: b\ntsort: c\ntsort: -: input contains a loop:\ntsort: b\ntsort: d\n"); } @@ -153,3 +153,95 @@ fn test_loop_for_iterative_dfs_correctness() { .fails_with_code(1) .stderr_contains("tsort: -: input contains a loop:\ntsort: B\ntsort: C"); } + +const TSORT_LOOP_STDERR: &str = "tsort: f: input contains a loop:\ntsort: s\ntsort: t\n"; +const TSORT_LOOP_STDERR_AC: &str = "tsort: f: input contains a loop:\ntsort: a\ntsort: b\ntsort: f: input contains a loop:\ntsort: a\ntsort: c\n"; +const TSORT_ODD_ERROR: &str = "tsort: -: input contains an odd number of tokens\n"; +const TSORT_EXTRA_OPERAND_ERROR: &str = + "tsort: extra operand 'g'\nTry 'tsort --help' for more information.\n"; + +#[test] +fn test_cycle_loop_from_file() { + let (at, mut ucmd) = at_and_ucmd!(); + at.write("f", "t b\nt s\ns t\n"); + + ucmd.arg("f") + .fails_with_code(1) + .stdout_is("s\nt\nb\n") + .stderr_is(TSORT_LOOP_STDERR); +} + +#[test] +fn test_cycle_loop_with_extra_node_from_file() { + let (at, mut ucmd) = at_and_ucmd!(); + at.write("f", "t x\nt s\ns t\n"); + + ucmd.arg("f") + .fails_with_code(1) + .stdout_is("s\nt\nx\n") + .stderr_is(TSORT_LOOP_STDERR); +} + +#[test] +fn test_cycle_loop_multiple_loops_from_file() { + let (at, mut ucmd) = at_and_ucmd!(); + at.write("f", "a a\na b\na c\nc a\nb a\n"); + + ucmd.arg("f") + .fails_with_code(1) + .stdout_is("a\nc\nb\n") + .stderr_is(TSORT_LOOP_STDERR_AC); +} + +#[test] +fn test_posix_graph_examples() { + new_ucmd!() + .pipe_in("a b c c d e\ng g\nf g e f\nh h\n") + .succeeds() + .stdout_only("a\nc\nd\nh\nb\ne\nf\ng\n"); + + new_ucmd!() + .pipe_in("b a\nd c\nz h x h r h\n") + .succeeds() + .stdout_only("b\nd\nr\nx\nz\na\nc\nh\n"); +} + +#[test] +fn test_linear_tree_graphs() { + new_ucmd!() + .pipe_in("a b b c c d d e e f f g\n") + .succeeds() + .stdout_only("a\nb\nc\nd\ne\nf\ng\n"); + + new_ucmd!() + .pipe_in("a b b c c d d e e f f g\nc x x y y z\n") + .succeeds() + .stdout_only("a\nb\nc\nx\nd\ny\ne\nz\nf\ng\n"); + + new_ucmd!() + .pipe_in("a b b c c d d e e f f g\nc x x y y z\nf r r s s t\n") + .succeeds() + .stdout_only("a\nb\nc\nx\nd\ny\ne\nz\nf\nr\ng\ns\nt\n"); +} + +#[test] +fn test_odd_number_of_tokens() { + new_ucmd!() + .pipe_in("a\n") + .fails_with_code(1) + .stdout_is("") + .stderr_is(TSORT_ODD_ERROR); +} + +#[test] +fn test_only_one_input_file() { + let (at, mut ucmd) = at_and_ucmd!(); + at.write("f", ""); + at.write("g", ""); + + ucmd.arg("f") + .arg("g") + .fails_with_code(1) + .stdout_is("") + .stderr_is(TSORT_EXTRA_OPERAND_ERROR); +} diff --git a/tests/fixtures/tsort/call_graph.expected b/tests/fixtures/tsort/call_graph.expected index e33aa72bd04..df1b950f627 100644 --- a/tests/fixtures/tsort/call_graph.expected +++ b/tests/fixtures/tsort/call_graph.expected @@ -1,17 +1,17 @@ main -parse_options -tail_file tail_forever -tail +tail_file +parse_options recheck +tail write_header -tail_lines -tail_bytes pretty_name -start_lines -file_lines -pipe_lines -xlseek -start_bytes +tail_bytes +tail_lines pipe_bytes +start_bytes +xlseek +pipe_lines +file_lines +start_lines dump_remainder diff --git a/util/gnu-patches/series b/util/gnu-patches/series index 451fe99daae..2d9b30b2cc1 100644 --- a/util/gnu-patches/series +++ b/util/gnu-patches/series @@ -7,7 +7,6 @@ tests_env_env-S.pl.patch tests_invalid_opt.patch tests_ls_no_cap.patch tests_sort_merge.pl.patch -tests_tsort.patch tests_du_move_dir_while_traversing.patch test_mkdir_restorecon.patch error_msg_uniq.diff diff --git a/util/gnu-patches/tests_tsort.patch b/util/gnu-patches/tests_tsort.patch deleted file mode 100644 index 1cc1603ee8f..00000000000 --- a/util/gnu-patches/tests_tsort.patch +++ /dev/null @@ -1,17 +0,0 @@ -Index: gnu/tests/misc/tsort.pl -=================================================================== ---- gnu.orig/tests/misc/tsort.pl -+++ gnu/tests/misc/tsort.pl -@@ -54,8 +54,10 @@ my @Tests = - - ['only-one', {IN => {f => ""}}, {IN => {g => ""}}, - {EXIT => 1}, -- {ERR => "tsort: extra operand 'g'\n" -- . "Try 'tsort --help' for more information.\n"}], -+ {ERR => "tsort: error: unexpected argument 'g' found\n\n" -+ . "Usage: tsort [OPTIONS] FILE\n\n" -+ . "For more information, try '--help'.\n" -+ }], - ); - - my $save_temps = $ENV{DEBUG};