# Copyright (C) 2006 Esteban Manchado Velázquez # # 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; either version 2 # of the License, or (at your option) any later version. # # 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # USA. =head1 DESCRIPTION C is a C class to inherit from, that has convenience methods for program functional testing. =cut package DpkgTest::FunctionalTest; use base qw(AbstractDpkgTestCase); =item assert_files_equal($cmd_line, $stdin, $expected_stdout_path, $expected_stderr_path) Compares if the standard output and standard error for the given command line, with the given input, are the expected ones. =item assert_output($cmd_line, %options) Compares if the standard output and standard error for the given command line, with the given input, are the expected ones. Valid options are: C, C and C. =cut sub assert_files_equal { my ($self, $file1, $file2, $comment) = @_; my $diff_output = `diff $file1 $file2 2>&1`; $self->assert_equals("", $diff_output, ($comment || "Files '$file1' and '$file2' equalness"). "\nDiff output was:\n$diff_output\n"); } sub assert_output { my ($self, $cmd_line, %options) = @_; my @valid_options = qw(stdin expected_stdout_file expected_stderr_file); foreach my $key (keys %options) { unless (grep { $_ eq $key } @valid_options) { die "Unknown option: '$key'\n"; } } if (!exists $options{expected_stdout_file} and !exists $options{expected_stderr_file}) { die "I need either expected_stdout_file or expected_stderr_file\n"; } my ($input_file, $output_file, $err_file) = ("stdin.$$", "cmd_out.$$", "err_out.$$"); my $input_option = ""; if (exists $options{stdin}) { open F, ">$input_file" or die "Can't open $input_file for writing"; print F $options{stdin}; close F; $input_option = "<$input_file"; } eval { system "$cmd_line $input_option >$output_file 2>$err_file"; }; if (exists $options{expected_stdout_file}) { $self->assert_files_equal($options{expected_stdout_file}, $output_file, "Check output for $cmd_line"); } if (exists $options{expected_stderr_file}) { $self->assert_files_equal($options{expected_stderr_file}, $err_file, "Check error output for $cmd_line"); } unlink $output_file, $err_file; } 1;