Skip to content

Commit

Permalink
add sibling method to Mojo::File
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 5, 2017
1 parent 798133c commit 09a566f
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@

7.28 2017-03-05
- Added copy_to method to Mojo::File.
- Added copy_to and sibling methods to Mojo::File.
- Added dir option to list_tree method in Mojo::File.
- Improved app generator command to generate a config file.
(tudorconstantin)
Expand Down
14 changes: 14 additions & 0 deletions lib/Mojo/File.pm
Expand Up @@ -97,6 +97,8 @@ sub remove_tree {
return $self;
}

sub sibling { shift->dirname->child(@_) }

sub slurp {
my $self = shift;

Expand Down Expand Up @@ -365,6 +367,18 @@ Open file with L<IO::File>.
Delete this directory and any files and subdirectories it may contain, any
additional arguments are passed through to L<File::Path>.
=head2 sibling
my $sibling = $path->sibling('.vimrc');
Return a new L<Mojo::File> object relative to the directory part of the path.
# "/home/sri/.vimrc" (on UNIX)
path('/home/sri/.bashrc')->sibling('.vimrc');
# "/home/sri/.ssh/known_hosts" (on UNIX)
path('/home/sri/.bashrc')->sibling('.ssh', 'known_hosts');
=head2 slurp
my $bytes = $path->slurp;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Home.pm
Expand Up @@ -38,7 +38,7 @@ sub list_files {
->to_array;
}

sub mojo_lib_dir { shift->new(__FILE__)->dirname->child('..') }
sub mojo_lib_dir { shift->new(__FILE__)->sibling('..') }

# DEPRECATED!
sub parse {
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/IOLoop/TLS.pm
Expand Up @@ -16,8 +16,8 @@ has reactor => sub { Mojo::IOLoop->singleton->reactor };

# To regenerate the certificate run this command (18.04.2012)
# openssl req -new -x509 -keyout server.key -out server.crt -nodes -days 7300
my $CERT = path(__FILE__)->dirname->child('resources', 'server.crt')->to_string;
my $KEY = path(__FILE__)->dirname->child('resources', 'server.key')->to_string;
my $CERT = path(__FILE__)->sibling('resources', 'server.crt')->to_string;
my $KEY = path(__FILE__)->sibling('resources', 'server.key')->to_string;

sub DESTROY { shift->_cleanup }

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -49,7 +49,7 @@ sub run {
# Preload application and configure server
my $prefork = $self->prefork->cleanup(0);
$prefork->load_app($app)->config->{hypnotoad}{pid_file}
//= path($ENV{HYPNOTOAD_APP})->dirname->child('hypnotoad.pid')->to_string;
//= path($ENV{HYPNOTOAD_APP})->sibling('hypnotoad.pid')->to_string;
$self->configure('hypnotoad');
weaken $self;
$prefork->on(wait => sub { $self->_manage });
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -1686,7 +1686,7 @@ automatically installed with the modules.
my $self = shift;

# Switch to installable home directory
$self->home(Mojo::Home->new(path(__FILE__)->dirname->child('MyApp')));
$self->home(Mojo::Home->new(path(__FILE__)->sibling('MyApp')));

# Switch to installable "public" directory
$self->static->paths->[0] = $self->home->child('public');
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -1159,7 +1159,7 @@ when C<register> is called.
my ($self, $app) = @_;

# Append "templates" and "public" directories
my $base = path(__FILE__)->dirname->child('AlertAssets');
my $base = path(__FILE__)->sibling('AlertAssets');
push @{$app->renderer->paths}, $base->child('templates')->to_string;
push @{$app->static->paths}, $base->child('public')->to_string;
}
Expand Down
6 changes: 5 additions & 1 deletion t/mojo/file.t
Expand Up @@ -24,6 +24,10 @@ is path('/home')->tap(sub { $$_ .= '/sri' })->to_string, '/home/sri',
is path('foo', 'bar')->child('baz', 'yada'),
catfile(catfile('foo', 'bar'), 'baz', 'yada'), 'same path';

# Siblings
is path('foo', 'bar')->sibling('baz', 'yada'),
catfile(dirname(catfile('foo', 'bar')), 'baz', 'yada'), 'same path';

# Array
is_deeply path('foo', 'bar')->to_array, [splitdir catfile('foo', 'bar')],
'same structure';
Expand Down Expand Up @@ -134,7 +138,7 @@ is $destination->slurp, 'works!', 'right content';
# List
is_deeply path('does_not_exist')->list->to_array, [], 'no files';
is_deeply path(__FILE__)->list->to_array, [], 'no files';
my $lib = path(__FILE__)->dirname->child('lib', 'Mojo');
my $lib = path(__FILE__)->sibling('lib', 'Mojo');
my @files = map { path($lib)->child(split '/') } (
'DeprecationTest.pm', 'LoaderException.pm',
'LoaderException2.pm', 'TestConnectProxy.pm'
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/prefork.t
Expand Up @@ -29,7 +29,7 @@ undef $prefork;
ok !-e $file, 'file has been cleaned up';

# Bad PID file
my $bad = path(__FILE__)->dirname->child('does_not_exist', 'test.pid');
my $bad = path(__FILE__)->sibling('does_not_exist', 'test.pid');
$prefork = Mojo::Server::Prefork->new(pid_file => $bad);
$prefork->app->log->level('fatal');
my $log = '';
Expand Down
8 changes: 4 additions & 4 deletions t/mojo/template.t
Expand Up @@ -1125,13 +1125,13 @@ EOF

# File
$mt = Mojo::Template->new;
my $file = path(__FILE__)->dirname->child('templates', 'test.mt');
my $file = path(__FILE__)->sibling('templates', 'test.mt');
$output = $mt->render_file($file, 3);
like $output, qr/23\nHello World!/, 'file';

# Exception in file
$mt = Mojo::Template->new;
$file = path(__FILE__)->dirname->child('templates', 'exception.mt');
$file = path(__FILE__)->sibling('templates', 'exception.mt');
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/exception\.mt line 2/, 'message contains filename';
Expand Down Expand Up @@ -1161,7 +1161,7 @@ like "$output", qr/foo\.mt from DATA section line 2/, 'right result';

# Exception with UTF-8 context
$mt = Mojo::Template->new;
$file = path(__FILE__)->dirname->child('templates', 'utf8_exception.mt');
$file = path(__FILE__)->sibling('templates', 'utf8_exception.mt');
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
ok $output->verbose, 'verbose exception';
Expand All @@ -1182,7 +1182,7 @@ is $output->lines_after->[0], undef, 'no lines after';

# Different encodings
$mt = Mojo::Template->new(encoding => 'shift_jis');
$file = path(__FILE__)->dirname->child('templates', 'utf8_exception.mt');
$file = path(__FILE__)->sibling('templates', 'utf8_exception.mt');
ok !eval { $mt->render_file($file) }, 'file not rendered';
like $@, qr/invalid encoding/, 'right error';

Expand Down
3 changes: 1 addition & 2 deletions t/mojolicious/json_config_lite_app.t
Expand Up @@ -21,8 +21,7 @@ like $@, qr/Malformed JSON/, 'right error';
# Load plugins
my $config
= plugin j_s_o_n_config => {default => {foo => 'baz', hello => 'there'}};
my $path
= path(__FILE__)->to_abs->dirname->child('json_config_lite_app_abs.json');
my $path = path(__FILE__)->to_abs->sibling('json_config_lite_app_abs.json');
plugin JSONConfig => {file => $path};
is $config->{foo}, 'bar', 'right value';
is $config->{hello}, 'there', 'right value';
Expand Down

0 comments on commit 09a566f

Please sign in to comment.