| 1 |
## no critic (Modules::ProhibitExcessMainComplexity) |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
use lib 't/lib'; |
| 6 |
use T::RequireDateTime; |
| 7 |
|
| 8 |
use Test::More; |
| 9 |
use Test::Fatal; |
| 10 |
|
| 11 |
use Cwd qw( abs_path cwd ); |
| 12 |
use DateTime::TimeZone::Local; |
| 13 |
use DateTime::TimeZone::Local::Unix; |
| 14 |
use File::Basename qw( basename ); |
| 15 |
use File::Spec::Functions qw( catdir catfile curdir ); |
| 16 |
use File::Path qw( mkpath ); |
| 17 |
use File::Temp qw( tempdir ); |
| 18 |
use Sys::Hostname qw( hostname ); |
| 19 |
use Try::Tiny; |
| 20 |
|
| 21 |
plan skip_all => 'HPUX is weird' |
| 22 |
if $^O eq 'hpux'; |
| 23 |
|
| 24 |
# Ensures that we can load our OS-specific subclass. Otherwise this |
| 25 |
# might happen later in an eval, and the error will get lost. |
| 26 |
|
| 27 |
## no critic (Subroutines::ProtectPrivateSubs) |
| 28 |
DateTime::TimeZone::Local->_load_subclass() =~ /Unix$/ |
| 29 |
or plan skip_all => 'These tests only run on Unix-ish OSes'; |
| 30 |
|
| 31 |
my $IsMaintainer = hostname() =~ /houseabsolute|quasar/ && -d '.hg'; |
| 32 |
my $CanWriteEtcLocaltime = -w '/etc/localtime' && -l '/etc/localtime'; |
| 33 |
my $CanSymlink = try { |
| 34 |
## no critic (InputOutput::RequireCheckedSyscalls) |
| 35 |
symlink q{}, q{}; |
| 36 |
1; |
| 37 |
}; |
| 38 |
my ($TestFile) = abs_path($0) =~ /(.+)/; |
| 39 |
|
| 40 |
local $ENV{TZ} = undef; |
| 41 |
|
| 42 |
{ |
| 43 |
my %links = DateTime::TimeZone->links(); |
| 44 |
|
| 45 |
for my $alias ( sort keys %{ DateTime::TimeZone::links() } ) { |
| 46 |
local $ENV{TZ} = $alias; |
| 47 |
my $tz = try { DateTime::TimeZone::Local->TimeZone() }; |
| 48 |
is( |
| 49 |
$tz->name(), $links{$alias}, |
| 50 |
"$alias in \$ENV{TZ} for Local->TimeZone()" |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
{ |
| 56 |
for my $name ( sort DateTime::TimeZone::all_names() ) { |
| 57 |
local $ENV{TZ} = $name; |
| 58 |
my $tz = try { DateTime::TimeZone::Local->TimeZone() }; |
| 59 |
is( |
| 60 |
$tz->name(), $name, |
| 61 |
"$name in \$ENV{TZ} for Local->TimeZone()" |
| 62 |
); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
{ |
| 67 |
local $ENV{TZ} = 'this will not work'; |
| 68 |
|
| 69 |
my $tz = DateTime::TimeZone::Local::Unix->FromEnv(); |
| 70 |
is( |
| 71 |
$tz, undef, |
| 72 |
'invalid time zone name in $ENV{TZ} fails' |
| 73 |
); |
| 74 |
|
| 75 |
local $ENV{TZ} = '123/456'; |
| 76 |
|
| 77 |
$tz = DateTime::TimeZone::Local::Unix->FromEnv(); |
| 78 |
is( |
| 79 |
$tz, undef, |
| 80 |
'invalid time zone name in $ENV{TZ} fails' |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
{ |
| 85 |
local $ENV{TZ} = 'Africa/Lagos'; |
| 86 |
|
| 87 |
my $tz = DateTime::TimeZone::Local::Unix->FromEnv(); |
| 88 |
is( |
| 89 |
$tz->name(), 'Africa/Lagos', |
| 90 |
'tz object name() is Africa::Lagos' |
| 91 |
); |
| 92 |
|
| 93 |
local $ENV{TZ} = 0; |
| 94 |
$tz = try { DateTime::TimeZone::Local->TimeZone() }; |
| 95 |
is( |
| 96 |
$tz->name(), 'UTC', |
| 97 |
'$ENV{TZ} set to 0 returns UTC' |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
{ |
| 102 |
|
| 103 |
# This passes the _IsValidName() check but when passed to |
| 104 |
# DT::TZ->new() will throw an exception. |
| 105 |
{ |
| 106 |
|
| 107 |
package Foo; |
| 108 |
use overload q{""} => sub {'Foo'}, 'eq' => sub { "$_[0]" eq "$_[1]" }; |
| 109 |
} |
| 110 |
local $ENV{TZ} = bless [], 'Foo'; |
| 111 |
|
| 112 |
DateTime::TimeZone::Local::Unix->FromEnv(); |
| 113 |
is( $@, q{}, 'FromEnv does not leave $@ set' ); |
| 114 |
} |
| 115 |
|
| 116 |
{ |
| 117 |
local $^O = 'DoesNotExist'; |
| 118 |
my @err; |
| 119 |
try { |
| 120 |
local $SIG{__DIE__} = sub { push @err, shift }; |
| 121 |
DateTime::TimeZone::Local->_load_subclass(); |
| 122 |
}; |
| 123 |
|
| 124 |
is_deeply( |
| 125 |
\@err, [], |
| 126 |
'error loading local time zone module is not seen by __DIE__ handler' |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
no warnings 'redefine'; |
| 131 |
|
| 132 |
SKIP: |
| 133 |
{ |
| 134 |
skip 'These tests require a file system that supports symlinks', 6 |
| 135 |
unless $CanSymlink; |
| 136 |
|
| 137 |
my $etc_dir = tempdir( CLEANUP => 1 ); |
| 138 |
## no critic (Variables::ProhibitPackageVars) |
| 139 |
local $DateTime::TimeZone::Local::Unix::EtcDir = $etc_dir; |
| 140 |
|
| 141 |
# It doesn't matter what this links to since we override _ReadLink below. |
| 142 |
symlink $TestFile => catfile( $etc_dir, 'localtime' ) or die $!; |
| 143 |
|
| 144 |
## no critic (Variables::ProtectPrivateVars) |
| 145 |
local *DateTime::TimeZone::Local::Unix::_Readlink |
| 146 |
= sub {'/usr/share/zoneinfo/US/Eastern'}; |
| 147 |
|
| 148 |
my $tz; |
| 149 |
is( |
| 150 |
exception { |
| 151 |
$tz = DateTime::TimeZone::Local::Unix->FromEtcLocaltime() |
| 152 |
}, |
| 153 |
undef, |
| 154 |
'valid time zone name in /etc/localtime symlink should not die' |
| 155 |
); |
| 156 |
is( |
| 157 |
$tz->name(), 'America/New_York', |
| 158 |
'FromEtchLocaltime() with _Readlink returning /usr/share/zoneinfo/US/Eastern' |
| 159 |
); |
| 160 |
|
| 161 |
local *DateTime::TimeZone::Local::Unix::_Readlink |
| 162 |
= sub {'/usr/share/zoneinfo/Foo/Bar'}; |
| 163 |
|
| 164 |
$tz = DateTime::TimeZone::Local::Unix->FromEtcLocaltime(); |
| 165 |
is( |
| 166 |
$@, q{}, |
| 167 |
'valid time zone name in /etc/localtime symlink should not leave $@ set' |
| 168 |
); |
| 169 |
ok( !$tz, 'no time zone was found' ); |
| 170 |
|
| 171 |
local *DateTime::TimeZone::Local::Unix::_Readlink = sub {undef}; |
| 172 |
local *DateTime::TimeZone::Local::Unix::_FindMatchingZoneinfoFile |
| 173 |
= sub {'America/Los_Angeles'}; |
| 174 |
|
| 175 |
is( |
| 176 |
exception { |
| 177 |
$tz = DateTime::TimeZone::Local::Unix->FromEtcLocaltime() |
| 178 |
}, |
| 179 |
undef, |
| 180 |
'fall back to _FindMatchZoneinfoFile if _Readlink finds nothing' |
| 181 |
); |
| 182 |
is( |
| 183 |
$tz->name(), 'America/Los_Angeles', |
| 184 |
'FromEtchLocaltime() with _FindMatchingZoneinfoFile returning America/Los_Angeles' |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
{ |
| 189 |
## no critic (Variables::ProhibitPackageVars, Variables::ProtectPrivateVars) |
| 190 |
|
| 191 |
my $etc_dir = tempdir( CLEANUP => 1 ); |
| 192 |
local $DateTime::TimeZone::Local::Unix::EtcDir = $etc_dir; |
| 193 |
|
| 194 |
## no critic (ValuesAndExpressions::ProhibitLeadingZeros) |
| 195 |
mkpath( catdir( $etc_dir, 'sysconfig' ), 0, 0755 ); |
| 196 |
open my $fh, '>', catfile( $etc_dir, 'sysconfig', 'clock' ) |
| 197 |
or die $!; |
| 198 |
close $fh or die $!; |
| 199 |
|
| 200 |
local *DateTime::TimeZone::Local::Unix::_ReadEtcSysconfigClock |
| 201 |
= sub {'US/Eastern'}; |
| 202 |
|
| 203 |
my $tz; |
| 204 |
is( |
| 205 |
exception { |
| 206 |
$tz = DateTime::TimeZone::Local::Unix->FromEtcSysconfigClock() |
| 207 |
}, |
| 208 |
undef, |
| 209 |
'valid time zone name in /etc/sysconfig/clock should not die' |
| 210 |
); |
| 211 |
|
| 212 |
is( |
| 213 |
$tz->name(), 'America/New_York', |
| 214 |
'FromEtcSysConfigClock() with _ReadEtcSysconfigClock returning US/Eastern' |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
{ |
| 219 |
## no critic (Variables::ProhibitPackageVars, Variables::ProtectPrivateVars) |
| 220 |
|
| 221 |
my $etc_dir = tempdir( CLEANUP => 1 ); |
| 222 |
local $DateTime::TimeZone::Local::Unix::EtcDir = $etc_dir; |
| 223 |
|
| 224 |
## no critic (ValuesAndExpressions::ProhibitLeadingZeros) |
| 225 |
mkpath( catdir( $etc_dir, 'default' ), 0, 0755 ); |
| 226 |
open my $fh, '>', catfile( $etc_dir, 'default', 'init' ) |
| 227 |
or die $!; |
| 228 |
close $fh or die $!; |
| 229 |
|
| 230 |
local *DateTime::TimeZone::Local::Unix::_ReadEtcDefaultInit |
| 231 |
= sub {'Asia/Tokyo'}; |
| 232 |
|
| 233 |
my $tz; |
| 234 |
is( |
| 235 |
exception { |
| 236 |
$tz = DateTime::TimeZone::Local::Unix->FromEtcDefaultInit() |
| 237 |
}, |
| 238 |
undef, |
| 239 |
'valid time zone name in /etc/default/init should not die' |
| 240 |
); |
| 241 |
|
| 242 |
is( |
| 243 |
$tz->name(), 'Asia/Tokyo', |
| 244 |
'FromEtcDefaultInit with _ReadEtcDefaultInit returning Asia/Tokyo' |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
{ |
| 249 |
## no critic (Variables::ProhibitPackageVars, Variables::ProtectPrivateVars) |
| 250 |
|
| 251 |
my $etc_dir = tempdir( CLEANUP => 1 ); |
| 252 |
local $DateTime::TimeZone::Local::Unix::EtcDir = $etc_dir; |
| 253 |
|
| 254 |
local $ENV{TZ} = q{}; |
| 255 |
|
| 256 |
SKIP: |
| 257 |
{ |
| 258 |
skip 'These tests require a file system that supports symlinks', 2 |
| 259 |
unless $CanSymlink; |
| 260 |
|
| 261 |
my $zoneinfo_dir = catdir( $etc_dir, qw( share zoneinfo ) ); |
| 262 |
local $DateTime::TimeZone::Local::Unix::ZoneinfoDir = $zoneinfo_dir; |
| 263 |
|
| 264 |
mkpath( catdir( $zoneinfo_dir, 'America' ) ); |
| 265 |
|
| 266 |
# The contents of this file are irrelevant but it cannot be zero size. All |
| 267 |
# that matters is the name. |
| 268 |
my $tz_file = catfile( $zoneinfo_dir, 'America', 'Chicago' ); |
| 269 |
open my $fh, '>', $tz_file or die $!; |
| 270 |
print {$fh} 'foo' or die $!; |
| 271 |
close $fh or die $!; |
| 272 |
|
| 273 |
symlink $tz_file => catfile( $etc_dir, 'localtime' ) or die $!; |
| 274 |
|
| 275 |
my $tz; |
| 276 |
is( |
| 277 |
exception { $tz = DateTime::TimeZone::Local->TimeZone() }, |
| 278 |
undef, |
| 279 |
'valid time zone name in /etc/localtime should not die' |
| 280 |
); |
| 281 |
is( |
| 282 |
$tz->name(), 'America/Chicago', |
| 283 |
'/etc/localtime should link to America/Chicago' |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
{ |
| 288 |
my $tz_file = catdir( $etc_dir, 'timezone' ); |
| 289 |
open my $fh, '>', $tz_file or die $!; |
| 290 |
print {$fh} "America/Chicago\n" or die $!; |
| 291 |
close $fh or die $!; |
| 292 |
|
| 293 |
local *DateTime::TimeZone::Local::Unix::FromEtcLocaltime |
| 294 |
= sub {undef}; |
| 295 |
|
| 296 |
my $tz; |
| 297 |
is( |
| 298 |
exception { $tz = DateTime::TimeZone::Local->TimeZone() }, |
| 299 |
undef, |
| 300 |
'valid time zone name in /etc/timezone should not die' |
| 301 |
); |
| 302 |
is( |
| 303 |
$tz->name(), 'America/Chicago', |
| 304 |
'/etc/timezone should contain America/Chicago' |
| 305 |
); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
{ |
| 310 |
## no critic (Variables::ProhibitPackageVars, Variables::ProtectPrivateVars) |
| 311 |
|
| 312 |
my $etc_dir = tempdir( CLEANUP => 1 ); |
| 313 |
local $DateTime::TimeZone::Local::Unix::EtcDir = $etc_dir; |
| 314 |
|
| 315 |
my $default_dir = catdir( $etc_dir, 'default' ); |
| 316 |
|
| 317 |
## no critic (ValuesAndExpressions::ProhibitLeadingZeros) |
| 318 |
mkpath( $default_dir, 0, 0755 ); |
| 319 |
|
| 320 |
my $tz_file = catfile( $default_dir, 'init' ); |
| 321 |
|
| 322 |
open my $fh, '>', $tz_file or die $!; |
| 323 |
print {$fh} "TZ=Australia/Melbourne\n" or die $!; |
| 324 |
close $fh or die $!; |
| 325 |
|
| 326 |
{ |
| 327 |
# requires that /etc/default/init contain |
| 328 |
# TZ=Australia/Melbourne to work. |
| 329 |
local *DateTime::TimeZone::Local::Unix::FromEtcLocaltime |
| 330 |
= sub {undef}; |
| 331 |
local *DateTime::TimeZone::Local::Unix::FromEtcTimezone = sub {undef}; |
| 332 |
local *DateTime::TimeZone::Local::Unix::FromEtcTIMEZONE = sub {undef}; |
| 333 |
|
| 334 |
my $tz; |
| 335 |
is( |
| 336 |
exception { $tz = DateTime::TimeZone::Local->TimeZone() }, |
| 337 |
undef, |
| 338 |
'/etc/default/init contains TZ=Australia/Melbourne' |
| 339 |
); |
| 340 |
is( |
| 341 |
$tz->name(), 'Australia/Melbourne', |
| 342 |
'/etc/default/init should contain Australia/Melbourne' |
| 343 |
); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
{ |
| 348 |
## no critic (Variables::ProhibitPackageVars) |
| 349 |
|
| 350 |
my $etc_dir = tempdir( CLEANUP => 1 ); |
| 351 |
local $DateTime::TimeZone::Local::Unix::EtcDir = $etc_dir; |
| 352 |
|
| 353 |
my $tz_file = catfile( $etc_dir, 'timezone' ); |
| 354 |
|
| 355 |
open my $fh, '>', $tz_file |
| 356 |
or die "Cannot write to $tz_file: $!"; |
| 357 |
print {$fh} 'Foo/Bar' or die $!; |
| 358 |
close $fh or die $!; |
| 359 |
|
| 360 |
DateTime::TimeZone::Local::Unix->FromEtcTimezone(); |
| 361 |
is( |
| 362 |
$@, q{}, |
| 363 |
'calling FromEtcTimezone when it contains a bad name should not leave $@ set' |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
{ |
| 368 |
## no critic (Variables::ProhibitPackageVars) |
| 369 |
|
| 370 |
my $etc_dir = tempdir( CLEANUP => 1 ); |
| 371 |
local $DateTime::TimeZone::Local::Unix::EtcDir = $etc_dir; |
| 372 |
|
| 373 |
my $tz_file = catfile( $etc_dir, 'timezone' ); |
| 374 |
|
| 375 |
open my $fh, '>', $tz_file |
| 376 |
or die "Cannot write to $tz_file: $!"; |
| 377 |
print {$fh} "TZ = Foo/Bar\n" or die $!; |
| 378 |
close $fh or die $!; |
| 379 |
|
| 380 |
DateTime::TimeZone::Local::Unix->FromEtcTIMEZONE(); |
| 381 |
is( |
| 382 |
$@, q{}, |
| 383 |
'calling FromEtcTIMEZONE when it contains a bad name should not leave $@ set' |
| 384 |
); |
| 385 |
} |
| 386 |
|
| 387 |
SKIP: |
| 388 |
{ |
| 389 |
## no critic (Variables::ProhibitPackageVars) |
| 390 |
|
| 391 |
my $zone_file = '/usr/share/zoneinfo/Asia/Kolkata'; |
| 392 |
skip |
| 393 |
'These tests require an up to date IANA database under /usr/share/zoneinfo', |
| 394 |
5 |
| 395 |
unless -f $zone_file && -s _; |
| 396 |
|
| 397 |
my $etc_dir = tempdir( CLEANUP => 1 ); |
| 398 |
local $DateTime::TimeZone::Local::Unix::EtcDir = $etc_dir; |
| 399 |
|
| 400 |
require File::Copy; |
| 401 |
File::Copy::copy( $zone_file, catfile( $etc_dir, 'localtime' ) ) |
| 402 |
or die |
| 403 |
"Cannot copy /usr/share/zoneinfo/Asia/Kolkata to '/etc/localtime': $!"; |
| 404 |
|
| 405 |
{ |
| 406 |
local $ENV{TZ} = q{}; |
| 407 |
|
| 408 |
my $cwd = cwd(); |
| 409 |
|
| 410 |
my $tz; |
| 411 |
is( |
| 412 |
exception { $tz = DateTime::TimeZone::Local->TimeZone() }, |
| 413 |
undef, |
| 414 |
'copy of zoneinfo file at /etc/localtime' |
| 415 |
); |
| 416 |
is( |
| 417 |
$tz->name(), 'Asia/Kolkata', |
| 418 |
'/etc/localtime should be a copy of Asia/Kolkata' |
| 419 |
); |
| 420 |
|
| 421 |
is( |
| 422 |
cwd(), $cwd, |
| 423 |
'cwd should not change after finding local time zone' |
| 424 |
); |
| 425 |
|
| 426 |
$tz = DateTime::TimeZone::Local->TimeZone(); |
| 427 |
is( $@, q{}, 'calling _FindMatchZoneinfoFile does not leave $@ set' ); |
| 428 |
} |
| 429 |
|
| 430 |
{ |
| 431 |
local $ENV{TZ} = q{}; |
| 432 |
|
| 433 |
# Make sure that a die handler does not break our use of die |
| 434 |
# to escape from File::Find::find() |
| 435 |
local $SIG{__DIE__} = sub { die 'haha'; }; |
| 436 |
|
| 437 |
my $tz; |
| 438 |
is( |
| 439 |
exception { $tz = DateTime::TimeZone::Local->TimeZone() }, |
| 440 |
undef, |
| 441 |
'no exception from DateTime::Time::Local->TimeZone' |
| 442 |
); |
| 443 |
is( |
| 444 |
$tz->name(), 'Asia/Kolkata', |
| 445 |
'a __DIE__ handler did not interfere with our use of File::Find' |
| 446 |
); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
{ |
| 451 |
local $ENV{TZ} = 'Australia/Melbourne'; |
| 452 |
my $tz = try { DateTime::TimeZone->new( name => 'local' ) }; |
| 453 |
is( |
| 454 |
$tz->name(), 'Australia/Melbourne', |
| 455 |
q|DT::TZ->new( name => 'local' )| |
| 456 |
); |
| 457 |
} |
| 458 |
|
| 459 |
SKIP: |
| 460 |
{ |
| 461 |
skip 'These tests require a filesystem which support symlinks', 1 |
| 462 |
unless $CanSymlink; |
| 463 |
|
| 464 |
my $tempdir = tempdir( CLEANUP => 1 ); |
| 465 |
|
| 466 |
my $first = File::Spec->catfile( $tempdir, 'first' ); |
| 467 |
open my $fh, '>', $first |
| 468 |
or die "Cannot open $first: $!"; |
| 469 |
close $fh or die $!; |
| 470 |
|
| 471 |
my $second = File::Spec->catfile( $tempdir, 'second' ); |
| 472 |
symlink $first => $second |
| 473 |
or die "Cannot symlink $first => $second: $!"; |
| 474 |
|
| 475 |
my $third = File::Spec->catfile( $tempdir, 'third' ); |
| 476 |
symlink $second => $third |
| 477 |
or die "Cannot symlink $first => $second: $!"; |
| 478 |
|
| 479 |
# It seems that on some systems (OSX, others?) the temp directory |
| 480 |
# returned by File::Temp may be a symlink (/tmp is a link to |
| 481 |
# /private/tmp), so when abs_path folows that link, we end up with |
| 482 |
# a different path to the "first" file. |
| 483 |
is( |
| 484 |
basename( DateTime::TimeZone::Local::Unix->_Readlink($third) ), |
| 485 |
basename($first), |
| 486 |
'_Readlink follows multiple levels of symlinking' |
| 487 |
); |
| 488 |
} |
| 489 |
|
| 490 |
done_testing(); |