Skip to content

Commit

Permalink
Hoisted tests into ack-c.yaml. Fixed bug where -c would not set exit …
Browse files Browse the repository at this point in the history
…code correctly.
  • Loading branch information
petdance committed Jan 20, 2025
1 parent 61d5a5a commit feaa4e5
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 82 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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
========================================
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions ack
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -350,7 +355,7 @@ sub file_loop_c {
App::Ack::say( $total_count );
}

return;
return $nmatched_files;
}


Expand Down Expand Up @@ -2517,7 +2522,7 @@ Andy Lester, C<< <andy at petdance.com> >>
=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.
Expand Down
2 changes: 1 addition & 1 deletion lib/App/Ack.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
87 changes: 9 additions & 78 deletions t/ack-c.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;
65 changes: 65 additions & 0 deletions t/ack-c.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit feaa4e5

Please sign in to comment.