Class-Shared

 view release on metacpan or  search on metacpan

t/path.t  view on Meta::CPAN

		no_dir       => 'Class::Shared: no directory given',
		dir_unsafe   => '%s is writable by group or others; refusing to use it',
		unix_missing => 'Class::Shared: %s is not available here: this perl has no unix '
			. 'domain sockets, so a backend could neither be reached nor started.  '
			. 'This distribution is Unix-only (see PLATFORMS)',
		nul_dir => 'Class::Shared: a directory name may not contain a NUL byte',
	},
);

$config{class_name_re} = qr/\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/;

my $tmp = File::Temp->newdir('class-shared-path-XXXXXX', TMPDIR => 1, CLEANUP => 1);

my $hex64   = join('', map { sprintf '%02x', $_ } 1 .. $config{sha256_bytes});
my @pairs   = $hex64 =~ /(..)/g;
my $colon64 = join(':', @pairs);
my $pin     = $config{fingerprint_prefix} . ('ab' x $config{sha256_bytes});

# ---------------------------------------------------------------- helpers

# A path that is expected to die with exactly this text, once.
sub dies_with {
	my ($code, $expected, $name) = @_;

	throws_ok { $code->() } qr/\A\Q$expected\E\n\z/, $name;

	return;
}

sub dies_like {
	my ($code, $pattern, $name) = @_;

	throws_ok { $code->() } $pattern, $name;

	return;
}

sub write_bytes {
	my ($name, $content) = @_;
	my $path = File::Spec->catfile("$tmp", $name);

	open(my $fh, '>:raw', $path) or die "cannot write $path: $!";
	print {$fh} $content or die "cannot write $path: $!";
	close($fh) or die "cannot write $path: $!";

	return $path;
}

sub make_dir {
	my ($name, $mode) = @_;
	my $dir = File::Spec->catdir("$tmp", $name);

	mkdir($dir, $config{create_mode}) or die "cannot make $dir: $!";
	chmod($mode, $dir) or die "cannot chmod $dir: $!";

	return $dir;
}

sub read_mode {
	my ($path) = @_;
	my @stat = stat($path) or return;

	return $stat[2] & 07777;
}

# A unix socket that is listening, for the endpoint proof.
sub listening_socket {
	my ($path) = @_;

	require IO::Socket::UNIX;

	return IO::Socket::UNIX->new(Type => Socket::SOCK_STREAM(), Local => $path, Listen => 1);
}

# ---------------------------------------------------------------- Class::Shared

subtest 'Class::Shared::validate_class: every guard, both ways' => sub {
	my $err = $config{err};

	# Path 1: undef, by the first guard.
	dies_with(sub { Class::Shared::validate_class(undef) }, $err->{no_class},
		'undef: no class name given');

	# Path 2: the empty string, by the same guard.
	dies_with(sub { Class::Shared::validate_class('') }, $err->{no_class},
		'the empty string: the same guard');

	# Path 3: characters match, words match: the identity.
	for my $valid ('A', '_', 'Foo', 'Foo::Bar2::_x', 'My_Class2') {
		is(Class::Shared::validate_class($valid), $valid, "'$valid' is returned unchanged");
	}

	# Path 4: the character guard refuses; the word loop is never reached.
	for my $invalid ('2Foo', 'Foo-Bar', 'Foo Bar', 'Foo.Bar') {
		dies_with(sub { Class::Shared::validate_class($invalid) },
			sprintf($err->{bad_class}, $invalid), "'$invalid' is refused by the character guard");
	}

	# Path 5: the characters pass and the word loop refuses (an empty word).
	for my $empty_word ('Foo::', '::Foo', 'A:::B', '::') {
		dies_with(sub { Class::Shared::validate_class($empty_word) },
			sprintf($err->{bad_class}, $empty_word),
			"'$empty_word' has an empty word: the loop is what refuses it");
	}

	# The same guard for text that has no ASCII form.  The name of the test is
	# ASCII and the data is the data: TAP prints the name, and a wide character
	# in it warns.
	my @wide = (
		[ "M\x{fc}ller", 'a umlaut' ],
		[ "\x{1F600}x",  'an emoji' ],
		[ "\x{202E}x",   'a right-to-left override' ],
	);
	for my $case (@wide) {
		my ($value, $label) = @{$case};
		dies_with(sub { Class::Shared::validate_class($value) },
			sprintf($err->{bad_class}, $value), "$label is refused by the character guard");
	}

	# Invariant: accepting is the same as the documented pattern.
	my $agree = 0;



( run in 0.976 second using v1.01-cache-2.11-cpan-eb134014566 )