Skip to content

Commit

Permalink
Centralize perl linters
Browse files Browse the repository at this point in the history
- Mixed all linter configurations from os-autoinst, openQA,
  os-autoinst-distri-opensuse.

  Perlcritic RC & Perltidy RC files are centralized now here. For
  convenience, the most laxed rules were picked, this is still
  subject of discussion.

- Improved perltidy & perlcritic wrappers

  Downstream repositories have a divergent copy of a tidyall
  wrapper that validates before that perltidy is in the correct
  version before running.

  The wrapper does *quite a lot* of text processing to assert the
  correct version of perltidy.

  The new and improved version will get the detected perltidy
  version directly from perl in a clean manner, and will invoke
  tidyall with all arguments passed to it.

  Additionally, perlcritic has a wrapper in Makefiles to inject
  a custom Perl critic policy that it's now provided in this
  repository.

  Both scripts are intended to be symlinked by downstream
  repositories that receive this repository via the subrepo flow.

  So rules can be enforced uniformly across repositories.

- `Perl::Critic::Policy::HashKeyQuotes` is now enforced automatically
  via .perlcriticrc.
  Added `|` symbol as an exception to
  `Perl::Critic::Policy::HashKeyQuotes`.
  • Loading branch information
josegomezr committed Oct 25, 2023
1 parent d23cda4 commit c8268d2
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.perltidyrc
*.tdy
.*.swp
*~
__pycache__/
.tidyall.d/
21 changes: 21 additions & 0 deletions .perlcriticrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# TODO: These rules are not longer called "Freenode" but "Community".
# Evaluate if they still apply.

theme = freenode
severity = 4
include = strict Perl::Critic::Policy::HashKeyQuotes

[Freenode::DiscouragedModules]
severity = 3

# Test modules have no package declaration
[Freenode::PackageMatchesFilename]
severity = 1

[Perl::Critic::Policy::HashKeyQuotes]
severity = 5

[ControlStructures::ProhibitDeepNests]
severity = 4
add_themes = freenode
max_nests = 4
17 changes: 17 additions & 0 deletions .perltidyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Workaround needed for handling non-ASCII in files.
# # See <https://github.com/houseabsolute/perl-code-tidyall/issues/84>.
--character-encoding=none
--no-valign

# TBD: openqa defines: -l=120 vs -l=160 in os-autoinst-distri-opensuse
# taking the most relaxed for now.

-l=160
-fbl # don't change blank lines
-nsfs # no spaces before semicolons
-baao # space after operators
-bbao # space before operators
-pt=2 # no spaces around ()
-bt=2 # no spaces around []
-sbt=2 # no spaces around {}
-sct # stack closing tokens )}
3 changes: 3 additions & 0 deletions .tidyallrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[PerlTidy]
select = **/*.{pl,pm,t}
argv = --profile=$ROOT/.perltidyrc
31 changes: 31 additions & 0 deletions lib/perlcritic/Perl/Critic/Policy/HashKeyQuotes.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Perl::Critic::Policy::HashKeyQuotes;

use Mojo::Base 'Perl::Critic::Policy';

use Perl::Critic::Utils qw( :severities :classification :ppi );

our $VERSION = '0.0.1';

sub default_severity { return $SEVERITY_HIGH }
sub default_themes { return qw(openqa) }
sub applies_to { return qw(PPI::Token::Quote::Single PPI::Token::Quote::Double) }

# check that hashes are not overly using quotes
# (os-autoinst coding style)

sub violates {
my ($self, $elem) = @_;

#we only want the check hash keys
return if !is_hash_key($elem);

my $c = $elem->content;
# special characters
return if $c =~ m/[- \/<>.=_:\\\$\|]/;

my $desc = q{Hash key with quotes};
my $expl = q{Avoid useless quotes};
return $self->violation($desc, $expl, $elem);
}

1;
15 changes: 15 additions & 0 deletions tools/perlcritic
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash -e
#
# Wrap perlcritic to provide custom critic rules transparently.

if ! command -v perlcritic > /dev/null 2>&1; then
echo "perlcritic not found: make sure you've installed all dependencies."
exit 1
fi

# This script will either be called directly from this project or symlinked from
# downstream projects.

OS_AUTOINST_COMMONS_DIR=$(dirname $(dirname $(readlink -e "$0")))

PERL5LIB="${OS_AUTOINST_COMMONS_DIR}/lib/perlcritic:${PERL5LIB}" perlcritic $@
34 changes: 34 additions & 0 deletions tools/tidyall
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env perl
#
# Tidyall command with perltidy version constraint.
use Perl::Tidy;
use strict;
use warnings "all";

use constant REQUIRED_VERSION => '20230912';
my $detected_version = $Perl::Tidy::VERSION;
my @tidyall_argv = @ARGV;

sub is_force_flag { $_ eq '--force' }

unless ($detected_version == REQUIRED_VERSION) {
print STDERR "Incorrect version of perltidy.\n";
printf STDERR "- Detected: %s\n+ Required: %s\n\n", $detected_version, REQUIRED_VERSION;

my $force_run = grep { is_force_flag } @ARGV;

unless ($force_run) {
printf STDERR "Please install the appropriate version of perltidy.\n";
printf STDERR "If you want to proceed anyways, re run with --force flag.\n";
exit 1;
}

# tidyall does not know about the --force flag.
@tidyall_argv = grep { !is_force_flag } @tidyall_argv;

print STDERR "Proceeding to run with incorrect version of perltidy. ";
print STDERR "Results might not be consistent.\n";
print STDERR "==================\n";
}

exit exec('tidyall', @tidyall_argv);

0 comments on commit c8268d2

Please sign in to comment.