| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
# Should be 15 |
| 6 |
use Test::More tests => 15; |
| 7 |
|
| 8 |
use lib "./t/lib"; |
| 9 |
|
| 10 |
use Config::IniFiles::TestPaths; |
| 11 |
|
| 12 |
use IO::File; |
| 13 |
|
| 14 |
BEGIN |
| 15 |
{ |
| 16 |
# TEST |
| 17 |
use_ok('Config::IniFiles'); |
| 18 |
} |
| 19 |
|
| 20 |
my $ini; |
| 21 |
|
| 22 |
# a simple filehandle, such as STDIN |
| 23 |
#** If anyone can come up with a test for STDIN that would be great |
| 24 |
# The following could be run in a separate file with data piped |
| 25 |
# in. e.g. ok( !system( "$^X stdin.pl < test.ini" ) ); |
| 26 |
# But it's only good on platforms that support redirection. |
| 27 |
# use strict; |
| 28 |
# use Config::IniFiles; |
| 29 |
# my $ini = Config::IniFiles->new(-file => STDIN); |
| 30 |
# exit $ini ? 0; 1 |
| 31 |
|
| 32 |
local *CONFIG; |
| 33 |
|
| 34 |
# TEST |
| 35 |
# a filehandle glob, such as *CONFIG |
| 36 |
if ( open( CONFIG, "<", t_file("test.ini") ) ) |
| 37 |
{ |
| 38 |
$ini = Config::IniFiles->new( -file => *CONFIG ); |
| 39 |
ok( $ini, q{$ini was initialized} ); |
| 40 |
close CONFIG; |
| 41 |
} |
| 42 |
else |
| 43 |
{ |
| 44 |
ok( 0, "Could not open file" ); |
| 45 |
} |
| 46 |
|
| 47 |
# TEST |
| 48 |
# a reference to a glob, such as \*CONFIG |
| 49 |
if ( open( CONFIG, "<", t_file("test.ini") ) ) |
| 50 |
{ |
| 51 |
$ini = Config::IniFiles->new( -file => \*CONFIG ); |
| 52 |
ok( $ini, q{$ini was initialized with a reference to a glob.} ); |
| 53 |
close CONFIG; |
| 54 |
} |
| 55 |
else |
| 56 |
{ |
| 57 |
ok( 0, q{could not open test.ini} ); |
| 58 |
} |
| 59 |
|
| 60 |
# an IO::File object |
| 61 |
# TEST |
| 62 |
if ( my $fh = IO::File->new( t_file("test.ini") ) ) |
| 63 |
{ |
| 64 |
$ini = Config::IniFiles->new( -file => $fh ); |
| 65 |
ok( $ini, q{$ini was initialized with an IO::File reference.} ); |
| 66 |
$fh->close; |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
ok( 0, "Could not open file" ); |
| 71 |
} # endif |
| 72 |
|
| 73 |
# TEST |
| 74 |
# Reread on an open handle |
| 75 |
if ( open( CONFIG, "<", t_file("test.ini") ) ) |
| 76 |
{ |
| 77 |
$ini = Config::IniFiles->new( -file => \*CONFIG ); |
| 78 |
ok( ( $ini && $ini->ReadConfig() ), qq{ReadConfig() was successful} ); |
| 79 |
close CONFIG; |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
ok( 0, "Could not open file" ); |
| 84 |
} |
| 85 |
|
| 86 |
use File::Temp qw(tempdir); |
| 87 |
use File::Spec (); |
| 88 |
|
| 89 |
my $dir_name = tempdir( CLEANUP => 1 ); |
| 90 |
my $test01_fn = File::Spec->catfile( $dir_name, 'test01.ini' ); |
| 91 |
|
| 92 |
# TEST |
| 93 |
if ( open( CONFIG, "<", t_file("test.ini") ) ) |
| 94 |
{ |
| 95 |
$ini = Config::IniFiles->new( -file => \*CONFIG ); |
| 96 |
$ini->SetFileName($test01_fn); |
| 97 |
$ini->RewriteConfig(); |
| 98 |
close CONFIG; |
| 99 |
|
| 100 |
# Now test opening and re-write to the same handle |
| 101 |
if ( !open( CONFIG, "+<", $test01_fn ) ) |
| 102 |
{ |
| 103 |
die "Could not open " . $test01_fn . " for read/write"; |
| 104 |
} |
| 105 |
$ini = Config::IniFiles->new( -file => \*CONFIG ); |
| 106 |
my $badname = scalar( \*CONFIG ); |
| 107 |
|
| 108 |
# Have to use open/close because -e seems to be always true! |
| 109 |
ok( |
| 110 |
$ini && $ini->RewriteConfig() && !( open( I, $badname ) && close(I) ), |
| 111 |
qq{Write to a new file name and write to it}, |
| 112 |
); |
| 113 |
close CONFIG; |
| 114 |
|
| 115 |
# In case it failed, remove the file |
| 116 |
# (old behavior was to write to a file whose filename is the scalar value of the handle!) |
| 117 |
unlink $badname; |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
ok( 0, "Could not open file" ); |
| 122 |
} # end if |
| 123 |
|
| 124 |
# the pathname of a file |
| 125 |
$ini = Config::IniFiles->new( -file => t_file("test.ini") ); |
| 126 |
|
| 127 |
# TEST |
| 128 |
ok( $ini, q{Opening with -file works} ); |
| 129 |
|
| 130 |
# A non-INI file should fail, but not throw warnings |
| 131 |
local $@ = ''; |
| 132 |
my $ERRORS = ''; |
| 133 |
local $SIG{__WARN__} = sub { $ERRORS .= $_[0] }; |
| 134 |
eval { $ini = Config::IniFiles->new( -file => t_file("00load.t") ) }; |
| 135 |
|
| 136 |
# TEST |
| 137 |
ok( !$@ && !$ERRORS && !defined($ini), |
| 138 |
"A non-INI file should fail, but not throw errors" ); |
| 139 |
|
| 140 |
$@ = ''; |
| 141 |
eval { $ini = Config::IniFiles->new( -file => \*DATA ) }; |
| 142 |
|
| 143 |
# TEST |
| 144 |
ok( !$@ && defined($ini), "Read in the DATA file without errors" ); |
| 145 |
|
| 146 |
# Try a file with utf-8 encoding (has a Byte-Order-Mark at the start) |
| 147 |
# TEST |
| 148 |
$ini = Config::IniFiles->new( -file => t_file("en.ini") ); |
| 149 |
ok( $ini, |
| 150 |
"Try a file with utf-8 encoding (has a Byte-Order-Mark at the start)" ); |
| 151 |
|
| 152 |
# Create a new INI file, and set the name using SetFileName |
| 153 |
$ini = Config::IniFiles->new(); |
| 154 |
my $filename = $ini->GetFileName; |
| 155 |
|
| 156 |
# TEST |
| 157 |
ok( ( !defined($filename) ), "Not defined filename on fresh Config::IniFiles" ); |
| 158 |
|
| 159 |
# TEST |
| 160 |
$ini->SetFileName( t_file("test9_name.ini") ); |
| 161 |
$filename = $ini->GetFileName; |
| 162 |
is( $filename, t_file("test9_name.ini"), "Check GetFileName method", ); |
| 163 |
|
| 164 |
$@ = ''; |
| 165 |
eval { $ini = Config::IniFiles->new( -file => t_file('blank.ini') ); }; |
| 166 |
|
| 167 |
# TEST |
| 168 |
ok( |
| 169 |
( !$@ && !defined($ini) ), |
| 170 |
"Make sure that no warnings are thrown for an empty file", |
| 171 |
); |
| 172 |
|
| 173 |
$@ = ''; |
| 174 |
eval { |
| 175 |
$ini = |
| 176 |
Config::IniFiles->new( -file => t_file('blank.ini'), -allowempty => 1 ); |
| 177 |
}; |
| 178 |
|
| 179 |
# TEST |
| 180 |
ok( |
| 181 |
( !$@ && defined($ini) ), |
| 182 |
"Empty files should cause no rejection when appropriate switch set", |
| 183 |
); |
| 184 |
|
| 185 |
$@ = ''; |
| 186 |
eval { $ini = Config::IniFiles->new( -file => t_file('bad.ini') ); }; |
| 187 |
|
| 188 |
# TEST |
| 189 |
ok( |
| 190 |
( !$@ && !defined($ini) && @Config::IniFiles::errors ), |
| 191 |
"A malformed file should throw an error message", |
| 192 |
); |
| 193 |
|
| 194 |
__END__ |
| 195 |
; File that has comments in the first line |
| 196 |
; Comments are marked with ';'. |
| 197 |
; This should not fail when checking if the file is valid |
| 198 |
[section] |
| 199 |
parameter=value |
| 200 |
|
| 201 |
|