Skip to content

Commit d0f4b25

Browse files
committed
Add Sys.is<os>
1 parent 1d33d04 commit d0f4b25

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ Currently, the `@compat` macro supports the following syntaxes:
154154

155155
* `takebuf_array` is now a method of `take!`. `takebuf_string(io)` becomes `String(take!(io))` ([#19088])
156156

157+
* `is_apple`, `is_bsd`, `is_linux`, `is_unix`, and `is_windows` are now `Sys.isapple`, `Sys.isbsd`,
158+
`Sys.islinux`, `Sys.isunix`, and `Sys.iswindows`, respectively. These are available in the `Compat.Sys`
159+
submodule. ([#22182])
160+
157161
## New macros
158162

159163
* `@__DIR__` has been added ([#18380])
@@ -285,4 +289,5 @@ includes this fix. Find the minimum version from there.
285289
[#21257]: https://github.com/JuliaLang/julia/issues/21257
286290
[#21346]: https://github.com/JuliaLang/julia/issues/21346
287291
[#22064]: https://github.com/JuliaLang/julia/issues/22064
292+
[#22182]: https://github.com/JuliaLang/julia/issues/22182
288293
[#22475]: https://github.com/JuliaLang/julia/issues/22475

src/Compat.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,4 +618,17 @@ include("deprecated.jl")
618618
# https://github.com/JuliaLang/julia/pull/21746
619619
const macros_have_sourceloc = VERSION >= v"0.7-" && length(:(@test).args) == 2
620620

621+
# https://github.com/JuliaLang/julia/pull/22182
622+
module Sys
623+
if VERSION < v"0.7.0-DEV.914"
624+
isapple(k::Symbol=Base.Sys.KERNEL) = k in (:Darwin, :Apple)
625+
isbsd(k::Symbol=Base.Sys.KERNEL) = isapple(k) || k in (:FreeBSD, :OpenBSD, :NetBSD, :DragonFly)
626+
islinux(k::Symbol=Base.Sys.KERNEL) = k == :Linux
627+
isunix(k::Symbol=Base.Sys.KERNEL) = isbsd(k) || islinux(k)
628+
iswindows(k::Symbol=Base.Sys.KERNEL) = k in (:Windows, :NT)
629+
else
630+
import Base.Sys: isapple, isbsd, islinux, isunix, iswindows
631+
end
632+
end
633+
621634
end # module Compat

test/runtests.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ let x = rand(rng, Int64, 3,4)
138138
@test x == rand(rng, Int64, (3,4))
139139
end
140140

141-
extrapath = is_windows() ? joinpath(JULIA_HOME,"..","Git","usr","bin")*";" : ""
141+
extrapath = Compat.Sys.iswindows() ? joinpath(JULIA_HOME,"..","Git","usr","bin")*";" : ""
142142
@compat withenv("PATH" => extrapath * ENV["PATH"]) do
143143
cmd1 = pipeline(`echo hello`, `sort`)
144144
cmd2 = pipeline(`true`, `true`)
145-
if is_windows()
145+
if Compat.Sys.iswindows()
146146
try # use busybox-w32
147147
success(`busybox`)
148148
cmd1 = pipeline(`busybox echo hello`, `busybox sort`)
@@ -921,7 +921,7 @@ cd(dirwalk) do
921921
touch(joinpath("sub_dir1", "file$i"))
922922
end
923923
touch(joinpath("sub_dir2", "file_dir2"))
924-
has_symlinks = is_unix() ? true : (isdefined(Base, :WINDOWS_VISTA_VER) && Base.windows_version() >= Base.WINDOWS_VISTA_VER)
924+
has_symlinks = Compat.Sys.isunix() ? true : (isdefined(Base, :WINDOWS_VISTA_VER) && Base.windows_version() >= Base.WINDOWS_VISTA_VER)
925925
follow_symlink_vec = has_symlinks ? [true, false] : [false]
926926
has_symlinks && symlink(abspath("sub_dir2"), joinpath("sub_dir1", "link"))
927927
for follow_symlinks in follow_symlink_vec
@@ -1228,6 +1228,15 @@ end
12281228
@test Compat.repeat(1:2, outer=[2]) == [1, 2, 1, 2]
12291229
@test Compat.repeat([1,2], inner=(2,)) == [1, 1, 2, 2]
12301230

1231+
for os in [:apple, :bsd, :linux, :unix, :windows]
1232+
from_base = if VERSION >= v"0.7.0-DEV.914"
1233+
Expr(:., Expr(:., :Base, Base.Meta.quot(:Sys)), Base.Meta.quot(Symbol("is", os)))
1234+
else # VERSION >= v"0.5.0-dev+4267"
1235+
Expr(:., :Base, Base.Meta.quot(Symbol("is_", os)))
1236+
end
1237+
@eval @test Compat.Sys.$(Symbol("is", os))() == $from_base()
1238+
end
1239+
12311240
io = IOBuffer()
12321241
@test @compat(get(io, :limit, false)) == false
12331242
@test @compat(get(io, :compact, false)) == false

0 commit comments

Comments
 (0)