From d0f4b25d2295cf3951a656f702e8920aa1b907a5 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Sat, 15 Jul 2017 14:07:45 -0700 Subject: [PATCH] Add Sys.is --- README.md | 5 +++++ src/Compat.jl | 13 +++++++++++++ test/runtests.jl | 15 ++++++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5f0ea2ec5..936eba4fa 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,10 @@ Currently, the `@compat` macro supports the following syntaxes: * `takebuf_array` is now a method of `take!`. `takebuf_string(io)` becomes `String(take!(io))` ([#19088]) +* `is_apple`, `is_bsd`, `is_linux`, `is_unix`, and `is_windows` are now `Sys.isapple`, `Sys.isbsd`, + `Sys.islinux`, `Sys.isunix`, and `Sys.iswindows`, respectively. These are available in the `Compat.Sys` + submodule. ([#22182]) + ## New macros * `@__DIR__` has been added ([#18380]) @@ -285,4 +289,5 @@ includes this fix. Find the minimum version from there. [#21257]: https://github.com/JuliaLang/julia/issues/21257 [#21346]: https://github.com/JuliaLang/julia/issues/21346 [#22064]: https://github.com/JuliaLang/julia/issues/22064 +[#22182]: https://github.com/JuliaLang/julia/issues/22182 [#22475]: https://github.com/JuliaLang/julia/issues/22475 diff --git a/src/Compat.jl b/src/Compat.jl index 5f0363316..ed0ad2112 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -618,4 +618,17 @@ include("deprecated.jl") # https://github.com/JuliaLang/julia/pull/21746 const macros_have_sourceloc = VERSION >= v"0.7-" && length(:(@test).args) == 2 +# https://github.com/JuliaLang/julia/pull/22182 +module Sys + if VERSION < v"0.7.0-DEV.914" + isapple(k::Symbol=Base.Sys.KERNEL) = k in (:Darwin, :Apple) + isbsd(k::Symbol=Base.Sys.KERNEL) = isapple(k) || k in (:FreeBSD, :OpenBSD, :NetBSD, :DragonFly) + islinux(k::Symbol=Base.Sys.KERNEL) = k == :Linux + isunix(k::Symbol=Base.Sys.KERNEL) = isbsd(k) || islinux(k) + iswindows(k::Symbol=Base.Sys.KERNEL) = k in (:Windows, :NT) + else + import Base.Sys: isapple, isbsd, islinux, isunix, iswindows + end +end + end # module Compat diff --git a/test/runtests.jl b/test/runtests.jl index 891a86aa3..fe56e6459 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -138,11 +138,11 @@ let x = rand(rng, Int64, 3,4) @test x == rand(rng, Int64, (3,4)) end -extrapath = is_windows() ? joinpath(JULIA_HOME,"..","Git","usr","bin")*";" : "" +extrapath = Compat.Sys.iswindows() ? joinpath(JULIA_HOME,"..","Git","usr","bin")*";" : "" @compat withenv("PATH" => extrapath * ENV["PATH"]) do cmd1 = pipeline(`echo hello`, `sort`) cmd2 = pipeline(`true`, `true`) - if is_windows() + if Compat.Sys.iswindows() try # use busybox-w32 success(`busybox`) cmd1 = pipeline(`busybox echo hello`, `busybox sort`) @@ -921,7 +921,7 @@ cd(dirwalk) do touch(joinpath("sub_dir1", "file$i")) end touch(joinpath("sub_dir2", "file_dir2")) - has_symlinks = is_unix() ? true : (isdefined(Base, :WINDOWS_VISTA_VER) && Base.windows_version() >= Base.WINDOWS_VISTA_VER) + has_symlinks = Compat.Sys.isunix() ? true : (isdefined(Base, :WINDOWS_VISTA_VER) && Base.windows_version() >= Base.WINDOWS_VISTA_VER) follow_symlink_vec = has_symlinks ? [true, false] : [false] has_symlinks && symlink(abspath("sub_dir2"), joinpath("sub_dir1", "link")) for follow_symlinks in follow_symlink_vec @@ -1228,6 +1228,15 @@ end @test Compat.repeat(1:2, outer=[2]) == [1, 2, 1, 2] @test Compat.repeat([1,2], inner=(2,)) == [1, 1, 2, 2] +for os in [:apple, :bsd, :linux, :unix, :windows] + from_base = if VERSION >= v"0.7.0-DEV.914" + Expr(:., Expr(:., :Base, Base.Meta.quot(:Sys)), Base.Meta.quot(Symbol("is", os))) + else # VERSION >= v"0.5.0-dev+4267" + Expr(:., :Base, Base.Meta.quot(Symbol("is_", os))) + end + @eval @test Compat.Sys.$(Symbol("is", os))() == $from_base() +end + io = IOBuffer() @test @compat(get(io, :limit, false)) == false @test @compat(get(io, :compact, false)) == false