bd654125640aa955cd488124aca6a1e3f705d65d
[p5sagit/p5-mst-13.2.git] / t / win32 / system.t
1 #!perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     # XXX this could be further munged to enable some parts on other
7     # platforms
8     unless ($^O =~ /^MSWin/) {
9         print "1..0 # skipped: windows specific test\n";
10         exit 0;
11     }
12 }
13
14 use File::Path;
15 use File::Copy;
16 use Config;
17 use Cwd;
18 use strict;
19
20 $| = 1;
21
22 my $cwd = cwd();
23
24 my $testdir = "t e s t";
25 my $exename = "showav";
26 my $plxname = "showargv";
27 rmtree($testdir);
28 mkdir($testdir);
29 die "Could not create '$testdir':$!" unless -d $testdir;
30
31 open(my $F, ">$testdir/$exename.c")
32     or die "Can't create $testdir/$exename.c: $!";
33 print $F <<'EOT';
34 #include <stdio.h>
35 int
36 main(int ac, char **av)
37 {
38     int i;
39     for (i = 0; i < ac; i++)
40         printf("[%s]", av[i]);
41     printf("\n");
42     return 0;
43 }
44 EOT
45
46 open($F, ">$testdir/$plxname.bat")
47     or die "Can't create $testdir/$plxname.bat: $!";
48 print $F <<'EOT';
49 @rem = '--*-Perl-*--
50 @echo off
51 if "%OS%" == "Windows_NT" goto WinNT
52 EOT
53
54 print $F <<EOT;
55 "$^X" -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
56 goto endofperl
57 :WinNT
58 "$^X" -x -S %0 %*
59 EOT
60 print $F <<'EOT';
61 if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
62 if %errorlevel% == 9009 echo You do not have Perl in your PATH.
63 if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
64 goto endofperl
65 @rem ';
66 #!perl
67 #line 15
68 print "[$_]" for ($0, @ARGV);
69 print "\n";
70 __END__
71 :endofperl
72 EOT
73
74 close $F;
75
76 # build the executable
77 chdir($testdir);
78 END {
79     chdir($cwd) && rmtree("$cwd/$testdir") if -d "$cwd/$testdir";
80 }
81 if (open(my $EIN, "$cwd/op/${exename}_exe.uu")) {
82     print "# Unpacking $exename.exe\n";
83     my $e;
84     {
85         local $/;
86         $e = unpack "u", <$EIN>;
87         close $EIN;
88     }
89     open my $EOUT, ">$exename.exe" or die "Can't write $exename.exe: $!";
90     binmode $EOUT;
91     print $EOUT $e;
92     close $EOUT;
93 }
94 else {
95     my $minus_o = '';
96     if ($Config{cc} eq 'gcc')
97      {
98       $minus_o = "-o $exename.exe";
99      }
100     print "# Compiling $exename.c\n# $Config{cc} $Config{ccflags} $exename.c\n";
101     if (system("$Config{cc} $Config{ccflags} $minus_o $exename.c >log 2>&1") != 0) {
102         print "# Could not compile $exename.c, status $?\n"
103              ."# Where is your C compiler?\n"
104              ."1..0 # skipped: can't build test executable\n";
105         exit(0);
106     }
107     unless (-f "$exename.exe") {
108         if (open(LOG,'<log'))
109          {
110           while(<LOG>) {
111              print "# ",$_;
112           } 
113          }
114         else {
115           warn "Cannot open log (in $testdir):$!";
116         }
117     }
118 }
119 copy("$plxname.bat","$plxname.cmd");
120 chdir($cwd);
121 unless (-x "$testdir/$exename.exe") {
122     print "# Could not build $exename.exe\n"
123          ."1..0 # skipped: can't build test executable\n";
124     exit(0);
125 }
126
127 open my $T, "$^X -I../lib -w op/system_tests |"
128     or die "Can't spawn op/system_tests: $!";
129 my $expect;
130 my $comment = "";
131 my $test = 0;
132 while (<$T>) {
133     chomp;
134     if (/^1\.\./) {
135         print "$_\n";
136     }
137     elsif (/^#+\s(.*)$/) {
138         $comment = $1;
139     }
140     elsif (/^</) {
141         $expect = $_;
142         $expect =~ tr/<>/[]/;
143         $expect =~ s/\Q$plxname\E]/$plxname.bat]/;
144     }
145     else {
146         if ($expect ne $_) {
147             print "# $comment\n" if $comment;
148             print "# want: $expect\n";
149             print "# got : $_\n";
150             print "not ";
151         }
152         ++$test;
153         print "ok $test\n";
154     }
155 }
156 close $T;