Skip to content

Commit 6b3e903

Browse files
committed
dispatch test_reference pipeline according to TestMode
1 parent 46d650a commit 6b3e903

File tree

3 files changed

+104
-21
lines changed

3 files changed

+104
-21
lines changed

src/ReferenceTests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export
1515
@test_reference,
1616
psnr_equality
1717

18+
include("testmode.jl")
1819
include("utils.jl")
1920
include("test_reference.jl")
2021
include("fileio.jl")

src/test_reference.jl

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ function test_reference(
9999

100100
path = file.filename
101101
dir, filename = splitdir(path)
102+
testmode = TESTMODE()
102103

103104
# infer the default rendermode here
104105
# since `nothing` is always passed to this method from
@@ -112,19 +113,11 @@ function test_reference(
112113
println("Reference file for \"$filename\" does not exist.")
113114
render(rendermode, raw_actual)
114115

115-
if !isinteractive()
116-
error("You need to run the tests interactively with 'include(\"test/runtests.jl\")' to create new reference images")
117-
end
118-
119-
if !input_bool("Create reference file with above content (path: $path)?")
116+
continue_test = _create_new_reference(testmode, file, raw_actual)
117+
if !continue_test
120118
@test false
121-
else
122-
mkpath(dir)
123-
savefile(file, raw_actual)
124-
@info("Please run the tests again for any changes to take effect")
119+
return nothing
125120
end
126-
127-
return nothing # skip current test case
128121
end
129122

130123
# file exists
@@ -137,21 +130,63 @@ function test_reference(
137130
end
138131

139132
if equiv(reference, actual)
140-
@test true # to increase test counter if reached
133+
@test true
141134
else
142135
# post-processing when test fails
143136
println("Test for \"$filename\" failed.")
144137
render(rendermode, reference, actual)
145138

146-
if !isinteractive()
147-
error("You need to run the tests interactively with 'include(\"test/runtests.jl\")' to update reference images")
148-
end
139+
increase_fail_count = _update_reference(testmode, file, reference, actual)
140+
increase_fail_count && @test false
141+
end
142+
end
149143

150-
if !input_bool("Replace reference with actual result (path: $path)?")
151-
@test false
152-
else
153-
savefile(file, actual)
154-
@info("Please run the tests again for any changes to take effect")
155-
end
144+
function _create_new_reference(::NonInteractiveMode, file::File, actual)::Bool
145+
error("You need to run the tests interactively with 'include(\"test/runtests.jl\")' to update reference images")
146+
147+
# # automatically create new reference file and continue test
148+
# path = file.filename
149+
# dir, filename = splitdir(path)
150+
151+
# println("Create new reference file \"$filename\".")
152+
# mkpath(dir)
153+
# savefile(file, actual)
154+
155+
# continue_test = true
156+
# return continue_test
157+
end
158+
159+
function _create_new_reference(::InteractiveMode, file::File, actual)::Bool
160+
path = file.filename
161+
dir = splitdir(path)[1]
162+
163+
if !input_bool("Create reference file with above content (path: $path)?")
164+
continue_test = false
165+
else
166+
mkpath(dir)
167+
savefile(file, actual)
168+
continue_test = true
169+
end
170+
return continue_test
171+
end
172+
173+
function _update_reference(::NonInteractiveMode, file::File, reference, actual)::Bool
174+
error("You need to run the tests interactively with 'include(\"test/runtests.jl\")' to update reference images")
175+
176+
# # Do not update reference
177+
# increase_fail_count = true
178+
# return increase_fail_count
179+
end
180+
181+
function _update_reference(::InteractiveMode, file::File, reference, actual)::Bool
182+
path = file.filename
183+
184+
if !input_bool("Replace reference with actual result (path: $path)?")
185+
increase_fail_count = true
186+
else
187+
savefile(file, actual)
188+
@info("Please run the tests again for any changes to take effect")
189+
increase_fail_count = false
156190
end
191+
return increase_fail_count
157192
end

src/testmode.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

0 commit comments

Comments
 (0)