Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add chmod method to Mojo::File
  • Loading branch information
kraih committed Mar 7, 2017
1 parent 466b3bb commit 0706a5a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@

7.28 2017-03-07
- Added copy_to, realpath and sibling methods to Mojo::File.
- Added chmod, copy_to, realpath 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
12 changes: 12 additions & 0 deletions lib/Mojo/File.pm
Expand Up @@ -25,6 +25,12 @@ sub basename { File::Basename::basename ${$_[0]}, @_ }

sub child { $_[0]->new(@_) }

sub chmod {
my ($self, $mode) = @_;
chmod $mode, $$self or croak qq{Can't chmod file "$$self": $!};
return $self;
}

sub copy_to {
my ($self, $to) = @_;
copy($$self, $to) or croak qq{Can't copy file "$$self" to "$to": $!};
Expand Down Expand Up @@ -236,6 +242,12 @@ Return a new L<Mojo::File> object relative to the path.
# "/home/sri/.vimrc" (on UNIX)
path('/home')->child('sri', '.vimrc');
=head2 chmod
$path = $path->chmod(0644);
Change permissions of a file.
=head2 copy_to
my $destination = $path->copy_to('/home/sri');
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Command.pm
Expand Up @@ -14,9 +14,9 @@ has 'quiet';
has usage => "Usage: APPLICATION\n";

sub chmod_file {
my ($self, $path, $mod) = @_;
chmod $mod, $path or croak qq{Can't chmod file "$path": $!};
return $self->_loud(" [chmod] $path " . sprintf('%lo', $mod));
my ($self, $path, $mode) = @_;
path($path)->chmod($mode);
return $self->_loud(" [chmod] $path " . sprintf('%lo', $mode));
}

sub chmod_rel_file { $_[0]->chmod_file($_[0]->rel_file($_[1]), $_[2]) }
Expand Down
9 changes: 9 additions & 0 deletions t/mojo/file.t
Expand Up @@ -218,4 +218,13 @@ is $file->spurt('w', 'orks', ' too!')->slurp, 'works too!', 'right content';
like $@, qr/Can't write to file ".*/, 'right error';
}

# Mode
$dir = tempdir;
$file = $dir->child('test.txt')->spurt('just works!');
is $file->chmod(0400)->slurp, 'just works!', 'right content';
eval { $file->spurt('works not!') };
ok $@, 'has error';
is $file->chmod(0600)->spurt('works too!')->slurp, 'works too!',
'right content';

done_testing();

0 comments on commit 0706a5a

Please sign in to comment.