File: command.t

package info (click to toggle)
libmojolicious-perl 9.37%2Bdfsg-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 4,320 kB
  • sloc: perl: 10,252; makefile: 31; javascript: 1
file content (129 lines) | stat: -rw-r--r-- 3,391 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use Mojo::Base -strict;

BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;
use Mojo::File qw(path tempdir);
use Mojolicious::Command;

subtest 'Application' => sub {
  my $command = Mojolicious::Command->new;
  isa_ok $command->app, 'Mojolicious', 'right application';
};

subtest 'Creating directories' => sub {
  my $command = Mojolicious::Command->new;
  my $cwd     = path;
  my $dir     = tempdir;
  my $buffer  = '';
  chdir $dir;
  {
    open my $handle, '>', \$buffer;
    local *STDOUT = $handle;
    $command->create_rel_dir('foo/bar');
  }
  like $buffer, qr/[mkdir]/, 'right output';
  ok -d path('foo', 'bar'), 'directory exists';

  $buffer = '';
  {
    open my $handle, '>', \$buffer;
    local *STDOUT = $handle;
    $command->create_rel_dir('foo/bar');
  }
  like $buffer, qr/\[exist\]/, 'right output';
  chdir $cwd;
};

subtest 'Generating files' => sub {
  my $command = Mojolicious::Command->new;
  my $cwd     = path;
  my $dir     = tempdir;
  is $command->rel_file('foo/bar.txt')->basename, 'bar.txt', 'right result';

  my $template = <<'EOF';
@@ foo_bar
% my $word = shift;
just <%= $word %>!
@@ dies
% die 'template error';
@@ bar_baz
just <%= $word %> too!
EOF
  open my $data, '<', \$template;
  no strict 'refs';
  *{"Mojolicious::Command::DATA"} = $data;
  chdir $dir;
  my $buffer = '';
  {
    open my $handle, '>', \$buffer;
    local *STDOUT = $handle;
    $command->template({})->render_to_rel_file('foo_bar', 'bar/baz.txt', 'works');
  }
  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';

  $buffer = '';
  {
    open my $handle, '>', \$buffer;
    local *STDOUT = $handle;
    $command->template({vars => 1})->render_to_rel_file('bar_baz', 'bar/two.txt', {word => 'works'});
  }
  like $buffer, qr/\[exist\].*\[write\]/s, 'right output';
  open $txt, '<', $command->rel_file('bar/two.txt');
  is join('', <$txt>), "just works too!\n", 'right result';

  $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';

  $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';

  eval { $command->render_data('dies') };
  like $@, qr/template error/, 'right error';
  chdir $cwd;
};

subtest 'Quiet' => sub {
  my $command = Mojolicious::Command->new;
  my $cwd     = path;
  my $dir     = tempdir;
  chdir $dir;
  my $buffer = '';
  {
    open my $handle, '>', \$buffer;
    local *STDOUT = $handle;
    $command->quiet(1)->write_rel_file('123.xml', 'fail');
  }
  is $buffer, '', 'no output';
  chdir $cwd;
};

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

done_testing();