Test-File

 view release on metacpan or  search on metacpan

t/owner.t  view on Meta::CPAN

use strict;

use Test::Builder::Tester;
use Test::More;
use Test::File;

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#let's test with the first file we find in the current dir
my( $filename, $file_gid, $owner_uid, $owner_name, $file_group_name );
eval
	{
	$filename = glob( "*" );

	die "Could not find a file" unless defined $filename;

	$owner_uid = ( stat $filename )[4];
	die "failed to find ${filename}'s owner\n" unless defined $owner_uid;

	$file_gid = ( stat $filename )[5];
	die "failed to find ${filename}'s owner\n" unless defined $file_gid;

	$owner_name = ( getpwuid $owner_uid )[0];
	die "failed to find ${filename}'s owner as name\n" unless defined $owner_name;

	$file_group_name = ( getgrgid $file_gid )[0];
	die "failed to find ${filename}'s group as name\n" unless defined $file_group_name;
	};
plan skip_all => "I can't find a file to test with: $@" if $@;

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# find some name that isn't the one we found before
my( $other_name, $other_uid, $other_group_name, $other_gid );
eval
	{
	for( my $i = 0; $i < 65535; $i++ )
		{
		next if $i == $owner_uid;

		my @stats = getpwuid $i;
		next unless @stats;

		( $other_uid, $other_name )  = ( $i, $stats[0] );
		last;
		}

 	# XXX: why the for loop?
	for( my $i = 0; $i < 65535; $i++ )
		{
		next if $i == $file_gid;

		my @stats = getgrgid $i;
		next unless @stats;

		( $other_gid, $other_group_name )  = ( $i, $stats[0] );
 		last;
 		}

	die "Failed to find another uid" unless defined $other_uid;
	die "Failed to find name for other uid ($other_uid)"
		unless defined $other_name;
	die "Failed to find another gid" unless defined $other_gid;
	die "Failed to find name for other gid ($other_gid)"
		unless defined $other_group_name;
	};
plan skip_all => "I can't find a second user id to test with: $@" if $@;

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# find some names that don't exist, to test bad input
my( $invalid_user_name, $invalid_group_name );
eval
	{
	foreach my $user ( 'aaaa' .. 'zzzz' )
		{
		my @stats = getpwnam $user;
		next if @stats;

		$invalid_user_name  = $user;
		#diag "Using invalid user [$user] for tests";
		last;
		}

	foreach my $group ( 'aaaa' .. 'zzzz' )
		{
		my @stats = getpwnam $group;
		next if @stats;

		$invalid_group_name  = $group;
		#diag "Using invalid group [$group] for tests";
		last;
		}

	diag "Failed to find an invalid username" unless defined $other_uid;

	diag "Failed to find another gid" unless defined $other_gid;
	};

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# test owner stuff
owner_is(   $filename, $owner_name, 'owner_is with text username'   );



( run in 0.761 second using v1.01-cache-2.11-cpan-df04353d9ac )