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