CLI-Cmdline
view release on metacpan or search on metacpan
t/05-integration.t view on Meta::CPAN
# t/05-integration.t - Real-world integration tests using executable scripts
use strict;
use warnings;
use Test::More tests => 25;
use Test::NoWarnings 'had_no_warnings';
use File::Temp qw(tempdir);
use Cwd qw(getcwd);
my $orig_dir = getcwd;
my $temp_dir = tempdir( CLEANUP => 1 );
chdir $temp_dir or die "Cannot chdir to $temp_dir: $!";
# Simple and robust script runner
sub run_script {
my ($name, $code, @user_args) = @_;
my $filename = "$name.pl";
# Write script with shebang
open my $fh, '>', $filename or die "Cannot write $filename: $!";
print $fh "#!/usr/bin/perl\n"; # <-- important shebang
print $fh $code;
close $fh;
# Make executable
chmod 0755, $filename or die "Cannot chmod +x $filename: $!";
# Run directly
my $output = `./$filename @user_args 2>&1`;
my $exit = $? >> 8;
return ($output, $exit);
}
sub script_ok {
my ($desc, $code, $expected_output, $expected_exit, @user_args) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($output, $exit) = run_script('script', $code, @user_args);
# Normalize line endings
$output =~ s/\r\n/\n/g;
if (defined $expected_output) {
is($output, $expected_output, "$desc - correct output");
} else {
note("Got output:\n$output");
}
is($exit, $expected_exit // 0, "$desc - correct exit code");
}
# ===========================================================================
# 01 - Simple switch counting with aliases
# ===========================================================================
my $code1 = <<'END';
use strict;
use warnings;
use CLI::Cmdline qw(parse);
my %opt = ( verbose => 0 );
parse(\%opt, 'verbose|v', '') or exit 1; # <-- removed |vvv
print "V:", $opt{verbose}, "\n";
print "ARGS:@ARGV\n";
END
script_ok('01 - -vvv counts as 3', $code1, "V:3\nARGS:\n", 0, '-vvv');
script_ok('01 - --verbose used twice', $code1, "V:2\nARGS:\n", 0, '--verbose', '--verbose');
# ===========================================================================
# 02 - Required option with aliases
# ===========================================================================
my $code2 = <<'END';
use strict;
use warnings;
use CLI::Cmdline qw(parse);
my %opt = ( input => '', output => '' );
parse(\%opt, '', 'input|i output|o') or exit 2;
if ($opt{input} eq '') {
print "MISSING\n";
exit 3;
( run in 0.346 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )