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

Unify Perl Tidyall #31

Merged
merged 1 commit into from
Dec 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/perl-lint-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
name: 'Perl static checks'

on:
pull_request:
push:
branches:
- 'master'

jobs:
perl-lint-checks:
runs-on: ubuntu-latest
name: "Perltidy"
container:
image: perldocker/perl-tester
steps:
- uses: actions/checkout@v4
- run: GITHUB_ACTIONS=1 ./tools/tidyall --check-only --all --quiet
Copy link
Member

Choose a reason for hiding this comment

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

Where is GITHUB_ACTIONS being used? I don't see it anywhere...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.tidyall.d/
14 changes: 14 additions & 0 deletions .perltidyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Workaround needed for handling non-ASCII in files.
# # See <https://github.com/houseabsolute/perl-code-tidyall/issues/84>.
--character-encoding=none
--no-valign
-l=160
-fbl # don't change blank lines
-fnl # don't remove new 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} tools/tidyall tools/perlcritic tools/update-deps
argv = --profile=$ROOT/.perltidyrc
26 changes: 26 additions & 0 deletions cpanfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
##################################################
# WARNING
# This file is autogenerated by tools/update-deps
# from dependencies.yaml
##################################################

requires 'Module::CPANfile';
requires 'Storable', '>= 3.06';

on 'test' => sub {
requires 'Test::Most';
requires 'Test::Warnings';

};

on 'develop' => sub {
requires 'Code::TidyAll';
requires 'Perl::Critic';
requires 'Perl::Critic::Community';
requires 'Perl::Tidy', '== 20230912';

};

feature 'coverage', 'coverage for CI' => sub {

};
32 changes: 32 additions & 0 deletions dependencies.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
# % is placeholder for section.
# e.g.:
# % => develop
# %_requires => develop_requires
targets:
# List all %_requires into a cpanfile
cpanfile: [main, develop, test]
cpanfile-targets:
# save %_require into cpanfile section
develop: develop
test: test

main_requires:
# Needed until preaction/Log-Any#105 is solved.
perl(Storable): '>= 3.06'
perl(Module::CPANfile):
perl(version):

develop_requires:
perl(Perl::Tidy): '== 20230912'
perl(Code::TidyAll):
perl(Perl::Critic):
perl(Perl::Critic::Community):

cover_requires:
perl(Devel::Cover):
perl(Devel::Cover::Report::Codecov):

test_requires:
perl(Test::Most):
perl(Test::Warnings):
4 changes: 2 additions & 2 deletions lib/OpenQA/Test/TimeLimit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ sub import {
# disable timeout if requested by ENV variable or running within debugger
return if ($ENV{OPENQA_TEST_TIMEOUT_DISABLE} or $INC{'perl5db.pl'});
$SCALE_FACTOR *= $ENV{OPENQA_TEST_TIMEOUT_SCALE_COVER} // 3 if Devel::Cover->can('report');
$SCALE_FACTOR *= $ENV{OPENQA_TEST_TIMEOUT_SCALE_CI} // 2 if $ENV{CI};
$limit *= $SCALE_FACTOR;
$SCALE_FACTOR *= $ENV{OPENQA_TEST_TIMEOUT_SCALE_CI} // 2 if $ENV{CI};
$limit *= $SCALE_FACTOR;
$SIG{ALRM} = sub { BAIL_OUT "test '$0' exceeds runtime limit of '$limit' seconds\n" };
alarm $limit;
}
Expand Down
71 changes: 71 additions & 0 deletions tools/tidyall
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env perl
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Tidyall command with perltidy version constraint.
use strict;
use warnings;
use version;
use Perl::Tidy;
use Module::CPANfile;
use FindBin '$Bin';

=item perltidy_version()

Grabs the perltidy version from cpanfile using Module::CPANfile.

=cut

sub perltidy_version() {
my $cpanfile_location;
# Try searching for a cpanfile in:
# - the current working directory
# - a directory above this command
# - the catch-all location in external/os-autoinst-common
my @locations = ('.', "$Bin/..", "$Bin/../external/os-autoinst-common");

foreach my $path (@locations) {
next unless -e "$path/cpanfile";
$cpanfile_location = "$path/cpanfile" and last;
}

my $version = Module::CPANfile->load($cpanfile_location)
->prereq_for_module('Perl::Tidy')
->requirement
->version;
# Version requirements may contain qualifiers >=, ==, <, etc. The convention
# is to separate the qualifier from the actual version with a space.
#
# It's safe enough to assume that the last item is really the version.
#
# Additionally version will take care of 1.2.0 being equal to 1.2
return version->new((split ' ', $version)[-1]);
}

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

my $required_version = perltidy_version();
my $detected_version = version->new($Perl::Tidy::VERSION);
my @tidyall_argv = @ARGV;

unless ($detected_version eq $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";
}

exec 'tidyall', @tidyall_argv;
35 changes: 19 additions & 16 deletions tools/update-deps
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ use FindBin qw($Bin);

GetOptions(
"help|h" => \my $help,
"cpanfile" => \my $cpanfile,
"specfile=s" => \my $specfile,
"dockerfile=s" => \my $dockerfile,
);

usage(0) if $help;
usage(1) unless $specfile;
usage(1) unless ($cpanfile || $specfile || $dockerfile);

my $scriptname = path(__FILE__)->to_rel("$Bin/..");
my $yamlfile = "dependencies.yaml";
my $file = "$Bin/../$yamlfile";
my $cpanfile = "$Bin/../cpanfile";
my $proj_root = "$Bin/..";

my $scriptname = path(__FILE__)->to_rel($proj_root);
my $dependencies_yaml_location = "dependencies.yaml";
my $file = "$proj_root/$dependencies_yaml_location";
my $cpanfile_location = "$proj_root/cpanfile";

my $data = YAML::PP->new->load_file($file);
my $spec = path($specfile)->slurp;

my $spectargets = $data->{targets}->{spec};
my $cpantargets = $data->{targets}->{cpanfile};
Expand All @@ -35,8 +37,8 @@ my $cpantarget_mapping = $data->{targets}->{'cpanfile-targets'};

my ($modules_by_target) = get_modules($data, $cpantargets, $cpantarget_mapping);

update_spec();
update_cpanfile($modules_by_target);
update_spec() if $specfile;
update_cpanfile($modules_by_target) if $cpanfile;
update_dockerfile($dockerfile) if $dockerfile;

sub update_dockerfile {
Expand Down Expand Up @@ -71,7 +73,7 @@ sub update_dockerfile {
my $begin = '# AUTODEPS START';
my $end = '# AUTODEPS END';
my $run = <<"EOM";
# This part is autogenerated by $scriptname from $yamlfile
# This part is autogenerated by $scriptname from $dependencies_yaml_location
# hadolint ignore=DL3034,DL3037
RUN zypper in -y -C \\
$dep && zypper clean
Expand All @@ -82,6 +84,7 @@ EOM
}

sub update_spec {
my $spec = path($specfile)->slurp if $specfile;

for my $target (@$spectargets) {
my $name = $target . '_requires';
Expand All @@ -101,7 +104,7 @@ sub update_spec {
$specline .= " $version";
}
}
my $comment = "# The following line is generated from $yamlfile";
my $comment = "# The following line is generated from $dependencies_yaml_location";
if ($spec =~ s/^# .*generated.*\n^$prefix.*/$comment\n$specline/m) {
next;
}
Expand Down Expand Up @@ -152,7 +155,7 @@ sub update_cpanfile {
##################################################
# WARNING
# This file is autogenerated by $scriptname
# from $yamlfile
# from $dependencies_yaml_location
##################################################

EOM
Expand All @@ -168,16 +171,16 @@ EOM
$cover_requires .= ' ' . _requires_line($modules_by_target->{cover}, $module);
}
my $devel_requires = '';
for my $module (sort keys %{$modules_by_target->{devel}}) {
$devel_requires .= ' ' . _requires_line($modules_by_target->{devel}, $module);
for my $module (sort keys %{$modules_by_target->{develop}}) {
$devel_requires .= ' ' . _requires_line($modules_by_target->{develop}, $module);
}
$cpan .= <<"EOM";

on 'test' => sub {
$test_requires
};

on 'devel' => sub {
on 'develop' => sub {
$devel_requires
};

Expand All @@ -186,8 +189,8 @@ $cover_requires
};
EOM

path($cpanfile)->spew($cpan);
say "Updated $cpanfile";
path($cpanfile_location)->spew($cpan);
say "Updated $cpanfile_location";
}

sub usage {
Expand Down