httpd-test-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stas Bekman <s...@stason.org>
Subject new helper module Apache::TestUtil
Date Sun, 02 Sep 2001 10:55:35 GMT

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/



Mime
View raw message