Skip to content

Commit

Permalink
Merge 1582a9a into 2d0b9f0
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Jan 21, 2020
2 parents 2d0b9f0 + 1582a9a commit 46b3481
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ See also [`Run.test`](@ref) and [`Run.docs`](@ref).
- `check_bounds::Union{Nothing, Bool} = nothing`: Control
`--check-bounds` option. `nothing` means to inherit the option
specified for the current Julia session.
- `xfail::bool = false`: If failure is expected.
- `exitcodes::AbstractVector{<:Integer} = xfail ? [1] : [0]`: List of
allowed exit codes.
- Other keywords are passed to `Run.prepare_test`.
"""
script
Expand Down Expand Up @@ -135,9 +138,41 @@ function Base.show(io::IO, ::MIME"text/plain", result::Result)
if exitcode !== 0
print(io, " ")
printstyled(io, "(exit code: ", exitcode, ")"; color=:red)
println(io)
println(io, "Command: ", Cmd(result.proc.cmd.exec))
print(io, "Environment variables:")
_printenv(io, something(result.proc.cmd.env, ENV))
end
end

function _printenv(io, env)
for nv in env
if nv isa AbstractString
name, value = split(nv, "=", limit = 2)
else
name, value = nv
end
# regex taken from `versioninfo`:
if startswith(name, "JULIA") || occursin(r"PATH|FLAG|^TERM$|HOME", name)
println(io)
print(io, " ", name, " = ", value)
end
end
end

struct Failed <: Exception
result::Result
end

function Base.show(io::IO, ::MIME"text/plain", failed::Failed)
print(io, "Failed ")
show(io, MIME"text/plain"(), failed.result)
end

function Base.showerror(io::IO, failed::Failed)
show(io, MIME"text/plain"(), failed)
end

function prepare(projectpath; precompile=true, parentproject=nothing)
projectpath = dirname(existingproject(projectpath))
code = prepare_code
Expand Down Expand Up @@ -188,6 +223,8 @@ function script(
compiled_modules = nothing,
precompile = (compiled_modules != false),
parentproject = nothing,
xfail::Bool = false,
exitcodes::AbstractVector{<:Integer} = xfail ? [1] : [0],
kwargs...,
)
if get(ENV, "CI", "false") == "true"
Expand Down Expand Up @@ -226,7 +263,13 @@ function script(
end
@info "Running $script"
cmd = setenv(`$(_julia_cmd()) $julia_options $script`, env)
return Result("run finished", run(cmd))
proc = run(ignorestatus(cmd))
result = Result("run finished", proc)
if proc.exitcode in exitcodes
return result
else
throw(Failed(result))
end
end
end
# Note: Copying toml files to make it work nicely with running script
Expand Down
Empty file added test/fail/Manifest.toml
Empty file.
Empty file added test/fail/Project.toml
Empty file.
1 change: 1 addition & 0 deletions test/fail/fail.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit(1)
Empty file added test/pass/Manifest.toml
Empty file.
Empty file added test/pass/Project.toml
Empty file.
1 change: 1 addition & 0 deletions test/pass/pass.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit(0)
28 changes: 28 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
using Run
using Run: Result, Failed
using Pkg
using Test

pass_script = joinpath(@__DIR__, "pass", "pass.jl")
fail_script = joinpath(@__DIR__, "fail", "fail.jl")

@testset "xfail" begin
@testset "true pass" begin
result = nothing
@test begin
result = Run.test(pass_script)
end isa Result
@test result.proc.exitcode == 0
end
@testset "true failure" begin
@test_throws Failed Run.test(fail_script)
end

@testset "expected failure" begin
result = nothing
@test begin
result = Run.test(fail_script; xfail = true)
end isa Result
@test result.proc.exitcode == 1
end
@testset "unexpected pass" begin
@test_throws Failed Run.test(pass_script; xfail = true)
end
end

@testset "smoke test" begin
withenv("DOCUMENTER_KEY" => nothing) do
@test Run.docs(joinpath(@__DIR__, "..", "docs")) isa Any
Expand Down

0 comments on commit 46b3481

Please sign in to comment.