20-config.t (3257B)
1 #!/usr/bin/env perl 2 3 use 5.16.0; 4 use strict; 5 use warnings FATAL => 'all'; 6 use Test::More; 7 use FindBin '$RealBin'; 8 use lib $RealBin; 9 use Test::Sbotools qw/ sboconfig /; 10 11 plan tests => 20; 12 13 # 1-7: test invalid arguments 14 sboconfig '-c', 'invalid', { exit => 1, expected => "You have provided an invalid parameter for -c\n" }; 15 sboconfig '-d', 'invalid', { exit => 1, expected => "You have provided an invalid parameter for -d\n" }; 16 sboconfig '-j', 'invalid', { exit => 1, expected => "You have provided an invalid parameter for -j\n" }; 17 sboconfig '-p', 'invalid', { exit => 1, expected => "You have provided an invalid parameter for -p\n" }; 18 sboconfig '-s', 'invalid', { exit => 1, expected => "You have provided an invalid parameter for -s\n" }; 19 sboconfig '-o', 'invalid', { exit => 1, expected => "You have provided an invalid parameter for -o\n" }; 20 sboconfig '-V', 'invalid', { exit => 1, expected => "You have provided an invalid parameter for -V\n" }; 21 22 # 8-9: move original dir away and run tests on the config file 23 SKIP: { 24 skip "Only run this test under Travis CI", 13 unless $ENV{TRAVIS}; 25 26 my $dir = '/etc/sbotools'; 27 rename $dir, "$dir.moved"; 28 system 'touch', $dir; 29 30 sboconfig '-V', '14.1', { exit => 1, expected => qr"\QUnable to create $dir. Exiting." }; 31 32 unlink $dir; 33 34 sboconfig '-V', '14.1', { test => 0 }; 35 ok(-d $dir, "$dir created correctly."); 36 37 unlink "$dir/sbotools.conf"; 38 39 # set up sbotools.conf 40 open my $fh, '>', "$dir/sbotools.conf" or do { 41 my $err = $!; 42 fail "Writing sbotools.conf"; 43 diag "Could not open $dir/sbotools.conf for writing: $err"; 44 skip 10, "Could not write sbotools.conf"; 45 goto CLEANUP; 46 }; 47 48 say $fh "#comment=foo"; 49 say $fh "#comment=bar"; 50 say $fh ""; 51 say $fh ""; 52 say $fh "FOO=FOO"; 53 say $fh "FOO=BAR"; 54 say $fh ""; 55 say $fh "SLACKWARE_VERSION=14.0"; 56 say $fh "SLACKWARE_VERSION=14.2"; 57 58 close $fh; 59 60 sboconfig '-V', '14.1', { test => 0 }; 61 62 open my $cfh, '<', "$dir/sbotools.conf" or do { 63 my $err = $!; 64 fail "Reading sbotools.conf"; 65 diag "Could not open $dir/sbotools.conf for reading: $err"; 66 skip 10, "Could not read sbotools.conf"; 67 goto CLEANUP; 68 }; 69 70 chomp(my @lines = readline $cfh); 71 72 close $cfh; 73 74 is($lines[0], "#comment=foo", "First comment preserved."); 75 is($lines[1], "#comment=bar", "Second comment preserved."); 76 is($lines[2], "", "First empty line preserved."); 77 is($lines[3], "", "Second empty line preserved."); 78 is($lines[4], "FOO=FOO", "First setting preserved."); 79 is($lines[5], "", "Second setting correctly collapsed. Third empty line preserved."); 80 is($lines[6], "SLACKWARE_VERSION=14.1", "SLACKWARE_VERSION correctly set."); 81 is($lines[7], undef, "SLACKWARE_VERSION correctly collapsed."); 82 83 sboconfig qw[ -V 14.0 -j 2 ], { test => 0 }; 84 85 open $cfh, '<', "$dir/sbotools.conf" or do { 86 my $err = $!; 87 fail "Reading sbotools.conf"; 88 diag "Could not open $dir/sbotools.conf for reading: $err"; 89 skip 3, "Could not read sbotools.conf"; 90 goto CLEANUP; 91 }; 92 93 chomp(@lines = readline $cfh); 94 95 close $cfh; 96 97 is($lines[6], "SLACKWARE_VERSION=14.0", "SLACKWARE_VERSION correctly set again."); 98 is($lines[7], "JOBS=2", "JOBS correctly set."); 99 is($lines[8], undef, "Nothing new collapsed."); 100 101 CLEANUP: 102 unlink "$dir/sbotools.conf"; 103 rmdir $dir; 104 rename "$dir.moved", $dir; 105 }