From d02740f4c5afbeeb7a76d42d8c247fdb89f69cbd Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Sat, 19 May 2018 11:28:01 -0700 Subject: [PATCH] Use IOContexts instead of Base.have_color on 0.7 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. --- src/DeepDiffs.jl | 7 ++++++ src/arrays.jl | 2 +- src/strings.jl | 4 +-- test/display.jl | 65 +++++++++++++++++++++++++++++------------------- test/runtests.jl | 3 +++ 5 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/DeepDiffs.jl b/src/DeepDiffs.jl index f28b047..f534f7d 100644 --- a/src/DeepDiffs.jl +++ b/src/DeepDiffs.jl @@ -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) diff --git a/src/arrays.jl b/src/arrays.jl index 6473ac6..4f182e6 100644 --- a/src/arrays.jl +++ b/src/arrays.jl @@ -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) diff --git a/src/strings.jl b/src/strings.jl index b232c5e..a92d7af 100644 --- a/src/strings.jl +++ b/src/strings.jl @@ -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, "-}") @@ -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 diff --git a/test/display.jl b/test/display.jl index 24f2014..3c34ae7 100644 --- a/test/display.jl +++ b/test/display.jl @@ -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. @@ -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 @@ -76,8 +92,7 @@ ) @testset "Color Diffs" begin - eval(Base, :(have_color=true)) - buf = IOBuffer() + buf = setcolor(true) display(TextDisplay(buf), d) expected = """ Dict( @@ -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( @@ -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 @@ -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 @@ -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 @@ -180,6 +193,6 @@ multiline output\"\"\"""" end - eval(Base, :(have_color=$orig_color)) + resetcolor() end end diff --git a/test/runtests.jl b/test/runtests.jl index 99d72ab..b7c2f13 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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")