File: Catmandu-CLI.t

package info (click to toggle)
libcatmandu-perl 1.1000-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,440 kB
  • sloc: perl: 15,726; makefile: 25; sh: 5
file content (137 lines) | stat: -rw-r--r-- 3,634 bytes parent folder | download | duplicates (4)
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
130
131
132
133
134
135
136
137
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;
use App::Cmd::Tester;

my $pkg;

BEGIN {
    $pkg = 'Catmandu::CLI';
    use_ok $pkg;
}
require_ok $pkg;

{

    package Catmandu::Fix::thisFixThrowsAnError;

    use Catmandu::Sane;
    use Moo;

    sub fix {Catmandu::FixError->throw("bad boy");}
}

# check -I / --lib_path
if ($^O ne 'MSWin32') {    # /dev/null required
        # TODO Catmandu dies if testing with output to STDOUT
    my $res;
    $res = test_app(
        'Catmandu::CLI' => [
            qw(
                -I t/lib convert Values --values 1;2;8 to JSON --file /dev/null
                )
        ]
    );
    ok !$res->error;
    is $res->stderr, "";

    $res = test_app(
        'Catmandu::CLI' => [
            qw(
                -I t/lib convert Values --values 1;2;8 to NotFound --file /dev/null
                )
        ]
    );
    ok !$res->error;
    like $res->stderr, qr/Oops! Can't find the exporter 'NotFound'/;
}

{
    my $result = test_app(qq|Catmandu::CLI| => [qw(-D3 help)]);
    like $result->stderr, qr/(debug activated|Log::Log4perl)/,
        'see some debug information';
}

{
    my $result = test_app(qq|Catmandu::CLI| => [qw(convert help)]);
    like $result->stderr, qr/Did you mean 'catmandu help convert'/,
        'wrong order help command';
}

{
    my $result = test_app(
        qq|Catmandu::CLI| => [qw(convert Null to Null --fix testing123() )]);
    like $result->stderr, qr/Oops/, 'wrong fix error';
}

{
    my $result = test_app(
        qq|Catmandu::CLI| => [qw(convert Null to Null --fix), "test("]);
    like $result->stderr, qr/Oops/, 'syntax error';
}

{
    my $result = test_app(qq|Catmandu::CLI| =>
            [qw(convert Null to Null --fix thisFixThrowsAnError())]);
    like $result->stderr, qr/One of your fixes threw an error/, 'fix error';
}

{
    my $result = test_app(
        qq|Catmandu::CLI| => [qw(convert Null to Null --fix add_field())]);
    like $result->stderr, qr/Oops/, 'wrong arguments';
}

{
    my $result
        = test_app(qq|Catmandu::CLI| => [qw(convert Null to Testing123 )]);
    like $result->stderr, qr/Catmandu::Exporter::Testing123/,
        'wrong exporter error';
}

{
    my $result = test_app(qq|Catmandu::CLI| => [qw(-D convert Null to Null)]);
    like $result->stderr, qr/debug activated/, 'debug activated';
}

{
    Catmandu->config->{log4perl} = <<EOF;
log4perl.category.Catmandu=DEBUG,STDERR
log4perl.categoty.Catmandu::Fix::log=TRACE,STDERR

log4perl.appender.STDOUT=Log::Log4perl::Appender::Screen
log4perl.appender.STDOUT.stderr=0
log4perl.appender.STDOUT.utf8=1

log4perl.appender.STDOUT.layout=PatternLayout
log4perl.appender.STDOUT.layout.ConversionPattern=%d [%P] - %p %l %M time=%r : %m%n

log4perl.appender.STDERR=Log::Log4perl::Appender::Screen
log4perl.appender.STDERR.stderr=1
log4perl.appender.STDERR.utf8=1

log4perl.appender.STDERR.layout=PatternLayout
log4perl.appender.STDERR.layout.ConversionPattern=%d [%P] - %l : %m%n
EOF
    my $result = test_app(qq|Catmandu::CLI| => [qw(-D convert Null to Null)]);
    like $result->stderr, qr/defined in catmandu\.yml/,
        'debug activated via catmandu.yml';
}

{
    Catmandu->config->{log4perl} = 't/log4perl.conf';
    my $result = test_app(qq|Catmandu::CLI| => [qw(-D convert Null to Null)]);
    like $result->stderr, qr/file: t\/log4perl\.conf/,
        'debug activated via t/log4perl.conf';
}

{
    my $result = test_app(qq|Catmandu::CLI| =>
            [qw(convert JSON --file http://google.com/nonononono to Null)]);
    like $result->stderr, qr/Oops! Got a HTTP error/, 'Got an HTTP error';
}

done_testing;