Skip to content

Commit

Permalink
improve generator commands not to overwrite existing files (closes #965)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 1, 2016
1 parent cd64844 commit 88cbb7c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

6.63 2016-05-22
6.63 2016-06-01
- Updated jQuery to version 2.2.4.
- Improved generator commands not to overwrite existing files.

6.62 2016-05-14
- Removed deprecated is_debug, is_error, is_info and is_warn methods from
Expand Down
24 changes: 12 additions & 12 deletions lib/Mojolicious/Command.pm
Expand Up @@ -20,22 +20,16 @@ has usage => "Usage: APPLICATION\n";
sub chmod_file {
my ($self, $path, $mod) = @_;
chmod $mod, $path or croak qq{Can't chmod file "$path": $!};
say " [chmod] $path " . sprintf('%lo', $mod) unless $self->quiet;
return $self;
return $self->_loud(" [chmod] $path " . sprintf('%lo', $mod));
}

sub chmod_rel_file { $_[0]->chmod_file($_[0]->rel_file($_[1]), $_[2]) }

sub create_dir {
my ($self, $path) = @_;

if (-d $path) { say " [exist] $path" unless $self->quiet }
else {
mkpath $path or croak qq{Can't make directory "$path": $!};
say " [mkdir] $path" unless $self->quiet;
}

return $self;
return $self->_loud(" [exist] $path") if -d $path;
mkpath $path or croak qq{Can't make directory "$path": $!};
return $self->_loud(" [mkdir] $path");
}

sub create_rel_dir { $_[0]->create_dir($_[0]->rel_dir($_[1])) }
Expand Down Expand Up @@ -76,14 +70,20 @@ sub run { croak 'Method "run" not implemented by subclass' }

sub write_file {
my ($self, $path, $data) = @_;
return $self->_loud(" [exist] $path") if -f $path;
$self->create_dir(dirname $path);
spurt $data, $path;
say " [write] $path" unless $self->quiet;
return $self;
return $self->_loud(" [write] $path");
}

sub write_rel_file { $_[0]->write_file($_[0]->rel_file($_[1]), $_[2]) }

sub _loud {
my ($self, $msg) = @_;
say $msg unless $self->quiet;
return $self;
}

1;

=encoding utf8
Expand Down
61 changes: 55 additions & 6 deletions t/mojolicious/command.t
Expand Up @@ -9,30 +9,79 @@ use File::Temp 'tempdir';
use Mojolicious::Command;

# Application
my $command = Mojolicious::Command->new(quiet => 1);
my $command = Mojolicious::Command->new;
isa_ok $command->app, 'Mojo', 'right application';
isa_ok $command->app, 'Mojolicious', 'right application';

# Generating files
# Creating directories
my $cwd = getcwd;
my $dir = tempdir CLEANUP => 1;
chdir $dir;
$command->create_rel_dir('foo/bar');
my $buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$command->create_rel_dir('foo/bar');
}
like $buffer, qr/[mkdir]/, 'right output';
ok -d catdir(qw(foo bar)), 'directory exists';
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$command->create_rel_dir('foo/bar');
}
like $buffer, qr/\[exist\]/, 'right output';

# Generating files
my $template = "@@ foo_bar\njust <%= 'works' %>!\n";
open my $data, '<', \$template;
no strict 'refs';
*{"Mojolicious::Command::DATA"} = $data;
$command->render_to_rel_file('foo_bar', 'bar/baz.txt');
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$command->render_to_rel_file('foo_bar', 'bar/baz.txt');
}
like $buffer, qr/\[mkdir\].*\[write\]/s, 'right output';
open my $txt, '<', $command->rel_file('bar/baz.txt');
is join('', <$txt>), "just works!\n", 'right result';
$command->chmod_rel_file('bar/baz.txt', 0700);
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$command->chmod_rel_file('bar/baz.txt', 0700);
}
like $buffer, qr/\[chmod\]/, 'right output';
ok -e $command->rel_file('bar/baz.txt'), 'file is executable';
$command->write_rel_file('123.xml', "seems\nto\nwork");
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$command->write_rel_file('123.xml', "seems\nto\nwork");
}
like $buffer, qr/\[exist\].*\[write\]/s, 'right output';
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$command->write_rel_file('123.xml', 'fail');
}
like $buffer, qr/\[exist\]/, 'right output';
open my $xml, '<', $command->rel_file('123.xml');
is join('', <$xml>), "seems\nto\nwork", 'right result';
chdir $cwd;

# Quiet
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$command->quiet(1)->write_rel_file('123.xml', 'fail');
}
is $buffer, '', 'no output';

# Abstract methods
eval { Mojolicious::Command->run };
like $@, qr/Method "run" not implemented by subclass/, 'right error';
Expand Down

0 comments on commit 88cbb7c

Please sign in to comment.