1#!/usr/local/bin/perl
2#
3# Run tv_augment against various input files and check the generated output
4# is as expected.
5#
6# This framework (borrowed from test_tv_imdb.t) tests each type of automatic
7# and user rule for tv_augment (lib/Augment.pm) by comparing the output
8# generated from input data against the expected output for each rule type.
9#
10# -- Nick Morrott, knowledgejunkie@gmail.com, 2016-07-07
11# $Id: test_tv_augment.t,v 1.1 2016/07/12 01:27:46 knowledgejunkie Exp $
12#
13
14use warnings;
15use strict;
16use Getopt::Long;
17use Cwd;
18use File::Temp qw(tempdir);
19use File::Copy;
20use XMLTV::Usage <<END
21$0: test suite for tv_augment
22usage: $0 [--tests-dir DIR] [--cmds-dir DIR] [--verbose]
23END
24;
25
26my $tests_dir = 't/data-tv_augment'; # where to find input XML files
27die "no directory $tests_dir" if not -d $tests_dir;
28my $cmds_dir = 'blib/script'; # directory tv_augment lives in
29die "no directory $cmds_dir" if not -d $cmds_dir;
30my $verbose = 0;
31
32GetOptions('tests-dir=s' => \$tests_dir,
33           'cmds-dir=s'  => \$cmds_dir,
34           'verbose'     => \$verbose)
35        or usage(0);
36
37usage(0) if @ARGV;
38
39my $tmpDir = tempdir(CLEANUP => 1);
40# my $tmpDir = tempdir(CLEANUP => 0);
41
42my @inputs = <$tests_dir/*.xml>;
43@inputs = sort (@inputs);
44die "no test cases (*.xml) found in $tests_dir"
45  if not @inputs;
46
47my $numtests = scalar @inputs;
48print "1..$numtests\n";
49
50my $n = 0;
51INPUT:
52foreach my $input (@inputs) {
53    ++$n;
54
55    use File::Basename;
56    my $input_basename = File::Basename::basename($input);
57    my $output="$tmpDir/".$input_basename."-output";
58
59    my $cmd="$cmds_dir/tv_augment --rule $tests_dir/rules/test_tv_augment.rules --config $tests_dir/configs/$input_basename.conf --input $input --output $output --nostats 2>&1";
60    # my $cmd="perl -I blib/lib $cmds_dir/tv_augment --rule $tests_dir/rules/test_tv_augment.rules --config $tests_dir/configs/$input_basename.conf --input $input --output $output --log $tmpDir/$input_basename.log --debug 5 >$tmpDir/$input_basename.debug 2>&1";
61
62    my $r = system($cmd);
63
64    # Check command return status.
65    if ($r) {
66        my ($status, $sig, $core) = ($? >> 8, $? & 127, $? & 128);
67        if ($sig) {
68            die "$cmd killed by signal $sig, aborting";
69        }
70        warn "$cmd failed: $status, $sig, $core\n";
71        print "not ok $n\n";
72        next INPUT;
73    }
74
75    open(FD, "$input-expected") || die "$input-expected:$!";
76    open(OD, "$output") || die "$output:$!";
77    my $line = 0;
78    my $failed = 0;
79    INPUT:
80    while(<FD>) {
81       my $in=$_;
82       $line++;
83
84       # ignore single line XML comments in "expected" data
85       next INPUT if ($in =~ m/\s*<!--/);
86
87       my $out=<OD>;
88       chomp($in);
89       chomp($out);
90
91        if ( $in ne $out ) {
92            warn "$input ($line) failed to match expected ('$in' != '$out')\n";
93            $failed = 1;
94            last INPUT;
95        }
96    }
97    close(FD);
98    close(OD);
99
100    if ($failed) {
101        print "not ok $n\n";
102    }
103    else {
104        print "ok $n\n";
105    }
106}
107
108