-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidate_line
executable file
·164 lines (153 loc) · 3.23 KB
/
validate_line
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
#!/bin/bash
base_results_file=""
usage()
{
echo Usage: $1
echo " --fields <string>: Contains a list in the following format"
echo " name:type,name:type"
echo " Valid types are"
echo " n: field is a numeric value"
echo " s: field is a string of some sort"
echo " sm: field is a string and must match exactly the name"
echo " --header_lines <n>: How many lines of header do we we expect."
echo " --results_file <path>: The results file generated by the test"
echo " --base_results_file <path>: The path to the expected results that we are"
echo " to compare to."
exit $2
}
error_out()
{
echo $4
echo $3
echo field index checking: $1
echo field type: $2
exit 1
}
validate_line()
{
re='^[0-9]+$'
line=$1
field_index=`echo $2 | cut -d':' -f 1`
field_type=`echo $2 | cut -d':' -f 2`
value=`echo $1 | cut -d':' -f $field_index | sed "s/ //g"`
if [[ $value == "" ]]; then
error_out $field_index $field_type $line "Either field is not present, or has no value."
fi
if [[ $field_type == "n" ]]; then
#
# Handle floats also.
#
check_value=`echo $value | sed "s/\./0/g"`
if ! [[ $check_value =~ $re ]] ; then
error_out $field_index $field_type $line "Non numerical field"
fi
fi
if [[ $field_type == "sm" ]]; then
#
# String, needs to match.
#
grep -Fq "^${value}:" $base_results_file
if [ $? -ne 0 ]; then
error_out $field_index $field_type $line "Does not have match in the results file"
fi
fi
if [[ $field_type == "s" ]]; then
#
# Just a simple string.
#
if [[ $value == "" ]]; then
error_out $field_index $field_type $line "Should be string value in the results file"
fi
fi
}
ARGUMENT_LIST=(
"fields"
"header_lines"
"results_file"
"base_results_file"
)
NO_ARGUMENTS=(
"usage"
)
# read arguments
opts=$(getopt \
--longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
--longoptions "$(printf "%s," "${NO_ARGUMENTS[@]}")" \
--name "$(basename "$0")" \
--options "h" \
-- "$@"
)
eval set --$opts
results_file=""
fields=""
header_lines=0
while [[ $# -gt 0 ]]; do
case "$1" in
--base_results_file)
base_results_file=$2
shift 2
;;
--fields)
fields=`echo $2 | sed "s/ /_/g" | sed "s/,/ /g"`
shift 2
;;
--header_lines)
header_lines=$2
shift 2
;;
--results_file)
results_file=$2
shift 2
;;
--usage)
usage $0 0
;;
-h)
usage $0 0
;;
--)
break;
;;
*)
echo "option not found ${1}"
usage $0 1
;;
esac
done
meta_head=0
header_line=""
field_location=""
separ=""
field_index=1
while IFS= read -r test_info
do
#
# Skip over meta head
#
if [ $meta_head -eq 0 ]; then
if [[ $test_info == *"# Test general meta end"* ]]; then
meta_head=1
fi
continue
fi
if [[ $header_line == "" ]]; then
header_line=`echo $test_info | sed "s/ /_/g" | sed "s/:/ /g"`
for field in $header_line; do
for find_field in $fields; do
value=`echo $find_field | cut -d':' -f 1`
type=`echo $find_field | cut -d':' -f 2`
if [[ $value == $field ]]; then
field_location=${field_location}${separ}${field_index}:${type}
let "field_index=${field_index}+1"
separ=" "
break
fi
done
done
continue
fi
for field in $field_location; do
validate_line "$test_info" $field
done
done < "$results_file"
exit 0