Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0 release and do not automatically create if missing #130

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/UnitTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ jobs:
run: |
julia --color=yes --check-bounds=yes --project -e "using Pkg; Pkg.test(coverage=true)"
rm -rf test/references
export JULIA_REFERENCETESTS_UPDATE=true
julia --color=yes --check-bounds=yes --project -e "using Pkg; Pkg.test(coverage=true)"
export JULIA_REFERENCETESTS_UPDATE=false
julia --color=yes --check-bounds=yes --project -e "using Pkg; Pkg.test(coverage=true)"
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
- name: Upload Mismatched Files as a Build Artifact
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ReferenceTests"
uuid = "324d217c-45ce-50fc-942e-d289b448e8cf"
authors = ["Christof Stocker <[email protected]>", "Frames White <[email protected]>", "Johnny Chen <[email protected]>"]
version = "0.10.4"
version = "1.0.0"

[deps]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Expand Down
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ using ReferenceTests

If you put the above code into your `test/runtests.jl` and
execute the file in an interactive julia session (i.e. with
`include`), then it will trigger an interactive dialog if the
results don't match. This dialog allows the user to update the
reference files. If you do not want to be prompted, just
delete the reference data before running the tests.
`include`), then it will trigger an interactive dialog if the refrence is missing or the results don't match.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`include`), then it will trigger an interactive dialog if the refrence is missing or the results don't match.
`include`), then it will trigger an interactive dialog if the reference is missing or the results don't match.

This dialog allows the user to create/update the reference files.
If you do not want to be prompted, set `ENV["JULIA_REFERENCETESTS_UPDATE"]=true` before running.

![readme1](https://user-images.githubusercontent.com/10854026/30002940-3ba480b0-90b6-11e7-93f6-148ac38bd695.png)

Expand Down Expand Up @@ -91,13 +90,11 @@ test suite. These tests are easy to run via `pkg> test` but
the child process used within `pkg> test` is non-interactive, so the
update prompt will not show if there are mismatches.

To update references within a package test suite, there are three options:
To update/create references within a package test suite, there are two options:

- Set the environment variable `JULIA_REFERENCETESTS_UPDATE` to `"true"`
and run `pkg> test`, which will force update any non-matches. You can then
check changes to any git-tracked reference images before commit.
- Delete any reference images you wish to update and run `pkg> test`, given
that missing references are created automatically.
and run `pkg> test`, which will force update any non-matches and create any missing files.
You can then check changes to any git-tracked reference images before commit.
- Run the `test/runtests.jl` interactively. This may be easier using
the [`TestEnv.jl`](https://github.com/JuliaTesting/TestEnv.jl) package,
given that the test environment used by `pkg> test` is a merge of the
Expand Down
67 changes: 38 additions & 29 deletions src/test_reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ function test_reference(
rendermode=nothing;
kw...) where {F <: DataFormat, T}

reference_path = reference_file.filename
reference_dir, reference_filename = splitdir(reference_path)

reference_filename = basename(reference_file.filename)
actual = _convert(F, raw_actual; kw...)

# infer the default rendermode here
Expand All @@ -129,14 +127,14 @@ function test_reference(
rendermode = default_rendermode(F, actual)
end

if !isfile(reference_path) # when reference file doesn't exists
mkpath(reference_dir)
savefile(reference_file, actual)
@info """Reference file for \"$reference_filename\" did not exist. It has been created:
if !isfile(reference_file.filename) # when reference file doesn't exists
actual_path = save_mismatch_file(reference_file, actual)

@info """Reference file for \"$reference_filename\" did not exist:
$(render(rendermode, actual))
""" new_reference = reference_path
""" actual=actual_path

@info "Please run the tests again for any changes to take effect"
handle_mismatch(reference_file, actual_path)
return nothing # skip current test case
end

Expand All @@ -152,33 +150,44 @@ function test_reference(
@test true # to increase test counter if reached
else # When test fails
# Saving actual file so user can look at it
actual_path = joinpath(mismatch_staging_dir(), reference_filename)
actual_file = typeof(reference_file)(actual_path)
savefile(actual_file, actual)
actual_path = save_mismatch_file(reference_file, actual)

# Report to user.
@info """Reference Test for \"$reference_filename\" failed:
$(render(rendermode, reference, actual))
""" reference = reference_path actual = actual_path

if !isinteractive() && !force_update()
error("""
To update the reference images either run the tests interactively with 'include(\"test/runtests.jl\")',
or to force-update all failing reference images set the environment variable `JULIA_REFERENCETESTS_UPDATE`
to "true" and re-run the tests via Pkg.
""")
end

if force_update() || input_bool("Replace reference with actual result?")
mv(actual_path, reference_path; force=true) # overwrite old file it
@info "Please run the tests again for any changes to take effect"
else
@test false
end
""" reference=reference_file.filename actual=actual_path
handle_mismatch(reference_file, actual_path)
end
end

function save_mismatch_file(reference_file::File{F}, actual) where F
# Saving actual file so user can look at it
actual_path = joinpath(mismatch_staging_dir(), basename(reference_file.filename))
actual_file = File{F}(actual_path)

savefile(actual_file, actual)
return actual_path
end

function handle_mismatch(reference_file, actual_path)
if !isinteractive() && !force_update()
error("""
To update the reference images either run the tests interactively with 'include(\"test/runtests.jl\")',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To update the reference images either run the tests interactively with 'include(\"test/runtests.jl\")',
To update the reference images either run the tests interactively with `include(\"test/runtests.jl\")`,

or to force-update/create all failing reference images set the environment variable `JULIA_REFERENCETESTS_UPDATE`
to "true" and re-run all tests.
""")
end

if force_update() || input_bool("Replace reference with actual result?")
mkpath(dirname(reference_file.filename))
mv(actual_path, reference_file.filename; force=true) # overwrite old file
@info "Please run the tests again for any changes to take effect"
else
@test false
end
end

force_update() = tryparse(Bool, get(ENV, "JULIA_REFERENCETESTS_UPDATE", "false")) === true
force_update() = tryparse(Bool, get(ENV, "JULIA_REFERENCETESTS_UPDATE", "false"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems problematic as this function can now return nothing which will fail when it's used in a conditional. I do think it's reasonable to throw an exception if a user sets this incorrectly though:

Suggested change
force_update() = tryparse(Bool, get(ENV, "JULIA_REFERENCETESTS_UPDATE", "false"))
force_update() = parse(Bool, get(ENV, "JULIA_REFERENCETESTS_UPDATE", "false"))

Alternatively, we can just silently default to false:

Suggested change
force_update() = tryparse(Bool, get(ENV, "JULIA_REFERENCETESTS_UPDATE", "false"))
force_update() = get(ENV, "JULIA_REFERENCETESTS_UPDATE", "false") == "true"


"""
mismatch_staging_dir()
Expand Down
12 changes: 8 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,25 @@ end
mktempdir() do path
newfilename = joinpath(path, "newfilename.$ext")
@assert !isfile(newfilename)
with_logger(test_logger) do
@test_reference newfilename val # this should create it
withenv("JULIA_REFERENCETESTS_UPDATE"=>true) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
withenv("JULIA_REFERENCETESTS_UPDATE"=>true) do
withenv("JULIA_REFERENCETESTS_UPDATE" => "true") do

with_logger(test_logger) do
@test_reference newfilename val # this should create it
end
end
@test isfile(newfilename) # Was created
@test_reference newfilename val # Matches expected content
end
end
end

@testset "Create new image as txt" begin
# This is a separate testset as need to use the `size` argument to ``@test_reference`
mktempdir() do path
newfilename = joinpath(path, "new_camera.txt")
@assert !isfile(newfilename)
with_logger(test_logger) do
@test_reference newfilename camera size=(5,10) # this should create it
withenv("JULIA_REFERENCETESTS_UPDATE"=>true) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
withenv("JULIA_REFERENCETESTS_UPDATE"=>true) do
withenv("JULIA_REFERENCETESTS_UPDATE" => "true") do

@test_reference newfilename camera size=(5,10) # this should create it
end
end
@test isfile(newfilename) # Was created
@test_reference newfilename camera size=(5,10) # Matches expected content
Expand Down
Loading