| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | SVN::Web::Test - automated web testing for SVN::Web | ||
| 5 | |||
| 6 | =head1 DESCRIPTION | ||
| 7 | |||
| 8 | =cut | ||
| 9 | |||
| 10 | package SVN::Web::Test; | ||
| 11 | |||
| 12 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | our $VERSION = 0.63; | ||
| 16 | |||
| 17 | 1 | ▶ | use File::Path; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use File::Temp qw(tempdir); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use POSIX (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | |||
| 22 | 1 | ▶ | use Test::More; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | |||
| 24 | 0 | ▶ | use SVN::Web; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 25 | use YAML (); | ||
| 26 | |||
| 27 | my $uri_base; | ||
| 28 | my $script; | ||
| 29 | my $fake_cgi = 0; | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | my $class = shift; | ||
| 33 | my $self = bless {}, $class; | ||
| 34 | |||
| 35 | %$self = @_; | ||
| 36 | |||
| 37 | my @mech_args = exists $self->{mech_args} ? $self->{mech_args} : (); | ||
| 38 | |||
| 39 | $self->{_mech} = | ||
| 40 | SVN::Web::Test::Mechanize->new(@mech_args); | ||
| 41 | |||
| 42 | if ( !exists $self->{root_url} ) { | ||
| 43 | if ( exists $self->{httpd_port} ) { | ||
| 44 | $self->{root_url} = "http://localhost:$self->{httpd_port}/svnweb"; | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | $self->{root_url} = "http://localhost/svnweb"; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | $self->{repo_path} = File::Spec->rel2abs( $self->{repo_path} ); | ||
| 52 | $self->{repo_dump} = File::Spec->rel2abs( $self->{repo_dump} ); | ||
| 53 | |||
| 54 | $self->create_env(); | ||
| 55 | $self->create_install(); | ||
| 56 | |||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | # Returns the Test::WWW::Mechanize object | ||
| 61 | sub mech { | ||
| 62 | return shift->{_mech}; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub install_dir { | ||
| 66 | return shift->{install_dir}; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub site_root { | ||
| 70 | return shift->{root_url}; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub set_config { | ||
| 74 | my $self = shift; | ||
| 75 | my $opts = shift; | ||
| 76 | |||
| 77 | $uri_base = $opts->{uri_base}; | ||
| 78 | $script = $opts->{script}; | ||
| 79 | $fake_cgi = 1; | ||
| 80 | |||
| 81 | my $config = { | ||
| 82 | version => $VERSION, | ||
| 83 | actions => { | ||
| 84 | 'browse' => { | ||
| 85 | 'class' => 'SVN::Web::Browse', | ||
| 86 | 'action_menu' => { | ||
| 87 | 'show' => ['directory'], | ||
| 88 | 'link_text' => '(browse directory)' | ||
| 89 | } | ||
| 90 | }, | ||
| 91 | 'blame' => { | ||
| 92 | 'class' => 'SVN::Web::Blame', | ||
| 93 | 'action_menu' => { | ||
| 94 | 'show' => ['file'], | ||
| 95 | 'link_text' => '(view blame)' | ||
| 96 | } | ||
| 97 | }, | ||
| 98 | 'checkout' => { | ||
| 99 | 'class' => 'SVN::Web::Checkout', | ||
| 100 | 'action_menu' => { | ||
| 101 | 'show' => ['file'], | ||
| 102 | 'link_text' => '(checkout)' | ||
| 103 | } | ||
| 104 | }, | ||
| 105 | 'revision' => { 'class' => 'SVN::Web::Revision' }, | ||
| 106 | 'view' => { | ||
| 107 | 'class' => 'SVN::Web::View', | ||
| 108 | 'action_menu' => { | ||
| 109 | 'show' => ['file'], | ||
| 110 | 'link_text' => '(view file)' | ||
| 111 | } | ||
| 112 | }, | ||
| 113 | 'diff' => { 'class' => 'SVN::Web::Diff' }, | ||
| 114 | 'log' => { | ||
| 115 | 'class' => 'SVN::Web::Log', | ||
| 116 | 'action_menu' => { | ||
| 117 | 'show' => [ 'file', 'directory' ], | ||
| 118 | 'link_text' => '(view revision log)' | ||
| 119 | } | ||
| 120 | }, | ||
| 121 | 'rss' => { | ||
| 122 | 'class' => 'SVN::Web::RSS', | ||
| 123 | 'action_menu' => { | ||
| 124 | 'icon' => '/css/trac/feed-icon-16x16.png', | ||
| 125 | 'show' => [ 'file', 'directory' ], | ||
| 126 | 'head_only' => '1', | ||
| 127 | 'link_text' => '(rss)' | ||
| 128 | } | ||
| 129 | }, | ||
| 130 | 'list' => { 'class' => 'SVN::Web::List' } | ||
| 131 | }, | ||
| 132 | cgi_class => 'Plack::Request', | ||
| 133 | templatedirs => ['template/trac'], | ||
| 134 | %{ $opts->{config} }, | ||
| 135 | }; | ||
| 136 | |||
| 137 | SVN::Web::set_config($config); | ||
| 138 | } | ||
| 139 | |||
| 140 | # Create a Subversion repo from a dump file. | ||
| 141 | sub create_env { | ||
| 142 | |||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | plan skip_all => 'Test::WWW::Mechanize not installed' | ||
| 146 | unless eval { require Test::WWW::Mechanize; 1; }; | ||
| 147 | |||
| 148 | plan skip_all => q{Can't find svnadmin} | ||
| 149 | unless `svnadmin --version` =~ m/version/; | ||
| 150 | |||
| 151 | rmtree( [ $self->{repo_path} ] ) if -d $self->{repo_path}; | ||
| 152 | $ENV{SVNFSTYPE} ||= ( ( $SVN::Core::VERSION =~ m/^1\.0/ ) ? 'bdb' : 'fsfs' ); | ||
| 153 | |||
| 154 | `svnadmin create --fs-type=$ENV{SVNFSTYPE} $self->{repo_path}`; | ||
| 155 | `svnadmin load $self->{repo_path} < $self->{repo_dump}`; | ||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | # Create a scratch area, run svnweb-install. The generated config.yaml | ||
| 160 | # file will be changed to list the repo created create_env(). | ||
| 161 | # | ||
| 162 | # Returns the directory in which the scratch area is rooted. | ||
| 163 | sub create_install { | ||
| 164 | |||
| 165 | my $self = shift; | ||
| 166 | |||
| 167 | $self->{install_dir} = tempdir( CLEANUP => 1 ); | ||
| 168 | #warn "Created $self->{install_dir}\n"; | ||
| 169 | my $cwd = POSIX::getcwd(); | ||
| 170 | chdir( $self->{install_dir} ); | ||
| 171 | my $lib_dir = File::Spec->catdir( $cwd, 'blib', 'lib' ); | ||
| 172 | my $svnweb_install = File::Spec->catfile( $cwd, 'bin', 'svnweb-install' ); | ||
| 173 | |||
| 174 | system "$^X -I$lib_dir $svnweb_install"; | ||
| 175 | |||
| 176 | # Make the directory world-readable by all. Otherwise, if Apache is | ||
| 177 | # started as root the default behaviour is to set user/group to -1. | ||
| 178 | # This results in the directory being unreadable by SVN::Web. | ||
| 179 | chmod 0755, $self->{install_dir}; | ||
| 180 | |||
| 181 | chdir($cwd); # Get back to the original directory | ||
| 182 | |||
| 183 | # Change the config to point to the test repo | ||
| 184 | my $config_file = | ||
| 185 | File::Spec->catfile( $self->{install_dir}, 'config.yaml' ); | ||
| 186 | my $config = YAML::LoadFile($config_file); | ||
| 187 | $config->{repos}{repos} = $self->{repo_path}; | ||
| 188 | YAML::DumpFile( $config_file, $config ); | ||
| 189 | |||
| 190 | return $self->{install_dir}; | ||
| 191 | } | ||
| 192 | |||
| 193 | # Walk the site | ||
| 194 | sub walk_site { | ||
| 195 | my $self = shift; | ||
| 196 | my $test = shift; | ||
| 197 | my $seen = shift || {}; | ||
| 198 | |||
| 199 | $test->($self); | ||
| 200 | |||
| 201 | my @links = $self->mech()->links(); | ||
| 202 | for my $i ( 0 .. $#links ) { | ||
| 203 | my $link_url = $links[$i]->url_abs; | ||
| 204 | |||
| 205 | diag sprintf "Fetching %d/%d %s (%s)", | ||
| 206 | $i + 1, $#links + 1, $link_url, $links[$i]->text()||'' | ||
| 207 | if exists $ENV{TEST_VERBOSE} and $ENV{TEST_VERBOSE}; | ||
| 208 | |||
| 209 | next if $seen->{$link_url}; | ||
| 210 | diag "Skipping $link_url", next | ||
| 211 | if $link_url !~ m/(?:localhost|127\.0\.0\.1)/; | ||
| 212 | |||
| 213 | ++$seen->{$link_url}; | ||
| 214 | |||
| 215 | $self->mech()->get($link_url); | ||
| 216 | $self->walk_site( $test, $seen ); | ||
| 217 | $self->mech()->back; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | package SVN::Web::Test::Mechanize; | ||
| 222 | |||
| 223 | use base qw(Test::WWW::Mechanize); | ||
| 224 | |||
| 225 | use Plack::Test; | ||
| 226 | use SVN::Web; | ||
| 227 | use Plack::Builder; | ||
| 228 | |||
| 229 | sub send_request { | ||
| 230 | my ( $self, $request ) = @_; | ||
| 231 | |||
| 232 | my $buf = ''; | ||
| 233 | my $uri = $request->uri; | ||
| 234 | my $response; | ||
| 235 | |||
| 236 | my $app = builder { | ||
| 237 | mount "/svnweb" => sub { SVN::Web->run_psgi(@_) }, | ||
| 238 | }; | ||
| 239 | |||
| 240 | test_psgi | ||
| 241 | app => $app, | ||
| 242 | client => sub { | ||
| 243 | my $cb = shift; | ||
| 244 | my $req = HTTP::Request->new(GET => $uri); | ||
| 245 | $response = $cb->($req); | ||
| 246 | } | ||
| 247 | ; | ||
| 248 | |||
| 249 | return $response; | ||
| 250 | } | ||
| 251 | |||
| 252 | =head1 AUTHORS | ||
| 253 | |||
| 254 | Chia-liang Kao E<lt>clkao@clkao.orgE<gt> and E<lt>nik@cpan.org<gt>. | ||
| 255 | |||
| 256 | Dean Hamstead E<lt>dean@fragfest.com.auE<gt> | ||
| 257 | |||
| 258 | =head1 COPYRIGHT | ||
| 259 | |||
| 260 | Copyright (c) 2012 by Dean Hamstead E<lt>dean@fragfest.com.auE<gt> | ||
| 261 | |||
| 262 | Copyright (c) 2005-2007 by Nik Clayton E<lt>nik@cpan.org<gt>. | ||
| 263 | |||
| 264 | Copyright (c) 2004 by Chia-liang Kao E<lt>clkao@clkao.orgE<gt>. | ||
| 265 | |||
| 266 | =head1 LICENSE | ||
| 267 | |||
| 268 | This program is free software; you can redistribute it and/or modify it | ||
| 269 | under the same terms as Perl itself. | ||
| 270 | |||
| 271 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | 1; | ||