Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and test perlcritic plugins #54

Merged
merged 5 commits into from
Aug 13, 2024
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
13 changes: 13 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
name: 'Unit Tests'

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
container:
image: perldocker/perl-tester
steps:
- uses: actions/checkout@v4
- run: make test-t
okurz marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions .perlcriticrc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ severity = 1

# == Custom Policies
# -- Useless quotes on hashes
[HashKeyQuotes]
[OpenQA::HashKeyQuotes]
severity = 5

# -- Superfluous use strict/warning.
[RedundantStrictWarning]
[OpenQA::RedundantStrictWarning]
equivalent_modules = Test::Most
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ update-deps:
tools/update-deps --cpanfile cpanfile

.PHONY: test
test: test-tidy test-critic test-yaml test-author
test: test-tidy test-critic test-yaml test-author test-t

.PHONY: test-tidy
test-tidy:
Expand All @@ -25,3 +25,7 @@ test-yaml:
.PHONY: test-author
test-author:
prove -l -r xt/

.PHONY: test-t
test-t:
prove -l -r t/
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

package Perl::Critic::Policy::ArgumentInUseStrictWarnings;
package Perl::Critic::Policy::OpenQA::ArgumentInUseStrictWarnings;

use strict;
use warnings;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

package Perl::Critic::Policy::HashKeyQuotes;
package Perl::Critic::Policy::OpenQA::HashKeyQuotes;

use strict;
use warnings;
Expand All @@ -22,6 +22,8 @@ sub applies_to { return qw(PPI::Token::Quote::Single PPI::Token::Quote::Double)
sub violates ($self, $elem, $document) {
# skip anything that's not a hash key
return () unless is_hash_key($elem);
# skip if it has a sibling, e.g. $h{'foo' . 'bar'}
return () if $elem->snext_sibling or $elem->sprevious_sibling;

# only some PPI::Token::Quote::* classes implement literal
my $k = $elem->can('literal') ? $elem->literal : $elem->string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

package Perl::Critic::Policy::RedundantStrictWarning;
package Perl::Critic::Policy::OpenQA::RedundantStrictWarning;

use strict;
use warnings;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

package Perl::Critic::Policy::SpaceAfterSubroutineName;
package Perl::Critic::Policy::OpenQA::SpaceAfterSubroutineName;

use strict;
use warnings;
Expand Down Expand Up @@ -68,6 +68,8 @@ sub check_complete_sub ($self, $elem, @tokens) {
# 5. Whitespace - must be 1
# 6. Structure - the actual code block

my $nsib = $elem->snext_sibling;
my $psib = $elem->sprevious_sibling;
Comment on lines +71 to +72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are those two used somewhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

return () if _is_surrounded_by_one_space($tokens[2]) && _is_surrounded_by_one_space($tokens[4]);
return $self->report_violation($elem);
}
Expand All @@ -77,7 +79,8 @@ sub _is_block ($token) {
}

sub _is_only_one_space ($token) {
return $token->isa('PPI::Token::Whitespace') && $token->content eq ' ';
# We also allow line breaks, e.g. perltidy forces to break up long subroutine headers
return $token->isa('PPI::Token::Whitespace') && $token->content =~ m/^[ \n]$/;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a better function name? Maybe is_whitespace_or_newline? but not sure

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe...

}

sub _is_surrounded_by_one_space ($token) {
Expand Down
11 changes: 11 additions & 0 deletions t/10-perlcritic.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/perl
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

use strict;
use warnings;
use FindBin '$Bin';
use lib "$Bin/../lib/perlcritic";
use Test::Perl::Critic::Policy qw/ all_policies_ok /;

all_policies_ok();
12 changes: 12 additions & 0 deletions t/OpenQA/ArgumentInUseStrictWarnings.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## name Passing
## failures 0
## cut

use strict;
use warnings;

## name Failing
## failures 1
## cut

use warnings 'redefined';
19 changes: 19 additions & 0 deletions t/OpenQA/HashKeyQuotes.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
my %h;
## name Passing
## failures 0
## cut

$h{bare} = 1;
$h{"line\nbreak"} = 2;
$h{'special!'} = 3;
$h{'concatenate_' . 'strings'} = 4;
$h{bare2 } = 5;

## name Failing
## failures 4
## cut

$h{'single'} = 1;
$h{ 'single' } = 2;
$h{"double"} = 3;
$h{"double2" } = 4;
16 changes: 16 additions & 0 deletions t/OpenQA/RedundantStrictWarning.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## name Passing
## failures 0
## cut

use Mojo::Base 'DBIx::Class::Core';

## name Passing
## failures 0
## TODO It can be neessary to use two modules that both enable strict
## cut

use Mojo::Base 'DBIx::Class::Core';

use Moose;
extends 'OpenQA::Schema::Result::Jobs';

31 changes: 31 additions & 0 deletions t/OpenQA/SpaceAfterSubroutineName.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## name Passing
## failures 0
## cut

sub long ($self, $jobid, $job_labels, $aggregated, $failed_modules, $actually_failed_modules,
$todo = undef)
{
}

sub long2
($x, $y) {
}

sub a ($x) {
}

## name Failing
## failures 4
## cut

sub foo($x) {
}

sub foo ($x){
}

sub foo ($x) {
}

sub foo ($x) {
}
Loading