httpd-test-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stas Bekman <s...@stason.org>
Subject Re: new helper module Apache::TestUtil
Date Thu, 06 Sep 2001 01:43:27 GMT
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/



Mime
View raw message