File: 04local.t

package info (click to toggle)
libdatetime-timezone-perl 1%3A0.77.01-1%2B2008c
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,208 kB
  • ctags: 2,636
  • sloc: perl: 3,691; sh: 56
file content (426 lines) | stat: -rw-r--r-- 12,915 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/perl -w

use strict;

use DateTime::TimeZone::Local;
use DateTime::TimeZone::Local::Unix;
use File::Basename qw( basename );
use File::Spec;
use Sys::Hostname qw( hostname );
use Test::More;

use lib File::Spec->catdir( File::Spec->curdir, 't' );

BEGIN { require 'check_datetime_version.pl' }

my $IsMaintainer = hostname() =~ /houseabsolute|quasar/ && -d '.svn';
my $CanWriteEtcLocaltime = -w '/etc/localtime' && -l '/etc/localtime';

my @aliases = sort keys %{ DateTime::TimeZone::links() };
my @names = DateTime::TimeZone::all_names();

# Needs to be in scope here in order to get used later
my $Registry;
my $WindowsTZKey;
my @win_tz_names = windows_tz_names();

plan tests => @aliases + @names + @win_tz_names + 33;


# Ensures that we can load our OS-specific subclass. Otherwise this
# might happen later in an eval, and the error will get lost.
DateTime::TimeZone::Local->_load_subclass();

{
    my %links = DateTime::TimeZone->links();

    for my $alias ( sort @aliases )
    {
        local $ENV{TZ} = $alias;
        my $tz = eval { DateTime::TimeZone::Local->TimeZone() };
        is( $tz->name(), $links{$alias},
            "$alias in \$ENV{TZ} for Local->TimeZone()" );
    }
}

{
    for my $name ( sort @names )
    {
        local $ENV{TZ} = $name;
        my $tz = eval { DateTime::TimeZone::Local->TimeZone() };
        is( $tz->name(), $name,
            "$name in \$ENV{TZ} for Local->TimeZone()" );
    }
}

{
    local $ENV{TZ} = 'this will not work';

    my $tz = DateTime::TimeZone::Local::Unix->FromEnv();
    is( $tz, undef,
        'invalid time zone name in $ENV{TZ} fails' );

    local $ENV{TZ} = '123/456';

    $tz = DateTime::TimeZone::Local::Unix->FromEnv();
    is( $tz, undef,
        'invalid time zone name in $ENV{TZ} fails' );
}

{
    local $ENV{TZ} = 'Africa/Kinshasa';

    my $tz = DateTime::TimeZone::Local::Unix->FromEnv();
    is( $tz->name(), 'Africa/Kinshasa', 'tz object name() is Africa::Kinshasa' );

    local $ENV{TZ} = 0;
    $tz = eval { DateTime::TimeZone::Local->TimeZone() };
    is( $tz->name(), 'UTC',
        "\$ENV{TZ} set to 0 returns UTC" );
}


{
    # This passes the _IsValidName() check but when passed to
    # DT::TZ->new() will throw an exception.
    {
        package Foo;
        use overload '""' => sub { "Foo" },
                     'eq' => sub { "$_[0]" eq "$_[1]" };
    }
    local $ENV{TZ} = bless [], 'Foo';

    DateTime::TimeZone::Local::Unix->FromEnv();
    is( $@, '', 'FromEnv does not leave $@ set' );
}


SKIP:
{
    skip "/etc/localtime is not a symlink", 6
        unless -l '/etc/localtime';

    $^W = 0;
    local *DateTime::TimeZone::Local::Unix::_Readlink = sub { '/usr/share/zoneinfo/US/Eastern' };
    $^W = 1;

    my $tz;
    eval { $tz = DateTime::TimeZone::Local::Unix->FromEtcLocaltime() };
    is( $@, '', 'valid time zone name in /etc/localtime symlink should not die' );
    is( $tz->name(), 'America/New_York',
        'FromEtchLocaltime() with _Readlink returning /usr/share/zoneinfo/US/Eastern' );

    $^W = 0;
    local *DateTime::TimeZone::Local::Unix::_Readlink = sub { '/usr/share/zoneinfo/Foo/Bar' };
    $^W = 1;

    $tz = DateTime::TimeZone::Local::Unix->FromEtcLocaltime() ;
    is( $@, '', 'valid time zone name in /etc/localtime symlink should not leave $@ set' );
    ok( ! $tz, 'no time zone was found' );


    $^W = 0;
    local *DateTime::TimeZone::Local::Unix::_Readlink = sub { undef };
    local *DateTime::TimeZone::Local::Unix::_FindMatchingZoneinfoFile = sub { 'America/Los_Angeles' };
    $^W = 1;

    eval { $tz = DateTime::TimeZone::Local::Unix->FromEtcLocaltime() };
    is( $@, '', 'fall back to _FindMatchZoneinfoFile if _Readlink finds nothing' );
    is( $tz->name(), 'America/Los_Angeles',
        'FromEtchLocaltime() with _FindMatchingZoneinfoFile returning America/Los_Angeles' );
}

SKIP:
{
    skip "cannot read /etc/sysconfig/clock", 2
        unless -r '/etc/sysconfig/clock' && -f _;

    $^W = 0;
    local *DateTime::TimeZone::Local::Unix::_ReadEtcSysconfigClock = sub { 'US/Eastern' };
    $^W = 1;

    my $tz;
    eval { $tz = DateTime::TimeZone::Local::Unix->FromEtcSysconfigClock() };
    is( $@, '', 'valid time zone name in /etc/sysconfig/clock should not die' );
    is( $tz->name(), 'America/New_York',
        'FromEtcSysConfigClock() with _ReadEtcSysconfigClock returning US/Eastern' );
}

SKIP:
{
    skip "cannot read /etc/default/init", 2
        unless -r '/etc/default/init' && -f _;

    $^W = 0;
    local *DateTime::TimeZone::Local::Unix::_ReadEtcDefaultInit = sub { 'Asia/Tokyo' };
    $^W = 1;

    my $tz;
    eval { $tz = DateTime::TimeZone::Local::Unix->FromEtcDefaultInit() };
    is( $@, '', 'valid time zone name in /etc/default/init should not die' );
    is( $tz->name(), 'Asia/Tokyo',
      'FromEtcDefaultInit with _ReadEtcDefaultInit returning Asia/Tokyo');
}

SKIP:
{
    skip "Cannot run these tests without explicitly knowing local time zone first (only runs on developers' machine)", 6
        unless $IsMaintainer;

    {
        local $ENV{TZ} = '';

        my $tz;
        eval { $tz = DateTime::TimeZone::Local->TimeZone() };
        is( $@, '', 'valid time zone name in /etc/localtime should not die' );
        is( $tz->name(), 'America/Chicago',
            '/etc/localtime should link to America/Chicago' );
    }

    {
        $^W = 0;
        local *DateTime::TimeZone::Local::Unix::FromEtcLocaltime = sub { undef };
        $^W = 1;

        my $tz;
        eval { $tz = DateTime::TimeZone::Local->TimeZone() };
        is( $@, '', 'valid time zone name in /etc/timezone should not die' );
        is( $tz->name(), 'America/Chicago',
            '/etc/timezone should contain America/Chicago' );
    }

    {
        # requires that /etc/default/init contain
        # TZ=Australia/Melbourne to work.
        $^W = 0;
        local *DateTime::TimeZone::Local::Unix::FromEtcLocaltime = sub { undef };
        local *DateTime::TimeZone::Local::Unix::FromEtcTimezone = sub { undef };
        local *DateTime::TimeZone::Local::Unix::FromEtcTIMEZONE = sub { undef };
        $^W = 1;

        my $tz;
        eval { $tz = DateTime::TimeZone::Local->TimeZone() };
        is( $@, '', '/etc/default/init contains TZ=Australia/Melbourne' );
        is( $tz->name(), 'Australia/Melbourne',
            '/etc/default/init should contain Australia/Melbourne' );
    }
}

SKIP:
{
    my $file = '/etc/timezone';

    skip "Cannot write this test unless we can write to /etc/timezone", 1
        unless $IsMaintainer && -w $file;

    open my $fh, '>', $file
        or die "Cannot write to $file: $!";
    print $fh 'Foo/Bar';
    close $fh;

    DateTime::TimeZone::Local::Unix->FromEtcTimezone();
    is( $@, '', 'calling FromEtcTimezone when it contains a bad name should not leave $@ set' );

    open $fh, '>', $file
        or die "Cannot write to $file: $!";
    print $fh 'America/Chicago';
    close $fh;
}

SKIP:
{
    my $file = '/etc/TIMEZONE';

    skip "Cannot write this test unless we can write to /etc/TIMEZONE", 1
        unless $IsMaintainer && -w '/etc';

    open my $fh, '>', $file
        or die "Cannot write to $file: $!";
    print $fh "TZ = Foo/Bar\n";
    close $fh;

    DateTime::TimeZone::Local::Unix->FromEtcTIMEZONE();
    is( $@, '', 'calling FromEtcTIMEZONE when it contains a bad name should not leave $@ set' );

    unlink $file
        or die "Cannot unlink $file: $!";
}

SKIP:
{
    skip "These tests are too dangerous to run on someone else's machine ;)", 5
        unless $IsMaintainer;

    skip "These tests can only be run if we can overwrite /etc/localtime", 5
        unless $CanWriteEtcLocaltime;

    my $tz_file = readlink '/etc/localtime';

    unlink '/etc/localtime' or die "Cannot unlink /etc/localtime: $!";

    require File::Copy;
    File::Copy::copy( '/usr/share/zoneinfo/Asia/Calcutta', '/etc/localtime' )
        or die "Cannot copy /usr/share/zoneinfo/Asia/Calcutta to '/etc/localtime': $!";

    {
        local $ENV{TZ} = '';

        require Cwd;
        my $cwd = Cwd::cwd();

        my $tz;
        eval { $tz = DateTime::TimeZone::Local->TimeZone() };
        is( $@, '', 'copy of zoneinfo file at /etc/localtime' );
        is( $tz->name(), 'Asia/Calcutta',
            '/etc/localtime should be a copy of Asia/Calcutta' );

        is( Cwd::cwd(), $cwd, 'cwd should not change after finding local time zone' );

        $tz = DateTime::TimeZone::Local->TimeZone();
        is( $@, '', 'calling _FindMatchZoneinfoFile does not leave $@ set' );
    }

    {
        local $ENV{TZ} = '';

        # Make sure that a die handler does not break our use of die
        # to escape from File::Find::find()
        local $SIG{__DIE__} = sub { die 'haha'; };

        my $tz;
        eval { $tz = DateTime::TimeZone::Local->TimeZone() };
        is( $tz->name(), 'Asia/Calcutta',
            'a __DIE__ handler did not interfere with our use of File::Find' );
    }

    unlink '/etc/localtime' or die "Cannot unlink /etc/localtime: $!";
    symlink $tz_file, '/etc/localtime'
        or die "Cannot symlink $tz_file to '/etc/localtime': $!";
}

{
    local $ENV{TZ} = 'Australia/Melbourne';
    my $tz = eval { DateTime::TimeZone->new( name => 'local' ) };
    is( $tz->name(), 'Australia/Melbourne',
        q|DT::TZ->new( name => 'local' )| );
}

SKIP:
{
    skip "These tests only run on Windows", @win_tz_names + 3
        unless $^O =~ /win32/i;

    my $tzi_key =
        $Registry->Open
            ( 'LMachine/SYSTEM/CurrentControlSet/Control/TimeZoneInformation/',
              { Access => Win32::TieRegistry::KEY_READ() | Win32::TieRegistry::KEY_WRITE() }
            );

    skip "No write access to TimeZoneInformation registry key", @win_tz_names + 3
        unless $tzi_key;

    foreach my $win_tz_name (@win_tz_names)
    {
        set_and_test_windows_tz( $win_tz_name, $tzi_key, $WindowsTZKey );
    }

    # We test these explicitly because we want to make sure that at
    # least a few known names do work, rather than just relying on
    # looping through a list.
    for my $name ( 'Eastern Standard Time',
                   'Dateline Standard Time',
                   'Israel Standard Time',
                 )
    {
        set_and_test_windows_tz($name);
    }
}


SKIP:
{
    skip "These tests require File::Temp", 1
        unless require File::Temp;
    skip "These tests require a filesystem which support symlinks", 1
        unless eval { symlink '', '' ; 1 };

    my $tempdir = File::Temp::tempdir( CLEANUP => 1 );

    my $first = File::Spec->catfile( $tempdir, 'first' );
    open my $fh, '>', $first
        or die "Cannot open $first: $!";
    close $fh;

    my $second = File::Spec->catfile( $tempdir, 'second' );
    symlink $first => $second
        or die "Cannot symlink $first => $second: $!";

    my $third = File::Spec->catfile( $tempdir, 'third' );
    symlink $second => $third
        or die "Cannot symlink $first => $second: $!";

    # It seems that on some systems (OSX, others?) the temp directory
    # returned by File::Temp may be a symlink (/tmp is a link to
    # /private/tmp), so when abs_path folows that link, we end up with
    # a different path to the "first" file.
    is( basename( DateTime::TimeZone::Local::Unix->_Readlink( $third ) ),
        basename( $first ),
        '_Readlink follows multiple levels of symlinking' );
}

sub windows_tz_names
{
    return unless $^O =~ /win32/i;

    eval 'use Win32::TieRegistry ( TiedRef => \$Registry, Delimiter => q{/} );';
    return if $@;

    $WindowsTZKey =
        $Registry->Open( 'LMachine/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Time Zones/',
                         { Access => Win32::TieRegistry::KEY_READ() }
                       );

    $WindowsTZKey ||=
        $Registry->Open( 'LMachine/SOFTWARE/Microsoft/Windows/CurrentVersion/Time Zones/',
                         { Access => Win32::TieRegistry::KEY_READ() }
                       );

    return unless $WindowsTZKey;

    return $WindowsTZKey->SubKeyNames()
}

sub set_and_test_windows_tz
{
    my $windows_tz_name = shift;
    my $tzi_key         = shift;

    if ( defined $tzi_key->{'/TimeZoneKeyName'}
         && $tzi_key->{'/TimeZoneKeyName'} ne '' )
    {
        local $tzi_key->{'/TimeZoneKeyName'} = $windows_tz_name;

        test_windows_zone($windows_tz_name);
    }
    else
    {
        local $tzi_key->{'/StandardName'} =
            ( $WindowsTZKey->{ $windows_tz_name . q{/} }
              ? $WindowsTZKey->{ $windows_tz_name . '/Std' }
              : 'MAKE BELIEVE VALUE'
            );

        test_windows_zone($windows_tz_name);
    }
}

sub test_windows_zone
{
    my $windows_tz_name = shift;

    my $tz;
    eval { $tz = DateTime::TimeZone::Local::Win32->FromRegistry() };

    ok( $tz && DateTime::TimeZone->is_valid_name( $tz->name() ),
        "$windows_tz_name - found valid Olson time zone from Windows" );
}