| 1 | package JSTAPd::Suite; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use FindBin; | ||
| 5 | use Path::Class (); | ||
| 6 | use Test::TCP (); | ||
| 7 | |||
| 8 | use JSTAPd::Server; | ||
| 9 | |||
| 10 | sub import { | ||
| 11 | my $class = shift; | ||
| 12 | my $caller = caller; | ||
| 13 | |||
| 14 | my $in_the_parse = do { | ||
| 15 | no strict 'refs'; | ||
| 16 | ${"$caller\::IN_THE_PARSE"}; | ||
| 17 | }; | ||
| 18 | |||
| 19 | strict->import; | ||
| 20 | warnings->import; | ||
| 21 | if ($in_the_parse) { | ||
| 22 | return; | ||
| 23 | } | ||
| 24 | |||
| 25 | my $suite_file = Path::Class::File->new((caller)[1]); | ||
| 26 | my $base_dir = detect_root($suite_file->dir); | ||
| 27 | run_server($suite_file, $base_dir); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub detect_root { | ||
| 31 | my $path = shift; | ||
| 32 | while (!-f $path->file('conf.pl')) { | ||
| 33 | die 'can not detect conf.pl' if $path eq $path->parent; | ||
| 34 | $path = $path->parent; | ||
| 35 | } | ||
| 36 | $path; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub run_server { | ||
| 40 | my($suite_file, $dir) = @_; | ||
| 41 | my $port = $ENV{JSTAP_PORT} || Test::TCP::empty_port(); | ||
| 42 | |||
| 43 | JSTAPd::Server->new( | ||
| 44 | dir => $dir, | ||
| 45 | ($ENV{JSTAP_HOST} ? (host => $ENV{JSTAP_HOST}) : ()), | ||
| 46 | port => $port, | ||
| 47 | run_file => $suite_file->relative($dir), | ||
| 48 | destroy => \&show_tap, | ||
| 49 | )->run; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub show_tap { | ||
| 53 | my($stdout, $tap) = @_; | ||
| 54 | print $stdout $tap->as_string; | ||
| 55 | exit; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _load_split_files { | ||
| 59 | my($class, $method, $ext) = @_; | ||
| 60 | return if $class->can($method); | ||
| 61 | |||
| 62 | my $file = do { | ||
| 63 | $class->can('path') && $class->path; | ||
| 64 | } || $0; | ||
| 65 | $file =~ s/\.t$/.$ext/; | ||
| 66 | return unless -f $file; | ||
| 67 | no strict 'refs'; | ||
| 68 | *{"$class\::$method"} = sub { | ||
| 69 | open my $fh, '<', $file or die "$file: $!"; | ||
| 70 | local $/; | ||
| 71 | <$fh>; | ||
| 72 | }; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub export { | ||
| 76 | my $class = shift; | ||
| 77 | |||
| 78 | do { | ||
| 79 | no strict 'refs'; | ||
| 80 | *{"$class\::new"} = \&new; | ||
| 81 | }; | ||
| 82 | |||
| 83 | _load_split_files $class, 'client_script' => 'js'; | ||
| 84 | _load_split_files $class, 'html_body' => 'html'; | ||
| 85 | |||
| 86 | die "missing client_script method in $0" unless $class->can('client_script'); | ||
| 87 | |||
| 88 | # set default method | ||
| 89 | for my $method (qw/ html_body server_api /) { | ||
| 90 | next if $class->can($method); | ||
| 91 | no strict 'refs'; | ||
| 92 | *{"$class\::$method"} = sub { '' }; | ||
| 93 | } | ||
| 94 | |||
| 95 | for my $method (qw/ include include_ex /) { | ||
| 96 | next if $class->can($method); | ||
| 97 | no strict 'refs'; | ||
| 98 | *{"$class\::$method"} = sub { +() }; | ||
| 99 | } | ||
| 100 | |||
| 101 | unless ($class->can('tests')) { | ||
| 102 | no strict 'refs'; | ||
| 103 | *{"$class\::tests"} = sub () { 0 }; | ||
| 104 | } | ||
| 105 | |||
| 106 | } | ||
| 107 | |||
| 108 | sub new { | ||
| 109 | my $class = shift; | ||
| 110 | bless {}, $class; | ||
| 111 | } | ||
| 112 | |||
| 113 | 1; |