diff options
Diffstat (limited to 't')
-rw-r--r-- | t/01_compile.t | 37 | ||||
-rw-r--r-- | t/02_main.t | 288 | ||||
-rw-r--r-- | t/10_test.t | 34 | ||||
-rw-r--r-- | t/11_darwin.t | 86 | ||||
-rw-r--r-- | t/12_darwin_carbon.t | 62 | ||||
-rw-r--r-- | t/13_darwin_cocoa.t | 64 |
6 files changed, 571 insertions, 0 deletions
diff --git a/t/01_compile.t b/t/01_compile.t new file mode 100644 index 0000000..44b7335 --- /dev/null +++ b/t/01_compile.t @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +# Compile-testing for File::HomeDir + +use strict; +BEGIN { + $| = 1; + $^W = 1; +} +use File::Spec::Functions ':ALL'; + +use Test::More tests => 11; + +# This module is destined for the core. +# Please do NOT use convenience modules +# use English; <-- don't do this + +ok( $] > 5.00503, 'Perl version is 5.005 or newer' ); + +use_ok( 'File::HomeDir::Driver' ); +use_ok( 'File::HomeDir::Unix' ); +use_ok( 'File::HomeDir::FreeDesktop' ); +use_ok( 'File::HomeDir::Darwin' ); +use_ok( 'File::HomeDir::Darwin::Carbon' ); +use_ok( 'File::HomeDir::Darwin::Cocoa' ); +use_ok( 'File::HomeDir::Windows' ); +use_ok( 'File::HomeDir::MacOS9' ); +use_ok( 'File::HomeDir' ); + +ok( defined &home, 'Using File::HomeDir exports home()' ); + +# Note the driver we are using for the purposes of +# understanding CPAN Testers failure reports. +diag( "Implemented by: $File::HomeDir::IMPLEMENTED_BY" ); + +# Prevent a warning +$File::HomeDir::IMPLEMENTED_BY = $File::HomeDir::IMPLEMENTED_BY; diff --git a/t/02_main.t b/t/02_main.t new file mode 100644 index 0000000..7a171ce --- /dev/null +++ b/t/02_main.t @@ -0,0 +1,288 @@ +#!/usr/bin/perl + +# Main testing for File::HomeDir + +# Testing "home directory" concepts is blood difficult, be delicate in +# your changes and don't forget to test on every OS at multiple versions +# (WinXP vs Win2003 etc) as both root and non-root users. + +use strict; +BEGIN { + $| = 1; + $^W = 1; +} +use File::Spec::Functions ':ALL'; +use Test::More; +use File::HomeDir; + +# This module is destined for the core. +# Please do NOT use convenience modules +# use English; <-- don't do this + +sub is_dir($) { + my $dir = shift or return; + return 1 if -d $dir; + return unless -l $dir; + $dir = readlink $dir or return; + return -d $dir; +} + + + + + +##################################################################### +# Environment Detection and Plan + +# For what scenarios can we be sure that we have desktop/documents +my $NO_GETPWUID = 0; +my $HAVEHOME = 0; +my $HAVEDESKTOP = 0; +my $HAVEMUSIC = 0; +my $HAVEPICTURES = 0; +my $HAVEVIDEOS = 0; +my $HAVEDOCUMENTS = 0; +my $HAVEOTHERS = 0; + +# Various cases of things we should try to test for +# Top level is entire classes of operating system. +# Below that are more general things. +if ( $^O eq 'MSWin32' ) { + $NO_GETPWUID = 1; + $HAVEHOME = 1; + $HAVEDESKTOP = 1; + $HAVEPICTURES = 1; + $HAVEDOCUMENTS = 1; + $HAVEOTHERS = 1; + + # My Music does not exist on Win2000 + require Win32; + my @version = Win32::GetOSVersion(); + my $v = ($version[4]||0) + + ($version[1]||0) * 0.001 + + ($version[2]||0) * 0.000001; + if ( $v <= 2.005000 ) { + $HAVEMUSIC = 0; + $HAVEVIDEOS = 0; + } else { + $HAVEMUSIC = 1; + $HAVEVIDEOS = 0; # If we ever support "maybe" this is a maybe + } + +# System is unix-like + +# Nobody users on all unix systems generally don't have home directories +} elsif ( getpwuid($<) eq 'nobody' ) { + $HAVEHOME = 0; + $HAVEDESKTOP = 0; + $HAVEMUSIC = 0; + $HAVEPICTURES = 0; + $HAVEVIDEOS = 0; + $HAVEOTHERS = 0; + +} elsif ( + $^O eq 'darwin' +) { + # "Unixes with proper desktops" special cases + if ( $ENV{AUTOMATED_TESTING} ) { + # Automated testers on Mac (notably BINGOS) will often have + # super stripped down testing users. + $HAVEHOME = 1; + $HAVEDESKTOP = 1; + $HAVEMUSIC = 0; + $HAVEPICTURES = 0; + $HAVEVIDEOS = 0; + $HAVEDOCUMENTS = 0; + $HAVEOTHERS = 1; + } elsif ( $< ) { + # Normal user + $HAVEHOME = 1; + $HAVEDESKTOP = 1; + $HAVEMUSIC = 1; + $HAVEPICTURES = 1; + $HAVEVIDEOS = 1; + $HAVEDOCUMENTS = 1; + $HAVEOTHERS = 1; + } else { + # Root can only be relied on to have a home + $HAVEHOME = 1; + $HAVEDESKTOP = 0; + $HAVEMUSIC = 0; + $HAVEPICTURES = 0; + $HAVEVIDEOS = 0; + $HAVEDOCUMENTS = 0; + $HAVEOTHERS = 0; + } + +} elsif ( $File::HomeDir::IMPLEMENTED_BY eq 'File::HomeDir::FreeDesktop' ) { + # On FreeDesktop we can't trust people to have a desktop (annoyingly) + $HAVEHOME = 1; + $HAVEDESKTOP = 0; + $HAVEMUSIC = 0; + $HAVEVIDEOS = 0; + $HAVEPICTURES = 0; + $HAVEDOCUMENTS = 0; + $HAVEOTHERS = 0; + +} else { + # Default to traditional Unix + $HAVEHOME = 1; + $HAVEDESKTOP = 1; + $HAVEMUSIC = 1; + $HAVEPICTURES = 1; + $HAVEVIDEOS = 1; + $HAVEDOCUMENTS = 1; + $HAVEOTHERS = 1; +} + +plan tests => 39; + + + + + +##################################################################### +# Test invalid uses + +eval { + home(undef); +}; +like( $@, qr{Can\'t use undef as a username}, 'home(undef)' ); + + + + + +##################################################################### +# API Test + +# Check the methods all exist +foreach ( qw{ home desktop documents music pictures videos data } ) { + can_ok( 'File::HomeDir', "my_$_" ); + can_ok( 'File::HomeDir', "users_$_" ); +} + + + + + +##################################################################### +# Main Tests + +# Find this user's homedir +my $home = home(); +if ( $HAVEHOME ) { + ok( !!($home and is_dir $home), 'Found our home directory' ); +} else { + is( $home, undef, 'Confirmed no home directory' ); +} + +# this call is not tested: +# File::HomeDir->home + +# Find this user's home explicitly +my $my_home = File::HomeDir->my_home; +if ( $HAVEHOME ) { + ok( !!($home and is_dir $home), 'Found our home directory' ); +} else { + is( $home, undef, 'Confirmed no home directory' ); +} + +# check that $ENV{HOME} is honored if set +{ + local $ENV{HOME} = rel2abs('.'); + is( File::HomeDir->my_home(), $ENV{HOME}, "my_home() returns $ENV{HOME}" ); +} + +my $my_home2 = File::HomeDir::my_home(); +if ( $HAVEHOME ) { + ok( !!($my_home2 and is_dir $my_home2), 'Found our home directory' ); +} else { + is( $home, undef, 'No home directory, as expected' ); +} +is( $home, $my_home2, 'Different APIs give same results' ); + +# shall we test using -w if the home directory is writable ? + +# Find this user's documents +SKIP: { + my $my_documents = File::HomeDir->my_documents; + my $my_documents2 = File::HomeDir::my_documents(); + is( $my_documents, $my_documents2, 'Different APIs give the same results' ); + + skip("Cannot assume existance of documents", 2) unless $HAVEDOCUMENTS; + ok( !!($my_documents and is_dir $my_documents), 'Found our documents directory' ); + ok( !!($my_documents2 and $my_documents2), 'Found our documents directory' ); +} + +# Find this user's pictures directory +SKIP: { + skip("Cannot assume existance of pictures", 3) unless $HAVEPICTURES; + my $my_pictures = File::HomeDir->my_pictures; + my $my_pictures2 = File::HomeDir::my_pictures(); + is( $my_pictures, $my_pictures2, 'Different APIs give the same results' ); + ok( !!($my_pictures and is_dir $my_pictures), 'Our pictures directory exists' ); + ok( !!($my_pictures2 and is_dir $my_pictures2), 'Our pictures directory exists' ); +} + +# Find this user's music directory +SKIP: { + skip("Cannot assume existance of music", 3) unless $HAVEMUSIC; + my $my_music = File::HomeDir->my_music; + my $my_music2 = File::HomeDir::my_music(); + is( $my_music, $my_music2, 'Different APIs give the same results' ); + ok( !!($my_music and is_dir $my_music), 'Our music directory exists' ); + ok( !!($my_music2 and is_dir $my_music2), 'Our music directory exists' ); +} + +# Find this user's video directory +SKIP: { + skip("Cannot assume existance of videos", 3) unless $HAVEVIDEOS; + my $my_videos = File::HomeDir->my_videos; + my $my_videos2 = File::HomeDir::my_videos(); + is( $my_videos, $my_videos2, 'Different APIs give the same results' ); + ok( !!($my_videos and is_dir $my_videos), 'Our videos directory exists' ); + ok( !!($my_videos2 and is_dir $my_videos2), 'Our videos directory exists' ); +} + +# Desktop cannot be assumed in all environments +SKIP: { + skip("Cannot assume existance of desktop", 3 ) unless $HAVEDESKTOP; + + # Find this user's desktop data + my $my_desktop = File::HomeDir->my_desktop; + my $my_desktop2 = File::HomeDir::my_desktop(); + is( $my_desktop, $my_desktop2, 'Different APIs give the same results' ); + ok( !!($my_desktop and is_dir $my_desktop), 'Our desktop directory exists' ); + ok( !!($my_desktop2 and is_dir $my_desktop2), 'Our desktop directory exists' ); +} + +# Find this user's local data +SKIP: { + skip("Cannot assume existance of application data", 3) unless $HAVEOTHERS; + my $my_data = File::HomeDir->my_data; + my $my_data2 = File::HomeDir::my_data(); + is( $my_data, $my_data2, 'Different APIs give the same results' ); + ok( !!($my_data and is_dir $my_data), 'Found our local data directory' ); + ok( !!($my_data2 and is_dir $my_data2), 'Found our local data directory' ); +} + +# Shall we check name space pollution by testing functions in main before +# and after calling use ? + +# On platforms other than windows, find root's homedir +SKIP: { + if ( $^O eq 'MSWin32' or $^O eq 'darwin') { + skip("Skipping root test on $^O", 1 ); + } + + # Determine root + my $root = getpwuid(0); + unless ( $root ) { + skip("Skipping, can't determine root", 1 ); + } + + # Get root's homedir + my $root_home1 = home($root); + ok( !!($root_home1 and is_dir $root_home1), "Found root's home directory" ); +} diff --git a/t/10_test.t b/t/10_test.t new file mode 100644 index 0000000..fc38c30 --- /dev/null +++ b/t/10_test.t @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +# Testing for the test driver + +use strict; +BEGIN { + $| = 1; + $^W = 1; +} +use File::Spec::Functions ':ALL'; +use Test::More tests => 30; +use File::HomeDir::Test; +use File::HomeDir; + +# Is the test driver enabled? +is( $File::HomeDir::Test::ENABLED, 1, 'File::HomeDir::Test is enabled' ); +is( $File::HomeDir::IMPLEMENTED_BY, 'File::HomeDir::Test', 'IMPLEMENTED_BY is correct' ); + +# Was everything hijacked correctly? +foreach my $method ( qw{ + my_home + my_desktop + my_documents + my_data + my_music + my_pictures + my_videos +} ) { + my $dir = File::HomeDir->$method(); + ok( $dir, "$method: Got a directory" ); + ok( -d $dir, "$method: Directory exists at $dir" ); + ok( -r $dir, "$method: Directory is readable" ); + ok( -w $dir, "$method: Directory is writeable" ); +} diff --git a/t/11_darwin.t b/t/11_darwin.t new file mode 100644 index 0000000..4318fcc --- /dev/null +++ b/t/11_darwin.t @@ -0,0 +1,86 @@ +#!/usr/bin/perl + +use strict; +BEGIN { + $| = 1; + $^W = 1; +} + +use Test::More; +use File::HomeDir; + +if ( $File::HomeDir::IMPLEMENTED_BY->isa('File::HomeDir::Darwin') ) { + # Force pure perl since it should work everywhere + $File::HomeDir::IMPLEMENTED_BY = 'File::HomeDir::Darwin'; + plan( tests => 9 ); +} else { + plan( skip_all => "Not running on Darwin" ); + exit(0); +} + +SKIP: { + my $user; + foreach ( 0 .. 9 ) { + my $temp = sprintf 'fubar%04d', rand(10000); + getpwnam $temp and next; + $user = $temp; + last; + } + $user or skip("Unable to find non-existent user", 1); + $@ = undef; + my $home = eval {File::HomeDir->users_home($user)}; + $@ and skip("Unable to execute File::HomeDir->users_home('$user')", 1); + ok (!defined $home, "Home of non-existent user should be undef"); +} + +SCOPE: { + # Reality Check + my $music = File::HomeDir->my_music; + my $videos = File::HomeDir->my_videos; + my $pictures = File::HomeDir->my_pictures; + my $data = File::HomeDir->my_data; + SKIP: { + skip( "No music directory", 1 ) unless defined $music; + like( $music, qr/Music/ ); + } + SKIP: { + skip( "Have music directory", 1 ) if defined $music; + is_deeply( + [ File::HomeDir->my_music ], [ undef ], + "Returns undef in list context", + ) + } + SKIP: { + skip( "No videos directory", 1 ) unless defined $videos; + like( $videos, qr/Movies/ ); + } + SKIP: { + skip( "Have videos directory", 1 ) if defined $videos; + is_deeply( + [ File::HomeDir->my_videos ], [ undef ], + "Returns undef in list context", + ) + } + SKIP: { + skip( "No pictures directory", 1 ) unless defined $pictures; + like( $pictures, qr/Pictures/ ); + } + SKIP: { + skip( "Have pictures directory", 1 ) if defined $pictures; + is_deeply( + [ File::HomeDir->my_pictures ], [ undef ], + "Returns undef in list context", + ) + } + SKIP: { + skip( "No application support directory", 1 ) unless defined $data; + like( $data, qr/Application Support/ ); + } + SKIP: { + skip( "Have data directory", 1 ) if defined $data; + is_deeply( + [ File::HomeDir->my_data ], [ undef ], + "Returns undef in list context", + ) + } +} diff --git a/t/12_darwin_carbon.t b/t/12_darwin_carbon.t new file mode 100644 index 0000000..2ef80e1 --- /dev/null +++ b/t/12_darwin_carbon.t @@ -0,0 +1,62 @@ +#!/usr/bin/perl + +use strict; +BEGIN { + $| = 1; + $^W = 1; +} + +use Test::More; +use File::HomeDir; + +if ( $File::HomeDir::IMPLEMENTED_BY->isa('File::HomeDir::Darwin::Carbon') ) { + plan( tests => 5 ); +} else { + plan( skip_all => "Not running on 32-bit Darwin" ); + exit(0); +} + +SKIP: { + my $user; + foreach (0 .. 9) { + my $temp = sprintf 'fubar%04d', rand(10000); + getpwnam $temp and next; + $user = $temp; + last; + } + $user or skip("Unable to find non-existent user", 1); + $@ = undef; + my $home = eval {File::HomeDir->users_home($user)}; + $@ and skip("Unable to execute File::HomeDir->users_home('$user')"); + ok (!defined $home, "Home of non-existent user should be undef"); +} + +# CPAN Testers results suggest we can't reasonably assume these directories +# will always exist +SKIP: { + my $dir = File::HomeDir->my_music; + unless ( defined $dir ) { + skip( "Testing user does not have a Music directory", 1 ); + } + like( $dir, qr/Music/ ); +} +SKIP: { + my $dir = File::HomeDir->my_videos; + unless ( defined $dir ) { + skip( "Testing user does not have a Movies directory", 1 ); + } + like( $dir, qr/Movies/ ); +} +SKIP: { + my $dir = File::HomeDir->my_pictures; + unless ( defined $dir ) { + skip( "Testing user does not have a Pictures directory", 1 ); + } + like( $dir, qr/Pictures/ ); +} + +SKIP: { + my $data = File::HomeDir->my_data; + skip( "No application support directory", 1 ) unless defined $data; + like( $data, qr/Application Support/ ); +} diff --git a/t/13_darwin_cocoa.t b/t/13_darwin_cocoa.t new file mode 100644 index 0000000..a22f9aa --- /dev/null +++ b/t/13_darwin_cocoa.t @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +use strict; +BEGIN { + $| = 1; + $^W = 1; +} + +use Test::More; +use File::HomeDir; + +if ( + $File::HomeDir::IMPLEMENTED_BY->isa('File::HomeDir::Darwin') + and + eval "require Mac::SystemDirectory; 1" + ) { + # Force Cocoa if you have Mac::SystemDirectory + require File::HomeDir::Darwin::Cocoa; + $File::HomeDir::IMPLEMENTED_BY = 'File::HomeDir::Darwin::Cocoa'; + plan( tests => 5 ); +} else { + plan( skip_all => "Not running on Darwin with Cocoa API using Mac::SystemDirectory" ); + exit(0); +} + +SKIP: { + my $user; + foreach ( 0 .. 9 ) { + my $temp = sprintf 'fubar%04d', rand(10000); + getpwnam $temp and next; + $user = $temp; + last; + } + $user or skip("Unable to find non-existent user", 1); + $@ = undef; + my $home = eval { File::HomeDir->users_home($user) }; + $@ and skip("Unable to execute File::HomeDir->users_home('$user')", 1); + ok (!defined $home, "Home of non-existent user should be undef"); +} + +SCOPE: { + # Reality Check + my $music = File::HomeDir->my_music; + my $video = File::HomeDir->my_videos; + my $pictures = File::HomeDir->my_pictures; + SKIP: { + skip( "No music directory", 1 ) unless defined $music; + like( File::HomeDir->my_music, qr/Music/ ); + } + SKIP: { + skip( "No videos directory", 1 ) unless defined $video; + like( File::HomeDir->my_videos, qr/Movies/ ); + } + SKIP: { + skip( "No pictures directory", 1 ) unless defined $pictures; + like( File::HomeDir->my_pictures, qr/Pictures/ ); + } + + SKIP: { + my $data = File::HomeDir->my_data; + skip( "No application support directory", 1 ) unless defined $data; + like( $data, qr/Application Support/ ); + } +} |