Skip to content

Commit 101448c

Browse files
committed
Make some global variables constant
1 parent ed87f07 commit 101448c

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

src/Sandbox.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ include("Docker.jl")
5151
# Load the UserNamespace executor
5252
include("UserNamespaces.jl")
5353

54-
all_executors = Type{<:SandboxExecutor}[
54+
const all_executors = Type{<:SandboxExecutor}[
5555
# We always prefer the UserNamespaces executor, if we can use it,
5656
# and the unprivileged one most of all. Only after that do we try `docker`.
5757
UnprivilegedUserNamespacesExecutor,
@@ -89,16 +89,15 @@ function select_executor(verbose::Bool)
8989
error("Could not find any available executors for $(triplet(HostPlatform()))!")
9090
end
9191

92-
_preferred_executor = nothing
92+
const _preferred_executor = Ref{Union{Nothing, (x->Type{x}).(all_executors)...}}(nothing)
9393
const _preferred_executor_lock = ReentrantLock()
9494
function preferred_executor(;verbose::Bool = false)
9595
lock(_preferred_executor_lock) do
9696
# If we've already asked this question, return the old answer
97-
global _preferred_executor
98-
if _preferred_executor === nothing
99-
_preferred_executor = select_executor(verbose)
97+
if _preferred_executor[] === nothing
98+
_preferred_executor[] = select_executor(verbose)
10099
end
101-
return _preferred_executor
100+
return _preferred_executor[]
102101
end
103102
end
104103

src/utils.jl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,27 +198,26 @@ Wrapper around libc's `getuid()` function
198198
"""
199199
getgid() = ccall(:getgid, Cint, ())
200200

201-
_sudo_cmd = nothing
201+
const _sudo_cmd = Ref{Union{Vector{String}, Nothing}}(nothing)
202202
function sudo_cmd()
203-
global _sudo_cmd
204203

205204
# Use cached value if we've already run this
206-
if _sudo_cmd !== nothing
207-
return _sudo_cmd
205+
if _sudo_cmd[] !== nothing
206+
return _sudo_cmd[]
208207
end
209208

210-
if getuid() == 0
209+
_sudo_cmd[] = if getuid() == 0
211210
# If we're already root, don't use any kind of sudo program
212-
_sudo_cmd = String[]
211+
String[]
213212
elseif Sys.which("sudo") !== nothing success(`sudo -V`)
214213
# If `sudo` is available, use that
215-
_sudo_cmd = ["sudo"]
214+
["sudo"]
216215
elseif Sys.which("su") !== nothing
217216
# Fall back to `su` if all else fails
218-
_sudo_cmd = ["su", "root", "-c"]
217+
["su", "root", "-c"]
219218
else
220219
@warn("No known sudo-like wrappers!")
221-
_sudo_cmd = String[]
220+
String[]
222221
end
223-
return _sudo_cmd
222+
return _sudo_cmd[]
224223
end

test/Sandbox.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Test, Sandbox, SHA
22

3-
all_executors = Sandbox.all_executors
3+
const all_executors = Sandbox.all_executors
44

55
# Can we run `sudo` without a password? If not, don't attempt to test the privileged runner
66
if !success(`sudo -k -n true`)
@@ -17,7 +17,7 @@ function print_if_nonempty(stderr::Vector{UInt8})
1717
return true
1818
end
1919

20-
rootfs_dir = Sandbox.alpine_rootfs()
20+
const rootfs_dir = Sandbox.alpine_rootfs()
2121
for executor in all_executors
2222
if !executor_available(executor)
2323
@error("Skipping $(executor) tests, as it does not seem to be available")

test/runtests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# If our test harness requests a local sandbox, make it so!
2-
REPO_ROOT = dirname(@__DIR__)
3-
should_build_local_sandbox = parse(Bool, get(ENV, "SANDBOX_BUILD_LOCAL_SANDBOX", "false"))
2+
const REPO_ROOT = dirname(@__DIR__)
3+
const should_build_local_sandbox = parse(Bool, get(ENV, "SANDBOX_BUILD_LOCAL_SANDBOX", "false"))
44
if should_build_local_sandbox
55
run(`$(Base.julia_cmd()) --project=$(REPO_ROOT) $(REPO_ROOT)/deps/build_local_sandbox.jl`)
66
# Ensure LocalPreferences.toml gets used by test project as well
@@ -21,7 +21,7 @@ end
2121
using Test, Sandbox, Scratch
2222

2323
# If we're on a UserNSSandbox_jll-compatible system, ensure that the sandbox is coming from where we expect.
24-
UserNSSandbox_jll = Sandbox.UserNSSandbox_jll
24+
using UserNSSandbox_jll
2525
if UserNSSandbox_jll.is_available()
2626
Artifacts = Sandbox.UserNSSandbox_jll.Artifacts
2727
sandbox_path = Sandbox.UserNSSandbox_jll.sandbox_path

0 commit comments

Comments
 (0)