Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
224 hknight 1
#!/usr/bin/perl -w
2
 
3
########################################################################
4
# Copyright (C) 2008 ERG Limited, All rights reserved
5
#
6
# Module name   : test_schemadump.pl
7
# Module type   : Stand alone unit test program
8
# Compiler(s)   : n/a
9
# Environment(s): windows (dos), unix (solaris and linux)
10
#
11
# Description   : Runs schemadump.pl and then ddlfile.pl and
12
#                 checks that no information is lost.
13
#
14
# History       : Created by Haydon Knight May 2008
15
#
16
# Usage         : test_schemadump.pl [options]
17
#
18
########################################################################
19
 
20
#######################################################################
21
# Use lines
22
#######################################################################
23
require 5.6.1;
24
use strict;
25
use warnings;
26
use Pod::Usage;                             # required for help support
27
use Getopt::Long;
28
use File::Path;
29
 
30
#######################################################################
31
# Function prototypes
32
#######################################################################
33
 
34
sub parseCommandLine();
35
sub startupChecks();
36
sub main();
37
 
38
sub quoteCommand(@);
39
sub readLines($);
40
sub cleanup();
41
sub runCommand($@);
42
sub logprint($);
43
sub hchomp;
44
sub finish($$);
45
 
46
#######################################################################
47
# Constant global variables
48
#######################################################################
49
 
50
my $VERSION = "1.0.1";
51
my $UNIX = ($ENV{'OS'} && $ENV{'OS'} =~ m/win/i) ? 0 : 1;
52
 
53
my $dotdat = $UNIX ? "../" : "..\\";
54
my $scriptsDir = $0;
55
$scriptsDir = $dotdat unless $scriptsDir =~ m~[/\\]~;
56
$scriptsDir =~ s~(.*[\\/]).*~$1$dotdat~ if( $scriptsDir =~ m~[/\\]~ );
57
 
58
my $ddlFile = "release_manager.ddl";
59
 
60
my $tmpDir = ($UNIX ? "/tmp" : "C:/temp");
61
my $unpackDir = "$tmpDir/test_schemadump";
62
my $repackFile = "$tmpDir/test_schemadump.results";
63
my $unpackLogFile = "$tmpDir/test_schemadump.log1";
64
my $repackLogFile = "$tmpDir/test_schemadump.log2";
65
 
66
#######################################################################
67
# Other global variables
68
#######################################################################
69
 
70
my $verbose = 0;
71
my $logFile;
72
 
73
#######################################################################
74
# Main code
75
#######################################################################
76
 
77
parseCommandLine();
78
startupChecks();
79
main();
80
finish(0,"");
81
 
82
#######################################################################
83
# Function definitions
84
#######################################################################
85
 
86
#-------------------------------------------------------------------------------
87
# Function  : quoteCommand
88
#
89
# Purpose   : Quotes its arguments
90
#
91
# Arguments : @words (i) - words to be surrounded by double quotes
92
#
93
# Returns   : $quotedCommand - words quoted and joined
94
#
95
# Notes     :
96
#
97
sub quoteCommand(@)
98
{
99
    my @words = @_;
100
    my $quotedCommand;
101
 
102
    foreach my $word (@words)
103
    {
104
        $quotedCommand .= qq("$word" );
105
    }
106
    return $quotedCommand;
107
}
108
 
109
 
110
#-------------------------------------------------------------------------------
111
# Function  : finish
112
#
113
# Purpose   : Prints a statement indicating success/failure and exits
114
#
115
# Arguments : $exitVal (i) - Exit value (0 for success, anything else for failure)
116
#             $msg (i) - Message to print out, if any
117
#
118
# Returns   : none
119
#
120
# Notes     : Closes log and cleans up
121
#
122
sub finish($$)
123
{
124
    my ($exitVal, $msg) = @_;
125
 
126
    print STDERR $msg if $msg;
127
 
128
    cleanup();
129
 
130
    my $str = ( ($exitVal == 0) ? "was successful!" : "failed!" );
131
 
132
    print "test_schemadump.pl test $str\n";
133
    close LOGFILE if $logFile;
134
    exit( $exitVal );
135
}
136
 
137
#-------------------------------------------------------------------------------
138
# Function  : parseCommandLine
139
#
140
# Purpose   : Parses command line
141
#
142
# Arguments : none
143
#
144
# Returns   : none
145
#
146
# Notes     : 
147
#
148
sub parseCommandLine()
149
{
150
    my $opt_help = 0;
151
    my $opt_manual = 0;
152
 
153
    my $result = GetOptions (
154
    "help"          => \$opt_help,              # flag
155
    "manual"        => \$opt_manual,            # flag
156
    "verbose+"      => \$verbose,               # flag
157
    "log=s"         => \$logFile,               # String
158
    );
159
 
160
    pod2usage(-verbose => 2) if( $opt_manual );
161
    pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help > 0 || ! $result );
162
}
163
 
164
 
165
#-------------------------------------------------------------------------------
166
# Function  : startupChecks
167
#
168
# Purpose   : Makes sure scripts are available and does other general sanity tests
169
#
170
# Arguments : none
171
#
172
# Returns   : none
173
#
174
# Notes     : 
175
#
176
sub startupChecks()
177
{
178
    finish( -1, "Could not open logfile '$logFile'\n")
179
        if( $logFile && !open( LOGFILE, ">$logFile") );
180
 
181
    $logFile    =~ s~\\~/~g if $logFile;
182
 
183
    finish( -1, "DDL file '$scriptsDir/test/$ddlFile' not found\n") unless -e "$scriptsDir/test/$ddlFile";
184
 
185
    finish( -1, "Don't put spaces in your DDL filename!\n")
186
        if( $ddlFile =~ m/\s/ );
187
 
188
    finish( -1, "Could not find schemadump.pl in ${scriptsDir}schemadump.pl\n") unless -e "${scriptsDir}schemadump.pl";
189
    finish( -1, "Could not find ddlfile.pl in ${scriptsDir}ddlfile.pl\n")    unless -e "${scriptsDir}ddlfile.pl";
190
}
191
 
192
 
193
#-------------------------------------------------------------------------------
194
# Function  : readLines
195
#
196
# Purpose   : Reads in the lines of a file into an array
197
#
198
# Arguments : $filename (i) - Filename to parse lines from
199
#
200
# Returns   : @lines - lines of the file
201
#
202
# Notes     : Strips out new line characters and skips over whitespace lines
203
#
204
sub readLines($)
205
{
206
    my ($filename) = @_;
207
 
208
    my @lines;
209
 
210
    open( F, $filename) or finish( -1, "Could not open '$filename' for reading\n");
211
    while( <F> )
212
    {
213
        next if m~^\s*$~;
214
        s~[\n\r]+$~~;
215
        push @lines, $_;
216
    }
217
    close( F );
218
 
219
    return @lines;
220
}
221
 
222
 
223
 
224
#-------------------------------------------------------------------------------
225
# Function  : cleanup
226
#
227
# Purpose   : Makes sure the temporary files used by this script are removed from
228
#             the file system.
229
#
230
# Arguments : $unpackDir (gi) - A directory to recursively remove
231
#             $repackFile (gi) - Reconstructed DDL file to remove
232
#             $repackLogFile (gi) - Log file for ddlfile.pl to remove
233
#             $unpackLogFile (gi) - Log file for schemadump.pl to remove
234
#
235
# Returns   : nothing
236
#
237
# Notes     : Calls die() as cleanup is called by finish()
238
#
239
sub cleanup()
240
{
241
    rmtree $unpackDir if -e $unpackDir;
242
    unlink $repackFile if -e $repackFile;
243
    unlink $unpackLogFile if -e $unpackLogFile;
244
    unlink $repackLogFile if -e $repackLogFile;
245
 
246
    die "Couldn't remove directory '$unpackDir'\n" if -e $unpackDir;
247
    die "Couldn't remove file '$repackFile'\n" if -e $repackFile;
248
    die "Couldn't remove file '$repackLogFile'\n" if -e $repackLogFile;
249
    die "Couldn't remove file '$unpackLogFile'\n" if -e $unpackLogFile;
250
}
251
 
252
 
253
#-------------------------------------------------------------------------------
254
# Function  : main
255
#
256
# Purpose   : Provides scope
257
#
258
# Arguments : none
259
#
260
# Returns   : none
261
#
262
# Notes     : Does all the real work of this script
263
#
264
sub main()
265
{
266
    cleanup();
267
 
268
    finish( -1, "Could not create temp directory '$unpackDir'\n") unless mkdir $unpackDir;
269
 
270
    #
271
    # Create the directory structure and the reconstructed DDL file
272
    #
273
    if( $UNIX )
274
    {
275
        runCommand "cd $scriptsDir;./schemadump.pl -src=test/$ddlFile -dest=$unpackDir -log=$unpackLogFile -verbose";
276
        runCommand "cd $scriptsDir;./ddlfile.pl -src=$unpackDir -dest=$repackFile -log=$repackLogFile -verbose";
277
    }
278
    else
279
    {
280
        runCommand "chdir $scriptsDir & schemadump.pl -src=test/$ddlFile -dest=$unpackDir -log=$unpackLogFile -verbose";
281
        runCommand "chdir $scriptsDir & ddlfile.pl -src=$unpackDir -dest=$repackFile -log=$repackLogFile -verbose";
282
    }
283
 
284
    my @origLines = readLines( "$scriptsDir/test/$ddlFile" );
285
    my @newLines  = readLines( $repackFile );
286
 
287
    #
288
    # Now, check that the two DDL files have the same information content
289
    #
290
    for( my $i = 0; $i < scalar(@origLines); $i++)
291
    {
292
        finish( -1, "Ran out of new lines at line $i\n") if( scalar(@newLines) < $i );
293
        finish( -1, "Line $i is the first line to differ\n") if( $origLines[$i] ne $newLines[$i] );
294
    }
295
 
296
    finish( -1, "Originally there were only " . scalar(@origLines)
297
        . " non-blank lines; now there are " . scalar(@newLines) . "\n")
298
        if( scalar(@newLines) > scalar(@origLines) );
299
}
300
 
301
 
302
#-------------------------------------------------------------------------------
303
# Function  : runCommand
304
#
305
# Purpose   : Runs a command in the shell
306
#
307
# Arguments : $cmd (i) - command to run.  Basically this is all arguments that should
308
#                        not be quoted.
309
#             @args (i) - additional arguments.  These are all quoted, so be careful
310
#                         when passing in wildcard arguments.
311
#
312
# Returns   : The shell stdout output of the command
313
#
314
# Notes     :
315
#
316
sub runCommand($@)
317
{
318
    my ($cmd, @args) = @_;
319
 
320
    my $fullCmd = $cmd;
321
    $fullCmd .= " " . quoteCommand(@args) if @args;
322
 
323
    logprint "Running command '$fullCmd'";
324
    return hchomp `$fullCmd`;
325
}
326
 
327
 
328
#-------------------------------------------------------------------------------
329
# Function  : logprint
330
#
331
# Purpose   : Prints comments
332
#
333
# Arguments : $s (i) - string to be printed
334
#
335
# Returns   : nothing
336
#
337
# Notes     : Prints to screen if verbose
338
#             Prints to log if logfile specified on command line
339
#
340
sub logprint($)
341
{
342
    my ($s) = @_;
343
    $s =~ s~[\n\r]$~~;
344
    print "Log: $s\n" if( $verbose );
345
    print LOGFILE "Log: $s\n" if( $logFile );
346
}
347
 
348
 
349
#-------------------------------------------------------------------------------
350
# Function  : hchomp
351
#
352
# Purpose   : Remove newline characters from the end of a string
353
#
354
# Arguments : A single string, or an array of strings
355
#
356
# Returns   : Input without newline characters
357
#
358
# Notes     :
359
#
360
sub hchomp
361
{
362
    return @_ unless @_;
363
 
364
    my @stuff = @_;
365
 
366
    foreach my $a (@stuff)
367
    {
368
        $a =~ s~[\n\r]+$~~;
369
    }
370
 
371
    return ( (scalar(@stuff) == 1) ? $stuff[0] : @stuff );
372
}
373
 
374
#######################################################################
375
# Documentation
376
#######################################################################
377
 
378
=pod
379
 
380
=head1 NAME
381
 
382
test_schemadump.pl - unit test for schemadump.pl and ddlfile.pl
383
 
384
=head1 SYNOPSIS
385
 
386
test_schemadump.pl [options]
387
 
388
Options:
389
 
390
  -help              - brief help message
391
  -man               - Full documentation
392
  -log=logFile       - Log messages to this file
393
 
394
=head1 OPTIONS
395
 
396
=over 8
397
 
398
=item B<-help>
399
 
400
Print a brief help message and exits.
401
 
402
=item B<-man>
403
 
404
Prints the manual page and exits.
405
 
406
=item B<-log=logFile>
407
 
408
Specify a file to write log messages to.  Default is to just write
409
to the terminal.
410
 
411
=back
412
 
413
=head1 DESCRIPTION
414
 
415
This script is used to unit test schemadump.pl and ddlfile.pl.
416
It does this by running schemadump.pl to create a directory
417
structure, then running ddlfile.pl to recreate the input
418
DDL file.  This recreated file is compared with the original
419
input file to schemadump.pl and a check is performed to ensure
420
that no information is lost.
421
 
422
=cut