| 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., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 18 | # |
|---|
| 19 | |
|---|
| 20 | require 5.004; |
|---|
| 21 | |
|---|
| 22 | use strict; |
|---|
| 23 | use Test::More; |
|---|
| 24 | use Test::Cmd; |
|---|
| 25 | |
|---|
| 26 | my ($scenario, $test); |
|---|
| 27 | my @scenarios = ( |
|---|
| 28 | { base => 'empty', status => 0, |
|---|
| 29 | desc => 'empty file' }, |
|---|
| 30 | { base => 'comment', status => 0, |
|---|
| 31 | desc => 'one comment line' }, |
|---|
| 32 | { base => 'comment-without-eol', status => 0, |
|---|
| 33 | desc => 'one comment line, without a trailing newline' }, |
|---|
| 34 | { base => 'keywords', status => 0, |
|---|
| 35 | desc => 'keywords with various whitespace variations' }, |
|---|
| 36 | { base => 'non-keywords', status => 0, |
|---|
| 37 | desc => 'various invalid keyword scenarios' }, |
|---|
| 38 | { base => 'names', status => 0, |
|---|
| 39 | desc => 'normal, unquoted names' }, |
|---|
| 40 | { base => 'names-errors', status => 0, |
|---|
| 41 | desc => 'invalid, unquoted names' }, |
|---|
| 42 | { base => 'names-quoted', status => 0, |
|---|
| 43 | desc => 'normal, quoted names' }, |
|---|
| 44 | { base => 'names-quoted-errors', status => 0, |
|---|
| 45 | desc => 'invalid, quoted names' }, |
|---|
| 46 | ); |
|---|
| 47 | |
|---|
| 48 | plan tests => ($#scenarios + 1) * 3; |
|---|
| 49 | |
|---|
| 50 | chomp(my $valgrind = `which valgrind 2>/dev/null`); |
|---|
| 51 | |
|---|
| 52 | if ($valgrind) { |
|---|
| 53 | $test = Test::Cmd->new(prog => "$valgrind --tool=memcheck --show-reachable=yes --leak-check=full --quiet ./test-scanner", workdir => ''); |
|---|
| 54 | } else { |
|---|
| 55 | diag("Couldn't find valgrind(1), running tests without it..."); |
|---|
| 56 | $test = Test::Cmd->new(prog => "test-scanner", workdir => ''); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | foreach $scenario (@scenarios) { |
|---|
| 60 | my ($filename, @stdin, @stdout, @expout, @stderr, @experr, @diff); |
|---|
| 61 | |
|---|
| 62 | $filename = $scenario->{"base"} . ".conf"; |
|---|
| 63 | open INPUT, "< $filename" or die "Cannot open $filename: $!"; |
|---|
| 64 | @stdin = <INPUT>; |
|---|
| 65 | close INPUT or die "Cannot close $filename: $!"; |
|---|
| 66 | |
|---|
| 67 | $filename = $scenario->{"base"} . ".conf.stdout"; |
|---|
| 68 | open OUTPUT, "< $filename" or die "Cannot open $filename: $!"; |
|---|
| 69 | @expout = <OUTPUT>; |
|---|
| 70 | close OUTPUT or die "Cannot close $filename: $!"; |
|---|
| 71 | |
|---|
| 72 | # if stderr file is not present, assume none is expected |
|---|
| 73 | $filename = $scenario->{"base"} . ".conf.stderr"; |
|---|
| 74 | if (open ERROR, "< $filename") { |
|---|
| 75 | @experr = <ERROR>; |
|---|
| 76 | close ERROR or die "Cannot close $filename: $!"; |
|---|
| 77 | } else { |
|---|
| 78 | @experr = (); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | $test->string($scenario->{"desc"}); |
|---|
| 82 | $test->run(stdin => \@stdin); |
|---|
| 83 | |
|---|
| 84 | # test return status |
|---|
| 85 | ok($scenario->{"status"} == $?, "status: " . $scenario->{"desc"}); |
|---|
| 86 | |
|---|
| 87 | # force the captured outputs into an array - for some reason, the |
|---|
| 88 | # 'standard invocation' of diff_exact() chokes without this |
|---|
| 89 | @stdout = $test->stdout; |
|---|
| 90 | @stderr = $test->stderr; |
|---|
| 91 | |
|---|
| 92 | # test stdout |
|---|
| 93 | ok($test->diff_exact(\@stdout, \@expout, \@diff), |
|---|
| 94 | "stdout: " . $scenario->{"desc"}) or print @diff; |
|---|
| 95 | |
|---|
| 96 | # test stderr |
|---|
| 97 | ok($test->diff_exact(\@stderr, \@experr, \@diff), |
|---|
| 98 | "stderr: " . $scenario->{"desc"}) or print @diff; |
|---|
| 99 | } |
|---|
| 100 | |
|---|