| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # This script attempts to reproduce: |
| 4 | # https://rt.cpan.org/Ticket/Display.html?id=36584 |
| 5 | |
| 6 | # Written by Shlomi Fish. |
| 7 | # This file is licensed under the MIT/Expat License. |
| 8 | |
| 9 | use strict; |
| 10 | use warnings; |
| 11 | |
| 12 | use Test::More tests => 7; |
| 13 | |
| 14 | use lib "./t/lib"; |
| 15 | use Config::IniFiles; |
| 16 | use File::Spec; |
| 17 | use Config::IniFiles::Slurp qw( slurp ); |
| 18 | |
| 19 | use File::Temp qw(tempdir); |
| 20 | |
| 21 | { |
| 22 | my $dir_name = tempdir( CLEANUP => 1 ); |
| 23 | my $filename = File::Spec->catfile( $dir_name, "foo.ini" ); |
| 24 | { |
| 25 | open my $fh, '>', $filename; |
| 26 | print {$fh} <<'EOF'; |
| 27 | |
| 28 | ; This is a malformed ini file with a key/value outside a section |
| 29 | |
| 30 | wrong = wronger |
| 31 | |
| 32 | [section] |
| 33 | |
| 34 | right = more right |
| 35 | |
| 36 | EOF |
| 37 | close($fh); |
| 38 | } |
| 39 | |
| 40 | my $ini = Config::IniFiles->new( -file => $filename ); |
| 41 | |
| 42 | # TEST |
| 43 | ok( !defined($ini), "Ini was not initialised" ); |
| 44 | |
| 45 | # TEST |
| 46 | is( scalar(@Config::IniFiles::errors), 1, "There is one error." ); |
| 47 | |
| 48 | # TEST |
| 49 | like( |
| 50 | $Config::IniFiles::errors[0], |
| 51 | qr/parameter found outside a section/, |
| 52 | "Error was correct - 'parameter found outside a section'", |
| 53 | ); |
| 54 | |
| 55 | $ini = Config::IniFiles->new( -file => $filename, -fallback => 'GENERAL' ); |
| 56 | |
| 57 | # TEST |
| 58 | ok( defined($ini), "(-fallback) Ini was initialised" ); |
| 59 | |
| 60 | # TEST |
| 61 | ok( $ini->SectionExists('GENERAL'), "(-fallback) Fallback section exists" ); |
| 62 | |
| 63 | # TEST |
| 64 | ok( $ini->exists( 'GENERAL', 'wrong' ), |
| 65 | "(-fallback) Fallback section catches parameter" ); |
| 66 | |
| 67 | # TEST |
| 68 | my $newfilename = File::Spec->catfile( $dir_name, "new.ini" ); |
| 69 | $ini->WriteConfig($newfilename); |
| 70 | my $content = slurp($newfilename); |
| 71 | ok( |
| 72 | $content =~ /^wrong/m && $content !~ /^\[GENERAL\]/m, |
| 73 | "(-fallback) Outputting fallback section without section header" |
| 74 | ); |
| 75 | } |