App-cpan2arch

 view release on metacpan or  search on metacpan

t/01-offline.t  view on Meta::CPAN

use Path::Tiny 0.150;
use Devel::CheckBin 0.04;

no warnings qw< experimental::builtin >;

my $expected = expected_data();

# Defaults
my %DEFS = (
    class    => 'App::cpan2arch',
    ver      => $App::cpan2arch::VERSION,
    packager => 'Your Name <email@domain.tld>',
);

my %FAKE = (
    mod  => 'Foo::Bar',
    dist => 'Foo-Bar',
    ver  => 'v2.0.0',
);

# Unit test each method separately.
subtest 'Unit test' => sub {
    #skip_all;

    subtest 'Construct App::cpan2arch instance' => sub {
        #skip_all;

        my $c2a = App::cpan2arch->new;

        isa_ok(
            $c2a, [ $DEFS{class} ],
            'return value (instance)',
        );
    };

    subtest 'Environment processing' => sub {
        #skip_all;

        my $TODO = todo 'Test fails when parent shell exports the same vars';

        my $ddp = do {
            try {
                require Data::Printer;
            }
            catch ($e) {
                false;
            }
        };

        my %ENV_VARS = (
            packager => {
                var     => 'PACKAGER',
                default => $DEFS{packager},
                custom  => 'Alice <alice@domain.tld>',
            },
            user_agent => {
                var     => 'C2A_USER_AGENT',
                default => "$DEFS{class}/$DEFS{ver}",
                custom  => 'App::Foo/v1.0.0',
            },
            cache_mcpan_path => {
                var     => 'C2A_CACHE_MCPAN_PATH',
                default => '/tmp/mcpan_cache',
                custom  => '/tmp/foo_cache',
            },
            cache_arch_path => {
                var     => 'C2A_CACHE_ARCH_PATH',
                default => '/tmp/arch_cache',
                custom  => '/tmp/bar_cache',
            },
            cache_expiration => {
                var     => 'C2A_CACHE_EXPIRATION',
                default => '1d',
                custom  => '1h',
            },
            cache_ignore => {
                var     => 'C2A_CACHE_IGNORE',
                default => false,
                custom  => true,
            },
            debug => {
                var     => 'C2A_DEBUG',
                default => false,
                custom  => true,
            },
        );

        foreach my ($t) ( qw< default custom > ) {
            subtest $t => sub {
                my $c2a = App::cpan2arch->new;
                my %local_env;

                # Set custom vars locally to not pollute the environment.
                if ( $t eq 'custom' ) {
                    foreach my ( $k, $v ) (%ENV_VARS) {
                        my $var = $v->{var};
                        my $val = $v->{custom};

                        # Skip custom debug test if DDP is not installed.
                        next if $var eq 'C2A_DEBUG' && !$ddp;

                        $local_env{$var} = $val;
                    }
                }
                local %ENV = %local_env if $t eq 'custom';

                my ( $stderr, @ret ) = capture_stderr {
                    return $c2a->_process_env;
                };
                my %env = $c2a->env;

                foreach my ( $k, $v ) (%ENV_VARS) {
                    my $var  = $v->{var};
                    my $val  = $v->{$t};
                    my $name = $val;

                    # Skip custom debug test if DDP is not installed.
                    next if $t eq 'custom' && $var eq 'C2A_DEBUG' && !$ddp;

                    if ( is_bool($val) ) {
                        $name = $val ? 'true' : 'false';



( run in 1.479 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )