App-Actions

 view release on metacpan or  search on metacpan

t/unit.t  view on Meta::CPAN

# defines and every return status the POD documents.  A message key is
# struck off when the code renders it (a spy on App::Actions::I18N::message
# sees each one); a status is struck off by the test that asserts it.  The
# script ends by asserting the ledger is empty, so a message or status no
# test reaches is a failure, not a gap nobody sees.

# Some documented failures need the operating system to refuse: a handle that
# cannot be duplicated or restored, a temporary file that cannot be rewound.
# Process.pm, Check.pm, Runner/Parallel.pm and Runner.pm (with what they
# load) are compiled with open, seek and fork overridden, so a test can make one
# particular call fail by setting a rule in %FAIL; with no rule every call
# goes straight to the real one.  The override is removed again before
# anything else is compiled.
our %FAIL;
BEGIN {
	require File::Spec;
	require File::Temp;
	require IO::Handle;
	require POSIX;
	require Symbol;
	require App::Actions::I18N;

	no warnings 'once';
	*CORE::GLOBAL::open = sub (*;$@) {
		if (my $rule = $main::FAIL{open}) {
			if ($rule->(@_)) {
				$! = POSIX::EBADF();
				return 0;
			}
		}
		# A bareword handle (STDOUT) arrives as its name.
		my $handle = (defined $_[0] && !ref $_[0]) ? Symbol::qualify_to_ref($_[0], scalar caller) : undef;
		return $handle
			? (@_ > 2 ? CORE::open($handle, $_[1], @_[2 .. $#_]) : CORE::open($handle, $_[1]))
			: (@_ > 2 ? CORE::open($_[0], $_[1], @_[2 .. $#_]) : CORE::open($_[0], $_[1]));
	};
	*CORE::GLOBAL::seek = sub (*$$) {
		if ($main::FAIL{seek}) {
			$! = POSIX::ESPIPE();
			return 0;
		}
		return CORE::seek($_[0], $_[1], $_[2]);
	};
	*CORE::GLOBAL::fork = sub () {
		if ($main::FAIL{fork}) {
			$! = POSIX::EAGAIN();
			return undef;
		}
		return CORE::fork();
	};
	require App::Actions::Process;
	require App::Actions::Check;
	require App::Actions::Runner::Parallel;
	require App::Actions::Runner;
	delete $CORE::GLOBAL::{open};
	delete $CORE::GLOBAL::{seek};
	delete $CORE::GLOBAL::{fork};
}

use Cwd ();
use Data::Dumper ();
use File::Path qw(make_path);
use File::Spec;
use File::Temp qw(tempdir);
use JSON::PP ();
use POSIX ();
use Readonly;
use Scalar::Util qw(blessed);
use Test::Mockingbird;
use Test::Most;
use Test::Returns;

use App::Actions;
use App::Actions::Access;
use App::Actions::Action;
use App::Actions::Cache;
use App::Actions::Check;
use App::Actions::Docker;
use App::Actions::Docker::Variable;
use App::Actions::Error;
use App::Actions::Expression;
use App::Actions::I18N;
use App::Actions::Logs;
use App::Actions::Process;
use App::Actions::Runner;
use App::Actions::Runner::Containers;
use App::Actions::Runner::Images;
use App::Actions::Runner::Parallel;
use App::Actions::Runner::Report;
use App::Actions::Runner::Steps;
use App::Actions::Runner::Support;
use App::Actions::Runner::Support::Env;
use App::Actions::Schema;
use App::Actions::Schema::Invalid;
use App::Actions::Util;
use App::Actions::Workflow;

# Nothing here may depend on the environment of whoever runs the suite: the
# language, a developer's ~/.conf (Object::Configure reads it), or the real
# cache and log directories.
delete local $ENV{ACTION_RUN_LANG};
local $ENV{CONFIG_DIR} = tempdir(CLEANUP => 1);
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');

Readonly::Hash my %config => (
	sentinel   => 'caller value',	# what $_, $@ and $! hold before a call
	errno      => POSIX::ENOENT(),	# the $! a routine must leave alone
	alarm      => 100,		# a caller's countdown, which must survive
	big        => 100_000,		# the size of a hostile input
	time_limit => 2,		# seconds a hostile input may take
);

# ---------------------------------------------------------------------------
# The ledger
# ---------------------------------------------------------------------------

# What the POD documents beyond the catalogue: return statuses ("status ...")
# and messages that are not catalogue entries ("text ...": a croak with a
# literal, or a problem a function returns rather than throws).  The test
# that asserts one calls settle() with the same text.

t/unit.t  view on Meta::CPAN

	subtest $name => sub {
		ok(blessed($error) && $error->isa('App::Actions::Error'), 'an App::Actions::Error')
			or return diag('got: ' . (defined $error ? $error : 'no error'));
		is($error->code, $want_code, "code $want_code");
		is($error->message, $want_message, 'the documented message') if defined $want_message;
	};
	return $error;
}

# The routine must leave the caller's $_, $@, $! and alarm() countdown as
# they were.  $! is checked only for a routine that does no I/O of its own.
sub keeps_globals {
	my ($code, $name, %options) = @_;

	local $_ = $config{sentinel};
	local $@ = $config{sentinel};
	local $! = $config{errno};
	my $previous = alarm $config{alarm};

	# The globals are set and read inside the eval, around the call: entering
	# an eval clears $@, and leaving one successfully clears it again.
	my %after;
	my $ok = eval {
		($_, $@, $!) = ($config{sentinel}, $config{sentinel}, $config{errno});
		$code->();
		%after = (topic => $_, error => $@, errno => $! + 0);
		1;
	};
	my $failure = $@;
	my $remaining = alarm 0;
	alarm $previous if $previous;
	return fail("$name: died: $failure") unless $ok;

	subtest "$name keeps the caller's globals" => sub {
		is($after{topic}, $config{sentinel}, '$_');
		is($after{error}, $config{sentinel}, '$@');
		is($after{errno}, $config{errno}, '$!') unless $options{io};
		cmp_ok($remaining, '>', 0, 'the alarm countdown');
	};
	return;
}

# A returned value that must match both its documented schema and the
# value the POD says it is.  (Test::Returns' returns_is checks the schema
# only.)
sub is_returned {
	my ($value, $schema, $want, $name) = @_;

	subtest $name => sub {
		returns_ok($value, $schema, 'the documented shape');
		ref $want ? is_deeply($value, $want, 'the documented value') : is($value, $want, 'the documented value');
	};
	return $value;
}

# A value as one line of Perl, for a test name.  (Test::Most's explain
# prints its diagnostics rather than returning them.)
sub shown {
	my ($value) = @_;

	local $Data::Dumper::Terse = 1;
	local $Data::Dumper::Indent = 0;
	local $Data::Dumper::Sortkeys = 1;
	local $Data::Dumper::Useqq = 1;
	return Data::Dumper::Dumper($value);
}

# Internal states, shown only when asked for.
sub show_state {
	my ($label, $state) = @_;

	explain($label, $state);
	return;
}

sub write_file {
	my ($name, $content, $dir) = @_;

	$dir = tempdir(CLEANUP => 1) unless defined $dir;
	my $path = File::Spec->catfile($dir, $name);
	open my $handle, '>', $path or die "$path: $!";
	binmode $handle;
	print {$handle} $content;
	close $handle or die "$path: $!";
	return $path;
}

# A command that runs this Perl, so no other program is needed.
sub perl_command {
	my ($code, @args) = @_;

	return [ $^X, '-e', $code, @args ];
}

# ---- sections ----

# ---------------------------------------------------------------------------
# App::Actions::Util
# ---------------------------------------------------------------------------

subtest 'App::Actions::Util' => sub {
	# The shared YAML rules: a file that must hold a mapping, values that
	# must be mappings, and the shape of a step.  Every rule has a message,
	# and each is reached here through the public functions only.
	my $job = [ 'where.job', { job => 'test' } ];

	subtest 'load_yaml_mapping' => sub {
		my $file = write_file('ci.yml', "zebra: 1\napple: 2\nmiddle: 3\n");
		my $data = App::Actions::Util::load_yaml_mapping($file, 'workflow');
		returns_ok($data, { type => 'hashref' }, 'a hash reference');
		is_deeply([ keys %{$data} ], [ qw(zebra apple middle) ], 'with its keys in file order');
		keeps_globals(sub { App::Actions::Util::load_yaml_mapping($file, 'action') }, 'load_yaml_mapping', io => 1);

		# Each documented message, for both kinds: the kind chooses the words.
		my %words = (
			workflow => { required => 'No workflow file given', missing => "Workflow file '%s' does not exist",
				parse => "Unable to parse workflow '%s': ", empty => "Workflow '%s' is empty",
				mapping => "Workflow '%s' must contain a YAML mapping" },
			action => { required => 'No action metadata file given', missing => "Action metadata file '%s' does not exist",
				parse => "Unable to parse action metadata '%s': ", empty => "Action metadata '%s' is empty",
				mapping => "Action metadata '%s' must contain a YAML mapping" },
		);
		for my $kind (sort keys %words) {
			my $say = $words{$kind};
			throws_code(sub { App::Actions::Util::load_yaml_mapping(undef, $kind) }, "$kind.file_required", $say->{required},



( run in 0.981 second using v1.01-cache-2.11-cpan-c32d3c3b8f1 )