-
Notifications
You must be signed in to change notification settings - Fork 313
/
CompFiles.pm
218 lines (189 loc) · 5.5 KB
/
CompFiles.pm
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
###############################################################################
#
# Module: NMLTest::CompFiles
#
# Created by Erik Kluzek NCAR
#
# This is a tester built on top of Test::More to compare namelist files
# (or really any ASCII text files). There is a mechanism for telling the
# test object that you should (or should NOT) expect the comparison to be
# exact or not.
#
###############################################################################
package NMLTest::CompFiles;
use strict;
use Test::More;
use IO::File;
=head1 NAME
NMLTest::CompFiles - A comparision tester for namelist (or ASCII text) files
=head1 SYNOPSIS
=head1 DESCRIPTION
=cut
sub new {
my $self = {};
my $class = shift;
my $dir = shift;
my @files = @_;
my $nm = ref($self)."\:\:new";
my %diffref = { };
$self->{'diffref'} = \%diffref;
if ( ! -d "$dir" ) {
die "ERROR::($nm) Input directory ($dir) does NOT exist!\n";
}
$self->{'dir'} = $dir;
$self->{'files'} = \@_;
bless( $self, $class );
}
sub checkfilesexist {
#
# Check that files exist
#
my $self = shift;
my $type = shift;
my $mode = shift;
my $nm = ref($self)."\:\:checkfilesexist";
my $filesref = $self->{'files'};
my $confdir = $self->{'dir'};
foreach my $file ( @$filesref ) {
my $exists = ( -f "$confdir/$file" );
ok( $exists, "$type $mode: $file file exists" );
if ( $exists ) {
$self->dodiffonfile( $file, $type, $mode );
} else {
$self->doNOTdodiffonfile( $file, $type, $mode );
}
}
}
sub comparefiles {
#
# Compare the resultant files to the default versions
#
my $self = shift;
my $type = shift;
my $comp_mode = shift;
my $compdir = shift;
my $nm = ref($self)."\:\:comparefiles";
$type =~ s#[ '"&/]#+#g;
$comp_mode =~ s#[ '"&/]#+#g;
my $confdir = $self->{'dir'};
my $diffref = $self->{'diffref'};
if ( ! defined($type) ) {
$type = "default";
}
my $compare = "compare to previous tag";
if ( ! defined($compdir) ) {
$compdir = ".";
$compare = undef;
}
if ( ! -d "$compdir" ) {
die "ERROR($nm):: Compare directory $compdir does NOT exist!\n";
}
print "Compare files for $type type MODE=$comp_mode $compare\n";
my $diffstat;
my %diffhas = %$diffref;
my $same = "file the same as expected";
my $diff = "file different as expected";
my $filesref = $self->{'files'};
foreach my $file ( @$filesref ) {
if ( ! -f "$compdir/${file}.$comp_mode.${type}" ) {
print "WARNING($nm):: File $compdir/${file}.$comp_mode.${type} does NOT exist!\n";
fail( "compare file $file DNE for $comp_mode and $type" );
} else {
if ( ! exists($diffhas{$comp_mode}{$type}{$file}) ) {
die "ERROR($nm):: difference is NOT setup for $comp_mode ${type} $file!\n";
}
system( "diff $confdir/${file} $compdir/${file}.$comp_mode.${type} > /dev/null" );
$diffstat = $?;
if ( $diffhas{$comp_mode}{$type}{$file} ) {
ok( ! $diffstat, "$file $same for $comp_mode" );
} else {
ok( $diffstat, "$file different as expected for $comp_mode" );
}
}
}
}
sub copyfiles {
#
# Copy the namelist files to default names for comparisions
#
my $self = shift;
my $type = shift;
my $mode = shift;
my $nm = ref($self)."\:\:copyfiles";
$type =~ s#[ '"&/]#+#g;
$mode =~ s#[ '"&/]#+#g;
my $diffref = $self->{'diffref'};
my $filesref = $self->{'files'};
my $confdir = $self->{'dir'};
foreach my $file ( @$filesref ) {
system( "/bin/cp $confdir/$file ${file}.${mode}.${type}" );
$$diffref{${mode}}{${type}}{$file} = 1;
}
print "$type namelists for $mode\n";
foreach my $file ( @$filesref ) {
system( "/bin/cat $file.${mode}.${type}" );
}
}
sub shownmldiff {
#
# Show the differences in the namelists
#
my $self = shift;
my $type = shift;
my $comp_mode = shift;
my $nm = ref($self)."\:\:shownmldiff";
$type =~ s#[ '"&/]#+#g;
$comp_mode =~ s#[ '"&/]#+#g;
my $filesref = $self->{'files'};
my $confdir = $self->{'dir'};
foreach my $file ( @$filesref ) {
my $file1 = "$confdir/$file";
if ( ! -f "$file1" ) {
print "$file1 does NOT exist\n";
return;
}
my $file2 = "${file}.${comp_mode}.${type}";
if ( ! -f "$file2" ) {
print "$file2 does NOT exist\n";
return;
}
print "Diff in in $file to $type $comp_mode version";
system( "diff $file1 $file2" );
}
}
sub dodiffonfile {
#
# Set it so that it does do a difference on the given input file
#
my $self = shift;
my $file = shift;
my $type = shift;
my $mode = shift;
my $nm = ref($self)."\:\:dodiffonfile";
$type =~ s#[ '"&/]#+#g;
$mode =~ s#[ '"&/]#+#g;
my $diffref = $self->{'diffref'};
if ( ! defined($type) ) {
$type = "default";
}
$$diffref{$mode}{$type}{$file} = 1;
}
sub doNOTdodiffonfile {
#
# Set it so that it does NOT do a difference on the given input file
#
my $self = shift;
my $file = shift;
my $type = shift;
my $mode = shift;
my $nm = ref($self)."\:\:doNOTdodiffonfile";
$type =~ s#[ '"&/]#+#g;
$mode =~ s#[ '"&/]#+#g;
my $diffref = $self->{'diffref'};
if ( ! defined($type) ) {
$type = "default";
}
$$diffref{$mode}{$type}{$file} = 0;
}
#-----------------------------------------------------------------------------------------------
1 # to make use or require happy