App-Actions

 view release on metacpan or  search on metacpan

t/unit.t  view on Meta::CPAN

		my ($pre, $main, undef, $post) = @{ $run->{docker}{execs} };
		is_deeply($main->{command}, [ 'node', '/github/workspace/js/main.js' ], 'with node');
		is($main->{env}{'INPUT_PERL-VERSION'}, '5.38', 'its inputs named as GitHub names them');
		is($main->{env}{STATE_seen}, 'pre', 'the state pre saved reaches main');
		is($post->{env}{STATE_seen}, 'pre', 'and post');
		is($main->{env}{GITHUB_ACTION_PATH}, '/github/workspace/js', 'which know where the action is');

		my $js = sub {
			my ($runs, @more) = @_;
			$action->('js2', "name: js2\nruns:\n  using: node20\n$runs", 'main.js' => "say main\n", 'bad.js' => "exit 6\n",
				'boom.js' => "die it blew up\n");
			return $harness->(steps => [ { uses => './js2' } ], @more);
		};
		$run = $js->("  main: main.js\n", facts => { path => '/bin', has_bash => 1, has_node => 0 });
		is($run->{logged}[-1], q{!! JavaScript action './js2' needs Node.js, but 'node' was not found in the job container}, 'node is needed');
		$run = $js->("  main: bad.js\n");
		is_deeply($run->{logged}, [ q{!! JavaScript action './js2' exited with status 6} ], 'main that fails');
		$run = $js->("  pre: bad.js\n  main: main.js\n");
		is_deeply($run->{logged}, [ q{!! pre step of JavaScript action './js2' exited with status 6} ], 'pre that fails');
		unlike($run->{out}, qr/^main$/m, 'and main is not run');
		$run = $js->("  pre: bad.js\n  pre-if: \${{ ( }}\n  main: main.js\n");
		like($run->{logged}[0], qr/\Aaction-run: unable to evaluate pre-if for '\.\/js2': /, 'a pre-if that does not evaluate');
		is($run->{result}{result}, 'success', 'skips pre');
		$run = $js->("  pre: bad.js\n  pre-if: false\n  main: main.js\n");
		is_deeply($run->{logged}, [], 'a pre-if that is false skips pre');

		$run = $js->("  main: main.js\n  post: bad.js\n");
		is_deeply($run->{logged}, [ q{!! post step for './js2' exited with status 6} ], 'post that fails');
		is($run->{result}{result}, 'failure', 'fails the job');
		$run = $js->("  main: main.js\n  post: boom.js\n");
		is_deeply($run->{logged}, [ q{action-run: post step for './js2' failed: it blew up} ], 'post that cannot be run');
		$run = $js->("  main: main.js\n  post: main.js\n  post-if: \${{ ( }}\n");
		like($run->{logged}[0], qr/\Aaction-run: unable to evaluate the post condition for '\.\/js2': /, 'a post-if that does not evaluate');
		unlike($run->{out}, qr/Post/, 'skips post');
		$run = $js->("  main: main.js\n  post: main.js\n  post-if: failure()\n");
		unlike($run->{out}, qr/Post/, 'as does one that is false');
		$run = $js->("  main: main.js\n  post: bad.js\n", quiet => 1);
		is_deeply($run->{logged}, [], 'a quiet run logs nothing');
	};
};

# ---------------------------------------------------------------------------
# App::Actions::Runner
# ---------------------------------------------------------------------------

# The suite's fake Docker daemon: it keeps its state in a directory, runs a
# step script with the host's shell, and is told what to get wrong through
# FAKE_* variables.
Readonly::Scalar my $SUITE_DOCKER => File::Spec->rel2abs(File::Spec->catfile('t', 'bin', 'fake-docker'));

# A Docker client that receives a signal as the run creates a job's
# network, as a run interrupted at the keyboard would.
{
	package Local::SignalDocker;
	our @ISA = ('App::Actions::Docker');

	sub network_create {
		my ($self, @args) = @_;

		kill $self->{test_signal}, $$;
		return $self->SUPER::network_create(@args);
	}
}

subtest 'App::Actions::Runner' => sub {
	# A project holding one workflow, and a runner for it on the fake daemon.
	my $project = sub {
		my ($yaml) = @_;
		my $dir = tempdir(CLEANUP => 1);
		my $workflows = File::Spec->catdir($dir, '.github', 'workflows');
		make_path($workflows);
		return ($dir, write_file('ci.yml', $yaml, $workflows));
	};
	my $runner = sub {
		my ($yaml, %options) = @_;
		my ($dir, $file) = $project->($yaml);
		return App::Actions::Runner->new(workflow => App::Actions::Workflow->load($file), project_dir => $dir,
			docker_bin => $SUITE_DOCKER, image => 'ubuntu:latest', %options);
	};
	# Run code with the fake daemon in a state of its own, and with the
	# knobs given; return what it printed.
	my $daemon = sub {
		my ($knobs, $code) = @_;
		my $state = tempdir(CLEANUP => 1);
		local $ENV{FAKE_DOCKER_STATE} = $state;
		local $ENV{FAKE_DOCKER_LOG} = File::Spec->catfile($state, 'log');
		local @ENV{ keys %{$knobs} } = values %{$knobs};
		return output_of(stdout => $code);
	};
	# Run a workflow; return the exit status, what it printed and what it logged.
	my $run = sub {
		my ($yaml, %options) = @_;
		my $knobs = delete $options{knobs} || {};
		my @logged;
		my $object = $runner->($yaml, logger => collecting_logger(\@logged), %options);
		my $exit;
		my $out = $daemon->($knobs, sub { $exit = $object->run });
		show_state(run => { exit => $exit, out => $out, logged => \@logged });
		return { exit => $exit, out => $out, logged => \@logged };
	};
	my $one_job = "name: CI\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n      - run: echo built\n";

	subtest 'new' => sub {
		my $object = $runner->($one_job);
		returns_ok($object, { type => 'object', isa => 'App::Actions::Runner' }, 'new');
		is_returned($object->workflow, { type => 'object', isa => 'App::Actions::Workflow' }, $object->{workflow}, 'workflow');
		my ($dir, $file) = $project->($one_job);
		my $workflow = App::Actions::Workflow->load($file);
		throws_code(sub { App::Actions::Runner->new(project_dir => $dir) }, 'runner.workflow_required', 'A workflow is required',
			'no workflow');
		throws_code(sub { App::Actions::Runner->new(workflow => $workflow) }, 'runner.project_dir_required', 'A project directory is required',
			'no project directory');
		my $missing = File::Spec->catdir($dir, 'nowhere');
		throws_code(sub { App::Actions::Runner->new(workflow => $workflow, project_dir => $missing) }, 'runner.project_dir_missing',
			"Project directory '$missing' does not exist", 'a project directory that does not exist');
		for my $bad ('0', 'four', '-1', '1.5') {
			throws_code(sub { $runner->($one_job, pull_concurrency => $bad) }, 'runner.pull_concurrency_invalid',
				"pull concurrency must be a positive integer, not '$bad'", "pull_concurrency $bad");
		}
		{
			local $ENV{ACTION_RUN_PULL_CONCURRENCY} = 'lots';



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