| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | # test-scanner.pl - test script for the libsensors config-file scanner |
|---|
| 4 | # Copyright (C) 2006 Mark M. Hoffman <mhoffman@lightlink.com> |
|---|
| 5 | # |
|---|
| 6 | # This program is free software; you can redistribute it and/or modify |
|---|
| 7 | # it under the terms of the GNU General Public License as published by |
|---|
| 8 | # the Free Software Foundation; version 2 of the License. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|---|
| 18 | # MA 02110-1301 USA. |
|---|
| 19 | # |
|---|
| 20 | |
|---|
| 21 | require 5.004; |
|---|
| 22 | |
|---|
| 23 | use strict; |
|---|
| 24 | use Test::More; |
|---|
| 25 | use Test::Cmd; |
|---|
| 26 | |
|---|
| 27 | my ($scenario, $test); |
|---|
| 28 | my @scenarios = ( |
|---|
| 29 | { base => 'empty', status => 0, |
|---|
| 30 | desc => 'empty file' }, |
|---|
| 31 | { base => 'comment', status => 0, |
|---|
| 32 | desc => 'one comment line' }, |
|---|
| 33 | { base => 'comment-without-eol', status => 0, |
|---|
| 34 | desc => 'one comment line, without a trailing newline' }, |
|---|
| 35 | { base => 'keywords', status => 0, |
|---|
| 36 | desc => 'keywords with various whitespace variations' }, |
|---|
| 37 | { base => 'non-keywords', status => 0, |
|---|
| 38 | desc => 'various invalid keyword scenarios' }, |
|---|
| 39 | { base => 'names', status => 0, |
|---|
| 40 | desc => 'normal, unquoted names' }, |
|---|
| 41 | { base => 'names-errors', status => 0, |
|---|
| 42 | desc => 'invalid, unquoted names' }, |
|---|
| 43 | { base => 'names-quoted', status => 0, |
|---|
| 44 | desc => 'normal, quoted names' }, |
|---|
| 45 | { base => 'names-quoted-errors', status => 0, |
|---|
| 46 | desc => 'invalid, quoted names' }, |
|---|
| 47 | ); |
|---|
| 48 | |
|---|
| 49 | plan tests => ($#scenarios + 1) * 3; |
|---|
| 50 | |
|---|
| 51 | chomp(my $valgrind = `which valgrind 2>/dev/null`); |
|---|
| 52 | |
|---|
| 53 | if ($valgrind) { |
|---|
| 54 | $test = Test::Cmd->new(prog => "$valgrind --tool=memcheck --show-reachable=yes --leak-check=full --quiet ./test-scanner", workdir => ''); |
|---|
| 55 | } else { |
|---|
| 56 | diag("Couldn't find valgrind(1), running tests without it..."); |
|---|
| 57 | $test = Test::Cmd->new(prog => "test-scanner", workdir => ''); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | foreach $scenario (@scenarios) { |
|---|
| 61 | my ($filename, @stdin, @stdout, @expout, @stderr, @experr, @diff); |
|---|
| 62 | |
|---|
| 63 | $filename = $scenario->{"base"} . ".conf"; |
|---|
| 64 | open INPUT, "< $filename" or die "Cannot open $filename: $!"; |
|---|
| 65 | @stdin = <INPUT>; |
|---|
| 66 | close INPUT or die "Cannot close $filename: $!"; |
|---|
| 67 | |
|---|
| 68 | $filename = $scenario->{"base"} . ".conf.stdout"; |
|---|
| 69 | open OUTPUT, "< $filename" or die "Cannot open $filename: $!"; |
|---|
| 70 | @expout = <OUTPUT>; |
|---|
| 71 | close OUTPUT or die "Cannot close $filename: $!"; |
|---|
| 72 | |
|---|
| 73 | # if stderr file is not present, assume none is expected |
|---|
| 74 | $filename = $scenario->{"base"} . ".conf.stderr"; |
|---|
| 75 | if (open ERROR, "< $filename") { |
|---|
| 76 | @experr = <ERROR>; |
|---|
| 77 | close ERROR or die "Cannot close $filename: $!"; |
|---|
| 78 | } else { |
|---|
| 79 | @experr = (); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | $test->string($scenario->{"desc"}); |
|---|
| 83 | $test->run(stdin => \@stdin); |
|---|
| 84 | |
|---|
| 85 | # test return status |
|---|
| 86 | ok($scenario->{"status"} == $?, "status: " . $scenario->{"desc"}); |
|---|
| 87 | |
|---|
| 88 | # force the captured outputs into an array - for some reason, the |
|---|
| 89 | # 'standard invocation' of diff_exact() chokes without this |
|---|
| 90 | @stdout = $test->stdout; |
|---|
| 91 | @stderr = $test->stderr; |
|---|
| 92 | |
|---|
| 93 | # test stdout |
|---|
| 94 | ok($test->diff_exact(\@stdout, \@expout, \@diff), |
|---|
| 95 | "stdout: " . $scenario->{"desc"}) or print @diff; |
|---|
| 96 | |
|---|
| 97 | # test stderr |
|---|
| 98 | ok($test->diff_exact(\@stderr, \@experr, \@diff), |
|---|
| 99 | "stderr: " . $scenario->{"desc"}) or print @diff; |
|---|
| 100 | } |
|---|
| 101 | |
|---|