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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
BEGIN {
unless ( eval 'use Test::Script::Run; 1' ) {
plan skip_all => "please install Test::Script::Run to run these tests"
}
}
plan tests => 18;
#######
use constant {
APP => 'bin/sql-split',
OSS => "\n--\n",
OFS => "\n-- >>>*<<< --\n"
};
my @files = qw{
t/data/create_table.sql
t/data/create_table_and_trigger.sql
};
my $statements;
my $stderr;
my ($oss, $ofs);
#######
run_ok( APP, \@files, 'script invokation' );
#######
($stderr, $statements) = test_script( \@files );
ok( length($stderr) == 0, 'no warnings' );
cmp_ok (
scalar(@$statements), '==', 2,
'number of files found in output'
);
cmp_ok (
scalar( @{ $statements->[0] } ), '==', 2,
'number of statements in the first file'
);
cmp_ok (
scalar( @{ $statements->[1] } ), '==', 6,
'number of statements in the second file'
);
#######
$oss = "\n--*\n"; $ofs = "\n--***\n";
$statements = test_script(
[ '--oss', $oss, '--ofs', $ofs, @files ],
$oss, $ofs
);
cmp_ok (
scalar(@$statements), '==', 2,
'number of files found in output - custom sep'
);
cmp_ok (
scalar( @{ $statements->[0] } ), '==', 2,
'number of statements in the first file - custom sep'
);
cmp_ok (
scalar( @{ $statements->[1] } ), '==', 6,
'number of statements in the second file - custom sep'
);
#######
$oss = "\n--#\n"; $ofs = "\n--###\n";
$statements = test_script(
[ '-s', $oss, '-f', $ofs, '-E', @files ],
$oss, $ofs
);
cmp_ok (
scalar(@$statements), '==', 2,
'number of files found in output - empty statements'
);
cmp_ok (
scalar( @{ $statements->[0] } ), '==', 3,
'number of statements in the first file - empty statements'
);
cmp_ok (
scalar( @{ $statements->[1] } ), '==', 7,
'number of statements in the second file - empty statements'
);
#######
($stderr, $statements) = test_script(
[ @files, 'non-existent.sql' ]
);
ok( length($stderr) > 0, 'file error warnings' );
cmp_ok (
scalar(@$statements), '==', 2,
'number of files found in output - file error'
);
cmp_ok (
scalar( @{ $statements->[0] } ), '==', 2,
'number of statements in the first file - file error'
);
cmp_ok (
scalar( @{ $statements->[1] } ), '==', 6,
'number of statements in the second file - file error'
);
#######
run_not_ok(
APP,
[ '--on-error=stop', @files, 'non-existent.sql' ],
'script dies on file error'
);
#######
run_not_ok(
APP,
[ '-e', 'CONTINU', @files ],
'script dies on wrong --on-error value'
);
#######
run_ok(
APP,
[ '--error', 'sToP', @files ],
'script accepts case-insensitive --on-error value'
);
#######
sub test_script {
my ($args, $oss, $ofs) = @_;
$oss ||= OSS;
$ofs ||= OFS;
my $stdout;
my $stderr;
my @statements;
run_script( APP, $args, \$stdout, \$stderr );
push @statements, [ split /\Q$oss/, $_, -1 ]
foreach split /\Q$ofs/, $stdout;
return ( $stderr, \@statements )
}
#######
|