summaryrefslogtreecommitdiff
path: root/lib/test/test-scanner.pl
blob: 4848ab427e14e6b186ca1fca089b64f0d25ffa32 (plain)
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
#!/usr/bin/perl -w

# test-scanner.pl - test script for the libsensors config-file scanner
# Copyright (C) 2006 Mark M. Hoffman <mhoffman@lightlink.com>

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' },
);

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

$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;
}