File: test-scanner.pl

package info (click to toggle)
lm-sensors 1%3A2.10.7-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,324 kB
  • ctags: 10,814
  • sloc: ansic: 63,969; perl: 8,111; sh: 1,823; makefile: 399; lex: 371; yacc: 312; python: 11
file content (100 lines) | stat: -rwxr-xr-x 3,337 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl -w

# test-scanner.pl - test script for the libsensors config-file scanner
# Copyright (C) 2006 Mark M. Hoffman <mhoffman@lightlink.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; version 2 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

require 5.004;

use strict;
use Test::More;
use Test::Cmd;

my ($scenario, $test);
my @scenarios = (
	{ base => 'empty', status => 0,
		desc => 'empty file' },
	{ base => 'comment', status => 0,
		desc => 'one comment line' },
	{ base => 'comment-without-eol', status => 0,
		desc => 'one comment line, without a trailing newline' },
	{ base => 'keywords', status => 0,
		desc => 'keywords with various whitespace variations' },
	{ base => 'non-keywords', status => 0,
		desc => 'various invalid keyword scenarios' },
	{ base => 'names', status => 0,
		desc => 'normal, unquoted names' },
	{ base => 'names-errors', status => 0,
		desc => 'invalid, unquoted names' },
	{ base => 'names-quoted', status => 0,
		desc => 'normal, quoted names' },
	{ base => 'names-quoted-errors', status => 0,
		desc => 'invalid, quoted names' },
);

plan tests => ($#scenarios + 1) * 3;

chomp(my $valgrind = `which valgrind 2>/dev/null`);

if ($valgrind) {
	$test = Test::Cmd->new(prog => "$valgrind --tool=memcheck --show-reachable=yes --leak-check=full --quiet ./test-scanner", workdir => '');
} else {
	diag("Couldn't find valgrind(1), running tests without it...");
	$test = Test::Cmd->new(prog => "test-scanner", workdir => '');
}

foreach $scenario (@scenarios) {
	my ($filename, @stdin, @stdout, @expout, @stderr, @experr, @diff);

	$filename = $scenario->{"base"} . ".conf";
	open INPUT, "< $filename" or die "Cannot open $filename: $!";
	@stdin = <INPUT>;
	close INPUT or die "Cannot close $filename: $!";

	$filename = $scenario->{"base"} . ".conf.stdout";
	open OUTPUT, "< $filename" or die "Cannot open $filename: $!";
	@expout = <OUTPUT>;
	close OUTPUT or die "Cannot close $filename: $!";

	# if stderr file is not present, assume none is expected
	$filename = $scenario->{"base"} . ".conf.stderr";
	if (open ERROR, "< $filename") {
		@experr = <ERROR>;
		close ERROR or die "Cannot close $filename: $!";
	} else {
		@experr = ();
	}

	$test->string($scenario->{"desc"});
	$test->run(stdin => \@stdin);

	# test return status
	ok($scenario->{"status"} == $?, "status: " . $scenario->{"desc"});

	# force the captured outputs into an array - for some reason, the
	# 'standard invocation' of diff_exact() chokes without this
	@stdout = $test->stdout;
	@stderr = $test->stderr;

	# test stdout
	ok($test->diff_exact(\@stdout, \@expout, \@diff),
		"stdout: " . $scenario->{"desc"}) or print @diff;

	# test stderr
	ok($test->diff_exact(\@stderr, \@experr, \@diff),
		"stderr: " . $scenario->{"desc"}) or print @diff;
}