xref: /openbsd-src/gnu/usr.bin/perl/cpan/Win32/t/Unicode.t (revision 256a93a44f36679bee503f12e49566c2183f6181)
1b39c5158Smillertuse strict;
2b39c5158Smillertuse Test;
391f110e0Safresh1use Config qw(%Config);
4b39c5158Smillertuse Cwd qw(cwd);
591f110e0Safresh1use Encode qw();
6b39c5158Smillertuse Win32;
7b39c5158Smillert
8b39c5158SmillertBEGIN {
9b39c5158Smillert    unless (defined &Win32::BuildNumber && Win32::BuildNumber() >= 820 or $] >= 5.008009) {
10b39c5158Smillert	print "1..0 # Skip: Needs ActivePerl 820 or Perl 5.8.9 or later\n";
11b39c5158Smillert	exit 0;
12b39c5158Smillert    }
13b39c5158Smillert    if ((((Win32::FsType())[1] & 4) == 0) || (Win32::FsType() =~ /^FAT/)) {
14b39c5158Smillert	print "1..0 # Skip: Filesystem doesn't support Unicode\n";
15b39c5158Smillert	exit 0;
16b39c5158Smillert    }
17b39c5158Smillert    unless ((Win32::GetOSVersion())[1] > 4) {
18b39c5158Smillert	print "1..0 # Skip: Unicode support requires Windows 2000 or later\n";
19b39c5158Smillert	exit 0;
20b39c5158Smillert    }
21*256a93a4Safresh1    Win32::CreateFile("8dot3test_canary_Unicode $$");
22*256a93a4Safresh1    my $canary = Win32::GetShortPathName("8dot3test_canary_Unicode $$");
23*256a93a4Safresh1    unlink("8dot3test_canary_Unicode $$");
24*256a93a4Safresh1    if ( length $canary > 12 ) {
25*256a93a4Safresh1        print "1..0 # Skip: The system and/or current volume is not configured to support short names.\n";
26*256a93a4Safresh1        exit 0;
27*256a93a4Safresh1    }
28b39c5158Smillert}
29b39c5158Smillert
30b39c5158Smillertmy $home = Win32::GetCwd();
31b39c5158Smillertmy $cwd  = cwd(); # may be a Cygwin path
32b39c5158Smillertmy $dir  = "Foo \x{394}\x{419} Bar \x{5E7}\x{645} Baz";
33b39c5158Smillertmy $file = "$dir\\xyzzy \x{394}\x{419} plugh \x{5E7}\x{645}";
34b39c5158Smillert
35b39c5158Smillertsub cleanup {
36b39c5158Smillert    chdir($home);
37b39c5158Smillert    my $ansi = Win32::GetANSIPathName($file);
38b39c5158Smillert    unlink($ansi) if -f $ansi;
39b39c5158Smillert    $ansi = Win32::GetANSIPathName($dir);
40b39c5158Smillert    rmdir($ansi) if -d $ansi;
41b39c5158Smillert}
42b39c5158Smillert
43b39c5158Smillertcleanup();
44b39c5158SmillertEND { cleanup() }
45b39c5158Smillert
46b39c5158Smillertplan test => 12;
47b39c5158Smillert
48b39c5158Smillert# Create Unicode directory
49b39c5158SmillertWin32::CreateDirectory($dir);
50b39c5158Smillertok(-d Win32::GetANSIPathName($dir));
51b39c5158Smillert
52b39c5158Smillert# Create Unicode file
53b39c5158SmillertWin32::CreateFile($file);
54b39c5158Smillertok(-f Win32::GetANSIPathName($file));
55b39c5158Smillert
56b39c5158Smillert# readdir() returns ANSI form of Unicode filename
57b39c5158Smillertok(opendir(my $dh, Win32::GetANSIPathName($dir)));
58b39c5158Smillertwhile ($_ = readdir($dh)) {
59b39c5158Smillert    next if /^\./;
6091f110e0Safresh1    # On Cygwin 1.7 readdir() returns the utf8 representation of the
6191f110e0Safresh1    # filename but doesn't turn on the SvUTF8 bit
6291f110e0Safresh1    Encode::_utf8_on($_) if $^O eq "cygwin" && $Config{osvers} !~ /^1.5/;
63b39c5158Smillert    ok($file, Win32::GetLongPathName("$dir\\$_"));
64b39c5158Smillert}
65b39c5158Smillertclosedir($dh);
66b39c5158Smillert
67b39c5158Smillert# Win32::GetLongPathName() of the absolute path restores the Unicode dir name
68b39c5158Smillertmy $full = Win32::GetFullPathName($dir);
69b39c5158Smillertmy $long = Win32::GetLongPathName($full);
70b39c5158Smillert
71b39c5158Smillertok($long, Win32::GetLongPathName($home)."\\$dir");
72b39c5158Smillert
73b39c5158Smillert# We can Win32::SetCwd() into the Unicode directory
74b39c5158Smillertok(Win32::SetCwd($dir));
75b39c5158Smillert
76b39c5158Smillertmy $w32dir = Win32::GetCwd();
77b39c5158Smillert# cwd() also returns a usable ANSI directory name
78b39c5158Smillertmy $subdir = cwd();
79b39c5158Smillert
80b39c5158Smillert# change back to home directory to make sure relative paths
81b39c5158Smillert# in @INC continue to work
82b39c5158Smillertok(chdir($home));
83b39c5158Smillertok(Win32::GetCwd(), $home);
84b39c5158Smillert
85b39c5158Smillertok(Win32::GetLongPathName($w32dir), $long);
86b39c5158Smillert
87b39c5158Smillert# cwd() on Cygwin returns a mapped path that we need to translate
88b39c5158Smillert# back to a Windows path. Invoking `cygpath` on $subdir doesn't work.
89b39c5158Smillertif ($^O eq "cygwin") {
90b39c5158Smillert    $subdir = Cygwin::posix_to_win_path($subdir, 1);
91b39c5158Smillert}
92b39c5158Smillert$subdir =~ s,/,\\,g;
93*256a93a4Safresh1# Cygwin64 no longer returns an ANSI name
94*256a93a4Safresh1skip($^O eq "cygwin", Win32::GetLongPathName($subdir), $long);
95b39c5158Smillert
96b39c5158Smillert# We can chdir() into the Unicode directory if we use the ANSI name
97b39c5158Smillertok(chdir(Win32::GetANSIPathName($dir)));
98b39c5158Smillertok(Win32::GetLongPathName(Win32::GetCwd()), $long);
99