Skip to content

Commit

Permalink
Use IOContexts instead of Base.have_color on 0.7
Browse files Browse the repository at this point in the history
In Julia 0.7, whether an IO object supports color is a property of its
context; Base.have_color refers only to whether Julia was launched with
--color=yes. In 0.6, controlling color requires manipulating that global
flag. Thus we'll hack around the difference with version checking.
  • Loading branch information
ararslan committed May 19, 2018
1 parent d75d12b commit d02740f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 29 deletions.
7 changes: 7 additions & 0 deletions src/DeepDiffs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ function fieldequal(x::T, y::T) where T
true
end

# Determine whether color is supported by the given stream
@static if VERSION >= v"0.7.0-DEV.3077"
hascolor(io::IO) = get(IOContext(io), :color, false)
else
hascolor(io::IO) = Base.have_color
end

"""
diff = deepdiff(obj1, obj2)
Expand Down
2 changes: 1 addition & 1 deletion src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ end

# prefix is printed if we're not using color
function printitem(io, v, color=:normal, prefix="")
if Base.have_color
if hascolor(io)
printstyled(io, v, color=color)
else
print(io, prefix, v)
Expand Down
4 changes: 2 additions & 2 deletions src/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function Base.show(io::IO, diff::StringDiff)

print(io, "\"")
visitall(diff.diff) do idx, state, last
if !Base.have_color
if !hascolor(io)
# check to see if we need to close a block
if laststate == :removed && state != :removed
print(io, "-}")
Expand All @@ -85,7 +85,7 @@ function Base.show(io::IO, diff::StringDiff)
end
laststate = state
end
if !Base.have_color
if !hascolor(io)
if laststate == :removed
print(io, "-}")
elseif laststate == :added
Expand Down
65 changes: 39 additions & 26 deletions test/display.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
@testset "Display tests" begin
# Return a stream with color set as specified. On 0.6 this requires setting
# a global flag, and the :color property in the IOContext has no effect.
function setcolor(yn::Bool)
if VERSION < v"0.7.0-DEV.3077"
eval(Base, :(have_color = $yn))
end
IOContext(IOBuffer(), :color=>yn)
end

function resetcolor()
global orig_color
if VERSION < v"0.7.0-DEV.3077"
eval(Base, :(have_color = $orig_color))
end
nothing
end

# check dictionary print output. This is a little complicated because
# the ordering isn't specified. To work around this we just split
# up both into lines and make sure they have the same lines in some ordering.
Expand All @@ -10,34 +27,33 @@
explines = sort(split(expected, "\n"))
@test outlines == explines
end
orig_color = Base.have_color

@testset "Array diffs print correctly" begin
d1 = deepdiff([1, 2, 7, 3], [2, 3, 4, 1, 2, 3, 5])
d2 = deepdiff([1], [2])

buf = IOBuffer()
eval(Base, :(have_color=true))
buf = setcolor(true)
expected1 = """
[2, 3, 4, 1, 2, 7, 3, 5]"""
expected2 = """[1, 2]"""
@testset "Color Diffs" begin
display(TextDisplay(buf), d1)
@test String(take!(buf)) == expected1
@test String(take!(buf.io)) == expected1
display(TextDisplay(buf), d2)
@test String(take!(buf)) == expected2
@test String(take!(buf.io)) == expected2
end

eval(Base, :(have_color=false))
buf = setcolor(false)
@testset "No-Color Diffs" begin
display(TextDisplay(buf), d1)
@test String(take!(buf)) == """
@test String(take!(buf.io)) == """
[(+)2, (+)3, (+)4, 1, 2, (-)7, 3, (+)5]"""
display(TextDisplay(buf), d2)
@test String(take!(buf)) == """
@test String(take!(buf.io)) == """
[(-)1, (+)2]"""
end

eval(Base, :(have_color=$orig_color))
resetcolor()
end

@testset "Dict diffs print correctly" begin
Expand Down Expand Up @@ -76,8 +92,7 @@
)

@testset "Color Diffs" begin
eval(Base, :(have_color=true))
buf = IOBuffer()
buf = setcolor(true)
display(TextDisplay(buf), d)
expected = """
Dict(
Expand All @@ -101,11 +116,10 @@
# This test is broken because the specifics of how the ANSI color
# codes are printed change based on the order, which changes with
# different julia versions.
@test_skip String(take!(buf)) == expected
@test_skip String(take!(buf.io)) == expected
end
@testset "No-Color Diffs" begin
eval(Base, :(have_color=false))
buf = IOBuffer()
buf = setcolor(false)
display(TextDisplay(buf), d)
expected = """
Dict(
Expand All @@ -126,19 +140,19 @@
),
+ :e => "e",
)"""
checkdictprint(String(take!(buf)), expected)
checkdictprint(String(take!(buf.io)), expected)
end
eval(Base, :(have_color=$orig_color))

resetcolor()
end

@testset "single-line strings display correctly" begin
# this test is just to handle some cases that don't get exercised elsewhere
diff = deepdiff("abc", "adb")
buf = IOBuffer()
eval(Base, :(have_color=false))
buf = setcolor(false)
display(TextDisplay(buf), diff)
@test String(take!(buf)) == "\"a{+d+}b{-c-}\""
eval(Base, :(have_color=true))
@test String(take!(buf.io)) == "\"a{+d+}b{-c-}\""
resetcolor()
end

@testset "Multi-line strings display correctly" begin
Expand All @@ -154,9 +168,8 @@
multiline
output"""
diff = deepdiff(s1, s2)
buf = IOBuffer()
@testset "Color Display" begin
eval(Base, :(have_color=true))
buf = setcolor(true)
expected = """
\"\"\"
differences can
Expand All @@ -166,12 +179,12 @@
multiline
output\"\"\""""
display(TextDisplay(buf), diff)
@test String(take!(buf)) == expected
@test String(take!(buf.io)) == expected
end
@testset "No-Color Display" begin
eval(Base, :(have_color=false))
buf = setcolor(false)
display(TextDisplay(buf), diff)
@test String(take!(buf)) == """
@test String(take!(buf.io)) == """
\"\"\"
differences can
- be hard to find
Expand All @@ -180,6 +193,6 @@
multiline
output\"\"\""""
end
eval(Base, :(have_color=$orig_color))
resetcolor()
end
end
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ using DeepDiffs
using Compat
using Compat.Test

# Capture the original state of the global flag
orig_color = Base.have_color

@testset "DeepDiff Tests" begin
include("arrays.jl")
include("dicts.jl")
Expand Down

0 comments on commit d02740f

Please sign in to comment.