| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # As of version 2.73 the .ini files generated on Microsoft Windows did not |
| 4 | # contain carriage returns (CRs). This is a regression test for that. |
| 5 | |
| 6 | # Thanks to Ben Johnson for reporting this and for a preliminary test |
| 7 | # script. |
| 8 | |
| 9 | use strict; |
| 10 | use warnings; |
| 11 | |
| 12 | use lib "./t/lib"; |
| 13 | use Config::IniFiles::Slurp qw( bin_slurp ); |
| 14 | |
| 15 | use Test::More; |
| 16 | |
| 17 | if ( $^O !~ m/\AMSWin/ ) |
| 18 | { |
| 19 | plan skip_all => 'Test is only relevant for Microsoft Windows'; |
| 20 | } |
| 21 | else |
| 22 | { |
| 23 | plan tests => 1; |
| 24 | } |
| 25 | |
| 26 | use Config::IniFiles; |
| 27 | use File::Spec; |
| 28 | |
| 29 | my $config_filename = |
| 30 | File::Spec->catdir( File::Spec->curdir(), "t", "testConfig.ini" ); |
| 31 | |
| 32 | writeNewUserIni($config_filename); |
| 33 | |
| 34 | for my $s ( 1 .. 4 ) |
| 35 | { |
| 36 | print "s = $s\n"; |
| 37 | for my $p ( 1 .. 4 ) |
| 38 | { |
| 39 | print "p = $p\n"; |
| 40 | writeIni( $config_filename, "Section$s", "Param$p", "Value$p" ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | # TEST |
| 45 | unlike( |
| 46 | scalar( bin_slurp($config_filename) ), |
| 47 | qr/[^\x0D]\x0A/, # \x0D is CR ; \x0A is LF. See "man ascii". |
| 48 | "Checking that all line feeds are preceded by carriage returns", |
| 49 | ); |
| 50 | |
| 51 | sub writeNewUserIni |
| 52 | { |
| 53 | my ($config_fn) = @_; |
| 54 | |
| 55 | open my $fh, '>', $config_fn |
| 56 | or die "Cannot open $config_fn for writing. - $!"; |
| 57 | print {$fh} "[UserConfigFile]\n"; |
| 58 | close($fh); |
| 59 | |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | sub writeIni |
| 64 | { |
| 65 | my ( $userConfig_fn, $section, $param, $value ) = @_; |
| 66 | |
| 67 | my $usrCfg = Config::IniFiles->new( -file => $userConfig_fn ) |
| 68 | or die |
| 69 | "Failed! Could not open $userConfig_fn with error @Config::IniFiles::errors\n"; |
| 70 | |
| 71 | $usrCfg->newval( $section, $param, $value ) |
| 72 | or die |
| 73 | "Could not set newval in writeIni for $section, $param -> $value\n"; |
| 74 | |
| 75 | my $c = 0; |
| 76 | |
| 77 | while ( $c < 6 ) |
| 78 | { |
| 79 | if ( $usrCfg->RewriteConfig() ) |
| 80 | { |
| 81 | $c = 6; |
| 82 | print "Writing [$section] $param -> $value\n"; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | warn "Error: could not write $param=$value to $userConfig_fn\n"; |
| 87 | sleep 1; |
| 88 | $c++; |
| 89 | print "c = $c\n"; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return; |
| 94 | } |