Skip to content

Commit

Permalink
refactor(tests): resolves linter warnings
Browse files Browse the repository at this point in the history
- add /x flag to regular expressions for readability
- initialize local variables upon declaration
- replace die with croak for better error handling
- split long regular expressions into smaller chunks
- use Capture::Tiny for safer output capturing in tests
  • Loading branch information
scriptprivate authored Aug 25, 2024
1 parent dc77e68 commit 6a1fd2f
Showing 1 changed file with 51 additions and 41 deletions.
92 changes: 51 additions & 41 deletions tests/security-gate.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,46 @@ use Test::More;
use Test::Exception;
use Test::MockObject;
use Test::Output;
use Carp qw(croak);
use Capture::Tiny qw(capture);
use Mojo::JSON qw(encode_json);

local $ENV{TEST_MODE} = 1;

my $mock_ua = Test::MockObject -> new();
$mock_ua -> fake_module('Mojo::UserAgent');
$mock_ua -> fake_new('Mojo::UserAgent');
my $mock_ua = Test::MockObject->new();
$mock_ua->fake_module('Mojo::UserAgent');
$mock_ua->fake_new('Mojo::UserAgent');

require_ok('../security-gate.pl');

subtest 'Command-line argument parsing' => sub {
local @ARGV = ();
stdout_like(
sub { main() },
qr/Security Gate v0\.0\.3/,
qr/Security\ Gate\ v0\.0\.3/x,
'Help message displayed when no arguments provided'
);
};

subtest 'Severity counting' => sub {
my $mock_response = Test::MockObject -> new();
$mock_response -> set_always('code', 200);
$mock_response -> set_always('json', [
my $mock_response = Test::MockObject->new();
$mock_response->set_always('code', 200);
$mock_response->set_always('json', [
{ state => 'open', security_vulnerability => { severity => 'high' } },
{ state => 'open', security_vulnerability => { severity => 'critical' } },
{ state => 'open', security_vulnerability => { severity => 'medium' } },
{ state => 'closed', security_vulnerability => { severity => 'low' } },
]);

my $mock_tx = Test::MockObject -> new();
$mock_tx -> set_always('result', $mock_response);
my $mock_tx = Test::MockObject->new();
$mock_tx->set_always('result', $mock_response);

$mock_ua -> set_always('get', $mock_tx);
$mock_ua->set_always('get', $mock_tx);

local @ARGV = ('-t', 'test_token', '-r', 'test_repo');
stdout_like(
sub { main() },
qr/critical: 1.*high: 1.*medium: 1.*low: 0/s,
qr/critical:\ 1.*high:\ 1.*medium:\ 1.*low:\ 0/xs,
'Severity counts are correct'
);
};
Expand Down Expand Up @@ -77,22 +79,27 @@ subtest 'Threshold checking' => sub {
};

subtest 'Output formatting' => sub {
my $mock_response = Test::MockObject -> new();
$mock_response -> set_always('code', 200);
$mock_response -> set_always('json', [
my $mock_response = Test::MockObject->new();
$mock_response->set_always('code', 200);
$mock_response->set_always('json', [
{ state => 'open', security_vulnerability => { severity => 'high' } },
{ state => 'open', security_vulnerability => { severity => 'critical' } },
]);

my $mock_tx = Test::MockObject -> new();
$mock_tx -> set_always('result', $mock_response);
my $mock_tx = Test::MockObject->new();
$mock_tx->set_always('result', $mock_response);

$mock_ua -> set_always('get', $mock_tx);
$mock_ua->set_always('get', $mock_tx);

local @ARGV = ('-t', 'test_token', '-r', 'test_repo');

my $total_alerts_re = qr/\[!\]\ Total\ of\ security\ alerts:/x;
my $critical_alerts_re = qr/\[-\]\ critical:\ 1/x;
my $high_alerts_re = qr/\[-\]\ high:\ 1/x;

stdout_like(
sub { main() },
qr/\[!\] Total of security alerts:.*\[-\] critical: 1.*\[-\] high: 1/s,
qr/$total_alerts_re.*$critical_alerts_re.*$high_alerts_re/xs,
'Output is correctly formatted'
);
};
Expand Down Expand Up @@ -133,37 +140,40 @@ subtest 'Empty response from GitHub API' => sub {
};

subtest 'Multiple severity thresholds' => sub {
my $mock_response = Test::MockObject -> new();
$mock_response -> set_always('code', 200);
$mock_response -> set_always('json', [
my $mock_response = Test::MockObject->new();
$mock_response->set_always('code', 200);
$mock_response->set_always('json', [
{ state => 'open', security_vulnerability => { severity => 'high' } },
{ state => 'open', security_vulnerability => { severity => 'critical' } },
{ state => 'open', security_vulnerability => { severity => 'medium' } },
]);

my $mock_tx = Test::MockObject -> new();
$mock_tx -> set_always('result', $mock_response);
my $mock_tx = Test::MockObject->new();
$mock_tx->set_always('result', $mock_response);

$mock_ua -> set_always('get', $mock_tx);
$mock_ua->set_always('get', $mock_tx);

local @ARGV = ('-t', 'test_token', '-r', 'test_repo', '-c', '0', '-h', '0', '-m', '0', '-l', '0');

my $stdout;
{
local *STDOUT;
open STDOUT, '>', \$stdout;

my $result = main();

diag("STDOUT: $stdout");
diag("Result: $result");

is(
$result,
1,
'Script exits with non-zero code when multiple thresholds are exceeded'
);
}
my ($stdout, $stderr, $result) = capture {
main();
};

diag("STDOUT: $stdout");
diag("STDERR: $stderr");
diag("Result: $result");

is(
$result,
1,
'Script exits with non-zero code when multiple thresholds are exceeded'
);

like(
$stdout,
qr/Total\ of\ security\ alerts:/x,
'Output contains expected content'
);
};

done_testing();
done_testing();

0 comments on commit 6a1fd2f

Please sign in to comment.