App-GHGen
view release on metacpan or search on metacpan
#!/usr/bin/env perl
# Regression test for https://github.com/nigelhorne/App-GHGen/pull/6
#
# Bug: "\$savings->{cost}/month" in bin/ghgen escaped the $ so the hash
# dereference was never interpolated. The output read literally
# "$savings->{cost}/month" instead of e.g. "$0.60/month".
#
# Fix: "\$$savings->{cost}/month" â \$ gives a literal $ then
# $savings->{cost} is interpolated normally.
use v5.36;
use Test::Most;
use Path::Tiny;
use IPC::Run3;
use_ok('App::GHGen::CostEstimator', qw(estimate_savings));
# ââ Unit: estimate_savings returns a numeric cost ââââââââââââââââââââââââââ
subtest 'estimate_savings cost field is a formatted decimal' => sub {
my @issues = (
{ type => 'performance', severity => 'warning', message => 'No dependency caching found' },
{ type => 'cost', severity => 'warning', message => 'No concurrency group defined' },
);
my $savings = estimate_savings(\@issues);
ok(defined $savings->{cost}, 'cost key is present');
like($savings->{cost}, qr/^\d+\.\d{2}$/, 'cost is a formatted decimal (e.g. "0.60")');
};
# ââ Unit: the fixed interpolation produces a $ + digits string âââââââââââââ
subtest 'fixed string interpolation yields dollar amount not literal variable' => sub {
my @issues = (
{ type => 'performance', severity => 'warning', message => 'No dependency caching found' },
);
my $savings = estimate_savings(\@issues);
# Reproduce what bin/ghgen now does after the fix
my $display = "\$$savings->{cost}/month";
like($display, qr/^\$\d+\.\d{2}\/month$/, 'interpolated value is $N.NN/month' );
unlike($display, qr/\$savings/, 'does not contain the literal variable name' );
};
# ââ Integration: CLI output must not contain the raw variable reference ââââ
subtest 'ghgen analyze --estimate output contains dollar amount not raw variable' => sub {
my $tmpdir = Path::Tiny->tempdir;
my $workflow_dir = $tmpdir->child('.github/workflows');
$workflow_dir->mkpath;
# Workflow with no caching and no concurrency â guarantees issues exist
# so the Potential Savings block is reached
$workflow_dir->child('test.yml')->spew_utf8(<<'END_YAML');
name: Test CI
on:
push: {}
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- run: npm test
( run in 0.479 second using v1.01-cache-2.11-cpan-a5162978ef8 )