| 1 | package JSTAPd::ContentsBag; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use JSTAPd::Contents; | ||
| 6 | |||
| 7 | sub dir { $_[0]->{dir} } | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my($class, %args) = @_; | ||
| 11 | |||
| 12 | my $self = bless { dir => $args{dir}, run_file => $args{run_file} }, $class; | ||
| 13 | $self; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub _loader { | ||
| 17 | my($self, $base) = @_; | ||
| 18 | |||
| 19 | my $dir = +{ children => +{}, map => +{}, path => $base }; | ||
| 20 | my @contents; | ||
| 21 | for my $path ($base->children) { | ||
| 22 | if ($path->is_dir) { | ||
| 23 | my $name = $path->dir_list(-1); | ||
| 24 | my $stuff = $self->_loader($path); | ||
| 25 | $stuff->{name} = $name; | ||
| 26 | push @contents, $stuff; | ||
| 27 | $dir->{children}->{$name} = $stuff; | ||
| 28 | next; | ||
| 29 | } | ||
| 30 | my $basename = $path->basename; | ||
| 31 | next unless $path =~ /\.t$/ || $basename eq 'index'; | ||
| 32 | if ($self->{run_file} && $basename ne 'index') { | ||
| 33 | next unless $path->relative($self->{dir}) eq $self->{run_file}; | ||
| 34 | } | ||
| 35 | my $stuff = JSTAPd::Contents->new( $basename => $path ); | ||
| 36 | push @contents, $stuff; | ||
| 37 | $dir->{map}->{$basename} = $stuff; | ||
| 38 | } | ||
| 39 | $dir->{contents} = \@contents; | ||
| 40 | $dir; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub load { | ||
| 44 | my $self = shift; | ||
| 45 | $self->{contents} = $self->_loader( $self->{dir} ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub fetch_file { | ||
| 49 | my($self, $basename, $chain, $is_inherit) = @_; | ||
| 50 | |||
| 51 | my $dir = $self->{contents}; | ||
| 52 | my $parent = $dir->{map}->{$basename}; | ||
| 53 | for my $name (@{ $chain || [] }) { | ||
| 54 | unless ($dir = $dir->{children}->{$name}) { | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | $parent = $dir->{map}->{$basename} || $parent;; | ||
| 58 | } | ||
| 59 | my $content = $dir->{map}->{$basename}; | ||
| 60 | $content = $parent if !$content && $is_inherit; | ||
| 61 | return $content; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub fetch_dir { | ||
| 65 | my($self, $chain) = @_; | ||
| 66 | |||
| 67 | my $dir = $self->{contents}; | ||
| 68 | for my $name (@{ $chain || [] }) { | ||
| 69 | unless ($dir = $dir->{children}->{$name}) { | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | return $dir; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub each { | ||
| 77 | my($self, $chain, $cb) = @_; | ||
| 78 | my $dir = $self->fetch_dir($chain); | ||
| 79 | for my $contents (@{ $dir->{contents} }) { | ||
| 80 | my $is_dir = ref($contents) eq 'HASH'; | ||
| 81 | $cb->($contents->{name}, $is_dir); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _visitor { | ||
| 86 | my($self, $path, $contents, $cb) = @_; | ||
| 87 | for my $child (@{ $contents }) { | ||
| 88 | my $current = $path; | ||
| 89 | $current .= '/' if $current; | ||
| 90 | $current .= $child->{name}; | ||
| 91 | my $is_dir = (ref($child) eq 'HASH'); | ||
| 92 | $cb->({ | ||
| 93 | name => $child->{name}, | ||
| 94 | path => $current, | ||
| 95 | is_dir => $is_dir, | ||
| 96 | child => $child, | ||
| 97 | }); | ||
| 98 | $self->_visitor($current, $child->{contents}, $cb) if $is_dir; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub visitor { | ||
| 103 | my($self, $cb) = @_; | ||
| 104 | $self->_visitor('', $self->{contents}->{contents}, $cb); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub test_plans { | ||
| 108 | my $self = shift; | ||
| 109 | my $files = 0; | ||
| 110 | my $tests = 0; | ||
| 111 | $self->visitor(sub { | ||
| 112 | my $obj = shift; | ||
| 113 | my $child = $obj->{child}; | ||
| 114 | return unless ref $child eq 'JSTAPd::Contents'; | ||
| 115 | next unless $child->can('suite'); | ||
| 116 | my $suite = $child->suite; | ||
| 117 | return unless $suite; | ||
| 118 | |||
| 119 | $files++; | ||
| 120 | $tests += $suite->tests; | ||
| 121 | }); | ||
| 122 | +{ | ||
| 123 | files => $files, | ||
| 124 | tests => $tests, | ||
| 125 | }; | ||
| 126 | } | ||
| 127 | |||
| 128 | 1; |