Skip to content

Commit d87fd0b

Browse files
committed
dispatch test_reference pipeline according to TestMode
1 parent 1dfdd1d commit d87fd0b

File tree

1 file changed

+106
-22
lines changed

1 file changed

+106
-22
lines changed

Diff for: src/core.jl

+106-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,51 @@
1+
#################################################
2+
# Test mode
3+
# This controls how test cases are handled
4+
abstract type TestMode end
5+
struct InteractiveMode <: TestMode end
6+
struct NonInteractiveMode <: TestMode end
7+
8+
"""
9+
Predefined CI environment variables
10+
11+
# References
12+
13+
* Travis: https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
14+
* Appveyor: https://www.appveyor.com/docs/environment-variables/
15+
"""
16+
const CI_ENVIRONMENTS = Dict(
17+
"CI" => "true",
18+
"APPVEYOR" => "true",
19+
"TRAVIS" => "true",
20+
"CONTINUOUS_INTEGRATION" => "true",
21+
"DEBIAN_FRONTEND" => "noninteractive"
22+
)
23+
24+
function TESTMODE()
25+
global GLOBAL_TESTMODE
26+
if !isdefined(ReferenceTests, :GLOBAL_TESTMODE)
27+
# it's only called once in runtime
28+
GLOBAL_TESTMODE = _get_testmode()
29+
end
30+
return GLOBAL_TESTMODE
31+
end
32+
33+
function _get_testmode()
34+
# test if this package is used in a CI environment
35+
common_keys = collect(intersect(keys(ENV), keys(CI_ENVIRONMENTS)))
36+
matched_envs = map(common_keys) do k
37+
# some variables might have different cases in different CI platforms
38+
# e.g., in Appveyor, ENV["CI] is "True" in Windows and "true" in Ubuntu.
39+
lowercase(ENV[k])==lowercase(CI_ENVIRONMENTS[k])
40+
end
41+
has_testenv = any(matched_envs)
42+
has_testenv && return NonInteractiveMode()
43+
44+
# fallback
45+
@info "You need to run the tests interactively with 'include(\"test/runtests.jl\")' to update references."
46+
return isinteractive() ? InteractiveMode() : NonInteractiveMode()
47+
end
48+
149
#################################################
250
# Rendering
351
# This controls how failures are displayed
@@ -79,44 +127,80 @@ end
79127
function _test_reference(equiv, rendermode, file::File, actual::T) where T
80128
path = file.filename
81129
dir, filename = splitdir(path)
130+
testmode = TESTMODE()
82131

83-
# preprocessing when reference file doesn't exists
84132
if !isfile(path)
85133
println("Reference file for \"$filename\" does not exist.")
86134
render(rendermode, actual)
87135

88-
if !isinteractive()
89-
error("You need to run the tests interactively with 'include(\"test/runtests.jl\")' to create new reference images")
90-
end
91-
92-
if !input_bool("Create reference file with above content (path: $path)?")
136+
continue_test = _create_new_reference(testmode, file, actual)
137+
if !continue_test
93138
@test false
94-
else
95-
mkpath(dir)
96-
savefile(file, actual)
97-
@info("Please run the tests again for any changes to take effect")
139+
return nothing
98140
end
99-
100-
return nothing # skip current test case
101141
end
102142

143+
# file exists
103144
reference = loadfile(T, file)
145+
104146
if equiv(reference, actual)
105-
@test true # to increase test counter if reached
147+
@test true
106148
else
107149
# post-processing when test fails
108150
println("Test for \"$filename\" failed.")
109151
render(rendermode, reference, actual)
110152

111-
if !isinteractive()
112-
error("You need to run the tests interactively with 'include(\"test/runtests.jl\")' to update reference images")
113-
end
153+
increase_fail_count = _update_reference(testmode, file, reference, actual)
154+
increase_fail_count && @test false
155+
end
156+
end
114157

115-
if !input_bool("Replace reference with actual result (path: $path)?")
116-
@test false
117-
else
118-
savefile(file, actual)
119-
@info("Please run the tests again for any changes to take effect")
120-
end
158+
function _create_new_reference(::NonInteractiveMode, file::File, actual)::Bool
159+
error("You need to run the tests interactively with 'include(\"test/runtests.jl\")' to update reference images")
160+
161+
# # automatically create new reference file and continue test
162+
# path = file.filename
163+
# dir, filename = splitdir(path)
164+
165+
# println("Create new reference file \"$filename\".")
166+
# mkpath(dir)
167+
# savefile(file, actual)
168+
169+
# continue_test = true
170+
# return continue_test
171+
end
172+
173+
function _create_new_reference(::InteractiveMode, file::File, actual)::Bool
174+
path = file.filename
175+
dir = splitdir(path)[1]
176+
177+
if !input_bool("Create reference file with above content (path: $path)?")
178+
continue_test = false
179+
else
180+
mkpath(dir)
181+
savefile(file, actual)
182+
continue_test = true
183+
end
184+
return continue_test
185+
end
186+
187+
function _update_reference(::NonInteractiveMode, file::File, reference, actual)::Bool
188+
error("You need to run the tests interactively with 'include(\"test/runtests.jl\")' to update reference images")
189+
190+
# # Do not update reference
191+
# increase_fail_count = true
192+
# return increase_fail_count
193+
end
194+
195+
function _update_reference(::InteractiveMode, file::File, reference, actual)::Bool
196+
path = file.filename
197+
198+
if !input_bool("Replace reference with actual result (path: $path)?")
199+
increase_fail_count = true
200+
else
201+
savefile(file, actual)
202+
@info("Please run the tests again for any changes to take effect")
203+
increase_fail_count = false
121204
end
205+
return increase_fail_count
122206
end

0 commit comments

Comments
 (0)