Integrate mainline
[p5sagit/p5-mst-13.2.git] / t / lib / MakeMaker / Test / Utils.pm
CommitLineData
f6d6199c 1package MakeMaker::Test::Utils;
2
3use File::Spec;
4use strict;
5use Config;
6
7use vars qw($VERSION @ISA @EXPORT);
8
9require Exporter;
10@ISA = qw(Exporter);
11
12$VERSION = 0.01;
13
14@EXPORT = qw(which_perl perl_lib makefile_name makefile_backup
15 make make_run make_macro
16 );
17
18my $Is_VMS = $^O eq 'VMS';
19
20
21=head1 NAME
22
23MakeMaker::Test::Utils - Utility routines for testing MakeMaker
24
25=head1 SYNOPSIS
26
27 use MakeMaker::Test::Utils;
28
29 my $perl = which_perl;
30 perl_lib;
31
32 my $makefile = makefile_name;
33 my $makefile_back = makefile_backup;
34
35 my $make = make;
36 my $make_run = make_run;
37 make_macro($make, $targ, %macros);
38
39=head1 DESCRIPTION
40
41A consolidation of little utility functions used through out the
42MakeMaker test suite.
43
44=head2 Functions
45
46The following are exported by default.
47
48=over 4
49
50=item B<which_perl>
51
52 my $perl = which_perl;
53
54Returns a path to perl which is safe to use in a command line, no
55matter where you chdir to.
56
57=cut
58
59sub which_perl {
60 my $perl = $^X;
61 $perl ||= 'perl';
62
63 # VMS should have 'perl' aliased properly
64 return $perl if $Is_VMS;
65
66 $perl = File::Spec->rel2abs( $perl );
67
68 unless( -x $perl ) {
69 # $^X was probably 'perl'
70 foreach my $path (File::Spec->path) {
71 $perl = File::Spec->catfile($path, $^X);
72 last if -x $perl;
73 }
74 }
75
76 return $perl;
77}
78
79=item B<perl_lib>
80
81 perl_lib;
82
83Sets up environment variables so perl can find its libraries.
84
85=cut
86
87my $old5lib = $ENV{PERL5LIB};
88my $had5lib = exists $ENV{PERL5LIB};
89sub perl_lib {
90 # perl-src/lib/ExtUtils/t/Foo
91 my $lib = $ENV{PERL_CORE} ? qq{../../../lib}
92 # ExtUtils-MakeMaker/t/Foo
93 : qq{../blib/lib};
94 $lib = File::Spec->rel2abs($lib);
95 my @libs = ($lib);
96 push @libs, $ENV{PERL5LIB} if exists $ENV{PERL5LIB};
97 $ENV{PERL5LIB} = join($Config{path_sep}, @libs);
98 unshift @INC, $lib;
99}
100
101END {
102 if( $had5lib ) {
103 $ENV{PERL5LIB} = $old5lib;
104 }
105 else {
106 delete $ENV{PERL5LIB};
107 }
108}
109
110
111=item B<makefile_name>
112
113 my $makefile = makefile_name;
114
115MakeMaker doesn't always generate 'Makefile'. It returns what it
116should generate.
117
118=cut
119
120sub makefile_name {
121 return $Is_VMS ? 'Descrip.MMS' : 'Makefile';
122}
123
124=item B<makefile_backup>
125
126 my $makefile_old = makefile_backup;
127
128Returns the name MakeMaker will use for a backup of the current
129Makefile.
130
131=cut
132
133sub makefile_backup {
134 my $makefile = makefile_name;
135 return $Is_VMS ? $makefile : "$makefile.old";
136}
137
138=item B<make>
139
140 my $make = make;
141
142Returns a good guess at the make to run.
143
144=cut
145
146sub make {
147 my $make = $Config{make};
148 $make = $ENV{MAKE} if exists $ENV{MAKE};
149
150 return $make;
151}
152
153=item B<make_run>
154
155 my $make_run = make_run;
156
157Returns the make to run as with make() plus any necessary switches.
158
159=cut
160
161sub make_run {
162 my $make = make;
163 $make .= ' -nologo' if $make eq 'nmake';
164
165 return $make;
166}
167
168=item B<make_macro>
169
170 my $make_cmd = make_macro($make, $target, %macros);
171
172Returns the command necessary to run $make on the given $target using
173the given %macros.
174
175 my $make_test_verbose = make_macro(make_run(), 'test',
176 TEST_VERBOSE => 1);
177
178This is important because VMS's make utilities have a completely
179different calling convention than Unix or Windows.
180
181%macros is actually a list of tuples, so the order will be preserved.
182
183=cut
184
185sub make_macro {
186 my($make, $target) = (shift, shift);
187
188 my $is_mms = $make =~ /^MM(K|S)/i;
189
190 my $cmd = $make;
191 my $macros = '';
192 while( my($key,$val) = splice(@_, 0, 2) ) {
193 if( $is_mms ) {
194 $macros .= qq{/macro="$key=$val"};
195 }
196 else {
197 $macros .= qq{ $key=$val};
198 }
199 }
200
201 return $is_mms ? "$make$macros $target" : "$make $target $macros";
202}
203
204=back
205
206=head1 AUTHOR
207
208Michael G Schwern <schwern@pobox.com>
209
210=cut
211
2121;