7 Test::Script - Basic cross-platform tests for scripts
11 The intent of this module is to provide a series of basic tests for 80%
12 of the testing you will need to do for scripts in the F<script> (or F<bin>
13 as is also commonly used) paths of your Perl distribution.
15 Further, it aims to provide this functionality with perfect
16 platform-compatibility, and in a way that is as unobtrusive as possible.
18 That is, if the program works on a platform, then B<Test::Script>
19 should always work on that platform as well. Anything less than 100% is
20 considered unacceptable.
22 In doing so, it is hoped that B<Test::Script> can become a module that
23 you can safely make a dependency of all your modules, without risking that
24 your module won't on some platform because of the dependency.
26 Where a clash exists between wanting more functionality and maintaining
27 platform safety, this module will err on the side of platform safety.
38 use File::Spec::Unix ();
43 use vars qw{$VERSION @ISA @EXPORT};
57 my $test = Test::Builder->new;
58 $test->exported_to($pack);
61 $self->export_to_level(1, $self, $_);
69 $perl = Probe::Perl->find_perl_interpreter;
74 unless ( defined $path ) {
75 Carp::croak("Did not provide a script name");
77 if ( File::Spec::Unix->file_name_is_absolute($path) ) {
78 Carp::croak("Script name must be relative");
90 #####################################################################
95 =head2 script_compiles
97 script_compiles( 'script/foo.pl', 'Main script compiles' );
99 The C<script_compiles> test calls the script with "perl -c script.pl",
100 and checks that it returns without error.
102 The path it should be passed is a relative unix-format script name. This
103 will be localised when running C<perl -c> and if the test fails the local
104 name used will be shown in the diagnostic output.
106 Note also that the test will be run with the same L<perl> interpreter that
107 is running the test script (and not with the default system perl). This
108 will also be shown in the diagnostic output on failure.
112 sub script_compiles {
113 my $args = _script(shift);
114 my $unix = shift @$args;
115 my $path = path( $unix );
116 my $cmd = [ perl, '-Mblib', '-c', $path, @$args ];
120 my $rv = IPC::Run3::run3( $cmd, \$stdin, \$stdout, \$stderr );
121 my $exit = $? ? ($? >> 8) : 0;
123 $rv and $exit == 0 and $stderr =~ /syntax OK\s+\z/si
126 my $test = Test::Builder->new;
127 $test->ok( $ok, $_[0] || "Script $unix compiles" );
128 $test->diag( "$exit - $stderr" ) unless $ok;
137 script_runs( 'script/foo.pl', 'Main script runs' );
139 The C<script_runs> test executes the script with "perl script.pl" and checks
140 that it returns success.
142 The path it should be passed is a relative unix-format script name. This
143 will be localised when running C<perl -c> and if the test fails the local
144 name used will be shown in the diagnostic output.
146 The test will be run with the same L<perl> interpreter that is running the
147 test script (and not with the default system perl). This will also be shown
148 in the diagnostic output on failure.
153 my $args = _script(shift);
154 my $unix = shift @$args;
155 my $path = path( $unix );
156 my $cmd = [ perl, '-Mblib', $path, @$args ];
160 my $rv = IPC::Run3::run3( $cmd, \$stdin, \$stdout, \$stderr );
161 my $exit = $? ? ($? >> 8) : 0;
162 my $ok = !! ( $rv and $exit == 0 );
164 my $test = Test::Builder->new;
165 $test->ok( $ok, $_[0] || "Script $unix runs" );
166 $test->diag( "$exit - $stderr" ) unless $ok;
175 ######################################################################
178 # Script params must be either a simple non-null string with the script
179 # name, or an array reference with one or more non-null strings.
182 if ( defined _STRING($in) ) {
186 unless ( scalar grep { not defined _STRING($_) } @$in ) {
190 Carp::croak("Invalid command parameter");
193 # Inline some basic Params::Util functions
196 (ref $_[0] eq 'ARRAY' and @{$_[0]}) ? $_[0] : undef;
200 (defined $_[0] and ! ref $_[0] and length($_[0])) ? $_[0] : undef;
205 *script_compiles_ok = *script_compiles;
214 All bugs should be filed via the bug tracker at
216 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Script>
218 For other issues, or commercial enhancement and support, contact the author.
222 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
226 L<prove>, L<http://ali.as/>
230 Copyright 2006 - 2009 Adam Kennedy.
232 This program is free software; you can redistribute
233 it and/or modify it under the same terms as Perl itself.
235 The full text of the license can be found in the
236 LICENSE file included with this module.