Container-Buildah

 view release on metacpan or  search on metacpan

t/013_cmd.t  view on Meta::CPAN


# test Container::Buildah::Subcommand::cmd()
my ($zero_flag, $nonzero_flag, $nonzero_value);
sub test_cmd
{
	my $cb = shift;
	my $number = shift; # test group number
	my $group = shift; # hash structure of test group parameters

	# loop through tests in group
	foreach my $test (@{$group->{tests}}) {
		$zero_flag = $nonzero_flag = 0;
		$nonzero_value = undef;
		my $opts = $group->{opts} // {};
		$opts->{name} = 'test_cmd';

		# generate name of test set including test group number
		(exists $test->{name})
			or croak "name not provided for test #$number";
		my $test_set = sprintf("%0".$test_digits."d %s", $number, $test->{name});

		# run test in exception handling wrapper
		my ($outstr, $retcode);
		if (not exists $test->{args}) {
			croak "missing args for test #$number";
		}
		if (ref $test->{args} ne "ARRAY") {
			croak "wrong type for args on test #$number - must be an array ref, got "
				.(ref $test->{args} ? ref $test->{args} : "scalar");
		}
		if (exists $test->{retcode}) {
			$opts->{save_retcode} = \$retcode;
		}
		eval {
			$outstr = $cb->cmd($opts, @{$test->{args}});
		};
		my $exception = $@;
		if ($exception) {
			($debug_level>0) and warn "exception: ".Dumper($exception);
		}

		# process results of test
		if (not exists $test->{expected_exception}) {
			# test: exception not expected
			is($exception, '', "$test_set: no exceptions");

			if (exists $test->{outstr}) {
				like($outstr, qr/$test->{outstr}/, "$test_set: output text");
			}
		} else {
			# exception expected
			my $expected_exception = $test->{expected_exception};
			like($exception, qr/$expected_exception/, "$test_set: expected exception");
		}

		# check return code
		if (exists $test->{retcode}) {
			is($retcode, $test->{retcode}, "$test_set: return code");
		}

		# return-code callback tests: test callbacks for zero flag, nonzero flag & nonzero value
		if ($test->{nonzero_set} // 0) {
			is($nonzero_flag, 1, "$test_set: nonzero flag set");
		}
		if ($test->{nonzero_unset} // 0) {
			is($nonzero_flag, 0, "$test_set: nonzero flag unset");
		}
		if ($test->{zero_set} // 0) {
			is($zero_flag, 1, "$test_set: zero flag set");
		}
		if ($test->{zero_unset} // 0) {
			is($zero_flag, 0, "$test_set: zero flag unset");
		}
		if (exists $test->{nonzero_value}) {
			is($nonzero_value, $test->{nonzero_value}, "$test_set: nonzero value");
		}
	}
}

# count tests for test plan
sub count_tests
{
	my @groups = @_;
	my $total = 0;
	foreach my $group (@groups) {
		foreach my $test (@{$group->{tests}}) {
			$total++;
			if (not exists $test->{expected_excetion}) {
				foreach my $argname (qw(outstr)) {
					if (exists $test->{$argname}) {
						$total++;
					}
				}
			}
			foreach my $argname (qw(retcode nonzero_value nonzero_set nonzero_unset zero_set zero_unset)) {
				if (exists $test->{$argname}) {
					$total++;
				}
			}
		}
	}
	return $total;
}

#
# main
#

# config for testing
Container::Buildah::init_config(
        basename => "cmd_test",
        testing_skip_yaml => 1,
);
my $cb = Container::Buildah->instance(($debug_level ? (debug => $debug_level) : ()));

## test fixtures
my @tests = (
	{
		opts => {},
		tests => [
			{



( run in 1.947 second using v1.01-cache-2.11-cpan-140bd7fdf52 )