Skip to content

Commit e167bbe

Browse files
committed
Add integration tests with expected exceptions for "strict" mode. (#210, #140)
1 parent 6291ed5 commit e167bbe

File tree

10 files changed

+94
-35
lines changed

10 files changed

+94
-35
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ package-lock.json
1212
/metafix/src/test/resources/org/metafacture/metafix/integration/**/*.err
1313
/metafix/src/test/resources/org/metafacture/metafix/integration/**/*.out
1414
/metafix/src/test/resources/org/metafacture/metafix/integration/**/output-*
15+
!/metafix/src/test/resources/org/metafacture/metafix/integration/**/expected.err

metafix/integrationTest.sh

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ todo_file=todo.txt
1212

1313
input_glob=input.*
1414
expected_glob=expected.*
15+
expected_errors_extension=err
1516

1617
metafix_output_glob=output-metafix.*
1718
catmandu_output_glob=output-catmandu.*
@@ -102,6 +103,8 @@ function get_file() {
102103
reason="Ambiguous $type files: $*"
103104
elif [ ! -r "$1" ]; then
104105
reason="No $type file: $1"
106+
elif [ "$type" != "output" ] && [ ! -s "$1" ]; then
107+
reason="Empty $type file: $1"
105108
else
106109
current_file=$1
107110
return 0
@@ -138,6 +141,40 @@ function skip_test() {
138141
fi
139142
}
140143

144+
function test_passed() {
145+
if [ -r "$2" ]; then
146+
log "$color_test$1$color_reset: ${color_failed}FAILED$color_reset (Marked as \"to do\", but passed.)"
147+
148+
((failed++)) || true
149+
else
150+
if parse_boolean "$METAFIX_LOG_PASSED"; then
151+
log "$color_test$1$color_reset: ${color_passed}PASSED$color_reset$3"
152+
fi
153+
154+
((passed++)) || true
155+
fi
156+
}
157+
158+
function test_failed() {
159+
if ! skip_test "$1" "$2"; then
160+
log "$color_test$1$color_reset: $color_failed$4$color_reset$3"
161+
162+
if [ $# -ge 13 ]; then
163+
log " Fix: $9"
164+
log " Input: ${10}"
165+
log " Expected: ${11}"
166+
log " Output: ${12}"
167+
log " Diff: ${13}"
168+
169+
[ -s "${13}" ] && $colordiff <"${13}" || rm -f "${13}"
170+
fi
171+
172+
command_info "$5" "$6" "$7" "$8"
173+
174+
((failed++)) || true
175+
fi
176+
}
177+
141178
function run_tests() {
142179
local test matched=1\
143180
test_directory test_fix test_input test_expected test_todo\
@@ -165,62 +202,51 @@ function run_tests() {
165202
test_todo="$test_directory/$todo_file"
166203

167204
if [ -z "$disable_todo" ] || ! skip_test "$test" "$test_todo"; then
205+
# TODO: catmandu (optional)
206+
168207
metafix_command_output="$test_directory/metafix.out"
169208
metafix_command_error="$test_directory/metafix.err"
170209

171210
metafix_start_time=$(current_time)
172211

173-
# TODO: catmandu (optional)
212+
run_metafix "$test_directory/$metafix_file" >"$metafix_command_output" 2>"$metafix_command_error"
213+
metafix_exit_status=$?
174214

175-
if run_metafix "$test_directory/$metafix_file" >"$metafix_command_output" 2>"$metafix_command_error"; then
176-
metafix_exit_status=$?
215+
metafix_elapsed_time=$(elapsed_time "$metafix_start_time")
177216

217+
if [ "$metafix_exit_status" -eq 0 ]; then
178218
if get_file "$test" output "$test_directory"/$metafix_output_glob; then
179219
metafix_output=$current_file
180220
metafix_diff="$test_directory/metafix.diff"
181221

182-
metafix_elapsed_time=$(elapsed_time "$metafix_start_time")
183-
184222
if diff -u "$test_expected" "$metafix_output" >"$metafix_diff"; then
185-
if [ -r "$test_todo" ]; then
186-
log "$color_test$test$color_reset: ${color_failed}FAILED$color_reset (Marked as \"to do\", but passed.)"
187-
188-
((failed++)) || true
189-
else
190-
if parse_boolean "$METAFIX_LOG_PASSED"; then
191-
log "$color_test$test$color_reset: ${color_passed}PASSED$color_reset$metafix_elapsed_time"
192-
fi
193-
194-
((passed++)) || true
195-
fi
223+
test_passed "$test" "$test_todo" "$metafix_elapsed_time"
196224

197225
rm -f "$metafix_diff" "$metafix_command_output" "$metafix_command_error"
198-
elif ! skip_test "$test" "$test_todo"; then
199-
log "$color_test$test$color_reset: ${color_failed}FAILED$color_reset$metafix_elapsed_time"
200-
201-
log " Fix: $test_fix"
202-
log " Input: $test_input"
203-
log " Expected: $test_expected"
204-
log " Output: $metafix_output"
205-
log " Diff: $metafix_diff"
206-
207-
[ -s "$metafix_diff" ] && $colordiff <"$metafix_diff" || rm -f "$metafix_diff"
208-
209-
command_info metafix "$metafix_exit_status" "$metafix_command_output" "$metafix_command_error"
210-
211-
((failed++)) || true
226+
else
227+
test_failed "$test" "$test_todo" "$metafix_elapsed_time" FAILED\
228+
metafix "$metafix_exit_status" "$metafix_command_output" "$metafix_command_error"\
229+
"$test_fix" "$test_input" "$test_expected" "$metafix_output" "$metafix_diff"
212230
fi
213231
else
214232
command_info metafix "$metafix_exit_status" "$metafix_command_output" "$metafix_command_error"
215233
fi
216-
elif ! skip_test "$test" "$test_todo"; then
217-
metafix_exit_status=$?
234+
elif [ "${test_expected##*.}" == "$expected_errors_extension" ]; then
235+
get_file "$test" error "$metafix_command_error" || { log; continue; }
218236

219-
log "$color_test$test$color_reset: ${color_error}ERROR$color_reset"
237+
while read -r pattern; do
238+
if ! grep -qE "$pattern" "$metafix_command_error"; then
239+
test_failed "$test" "$test_todo" " (Pattern not found: $pattern)" FAILED\
240+
metafix "$metafix_exit_status" "$metafix_command_output" "$metafix_command_error"
220241

221-
command_info metafix "$metafix_exit_status" "$metafix_command_output" "$metafix_command_error"
242+
continue 2
243+
fi
244+
done <"$test_expected"
222245

223-
((failed++)) || true
246+
test_passed "$test" "$test_todo" "$metafix_elapsed_time"
247+
else
248+
test_failed "$test" "$test_todo" "$metafix_elapsed_time" ERROR\
249+
metafix "$metafix_exit_status" "$metafix_command_output" "$metafix_command_error"
224250
fi
225251
fi
226252
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^Exception in thread "main" org\.metafacture\.metafix\.FixExecutionException: Error while executing Fix expression \(at file:.*/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/strictnessAbortProcessOnExecutionException/test\.fix, line 2\): upcase\("data"\)$
2+
^Caused by: java\.lang\.IllegalStateException: Expected String, got Array$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"data":"foo"}
2+
{"data":"foo","data":"bar"}
3+
{"data":"bar"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_field("before", "")
2+
upcase("data")
3+
add_field("after", "")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FLUX_DIR + "input.json"
2+
|open-file
3+
|as-records
4+
|decode-json
5+
|fix(FLUX_DIR + "test.fix", strictness="process")
6+
|encode-json
7+
|write(FLUX_DIR + "output-metafix.json")
8+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^Exception in thread "main" org\.metafacture\.metafix\.FixProcessException: Error while executing Fix expression \(at file:.*/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/strictnessAbortProcessOnProcessException/test\.fix, line 2\): foo\(\)$
2+
^Caused by: java\.lang\.IllegalArgumentException: No enum constant org\.metafacture\.metafix\.FixMethod.foo$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"data":"foo"}
2+
{"data":"foo","data":"bar"}
3+
{"data":"bar"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_field("before", "")
2+
foo()
3+
add_field("after", "")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FLUX_DIR + "input.json"
2+
|open-file
3+
|as-records
4+
|decode-json
5+
|fix(FLUX_DIR + "test.fix", strictness="expression")
6+
|encode-json
7+
|write(FLUX_DIR + "output-metafix.json")
8+
;

0 commit comments

Comments
 (0)