RE: [PATCH] Skip ext/Win32/t/Unicode.t under Cygwin
[p5sagit/p5-mst-13.2.git] / ext / Win32 / t / Unicode.t
1 use strict;
2 use Test;
3
4 use Cwd qw(cwd);
5 use Win32;
6
7 BEGIN {
8     unless (defined &Win32::BuildNumber && Win32::BuildNumber() >= 820 or $] >= 5.008009) {
9         print "1..0 # Skip: Needs ActivePerl 820 or Perl 5.8.9 or later\n";
10         exit 0;
11     }
12     unless ((Win32::FsType())[1] & 4) {
13         print "1..0 # Skip: Filesystem doesn't support Unicode\n";
14         exit 0;
15     }
16     unless ((Win32::GetOSVersion())[1] > 4) {
17         print "1..0 # Skip: Unicode support requires Windows 2000 or later\n";
18         exit 0;
19     }
20 }
21
22 my $home = Win32::GetCwd();
23 my $cwd  = cwd(); # may be a Cygwin path
24 my $dir  = "Foo \x{394}\x{419} Bar \x{5E7}\x{645} Baz";
25 my $file = "$dir\\xyzzy \x{394}\x{419} plugh \x{5E7}\x{645}";
26
27 sub cleanup {
28     chdir($home);
29     my $ansi = Win32::GetANSIPathName($file);
30     unlink($ansi) if -f $ansi;
31     $ansi = Win32::GetANSIPathName($dir);
32     rmdir($ansi) if -d $ansi;
33 }
34
35 cleanup();
36 END { cleanup() }
37
38 plan test => 12;
39
40 # Create Unicode directory
41 Win32::CreateDirectory($dir);
42 ok(-d Win32::GetANSIPathName($dir));
43
44 # Create Unicode file
45 Win32::CreateFile($file);
46 ok(-f Win32::GetANSIPathName($file));
47
48 # readdir() returns ANSI form of Unicode filename
49 ok(opendir(my $dh, Win32::GetANSIPathName($dir)));
50 while ($_ = readdir($dh)) {
51     next if /^\./;
52     ok($file, Win32::GetLongPathName("$dir\\$_"));
53 }
54 closedir($dh);
55
56 # Win32::GetLongPathName() of the absolute path restores the Unicode dir name
57 my $full = Win32::GetFullPathName($dir);
58 my $long = Win32::GetLongPathName($full);
59
60 ok($long, Win32::GetLongPathName($home)."\\$dir");
61
62 # We can Win32::SetCwd() into the Unicode directory
63 ok(Win32::SetCwd($dir));
64 ok(Win32::GetLongPathName(Win32::GetCwd()), $long);
65
66 # cwd() also returns a usable ANSI directory name
67 my $subdir = cwd();
68
69 # change back to home directory to make sure relative paths
70 # in @INC continue to work
71 ok(chdir($home));
72 ok(Win32::GetCwd(), $home);
73
74 # cwd() on Cygwin returns a mapped path that we need to translate
75 # back to a Windows path. Invoking `cygpath` on $subdir doesn't work.
76 if ($^O eq "cygwin") {
77     chomp(my $cygpath = `cygpath -w "$cwd"`);
78     $subdir =~ s,\Q$cwd\E,$cygpath,;
79 }
80 $subdir =~ s,/,\\,g;
81 ok(Win32::GetLongPathName($subdir), $long);
82
83 # We can chdir() into the Unicode directory if we use the ANSI name
84 ok(chdir(Win32::GetANSIPathName($dir)));
85 ok(Win32::GetLongPathName(Win32::GetCwd()), $long);