|
| 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 | + |
1 | 49 | #################################################
|
2 | 50 | # Rendering
|
3 | 51 | # This controls how failures are displayed
|
|
79 | 127 | function _test_reference(equiv, rendermode, file::File, actual::T) where T
|
80 | 128 | path = file.filename
|
81 | 129 | dir, filename = splitdir(path)
|
| 130 | + testmode = TESTMODE() |
82 | 131 |
|
83 |
| - # preprocessing when reference file doesn't exists |
84 | 132 | if !isfile(path)
|
85 | 133 | println("Reference file for \"$filename\" does not exist.")
|
86 | 134 | render(rendermode, actual)
|
87 | 135 |
|
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 |
93 | 138 | @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 |
98 | 140 | end
|
99 |
| - |
100 |
| - return nothing # skip current test case |
101 | 141 | end
|
102 | 142 |
|
| 143 | + # file exists |
103 | 144 | reference = loadfile(T, file)
|
| 145 | + |
104 | 146 | if equiv(reference, actual)
|
105 |
| - @test true # to increase test counter if reached |
| 147 | + @test true |
106 | 148 | else
|
107 | 149 | # post-processing when test fails
|
108 | 150 | println("Test for \"$filename\" failed.")
|
109 | 151 | render(rendermode, reference, actual)
|
110 | 152 |
|
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 |
114 | 157 |
|
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 |
121 | 204 | end
|
| 205 | + return increase_fail_count |
122 | 206 | end
|
0 commit comments