File Coverage

blib/lib/WWW/Github/Files/Mock.pm
Criterion Covered Total %
statement 49 55 89.0
branch 13 18 72.2
condition n/a
subroutine 12 18 66.6
pod 0 2 0.0
total 74 93 79.5


line stmt bran cond sub pod time code
1             package WWW::Github::Files::Mock;
2 1     1   789 use strict;
  1         2  
  1         39  
3 1     1   6 use warnings;
  1         1  
  1         32  
4 1     1   6 use File::Spec;
  1         2  
  1         23  
5 1     1   5 use Carp;
  1         2  
  1         1186  
6              
7             our $VERSION = 0.12;
8              
9             sub new {
10 1     1 0 2519 my ($class, $root) = @_;
11 1         11 return bless { root => $root }, $class;
12             }
13              
14             sub open {
15 2     2 0 735 my ($self, $path) = @_;
16 2 50       13 croak("Path should start with '/'! |$path|")
17             unless $path =~ m!^/!;
18 2         6 $path =~ s!/$!!;
19 2 100       16 my $full_path = $path eq '' ? $self->{root} : File::Spec->catdir( $self->{root}, $path );
20 2 50       62 die "File $full_path does not exists"
21             unless -e $full_path;
22 2 100       38 my $oclass = "WWW::Github::Files::Mock::" . (-d $full_path ? "Dir" : "File");
23 2         12 return $oclass->new( $path, $full_path );
24             }
25              
26             package WWW::Github::Files::Mock::Dir;
27              
28 0     0   0 sub is_file { 0 }
29 0     0   0 sub is_dir { 1 }
30 8     8   3364 sub name { return $_[0]->{name} }
31 0     0   0 sub path { return $_[0]->{path} }
32              
33             sub new {
34 5     5   9 my ($class, $path, $full_path) = @_;
35 5         16 my ($name) = $path =~ m![/\\]([^/\\]+)$!;
36 5 100       19 $name = '' unless defined $name;
37 5         36 return bless { path => $path, name => $name, full_path => $full_path }, $class;
38             }
39              
40             sub readdir {
41 2     2   373 my $self = shift;
42 2 50       100 opendir my $dh, $self->{full_path} or return;
43 2         268 my @files = grep { not m/^\.\.?$/ } readdir $dh;
  20         46  
44 2         32 closedir $dh;
45 2         3 my @objs;
46 2         5 foreach my $name (@files) {
47 16         103 my $full_path = File::Spec->catdir( $self->{full_path}, $name );
48 16 100       346 my $oclass = "WWW::Github::Files::Mock::" . (-d $full_path ? "Dir" : "File");
49 16         30 my $path = $self->{path} . '/' . $name;
50 16         103 push @objs, $oclass->new( $path, $full_path );
51             }
52 2         17 return @objs;
53             }
54              
55             package WWW::Github::Files::Mock::File;
56              
57 0     0   0 sub is_file { 1 }
58 0     0   0 sub is_dir { 0 }
59 20     20   91 sub name { return $_[0]->{name} }
60 0     0   0 sub path { return $_[0]->{path} }
61              
62             sub new {
63 13     13   22 my ($class, $path, $full_path) = @_;
64 13         48 my ($name) = $path =~ m![/\\]([^/\\]+)$!;
65 13 50       26 $name = '' unless defined $name;
66 13         81 return bless { path => $path, name => $name, full_path => $full_path }, $class;
67             }
68              
69             sub read {
70 2     2   1535 my $self = shift;
71 2 50       134 open my $fh, "<", $self->{full_path}
72             or die "could not find file " . $self->full_path;
73 2         3 my $content = do { local $/ = <$fh> };
  2         248  
74 2         22 close $fh;
75 2         11 return $content;
76             }
77              
78             1;
79              
80             =head1 NAME
81              
82             WWW::Github::Files::Mock - Read files and directories from local directory, as if they came from Gibhub
83              
84             =head1 SYNOPSIS
85              
86             my $gitfiles = WWW::Github::Files::Mock->new($respodir);
87             my @files = $gitfiles->open('/')->readdir();
88              
89             =head1 DESCRIPTION
90              
91             Suppose that you wrote some code that is based on WWW::Github::Files
92             accessing some github repository and reading files.
93              
94             Now suppose you want to use the same code on a local repository.
95             Say for testing, or whatever.
96             Fear not, this module will abstract the disk access as if the files
97             where hosted on Github.
98              
99             What is doesn't do - Can't select branch or commit to read from.
100             This module assumes that the current state is the desired commit.
101             (If you think that the ability to select commit/branh is important,
102             please file a feature request)
103              
104             This module mocks L, so go look there for interface documentation.
105              
106             =head1 AUTHOR
107            
108             Fomberg Shmuel, Eshmuelfomberg@gmail.comE
109            
110             =head1 COPYRIGHT AND LICENSE
111            
112             Copyright 2013 by Shmuel Fomberg.
113            
114             This library is free software; you can redistribute it and/or modify
115             it under the same terms as Perl itself.
116              
117             =cut