| 1 | package App::SD::Test; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | require Prophet::Test; | ||
| 7 | use Test::More; | ||
| 8 | use File::Spec; | ||
| 9 | use File::Temp (); | ||
| 10 | use Test::Script::Run qw(:all); | ||
| 11 | use base qw/Exporter/; | ||
| 12 | our @EXPORT = qw(create_ticket_ok update_ticket_ok | ||
| 13 | create_ticket_with_editor_ok create_ticket_comment_ok get_uuid_for_luid | ||
| 14 | get_luid_for_uuid get_ticket_info run_ok run_output_matches | ||
| 15 | run_output_matches_unordered run_script is_script_output); | ||
| 16 | delete $ENV{'PROPHET_APP_CONFIG'}; | ||
| 17 | $ENV{'EDITOR'} = '/bin/true'; | ||
| 18 | |||
| 19 | $Prophet::Test::CLI_CLASS = 'App::SD::CLI'; | ||
| 20 | |||
| 21 | our ($A, $B, $C, $D); | ||
| 22 | |||
| 23 | BEGIN { | ||
| 24 | # create a blank config file so per-user configs don't break tests | ||
| 25 | my $tmp_config = File::Temp->new( UNLINK => 0 ); | ||
| 26 | print $tmp_config ''; | ||
| 27 | close $tmp_config; | ||
| 28 | print "setting SD_CONFIG to " . $tmp_config->filename . "\n"; | ||
| 29 | $ENV{'SD_CONFIG'} = $tmp_config->filename; | ||
| 30 | $ENV{'PROPHET_EMAIL'} = 'nobody@example.com'; | ||
| 31 | $ENV{'USER'} ||= 'nobody'; | ||
| 32 | } | ||
| 33 | |||
| 34 | =head2 create_ticket_ok ARGS | ||
| 35 | |||
| 36 | Creates a new ticket, passing ARGS along to the creation command (after the | ||
| 37 | props separator). | ||
| 38 | |||
| 39 | Returns a list of the luid and uuid of the newly created ticket. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub create_ticket_ok { | ||
| 44 | my @args = (@_); | ||
| 45 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 46 | run_output_matches( 'sd', [ 'ticket', 'create', '--', @args ], | ||
| 47 | [qr/Created ticket (.*?)(?{ $A = $1})\s+\((.*)(?{ $B = $2 })\)/] | ||
| 48 | ); | ||
| 49 | |||
| 50 | my ( $uuid, $luid ) =($B,$A); | ||
| 51 | return ( $luid, $uuid ); | ||
| 52 | } | ||
| 53 | |||
| 54 | =head2 update_ticket_ok ID ARGS | ||
| 55 | |||
| 56 | Updates the ticket #ID, passing ARGS along to the update command. | ||
| 57 | |||
| 58 | Returns nothing interesting. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub update_ticket_ok { | ||
| 63 | my ($id, @args) = (@_); | ||
| 64 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 65 | run_output_matches( 'sd', [ 'ticket', 'update', $id, '--', @args ], | ||
| 66 | [qr/ticket \d+\s+\([^)]*\)\s+updated\./i] | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 create_ticket_comment_ok ARGS | ||
| 71 | |||
| 72 | Creates a new ticket comment, passing ARGS along to the creation command. | ||
| 73 | |||
| 74 | Returns a list of the luid and uuid of the newly created comment. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub create_ticket_comment_ok { | ||
| 79 | my @args = (@_); | ||
| 80 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 81 | run_output_matches( | ||
| 82 | 'sd', | ||
| 83 | [ 'ticket', 'comment', 'create', @args ], | ||
| 84 | [qr/Created comment (.*?)(?{ $A = $1})\s+\((.*)(?{ $B = $2 })\)/] | ||
| 85 | ); | ||
| 86 | my ( $uuid, $luid ) = ($B, $A); | ||
| 87 | |||
| 88 | return ( $luid, $uuid ); | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 create_ticket_ok luid | ||
| 92 | |||
| 93 | Takes a LUID and returns the corresponding UUID. | ||
| 94 | |||
| 95 | Returns undef if none can be found. | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | sub get_uuid_for_luid { | ||
| 100 | my $luid = shift; | ||
| 101 | my ($ok, $out, $err) = run_script( 'sd', [ 'ticket', 'show', '--batch', '--id', $luid ]); | ||
| 102 | if ($out =~ /^id: \d+ \((.*)\)/m) { | ||
| 103 | return $1; | ||
| 104 | } | ||
| 105 | return undef; | ||
| 106 | } | ||
| 107 | |||
| 108 | =head2 get_luid_for_uuid UUID | ||
| 109 | |||
| 110 | Takes a UUID and returns the corresponding LUID. | ||
| 111 | |||
| 112 | Returns undef if none can be found. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub get_luid_for_uuid { | ||
| 117 | my $uuid = shift; | ||
| 118 | my ($ok, $out, $err) = run_script( 'sd', [ 'ticket', 'show', '--batch', '--id', $uuid ]); | ||
| 119 | if ($out =~ /^id: (\d+)/m) { | ||
| 120 | return $1; | ||
| 121 | } | ||
| 122 | return undef; | ||
| 123 | } | ||
| 124 | |||
| 125 | =head2 create_ticket_with_editor_ok [ '--verbose' ... ] | ||
| 126 | |||
| 127 | Creates a ticket and comment at the same time using a spawned editor. It's | ||
| 128 | expected that C<$ENV{VISUAL}> has been frobbed into something non-interactive, | ||
| 129 | or this test will just hang forever. Any extra arguments passed in will be | ||
| 130 | passed on to sd ticket create. | ||
| 131 | |||
| 132 | Returns a list of the ticket luid, ticket uuid, comment luid, and comment uuid. | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub create_ticket_with_editor_ok { | ||
| 137 | my @extra_args = @_; | ||
| 138 | |||
| 139 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 140 | run_output_matches( 'sd', [ 'ticket', 'create', @extra_args ], | ||
| 141 | [qr/Created ticket (.*?)(?{ $A = $1})\s+\((.*)(?{ $B = $2 })\)/, | ||
| 142 | qr/Created comment (.*?)(?{ $C = $1})\s+\((.*)(?{ $D = $2 })\)/] | ||
| 143 | ); | ||
| 144 | |||
| 145 | my ( $ticket_uuid, $ticket_luid, $comment_uuid, $comment_luid )= ($B,$A,$D,$C); | ||
| 146 | return ( $ticket_luid, $ticket_uuid, $comment_luid, $comment_uuid ); | ||
| 147 | } | ||
| 148 | |||
| 149 | =head2 update_ticket_with_editor_ok TICKET_LUID, TICKET_UUID [ '--verbose' ] | ||
| 150 | |||
| 151 | Updates the ticket given by TICKET_UUID using a spawned editor. It's | ||
| 152 | expected that C<$ENV{VISUAL}> has been frobbed into something non-interactive, | ||
| 153 | or this test will just hang forever. Any extra arguments passed in will | ||
| 154 | be passed on to sd ticket update. | ||
| 155 | |||
| 156 | Returns the luid and uuid of the comment created during the update (both will | ||
| 157 | be undef if none is created). | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub update_ticket_with_editor_ok { | ||
| 162 | my $self = shift; | ||
| 163 | my $ticket_luid = shift; | ||
| 164 | my $ticket_uuid = shift; | ||
| 165 | my @extra_args = @_; | ||
| 166 | |||
| 167 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 168 | run_output_matches( 'sd', [ 'ticket', 'update', $ticket_uuid, | ||
| 169 | @extra_args ], | ||
| 170 | [ qr/Updated ticket (.*?)\s+\((.*)\)/, | ||
| 171 | qr/Created comment (.*?)(?{ $A = $1 })\s+\((.*)(?{ $B = $2 })\)/ ] | ||
| 172 | ); | ||
| 173 | |||
| 174 | my ($comment_luid, $comment_uuid) = ($A, $B); | ||
| 175 | return ( $comment_luid, $comment_uuid ); | ||
| 176 | } | ||
| 177 | |||
| 178 | =head2 update_ticket_comment_with_editor_ok COMMENT_LUID, COMMENT_UUID | ||
| 179 | |||
| 180 | Updates the ticket comment given by COMMENT_UUID using a spawned editor. It's | ||
| 181 | expected that C<$ENV{VISUAL}> has been frobbed into something non-interactive, | ||
| 182 | or this test will just hang forever. | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub update_ticket_comment_with_editor_ok { | ||
| 187 | my $self = shift; | ||
| 188 | my ($comment_luid, $comment_uuid) = @_; | ||
| 189 | |||
| 190 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 191 | run_output_matches( 'sd', | ||
| 192 | [ 'ticket', 'comment', 'update', $comment_uuid ], | ||
| 193 | [ 'Updated comment '.$comment_luid . ' ('. $comment_uuid .')'] | ||
| 194 | ); | ||
| 195 | } | ||
| 196 | |||
| 197 | =head2 get_ticket_info LUID/UUID | ||
| 198 | |||
| 199 | Returns a hash reference with information about ticket. | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | sub get_ticket_info { | ||
| 204 | my $id = shift; | ||
| 205 | my ($ok, $out, $err) = run_script( 'sd', [qw(ticket show --batch --verbose --id), $id ]); | ||
| 206 | |||
| 207 | my @lines = split /\n/, $out; | ||
| 208 | |||
| 209 | my %res; | ||
| 210 | my $section = ''; | ||
| 211 | while ( defined( $_ = shift @lines ) ) { | ||
| 212 | if ( /^= ([A-Z]+)\s*$/ ) { | ||
| 213 | $section = lc $1; | ||
| 214 | next; | ||
| 215 | } | ||
| 216 | next unless $section; | ||
| 217 | |||
| 218 | if ( $section eq 'metadata' ) { | ||
| 219 | next unless /^(\w+):\s*(.*?)\s*$/; | ||
| 220 | $res{$section}{$1} = $2; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | if ( $res{'metadata'}{'id'} ) { | ||
| 225 | @{ $res{'metadata'} }{'luid', 'uuid'} = ( | ||
| 226 | $res{'metadata'}{'id'} =~ /^(\d+)\s+\((.*?)\)\s*$/ | ||
| 227 | ); | ||
| 228 | } | ||
| 229 | |||
| 230 | return \%res; | ||
| 231 | } | ||
| 232 | |||
| 233 | 1; |