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

Test: Add syntax tests and add missing use warnings #712

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ requires "File::Copy::Recursive";
requires "Moo", ">= 2.0";

test_requires "Test::MockModule";
test_requires "Test2::AsyncSubtest";
test_requires "Test::Strict";
1 change: 1 addition & 0 deletions share/shutter/resources/modules/Shutter/App/AboutDialog.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package Shutter::App::AboutDialog;
#--------------------------------------
use utf8;
use strict;
use warnings;
use Gtk3;

#Glib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package Shutter::App::GlobalSettings;
#--------------------------------------
use utf8;
use strict;
use warnings;

#Glib
use Glib qw/TRUE FALSE/;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package Shutter::App::HelperFunctions;
#--------------------------------------
use utf8;
use strict;
use warnings;
use Gtk3;

#Glib
Expand Down
1 change: 1 addition & 0 deletions share/shutter/resources/modules/Shutter/App/Menu.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package Shutter::App::Menu;
#--------------------------------------
use utf8;
use strict;
use warnings;
use Gtk3;

#Glib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package Shutter::App::Optional::Exif;
#--------------------------------------
use utf8;
use strict;
use warnings;

use Glib qw/TRUE FALSE/;

Expand Down
1 change: 1 addition & 0 deletions share/shutter/resources/modules/Shutter/App/Toolbar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package Shutter::App::Toolbar;
#--------------------------------------
use utf8;
use strict;
use warnings;
use Gtk3;

#Glib
Expand Down
1 change: 1 addition & 0 deletions share/shutter/resources/modules/Shutter/Upload/FTP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package Shutter::Upload::FTP;

use utf8;
use strict;
use warnings;
use Net::FTP;
use URI;
use URI::Split qw(uri_split);
Expand Down
1 change: 1 addition & 0 deletions share/shutter/resources/modules/Shutter/Upload/Shared.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package Shutter::Upload::Shared;

use utf8;
use strict;
use warnings;
use POSIX qw/setlocale/;
use Locale::gettext;
use Glib qw/TRUE FALSE/;
Expand Down
1 change: 1 addition & 0 deletions share/shutter/resources/modules/X11/Protocol/Ext/XFIXES.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ BEGIN { require 5 }
package X11::Protocol::Ext::XFIXES;
use X11::Protocol 'padded';
use strict;
use warnings;
use Carp;

use vars '$VERSION', '@CARP_NOT';
Expand Down
52 changes: 52 additions & 0 deletions t/syntax.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Strict;
use Test2::AsyncSubtest;

use FindBin qw/$Bin/;
use POSIX qw( waitpid WNOHANG );
use Time::HiRes qw( sleep );

local $Test::Strict::TEST_WARNINGS = 1;

my @dirs = ('t', 'bin', "$Bin/../share/shutter/resources/modules/");
my @files = Test::Strict::_all_perl_files(@dirs);
Copy link
Member

Choose a reason for hiding this comment

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

This is a private function of Test::Strict? It's not documented at https://metacpan.org/pod/Test::Strict

Why all_perl_files_ok was not enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To run them in parallell. First it finds all files to test with _all_perl_files, then in the next part below it runs them in parallell, in this case 4 at a time. I could find "all perl-like" files myself, but thought it was best to use Test::Strict own way to find them.

This is just a standard syntax check I add in all projects.

When a project is big it gets slow to run syntax checks on all files sequentially.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can be just this:

#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Test::Strict;

use FindBin qw/$Bin/;

# Check syntax, use strict and use warnings on all perl files

local $Test::Strict::TEST_WARNINGS = 1;

my @dirs  = ('t', 'bin', "$Bin/../share/shutter/resources/modules/");

all_perl_files_ok(@dirs);

done_testing;

Locally that takes 10 seconds vs 4 seconds in parallell.

If you prefer I can use the "simple" approach instead.

Copy link
Member

Choose a reason for hiding this comment

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

10 seconds is good enough. Additional 6 seconds don't deserve the added complexity


run_parallel_tests(
4,
[
map {
my $file = $_;
sub { all_perl_files_ok($file); };
} @files
],
"Syntax and strict checks for @dirs"
);

sub run_parallel_tests {
my ($MAX_JOBS, $tests, $name) = @_;

my $ast = Test2::AsyncSubtest->new(name => $name);

my @tests = @{$tests || []};

my %children;

while (@tests) {
if (keys %children < $MAX_JOBS) {
my $pid = $ast->run_fork(shift @tests);
$children{$pid}++;
}
waitpid($_, WNOHANG) && delete $children{$_} for keys %children;

last if !@tests;
sleep 0.01;
}
$ast->finish;
}

done_testing;