MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
assert_command_output.inc
1 # ==== Purpose ====
2 #
3 # Execute a command and check:
4 # - the exit status is as specified
5 # - the output (stdout) matches a given regex
6 
7 # ==== Usage ====
8 
9 --let $include_filename= assert_command_output.inc
10 --source include/begin_include_file.inc
11 
12 if ($rpl_debug)
13 {
14  --echo # debug: assert_command='$assert_command' assert_regex='$assert_regex' assert_negated='$assert_negated' assert_status='$assert_status'
15 }
16 
17 --let _ASSERT_COMMAND= $assert_command
18 --let _ASSERT_REGEX= $assert_regex
19 --let _ASSERT_NEGATED= $assert_negated
20 --let _ASSERT_STATUS= $assert_status
21 --let $_assert_suffix= `SELECT UUID()`
22 --let _ASSERT_ERROR_FILE= $MYSQLTEST_VARDIR/tmp/_assert_$_assert_suffix.inc
23 --let _ASSERT_DEBUG= $rpl_debug
24 
25 if ($rpl_debug)
26 {
27  --echo # debug: assert_error_file='$_ASSERT_ERROR_FILE'
28 }
29 
30 perl;
31  my $cmd= $ENV{'_ASSERT_COMMAND'};
32  my $positive= $ENV{'_ASSERT_NEGATED'} ? 0 : 1;
33  my $regex= $ENV{'_ASSERT_REGEX'};
34  my $status= $ENV{'_ASSERT_STATUS'};
35  my $debug= $ENV{'_ASSERT_DEBUG'};
36  my $output= `$cmd`;
37  my $error= 0;
38  $status= 0 if $status eq '';
39  if ($status ne '*' and ($status ? $? >> 8 != $status : $? != 0))
40  {
41  print "ERROR: Command returned wrong exit status! " .
42  "Expected '$status', got '".($?>>8)."'\n";
43  $error= 1;
44  }
45  if ((($output =~ m{$regex}ms) ? 1 : 0) != $positive)
46  {
47  print "ERROR: Command produced wrong output!\n";
48  print "ERROR: Command: '$cmd'\n";
49  print "ERROR: Output expected to " . ($positive?'':'not '). "match " .
50  "perl regex: '$regex'\n";
51  $error= 1;
52  }
53  if (!$error)
54  {
55  my $file= $ENV{_ASSERT_ERROR_FILE};
56  open FILE, "> $file" or die "Error opening $file: $!";
57  print FILE "X" or die "Error writing to $file: $!";
58  close FILE or die "Error closing $file: $!";
59  }
60  if ($error || $debug)
61  {
62  print "======== BEGIN output ========\n$output\n" .
63  "======== END OUTPUT ========\n";
64  }
65 EOF
66 
67 --file_exists $_ASSERT_ERROR_FILE
68 --remove_file $_ASSERT_ERROR_FILE
69 
70 --let $include_filename= assert_command_output.inc
71 --source include/end_include_file.inc