App-Actions

 view release on metacpan or  search on metacpan

t/locales.t  view on Meta::CPAN

	}
	is(App::Actions::I18N->shared->language, 'en', 'the shared default is untouched');

	# Plural rules are per language: the same count, rendered by two
	# instances at once, takes each language's form.
	is($translator{FR}->message('span.day', { count => 0 }), '0 jour', 'French singular for 0');
	is($translator{GB}->message('span.day', { count => 0 }), '0 days', 'English plural for 0, at the same time');
};

# _in_locale
# Purpose:     Run a block with LC_ALL set in the environment and, where the
#              locale is installed, in the C library too.
# Entry:       $locale - the locale name; $code - the block.
# Exit status: Returns whether setlocale accepted the locale.
# Side effects: Changes and restores the process locale.
sub _in_locale {
	my ($locale, $code) = @_;

	local $ENV{LC_ALL} = $locale;
	my $previous = setlocale(LC_ALL);
	my $applied = setlocale(LC_ALL, $locale);

	my $ok = eval { $code->(); 1 };
	my $error = $@;
	setlocale(LC_ALL, $previous);
	die $error unless $ok;

	return defined $applied ? 1 : 0;
}

for my $locale (@LOCALES) {
	subtest "system: error paths under LC_ALL=$locale" => sub {
		my $applied = _in_locale($locale, sub {
			# The OS text is taken from Perl's own $!, set to the errno the
			# failure produces, rather than from POSIX::strerror: that keeps
			# the expectation in the same layer as the message being tested,
			# whatever the C library would say for this locale.
			my $enoent = do { local $! = ENOENT; "$!" };

			throws_ok {
				App::Actions::Process::run([ $^X, '-e', '1' ], stdout => $unreachable);
			} qr/Unable to redirect command output: \Q$enoent\E/,
				"an unwritable output file reports '$enoent'";

			# The catalogue deliberately ignores the POSIX locale, so the
			# message a CI script matches cannot change under it.
			throws_ok { App::Actions::Workflow->load($missing_workflow) }
				qr/^Workflow file '\Q$missing_workflow\E' does not exist/,
				'a catalogue error is still in English';

			local $ENV{ACTION_RUN_LANG} = 'de';
			throws_ok { App::Actions::Workflow->load($missing_workflow) }
				qr/^Die Workflow-Datei '\Q$missing_workflow\E' existiert nicht/,
				'ACTION_RUN_LANG, not the locale, chooses the language';
		});
		diag("$locale is not installed here; the C locale was in force") unless $applied;

		# The command a user runs: the error must still be raised, reported
		# on standard error and given the infrastructure exit status.
		local $ENV{LC_ALL} = $locale;
		my ($stdout, $stderr, $status) = capture {
			system($^X, "-I$lib", $script, $missing_workflow);
		};
		is($status >> 8, 2, "action-run exits 2 for a missing workflow under $locale");
		like($stderr, qr/^action-run: Workflow file '.*absent\.yml' does not exist$/m,
			'and says why on standard error');
	};
}

done_testing();



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