From feaa4e54940a5f2ba1ec556a526977c1df480e8d Mon Sep 17 00:00:00 2001 From: Andy Lester Date: Sun, 19 Jan 2025 23:07:03 -0600 Subject: [PATCH] Hoisted tests into ack-c.yaml. Fixed bug where -c would not set exit code correctly. --- Changes | 4 +++ MANIFEST | 2 ++ README.md | 2 +- ack | 9 ++++-- lib/App/Ack.pm | 2 +- t/ack-c.t | 87 ++++++-------------------------------------------- t/ack-c.yaml | 65 +++++++++++++++++++++++++++++++++++++ 7 files changed, 89 insertions(+), 82 deletions(-) create mode 100644 t/ack-c.yaml diff --git a/Changes b/Changes index 8b3888a4..02e5e744 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,10 @@ NEXT ======================================== ack now needs YAML::PP to run its tests. +[FIXES] +ack's would always set a return code of 1 if -c was used. Now it properly +returns 1 if no files match, and 0 if any files match. + v3.8.1 Tue Dec 31 21:22:59 CST 2024 ======================================== diff --git a/MANIFEST b/MANIFEST index f935f3a5..9cca8da8 100644 --- a/MANIFEST +++ b/MANIFEST @@ -36,6 +36,7 @@ t/FilterTest.pm t/ack-1.t t/ack-Q.t t/ack-c.t +t/ack-c.yaml t/ack-color.t t/ack-column.t t/ack-create-ackrc.t @@ -78,6 +79,7 @@ t/ack-x.t t/anchored.t t/bad-ackrc-opt.t t/basic.t +t/basic.yaml t/boolean.t t/build_regex.t t/command-line-files.t diff --git a/README.md b/README.md index 03c7d1e9..3647841e 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Please follow the [Code of Conduct](CODE_OF_CONDUCT.md) in all your interactions # License -Copyright 2005-2024 Andy Lester. +Copyright 2005-2025 Andy Lester. This program is free software; you can redistribute it and/or modify it under the terms of the diff --git a/ack b/ack index 6b83cf60..5ea0ebd3 100755 --- a/ack +++ b/ack @@ -323,9 +323,14 @@ sub file_loop_fg { sub file_loop_c { my $files = shift; + my $nmatched_files = 0; + my $total_count = 0; while ( defined( my $file = $files->next ) ) { my $matches_for_this_file = count_matches_in_file( $file ); + if ( $matches_for_this_file ) { + ++$nmatched_files; + } if ( not $opt_show_filename ) { $total_count += $matches_for_this_file; @@ -350,7 +355,7 @@ sub file_loop_c { App::Ack::say( $total_count ); } - return; + return $nmatched_files; } @@ -2517,7 +2522,7 @@ Andy Lester, C<< >> =head1 COPYRIGHT & LICENSE -Copyright 2005-2024 Andy Lester. +Copyright 2005-2025 Andy Lester. This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License v2.0. diff --git a/lib/App/Ack.pm b/lib/App/Ack.pm index 6ffad5d3..86041379 100644 --- a/lib/App/Ack.pm +++ b/lib/App/Ack.pm @@ -17,7 +17,7 @@ our $VERSION; our $COPYRIGHT; BEGIN { $VERSION = 'v3.8.1'; # Check https://beyondgrep.com/ for updates - $COPYRIGHT = 'Copyright 2005-2024 Andy Lester.'; + $COPYRIGHT = 'Copyright 2005-2025 Andy Lester.'; } our $STANDALONE = 0; our $ORIGINAL_PROGRAM_NAME; diff --git a/t/ack-c.t b/t/ack-c.t index 0080784c..5158124d 100644 --- a/t/ack-c.t +++ b/t/ack-c.t @@ -10,84 +10,15 @@ use Util; prep_environment(); -DASH_C: { - my @expected = qw( - t/text/amontillado.txt:2 - t/text/bill-of-rights.txt:0 - t/text/constitution.txt:0 - t/text/gettysburg.txt:1 - t/text/movies.txt:0 - t/text/number.txt:0 - t/text/numbered-text.txt:0 - t/text/ozymandias.txt:0 - t/text/raven.txt:2 - ); - - my @args = qw( God -c --sort-files ); - my @files = qw( t/text ); - - ack_sets_match( [ @args, @files ], \@expected, 'God counts' ); - - push( @args, '--no-filename' ); - ack_sets_match( [ @args, @files ], [ 5 ], 'God counts, total only' ); -} - - -WITH_DASH_V: { - my @expected = qw( - t/text/amontillado.txt:206 - t/text/bill-of-rights.txt:45 - t/text/constitution.txt:259 - t/text/gettysburg.txt:15 - t/text/movies.txt:25 - t/text/number.txt:1 - t/text/numbered-text.txt:20 - t/text/ozymandias.txt:9 - t/text/raven.txt:77 - ); - - my @args = qw( the -i -w -v -c --sort-files ); - my @files = qw( t/text ); - - ack_sets_match( [ @args, @files ], \@expected, 'Non-the counts' ); -} - - -DASH_LC: { - my @expected = qw( - t/text/bill-of-rights.txt:1 - t/text/constitution.txt:29 - ); - - my @args = qw( congress -i -l -c --sort-files ); - my @files = qw( t/text ); - - ack_sets_match( [ @args, @files ], \@expected, 'congress counts with -l -c' ); -} - - -DASH_HC: { - my @args = qw( Montresor -c -h ); - my @files = qw( t/text ); - my @expected = ( '3' ); - - ack_sets_match( [ @args, @files ], \@expected, 'ack -c -h should return one line of results' ); -} - -SINGLE_FILE_COUNT: { - my @args = qw( Montresor -c -h ); - my @files = ( 't/text/amontillado.txt' ); - my @expected = ( '3' ); - - ack_sets_match( [ @args, @files ], \@expected, 'ack -c -h should return one line of results' ); -} - -NOT: { - my @args = qw( Montresor -c -h --not God ); - my @files = ( 't/text/amontillado.txt' ); - my @expected = ( 2 ); - - ack_sets_match( [ @args, @files ], \@expected, 'One line of results, with an accurate count' ); +my @tests = read_tests( 't/ack-c.yaml' ); + +for my $test ( @tests ) { + subtest $test->{name} => sub () { + for my $args ( @{$test->{args}} ) { + ack_sets_match( $args, $test->{output}, $test->{name} ); + is( get_rc(), $test->{rc} ); + } + }; } exit 0; diff --git a/t/ack-c.yaml b/t/ack-c.yaml new file mode 100644 index 00000000..1d54406e --- /dev/null +++ b/t/ack-c.yaml @@ -0,0 +1,65 @@ +--- +name: -c +args: God -c --sort-files t/text +rc: 0 +output: | + t/text/amontillado.txt:2 + t/text/bill-of-rights.txt:0 + t/text/constitution.txt:0 + t/text/gettysburg.txt:1 + t/text/movies.txt:0 + t/text/number.txt:0 + t/text/numbered-text.txt:0 + t/text/ozymandias.txt:0 + t/text/raven.txt:2 + +--- +name: -c and --no-filename +args: God -c --no-filename t/text +rc: 0 +output: | + 5 + +--- +name: -c and -v +args: the -i -w -v -c --sort-files t/text +rc: 0 +output: | + t/text/amontillado.txt:206 + t/text/bill-of-rights.txt:45 + t/text/constitution.txt:259 + t/text/gettysburg.txt:15 + t/text/movies.txt:25 + t/text/number.txt:1 + t/text/numbered-text.txt:20 + t/text/ozymandias.txt:9 + t/text/raven.txt:77 + +--- +name: -c and -l +args: congress -i -l -c --sort-files t/text +rc: 0 +output: | + t/text/bill-of-rights.txt:1 + t/text/constitution.txt:29 + +--- +name: -c and -h +args: Montresor -c -h t/text +rc: 0 +output: | + 3 + +--- +name: Normal count +args: Montresor -c -h t/text/amontillado.txt +rc: 0 +output: | + 3 + +--- +name: Count with --not +args: Montresor -c -h --not God t/text/amontillado.txt +rc: 0 +output: | + 2