Commit | Line | Data |
bb4e9162 |
1 | package MBTest; |
2 | |
3 | use strict; |
4 | |
5 | use File::Spec; |
66e531b6 |
6 | use File::Temp (); |
7a827510 |
7 | use File::Path (); |
bb4e9162 |
8 | |
15cb7b9d |
9 | |
10 | # Setup the code to clean out %ENV |
11 | BEGIN { |
12 | # Environment variables which might effect our testing |
13 | my @delete_env_keys = qw( |
14 | DEVEL_COVER_OPTIONS |
15 | MODULEBUILDRC |
613f422f |
16 | PERL_MB_OPT |
15cb7b9d |
17 | HARNESS_TIMER |
18 | HARNESS_OPTIONS |
19 | HARNESS_VERBOSE |
20 | PREFIX |
21 | INSTALL_BASE |
22 | INSTALLDIRS |
23 | ); |
24 | |
25 | # Remember the ENV values because on VMS %ENV is global |
26 | # to the user, not the process. |
27 | my %restore_env_keys; |
28 | |
29 | sub clean_env { |
30 | for my $key (@delete_env_keys) { |
31 | if( exists $ENV{$key} ) { |
32 | $restore_env_keys{$key} = delete $ENV{$key}; |
33 | } |
34 | else { |
35 | delete $ENV{$key}; |
36 | } |
37 | } |
38 | } |
39 | |
40 | END { |
41 | while( my($key, $val) = each %restore_env_keys ) { |
42 | $ENV{$key} = $val; |
43 | } |
44 | } |
45 | } |
46 | |
47 | |
bb4e9162 |
48 | BEGIN { |
15cb7b9d |
49 | clean_env(); |
bb4e9162 |
50 | |
738349a8 |
51 | # In case the test wants to use our other bundled |
52 | # modules, make sure they can be loaded. |
613f422f |
53 | my $t_lib = File::Spec->catdir('t', 'bundled'); |
54 | push @INC, $t_lib; # Let user's installed version override |
55 | |
56 | if ($ENV{PERL_CORE}) { |
57 | # We change directories, so expand @INC and $^X to absolute paths |
58 | # Also add . |
59 | @INC = (map(File::Spec->rel2abs($_), @INC), "."); |
60 | $^X = File::Spec->rel2abs($^X); |
61 | } |
bb4e9162 |
62 | } |
63 | |
64 | use Exporter; |
65 | use Test::More; |
66 | use Config; |
7a827510 |
67 | use Cwd (); |
bb4e9162 |
68 | |
69 | # We pass everything through to Test::More |
70 | use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS $TODO); |
66e531b6 |
71 | $VERSION = 0.01_01; |
bb4e9162 |
72 | @ISA = qw(Test::More); # Test::More isa Exporter |
73 | @EXPORT = @Test::More::EXPORT; |
74 | %EXPORT_TAGS = %Test::More::EXPORT_TAGS; |
75 | |
76 | # We have a few extra exports, but Test::More has a special import() |
77 | # that won't take extra additions. |
7a827510 |
78 | my @extra_exports = qw( |
79 | stdout_of |
80 | stderr_of |
81 | stdout_stderr_of |
82 | slurp |
83 | find_in_path |
84 | check_compiler |
85 | have_module |
613f422f |
86 | blib_load |
7a827510 |
87 | ); |
bb4e9162 |
88 | push @EXPORT, @extra_exports; |
89 | __PACKAGE__->export(scalar caller, @extra_exports); |
7a827510 |
90 | # XXX ^-- that should really happen in import() |
15cb7b9d |
91 | |
92 | |
7a827510 |
93 | ######################################################################## |
94 | |
66e531b6 |
95 | # always return to the current directory |
96 | { |
613f422f |
97 | my $cwd = File::Spec->rel2abs(Cwd::cwd); |
98 | |
99 | sub original_cwd { return $cwd } |
7a827510 |
100 | |
7a827510 |
101 | END { |
66e531b6 |
102 | # Go back to where you came from! |
103 | chdir $cwd or die "Couldn't chdir to $cwd"; |
7a827510 |
104 | } |
105 | } |
106 | ######################################################################## |
bb4e9162 |
107 | |
7a827510 |
108 | { # backwards compatible temp filename recipe adapted from perlfaq |
109 | my $tmp_count = 0; |
110 | my $tmp_base_name = sprintf("%d-%d", $$, time()); |
111 | sub temp_file_name { |
112 | sprintf("%s-%04d", $tmp_base_name, ++$tmp_count) |
113 | } |
114 | } |
115 | ######################################################################## |
bb4e9162 |
116 | |
613f422f |
117 | # Setup a temp directory |
118 | sub tmpdir { |
119 | my ($self, @args) = @_; |
120 | my $dir = $ENV{PERL_CORE} ? MBTest->original_cwd : File::Spec->tmpdir; |
121 | return File::Temp::tempdir('MB-XXXXXXXX', CLEANUP => 1, DIR => $dir, @args); |
66e531b6 |
122 | } |
123 | |
bb4e9162 |
124 | sub save_handle { |
125 | my ($handle, $subr) = @_; |
7a827510 |
126 | my $outfile = temp_file_name(); |
bb4e9162 |
127 | |
128 | local *SAVEOUT; |
7a827510 |
129 | open SAVEOUT, ">&" . fileno($handle) |
130 | or die "Can't save output handle: $!"; |
bb4e9162 |
131 | open $handle, "> $outfile" or die "Can't create $outfile: $!"; |
132 | |
133 | eval {$subr->()}; |
134 | open $handle, ">&SAVEOUT" or die "Can't restore output: $!"; |
135 | |
136 | my $ret = slurp($outfile); |
137 | 1 while unlink $outfile; |
138 | return $ret; |
139 | } |
140 | |
141 | sub stdout_of { save_handle(\*STDOUT, @_) } |
142 | sub stderr_of { save_handle(\*STDERR, @_) } |
7a827510 |
143 | sub stdout_stderr_of { |
144 | my $subr = shift; |
145 | my ($stdout, $stderr); |
146 | $stdout = stdout_of ( sub { |
147 | $stderr = stderr_of( $subr ) |
148 | }); |
613f422f |
149 | return wantarray ? ($stdout, $stderr) : $stdout . $stderr; |
7a827510 |
150 | } |
bb4e9162 |
151 | |
152 | sub slurp { |
153 | my $fh = IO::File->new($_[0]) or die "Can't open $_[0]: $!"; |
154 | local $/; |
155 | return scalar <$fh>; |
156 | } |
157 | |
7a827510 |
158 | # Some extensions we should know about if we're looking for executables |
7253302f |
159 | sub exe_exts { |
7253302f |
160 | |
161 | if ($^O eq 'MSWin32') { |
162 | return split($Config{path_sep}, $ENV{PATHEXT} || '.com;.exe;.bat'); |
163 | } |
164 | if ($^O eq 'os2') { |
165 | return qw(.exe .com .pl .cmd .bat .sh .ksh); |
166 | } |
167 | return; |
168 | } |
169 | |
bb4e9162 |
170 | sub find_in_path { |
171 | my $thing = shift; |
613f422f |
172 | |
7253302f |
173 | my @exe_ext = exe_exts(); |
613f422f |
174 | if ( File::Spec->file_name_is_absolute( $thing ) ) { |
7253302f |
175 | foreach my $ext ( '', @exe_ext ) { |
613f422f |
176 | return "$thing$ext" if -e "$thing$ext"; |
177 | } |
178 | } |
179 | else { |
180 | my @path = split $Config{path_sep}, $ENV{PATH}; |
181 | foreach (@path) { |
182 | my $fullpath = File::Spec->catfile($_, $thing); |
183 | foreach my $ext ( '', @exe_ext ) { |
184 | return "$fullpath$ext" if -e "$fullpath$ext"; |
185 | } |
bb4e9162 |
186 | } |
187 | } |
188 | return; |
189 | } |
190 | |
191 | # returns ($have_c_compiler, $C_support_feature); |
192 | sub check_compiler { |
193 | return (1,1) if $ENV{PERL_CORE}; |
194 | |
195 | local $SIG{__WARN__} = sub {}; |
196 | |
613f422f |
197 | blib_load('Module::Build'); |
bb4e9162 |
198 | my $mb = Module::Build->current; |
199 | $mb->verbose( 0 ); |
200 | |
201 | my $have_c_compiler; |
202 | stderr_of( sub {$have_c_compiler = $mb->have_c_compiler} ); |
203 | |
cdbde1c3 |
204 | # check noexec tmpdir |
205 | my $tmp_exec; |
206 | if ( $have_c_compiler ) { |
207 | my $dir = MBTest->tmpdir; |
208 | my $c_file = File::Spec->catfile($dir,'test.c'); |
209 | open my $fh, ">", $c_file; |
210 | print {$fh} "int main() { return 0; }\n"; |
211 | close $fh; |
212 | my $exe = $mb->cbuilder->link_executable( |
213 | objects => $mb->cbuilder->compile( source => $c_file ) |
214 | ); |
215 | $tmp_exec = 0 == system( $exe ); |
216 | } |
217 | return ($have_c_compiler, $mb->feature('C_support'), $tmp_exec); |
bb4e9162 |
218 | } |
219 | |
f943a5bf |
220 | sub have_module { |
221 | my $module = shift; |
613f422f |
222 | return eval "require $module; 1"; |
f943a5bf |
223 | } |
224 | |
613f422f |
225 | sub blib_load { |
226 | # Load the given module and ensure it came from blib/, not the larger system |
738349a8 |
227 | my $mod = shift; |
613f422f |
228 | have_module($mod) or die "Error loading $mod\: $@\n"; |
229 | |
738349a8 |
230 | (my $path = $mod) =~ s{::}{/}g; |
613f422f |
231 | $path .= ".pm"; |
232 | my ($pkg, $file, $line) = caller; |
233 | unless($ENV{PERL_CORE}) { |
234 | unless($INC{$path} =~ m/\bblib\b/) { |
235 | (my $load_from = $INC{$path}) =~ s{$path$}{}; |
236 | die "$mod loaded from '$load_from'\nIt should have been loaded from blib. \@INC contains:\n ", |
237 | join("\n ", @INC) . "\nFatal error occured in blib_load() at $file, line $line.\n"; |
238 | } |
738349a8 |
239 | } |
240 | } |
241 | |
bb4e9162 |
242 | 1; |
7a827510 |
243 | # vim:ts=2:sw=2:et:sta |