App-GitHooks

 view release on metacpan or  search on metacpan

t/90-githooks/10-commands.t  view on Meta::CPAN

		expected    => qr/\QThe git hooks have been installed successfully.\E/,
		hooks_exist => 1,
	},
	{
		name        => 'Uninstall git hooks.',
		arguments   => [ 'uninstall' ],
		expected    => qr/\QThe git hooks have been uninstalled successfully.\E/,
		hooks_exist => 0,
	},
];

plan( tests => scalar( @$tests ) + 4 );

# Set up test repository.
ok(
	defined(
		my $source_directory = Cwd::getcwd(),
	),
	'Retrieve the current directory.',
);

ok(
	defined(
		my $repository = test_repository()
	),
	'Create a test git repository.',
);

ok(
	chdir $repository->work_tree(),
	'Switch the current directory to the test git repository.',
);

# Execute tests.
foreach my $test ( @$tests )
{
	subtest(
		$test->{'name'},
		sub
		{
			plan( tests => 5 );
			ok(
				defined(
					my $command = System::Command->new(
						# Specify explicitly which Perl to use, to prevent conflicts
						# between the system Perl, /usr/bin/env perl, and others specified
						# by smoke testers.
						$^X,
						# Make sure we have the same includes as the parents, or we may
						# miss some of the dependencies installed for testing.
						( map { "-I$_" } @INC ),
						# The script to test.
						File::Spec->catfile( $source_directory, 'bin', 'githooks' ),
						# The arguments to pass to the script.
						@{ $test->{'arguments'} }
					)
				),
				'Execute "githooks".',
			);

			my $stderr = do { local $/; my $fh = $command->stderr(); <$fh> };
			ok(
				!defined( $stderr ) || ( $stderr eq '' ),
				'The command did not return any errors.',
			) || diag( $stderr );

			my $stdout = do { local $/; my $fh = $command->stdout(); <$fh> };
			ok(
				defined( $stdout ),
				'Capture the standard output.',
			);

			like(
				$stdout,
				$test->{'expected'},
				'Test output.',
			) || diag( $stdout );

			my $commit_hook_path = File::Spec->catfile( $repository->git_dir(), 'hooks', 'pre-commit' );
			if ( $test->{'hooks_exist'} )
			{
				ok(
					-e $commit_hook_path,
					'The commit hook exists.',
				);
			}
			else
			{
				ok(
					! -e $commit_hook_path,
					'The commit hook does not exist.',
				);
			}
		}
	);
}

# Restore working directory.
ok(
	chdir $source_directory,
	'Switch the current directory back to the original directory.',
);



( run in 3.623 seconds using v1.01-cache-2.11-cpan-364913b4093 )