From 4b6595c9d088d0253f3013e0e6c2dc1910ea1b8d Mon Sep 17 00:00:00 2001 From: t-bltg Date: Sun, 4 Dec 2022 09:36:20 +0100 Subject: [PATCH] cleanup --- .github/workflows/ci.yml | 6 -- src/backends.jl | 23 +++--- src/examples.jl | 2 +- src/init.jl | 8 +-- test/test_backends.jl | 147 ++++++++++++++++++++------------------- 5 files changed, 91 insertions(+), 95 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 09424884b..ac95c4a51 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,6 @@ jobs: GKS_ENCODING: "utf8" GKSwstype: "nul" PYTHON: "Conda" # for `PyPlot` - JULIA_PYTHONCALL_EXE: "@PyCall" # for `PythonCall` name: Julia ${{ matrix.version }} - ${{ matrix.os }} runs-on: ${{ matrix.os }} continue-on-error: ${{ matrix.version == 'nightly' }} @@ -94,11 +93,6 @@ jobs: end Conda.add([libgcc..., "matplotlib"]) Conda.list() - # configure `CondPkg` to use `Conda` setup - open(ENV["GITHUB_ENV"], "a") do io - println(io, "JULIA_CONDAPKG_EXE=", Conda.CONDA_EXE) - println(io, "JULIA_CONDAPKG_BACKEND=Null") - end Pkg.add("PyCall"); Pkg.build("PyCall"; verbose=true) - uses: julia-actions/julia-runtest@latest diff --git a/src/backends.jl b/src/backends.jl index 3bcf69aff..b2a9000ea 100644 --- a/src/backends.jl +++ b/src/backends.jl @@ -745,14 +745,14 @@ const _plotlyjs_scale = _plotly_scale # pyplot _post_init(::PyPlotBackend) = @eval begin - pycolors = PyPlot.pyimport("matplotlib.colors") - pypath = PyPlot.pyimport("matplotlib.path") - mplot3d = PyPlot.pyimport("mpl_toolkits.mplot3d") - axes_grid1 = PyPlot.pyimport("mpl_toolkits.axes_grid1") - pypatches = PyPlot.pyimport("matplotlib.patches") - pyticker = PyPlot.pyimport("matplotlib.ticker") - pycmap = PyPlot.pyimport("matplotlib.cm") - pynp = PyPlot.pyimport("numpy") + pycolors = PyCall.pyimport("matplotlib.colors") + pypath = PyCall.pyimport("matplotlib.path") + mplot3d = PyCall.pyimport("mpl_toolkits.mplot3d") + axes_grid1 = PyCall.pyimport("mpl_toolkits.axes_grid1") + pypatches = PyCall.pyimport("matplotlib.patches") + pyticker = PyCall.pyimport("matplotlib.ticker") + pycmap = PyCall.pyimport("matplotlib.cm") + pynp = PyCall.pyimport("numpy") pynp."seterr"(invalid = "ignore") @@ -765,7 +765,10 @@ function _initialize_backend(pkg::PyPlotBackend) export PyPlot $(_check_compat)(PyPlot) end - @eval const PyPlot = Main.PyPlot + @eval begin + const PyPlot = Main.PyPlot + const PyCall = Main.PyPlot.PyCall + end _post_init(pkg) end @@ -897,7 +900,7 @@ _post_init(::PythonPlotBackend) = @eval begin mpl = PythonCall.pyimport("matplotlib") numpy = PythonCall.pyimport("numpy") - PythonPlot.pyimport("mpl_toolkits.axes_grid1") + PythonCall.pyimport("mpl_toolkits.axes_grid1") numpy.seterr(invalid = "ignore") PythonPlot.ioff() # we don't want every command to update the figure diff --git a/src/examples.jl b/src/examples.jl index 477a87171..4850c0705 100644 --- a/src/examples.jl +++ b/src/examples.jl @@ -1349,7 +1349,7 @@ function test_examples( ) @info "Testing plot: $pkgname:$i:$(_examples[i].header)" - m = Module(:PlotsExamplesModule) + m = Module(Symbol(:PlotsExamples, pkgname)) # prevent leaking variables (esp. functions) directly into Plots namespace Base.eval(m, quote diff --git a/src/init.jl b/src/init.jl index 6d0a6e8a9..6b5c04f94 100644 --- a/src/init.jl +++ b/src/init.jl @@ -33,11 +33,9 @@ end function _plots_plotly_defaults() if bool_env("PLOTS_HOST_DEPENDENCY_LOCAL", "false") _plotly_local_file_path[] = - joinpath(@get_scratch!("plotly"), _plotly_min_js_filename) - isfile(_plotly_local_file_path[]) || Downloads.download( - "https://cdn.plot.ly/$(_plotly_min_js_filename)", - _plotly_local_file_path[], - ) + fn = joinpath(@get_scratch!("plotly"), _plotly_min_js_filename) + isfile(fn) || + Downloads.download("https://cdn.plot.ly/$(_plotly_min_js_filename)", fn) _use_local_plotlyjs[] = true end _use_local_dependencies[] = _use_local_plotlyjs[] diff --git a/test/test_backends.jl b/test/test_backends.jl index a8e93e663..b7b371739 100644 --- a/test/test_backends.jl +++ b/test/test_backends.jl @@ -83,9 +83,10 @@ function image_comparison_tests( example = Plots._examples[idx] @info "Testing plot: $pkg:$idx:$(example.header)" - reffn = reference_file(pkg, Plots._current_plots_version, idx) - newfn = - joinpath(reference_path(pkg, Plots._current_plots_version), ref_name(idx) * ".png") + ver = Plots._current_plots_version + ver = VersionNumber(ver.major, ver.minor, ver.patch) + reffn = reference_file(pkg, ver, idx) + newfn = joinpath(reference_path(pkg, ver), ref_name(idx) * ".png") imports = something(example.imports, :()) exprs = quote @@ -142,64 +143,6 @@ with(:pgfplotsx) do end =# -@testset "Preferences" begin - Plots.set_default_backend!() # start with empty preferences - - withenv("PLOTS_DEFAULT_BACKEND" => "invalid") do - @test_logs (:warn, r".*is not a supported backend") Plots.load_default_backend() - end - @test_logs (:warn, r".*is not a supported backend") backend(:invalid) - - @test Plots.load_default_backend() == Plots.GRBackend() - - withenv("PLOTS_DEFAULT_BACKEND" => "unicodeplots") do - @test_logs (:info, r".*environment variable") Plots.diagnostics(devnull) - @test Plots.load_default_backend() == Plots.UnicodePlotsBackend() - end - - @test Plots.load_default_backend() == Plots.GRBackend() - @test Plots.backend_package_name() === :GR - @test Plots.backend_name() === :gr - - @test_logs (:info, r".*fallback") Plots.diagnostics(devnull) - - @test Plots.merge_with_base_supported([:annotations, :guide]) isa Set - @test Plots.CurrentBackend(:gr).sym === :gr - - @test_logs (:warn, r".*is not compatible with") Plots.set_default_backend!(:invalid) - - @testset "simple restart" begin - # this test mimics a restart, which is needed after a preferences change - Plots.set_default_backend!(:unicodeplots) - script = tempname() - write( - script, - """ - ENV["PLOTS_PRECOMPILE"] = false - using Pkg, Test; io = (devnull, stdout)[1] # toggle for debugging - Pkg.activate(; temp = true, io) - Pkg.develop(; path = "$(escape_string(pkgdir(Plots)))", io) - Pkg.add("UnicodePlots"; io) # checked by Plots - using Plots - res = @testset "Prefs" begin - @test_logs (:info, r".*Preferences") Plots.diagnostics(io) - @test backend() == Plots.UnicodePlotsBackend() - end - exit(res.n_passed == 2 ? 0 : 1) - """, - ) - @test success(run(```$(Base.julia_cmd()) $script```)) - end - - is_pkgeval() || for be in CONCRETE_BACKENDS - (Sys.isapple() && be === :gaston) && continue # FIXME: hangs - @test_logs Plots.set_default_backend!(be) # test the absence of warnings - @test Base.compilecache(Base.module_keys[Plots]) isa String # test default precompilation - end - - Plots.set_default_backend!() # clear `Preferences` key -end - @testset "UnicodePlots" begin with(:unicodeplots) do @test backend() == Plots.UnicodePlotsBackend() @@ -262,6 +205,64 @@ end end end +@testset "Preferences" begin + Plots.set_default_backend!() # start with empty preferences + + withenv("PLOTS_DEFAULT_BACKEND" => "invalid") do + @test_logs (:warn, r".*is not a supported backend") Plots.load_default_backend() + end + @test_logs (:warn, r".*is not a supported backend") backend(:invalid) + + @test Plots.load_default_backend() == Plots.GRBackend() + + withenv("PLOTS_DEFAULT_BACKEND" => "unicodeplots") do + @test_logs (:info, r".*environment variable") Plots.diagnostics(devnull) + @test Plots.load_default_backend() == Plots.UnicodePlotsBackend() + end + + @test Plots.load_default_backend() == Plots.GRBackend() + @test Plots.backend_package_name() === :GR + @test Plots.backend_name() === :gr + + @test_logs (:info, r".*fallback") Plots.diagnostics(devnull) + + @test Plots.merge_with_base_supported([:annotations, :guide]) isa Set + @test Plots.CurrentBackend(:gr).sym === :gr + + @test_logs (:warn, r".*is not compatible with") Plots.set_default_backend!(:invalid) + + @testset "persistent backend" begin + # this test mimics a restart, which is needed after a preferences change + Plots.set_default_backend!(:unicodeplots) + script = tempname() + write( + script, + """ + ENV["PLOTS_PRECOMPILE"] = false + using Pkg, Test; io = (devnull, stdout)[1] # toggle for debugging + Pkg.activate(; temp = true, io) + Pkg.develop(; path = "$(escape_string(pkgdir(Plots)))", io) + Pkg.add("UnicodePlots"; io) # checked by Plots + using Plots + res = @testset "Prefs" begin + @test_logs (:info, r".*Preferences") Plots.diagnostics(io) + @test backend() == Plots.UnicodePlotsBackend() + end + exit(res.n_passed == 2 ? 0 : 1) + """, + ) + @test success(run(```$(Base.julia_cmd()) $script```)) + end + + is_pkgeval() || for be in CONCRETE_BACKENDS + (Sys.isapple() && be === :gaston) && continue # FIXME: hangs + @test_logs Plots.set_default_backend!(be) # test the absence of warnings + @test Base.compilecache(Base.module_keys[Plots]) isa String # test default precompilation + end + + Plots.set_default_backend!() # clear `Preferences` key +end + is_pkgeval() || @testset "PlotlyJS" begin with(:plotlyjs) do @test backend() == Plots.PlotlyJSBackend() @@ -272,18 +273,18 @@ is_pkgeval() || @testset "PlotlyJS" begin end is_pkgeval() || @testset "Examples" begin - if Sys.islinux() - callback(m, pkgname, i) = begin - pl = m.Plots.current() - save_func = (; pgfplotsx = m.Plots.pdf, unicodeplots = m.Plots.txt) # fastest `savefig` for each backend - fn = Base.invokelatest( - get(save_func, pkgname, m.Plots.png), - pl, - tempname() * ref_name(i), - ) - @test filesize(fn) > 1_000 - end - for be in CONCRETE_BACKENDS + callback(m, pkgname, i) = begin + pl = m.Plots.current() + save_func = (; pgfplotsx = m.Plots.pdf, unicodeplots = m.Plots.txt) # fastest `savefig` for each backend + fn = Base.invokelatest( + get(save_func, pkgname, m.Plots.png), + pl, + tempname() * ref_name(i), + ) + @test filesize(fn) > 1_000 + end + Sys.islinux() && for be in CONCRETE_BACKENDS + withenv("JULIA_PYTHONCALL_EXE" => "@PyCall", "JULIA_CONDAPKG_BACKEND" => "Null") do skip = vcat(Plots._backend_skips[be], blacklist) Plots.test_examples(be; skip, callback, disp = is_ci(), strict = true) # `ci` display for coverage closeall()