You are viewing a plain text version of this content. The canonical link for it is here.
Posted to test-dev@httpd.apache.org by Stas Bekman <st...@stason.org> on 2001/09/02 12:55:35 UTC
new helper module Apache::TestUtil
Here is a new Apache::TestUtil that makes:
- the writing tests that mess with files and dirs much easier.
- it automatically takes care of cleanup for all files and dirs created.
- makes it simpler to write debugable tests: ok $expected eq $received.
After the patch I've attached a new vhost_alias.t that uses it, so you can
see how it's used and how shorter it makes the test. If it's all fine,
I'll commit the changes.
--- /dev/null Sat Apr 14 19:06:21 2001
+++ Apache-Test/lib/Apache/TestUtil.pm Sun Sep 2 18:45:08 2001
@@ -0,0 +1,109 @@
+package Apache::TestUtil;
+
+use strict;
+use warnings FATAL => 'all';
+use File::Find ();
+use Exporter ();
+
+our $VERSION = '0.01';
+our @ISA = qw(Exporter);
+our @EXPORT = qw(t_cmp_str t_cmp_num t_write_file t_open_file t_mkdir
+ t_del_tree);
+
+our %CLEAN = ();
+
+# t_cmp_str($expect,$received,$comment)
+# returns the result of string comparison of $expect and $received
+# first prints all the arguments for debug.
+##################
+sub t_cmp_str{
+ my ($expect,$received,$comment) = @_;
+ print "testing : $comment\n" if defined $comment;
+ print "expected: $expect\n";
+ print "received: $received\n";
+ $expect eq $received;
+}
+
+# t_cmp_num($expect,$received,$comment)
+# returns the result of numerical comparison of $expect and $received
+# first prints all the arguments for debug.
+##################
+sub t_cmp_num{
+ my ($expect,$received,$comment) = @_;
+ print "testing : $comment\n" if defined $comment;
+ print "expected: $expect\n";
+ print "received: $received\n";
+ $expect == $received;
+}
+
+# t_write_file($filename,@lines);
+# the file will be deleted at the end of the tests run
+#################
+sub t_write_file{
+ my $file = shift;
+ open my $fh, ">$file" or die "can't open $file: $!";
+ print STDERR "writing $file\n";
+ print $fh join '', @_ if @_;
+ close $fh;
+ $CLEAN{files}{$file}++;
+}
+
+# t_open_file($filename);
+# open a file for writing and return the open fh
+# the file will be deleted at the end of the tests run
+################
+sub t_open_file{
+ my $file = shift;
+ open my $fh, ">$file" or die "can't open $file: $!";
+ print STDERR "writing $file\n";
+ $CLEAN{files}{$file}++;
+ return $fh;
+}
+
+# t_mkdir($dirname)
+# create a dir
+# the dir will be deleted at the end of the tests run
+############
+sub t_mkdir{
+ my $dir = shift;
+
+ mkdir $dir, 0755 unless -d $dir;
+ $CLEAN{dirs}{$dir}++;
+}
+
+# deletes the whole tree(s) or just file(s)
+###############
+sub t_del_tree{
+ for my $file (@_) {
+ next unless -e $file;
+ File::Find::finddepth(sub {-d $_ ? rmdir : unlink; }, $file);
+ }
+}
+
+
+END{
+
+ # cleanup first files than dirs
+ map { unlink $_ } grep {-e $_ && -f _ } keys %{ $CLEAN{files} };
+ map { t_del_tree($_) } grep {-e $_ && -d _ } keys %{ $CLEAN{dirs} };
+
+}
+
+1;
+__END__
+
+
+=head1 NAME
+
+Apache::TestUtil - Utilities for writing tests
+
+=head1 SYNOPSIS
+
+
+
+=head1 DESCRIPTION
+
+
+
+=cut
+
Here is the new vhost_alias:
use strict;
use warnings FATAL => 'all';
use Apache::Test;
use Apache::TestUtil;
use Apache::TestRequest;
use Apache::TestConfig ();
my $url = '/index.html';
my $cgi_name = "test-cgi.sh";
my $cgi_string = "test cgi for";
my $root = "htdocs/modules/vhost_alias";
my @vh = qw(www.vha-test.com big.server.name.from.heck.org ab.com w-t-f.net);
plan tests => @vh * 2, ['vhost_alias'] && \&have_cgi;
Apache::TestRequest::scheme('http'); #ssl not listening on this vhost
my $config = Apache::TestRequest::test_config();
my $vars = Apache::TestRequest::vars();
local $vars->{port} = $config->port('mod_vhost_alias');
## test environment setup ##
t_mkdir($root);
foreach (@vh) {
my @part = split /\./, $_;
my $d = "$root/";
## create VirtualDocumentRoot htdocs/modules/vhost_alias/%2/%1.4/%-2/%2+
## %2 ##
if ($part[1]) {
$d .= $part[1];
} else {
$d .= "_";
}
t_mkdir($d);
$d .= "/";
## %1.4 ##
if (length($part[0]) < 4) {
$d .= "_";
} else {
$d .= substr($part[0], 3, 1);
}
t_mkdir($d);
$d .= "/";
## %-2 ##
if ($part[@part-2]) {
$d .= $part[@part-2];
} else {
$d .= "_";
}
t_mkdir($d);
$d .= "/";
## %2+ ##
for (my $i = 1;$i < @part;$i++) {
$d .= $part[$i];
$d .= "." if $part[$i+1];
}
t_mkdir($d);
## write index.html for the VirtualDocumentRoot ##
t_write_file("$d$url",$_);
## create directories for VirtualScriptAlias tests ##
$d = "$root/$_";
t_mkdir($d);
$d .= "/";
## write cgi ##
my $cgi_content = <<SCRIPT;
#!/bin/sh
echo Content-type: text/html
echo
echo $cgi_string $_
SCRIPT
t_write_file("$d$cgi_name",$cgi_content);
chmod 0755, "$d$cgi_name";
}
## run tests ##
foreach (@vh) {
## test VirtalDocumentRoot ##
ok t_cmp_str($_,
GET_BODY($url, Host => $_),
"VirtalDocumentRoot test"
);
## test VirtualScriptAlias ##
my $cgi_uri = "/cgi-bin/$cgi_name";
my $actual = GET_BODY $cgi_uri, Host => $_;
chomp $actual;
ok t_cmp_str("$cgi_string $_",
$actual,
"VirtualScriptAlias test"
);
}
_____________________________________________________________________
Stas Bekman JAm_pH -- Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide http://perl.apache.org/guide
mailto:stas@stason.org http://apachetoday.com http://eXtropia.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/
Re: new helper module Apache::TestUtil
Posted by Stas Bekman <st...@stason.org>.
On Wed, 5 Sep 2001, Doug MacEachern wrote:
> On Thu, 6 Sep 2001, Stas Bekman wrote:
>
> > I'd like to start properly document the packages, especially something
> > like TestUtil. Should I do a pod section at the bottom of the package, or
> > do inline pods? I know you prefer the former.
>
> i do. which do you prefer? anybody else?
I'm fine with adding all the pods at the bottom.
_____________________________________________________________________
Stas Bekman JAm_pH -- Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide http://perl.apache.org/guide
mailto:stas@stason.org http://apachetoday.com http://eXtropia.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/
Re: new helper module Apache::TestUtil
Posted by Doug MacEachern <do...@covalent.net>.
On Thu, 6 Sep 2001, Stas Bekman wrote:
> I'd like to start properly document the packages, especially something
> like TestUtil. Should I do a pod section at the bottom of the package, or
> do inline pods? I know you prefer the former.
i do. which do you prefer? anybody else?
Re: new helper module Apache::TestUtil
Posted by Stas Bekman <st...@stason.org>.
On Wed, 5 Sep 2001, Doug MacEachern wrote:
> On Sun, 2 Sep 2001, Stas Bekman wrote:
>
> >
> > Here is a new Apache::TestUtil that makes:
> >
> > - the writing tests that mess with files and dirs much easier.
> > - it automatically takes care of cleanup for all files and dirs created.
> > - makes it simpler to write debugable tests: ok $expected eq $received.
> >
> > After the patch I've attached a new vhost_alias.t that uses it, so you can
> > see how it's used and how shorter it makes the test. If it's all fine,
> > I'll commit the changes.
>
> cool, +1.
> could you change del_tree to use File::Path::rmtree instead?
ok
> and please fix style:
> > +sub t_cmp_str{
> > + my ($expect,$received,$comment) = @_;
> +sub t_cmp_str {
> + my($expect, $received, $comment) = @_;
>
> also, i don't think there is a need for both t_cmp_str and t_cmp_num,
> a t_cmp that uses 'eq' should work fine for both numbers and strings, no?
ok
I'd like to start properly document the packages, especially something
like TestUtil. Should I do a pod section at the bottom of the package, or
do inline pods? I know you prefer the former.
--- /dev/null Sat Apr 14 19:06:21 2001
+++ Apache-Test/lib/Apache/TestUtil.pm Thu Sep 6 09:42:43 2001
@@ -0,0 +1,95 @@
+package Apache::TestUtil;
+
+use strict;
+use warnings FATAL => 'all';
+use File::Find ();
+use File::Path ();
+use Exporter ();
+
+our $VERSION = '0.01';
+our @ISA = qw(Exporter);
+our @EXPORT = qw(t_cmp t_write_file t_open_file t_mkdir t_rm_tree);
+
+our %CLEAN = ();
+
+# t_cmp($expect,$received,$comment)
+# returns the result of comparison of $expect and $received
+# first prints all the arguments for debug.
+##################
+sub t_cmp {
+ my ($expect, $received, $comment) = @_;
+ print "testing : $comment\n" if defined $comment;
+ print "expected: $expect\n";
+ print "received: $received\n";
+ $expect eq $received;
+}
+
+# t_write_file($filename,@lines);
+# the file will be deleted at the end of the tests run
+#################
+sub t_write_file {
+ my $file = shift;
+ open my $fh, ">", $file or die "can't open $file: $!";
+ print "writing file: $file\n";
+ print $fh join '', @_ if @_;
+ close $fh;
+ $CLEAN{files}{$file}++;
+}
+
+# t_open_file($filename);
+# open a file for writing and return the open fh
+# the file will be deleted at the end of the tests run
+################
+sub t_open_file {
+ my $file = shift;
+ open my $fh, ">", $file or die "can't open $file: $!";
+ print "writing file: $file\n";
+ $CLEAN{files}{$file}++;
+ return $fh;
+}
+
+# t_mkdir($dirname)
+# create a dir
+# the dir will be deleted at the end of the tests run
+############
+sub t_mkdir {
+ my $dir = shift;
+
+ mkdir $dir, 0755 unless -d $dir;
+ print "creating dir: $dir\n";
+ $CLEAN{dirs}{$dir}++;
+}
+
+# deletes the whole tree(s) or just file(s)
+# accepts a list of dirs to delete
+###############
+sub t_rm_tree {
+ File::Path::rmtree((@_ > 1 ? \@_ : $_[0]), 0, 1);
+}
+
+END{
+
+ # cleanup first files than dirs
+ map { unlink $_ } grep {-e $_ && -f _ } keys %{ $CLEAN{files} };
+ map { t_rm_tree($_) } grep {-e $_ && -d _ } keys %{ $CLEAN{dirs} };
+
+}
+
+1;
+__END__
+
+
+=head1 NAME
+
+Apache::TestUtil - Utilities for writing tests
+
+=head1 SYNOPSIS
+
+
+
+=head1 DESCRIPTION
+
+
+
+=cut
+
_____________________________________________________________________
Stas Bekman JAm_pH -- Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide http://perl.apache.org/guide
mailto:stas@stason.org http://apachetoday.com http://eXtropia.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/
Re: new helper module Apache::TestUtil
Posted by Doug MacEachern <do...@covalent.net>.
On Sun, 2 Sep 2001, Stas Bekman wrote:
>
> Here is a new Apache::TestUtil that makes:
>
> - the writing tests that mess with files and dirs much easier.
> - it automatically takes care of cleanup for all files and dirs created.
> - makes it simpler to write debugable tests: ok $expected eq $received.
>
> After the patch I've attached a new vhost_alias.t that uses it, so you can
> see how it's used and how shorter it makes the test. If it's all fine,
> I'll commit the changes.
cool, +1.
could you change del_tree to use File::Path::rmtree instead?
and please fix style:
> +sub t_cmp_str{
> + my ($expect,$received,$comment) = @_;
+sub t_cmp_str {
+ my($expect, $received, $comment) = @_;
also, i don't think there is a need for both t_cmp_str and t_cmp_num,
a t_cmp that uses 'eq' should work fine for both numbers and strings, no?