App-Actions

 view release on metacpan or  search on metacpan

t/cli.t  view on Meta::CPAN

use strict;
use warnings;
use Test::Most;
use Capture::Tiny qw(capture);
use File::Basename qw(dirname);
use File::Path qw(make_path);
use File::Spec;
use File::Temp qw(tempdir);
use Time::HiRes ();

use JSON::PP ();

my $root = File::Spec->rel2abs(File::Spec->catdir(dirname(__FILE__), '..'));
my $lib = File::Spec->catdir($root, 'lib');
my $script = File::Spec->catfile($root, 'bin', 'action-run');
my $fake_docker = File::Spec->catfile($root, 't', 'bin', 'fake-docker');

# Object::Configure reads the developer's own configuration files (~/.conf
# and the like), where a UNIVERSAL logger would take the messages these tests
# read off standard error.  An empty directory leaves the defaults in force.
local $ENV{CONFIG_DIR} = tempdir(CLEANUP => 1);

# The images a run keeps are recorded for "action-run cache"; point that
# record at a temporary file so the suite does not touch the real cache.
local $ENV{ACTION_RUN_CACHE_FILE}
    = File::Spec->catfile(tempdir(CLEANUP => 1), 'cache.json');
local $ENV{ACTION_RUN_LOG_DIR}
    = File::Spec->catdir(tempdir(CLEANUP => 1), 'logs');

sub make_project {
    my ($yaml) = @_;

    my $project = tempdir(CLEANUP => 1);
    make_path(File::Spec->catdir($project, '.github', 'workflows'));
    my $workflow = File::Spec->catfile($project, '.github', 'workflows', 'test.yml');
    open my $handle, '>', $workflow or die $!;
    print {$handle} $yaml;
    close $handle;

    return ($project, $workflow);
}

# Windows writes CRLF when a program's output is redirected to a file, and
# the tests below match against the end of the line.
sub useful_text {
    my ($text) = @_;
    return '' unless defined $text;
    $text =~ s/\r\n/\n/g;
    return $text;
}

sub run_cli {
    my (@args) = @_;

    my ($stdout, $stderr, $status) = capture {
        system($^X, '-I' . $lib, $script, @args);
    };

    # $? carries the POSIX-compatible status on Windows too.
    return (useful_text($stdout), useful_text($stderr), $status >> 8);
}

# Windows pulls one image at a time whatever is asked for, and the preflight
# reports the number the puller was given rather than the number requested.
sub effective_concurrency {
    my ($asked) = @_;

    return $^O eq 'MSWin32' ? 1 : $asked;
}

my ($version_out, $version_err, $version_exit) = run_cli('--version');
is($version_exit, 0, '--version exits 0');
like($version_out, qr/^action-run 0\.002\.1$/, '--version prints the version');

my ($help_out, $help_err, $help_exit) = run_cli('--help');
is($help_exit, 0, '--help exits 0');
like($help_out, qr/Usage:/, '--help prints usage');
like($help_out, qr/--dry-run/, '--help lists options');
like($help_out, qr/--explain/, '--help lists the container report');
like($help_out, qr/--no-keep-images/, '--help lists the way to drop the image cache');
like($help_out, qr/--parallel/, '--help lists the parallel option');

my $yaml = <<'YAML';
name: CLI test
on: push
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        perl: ['5.38', '5.40']
    steps:
      - run: echo hello
YAML

my ($project, $workflow) = make_project($yaml);

my ($list_out, $list_err, $list_exit) = run_cli('--list', $workflow, $project);
is($list_exit, 0, '--list exits 0');
like($list_out, qr/^test\t/m, '--list shows job ids');
like($list_out, qr/test \(perl=5\.38\)/, '--list shows matrix display names');

my ($infer_out, $infer_err, $infer_exit) = run_cli('--list', $workflow);
is($infer_exit, 0, 'project directory can be inferred');
like($infer_out, qr/test \(perl=5\.40\)/, 'inferred project lists jobs');

my ($dry_out, $dry_err, $dry_exit) = run_cli('--dry-run', '--verbose', $workflow, $project);
is($dry_exit, 0, '--dry-run exits 0');
like($dry_out, qr/\[dry-run\]/, '--dry-run prints Docker commands');
like($dry_out, qr/--- script ---/, '--dry-run --verbose prints scripts');

# --explain is the same dry run with the containers as its output, so it must
# not need the Docker command it is given.
my ($explain_out, $explain_err, $explain_exit) = run_cli(
    '--explain', '--docker', File::Spec->catfile($project, 'absent-docker'),
    $workflow, $project,
);
is($explain_exit, 0, '--explain exits 0 without a Docker daemon');
is($explain_err, '', '--explain writes nothing to standard error');
like($explain_out, qr/^==> Job 'test'$/m, '--explain reports the job container');



( run in 1.934 second using v1.01-cache-2.11-cpan-60884a8df76 )