Skip to content

Commit 114ee17

Browse files
Update the file.jl tests to allow for both EPERM and EINVAL in the non-root CHOWN tests (#41682)
Co-authored-by: Elliot Saba <staticfloat@gmail.com> Co-authored-by: Elliot Saba <staticfloat@gmail.com>
1 parent 9232de9 commit 114ee17

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

test/file.jl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,29 @@ rm(c_tmpdir, recursive=true)
506506
@test_throws Base._UVError("unlink($(repr(c_tmpdir)))", Base.UV_ENOENT) rm(c_tmpdir, recursive=true)
507507
@test rm(c_tmpdir, force=true, recursive=true) === nothing
508508

509+
# Some operations can return multiple different error codes depending on the system environment.
510+
function throws_matching_exception(f::Function, acceptable_exceptions::AbstractVector)
511+
try
512+
f()
513+
@error "No exception was thrown."
514+
return false
515+
catch ex
516+
if ex in acceptable_exceptions
517+
return true
518+
else
519+
@error "The thrown exception is not in the list of acceptable exceptions" acceptable_exceptions exception=(ex, catch_backtrace())
520+
return false
521+
end
522+
end
523+
end
524+
function throws_matching_uv_error(f::Function, pfx::AbstractString, codes::AbstractVector{<:Integer})
525+
acceptable_exceptions = multiple_uv_errors(pfx, codes)
526+
return throws_matching_exception(f, acceptable_exceptions)
527+
end
528+
function multiple_uv_errors(pfx::AbstractString, codes::AbstractVector{<:Integer})
529+
return [Base._UVError(pfx, code) for code in codes]
530+
end
531+
509532
if !Sys.iswindows()
510533
# chown will give an error if the user does not have permissions to change files
511534
if get(ENV, "USER", "") == "root" || get(ENV, "HOME", "") == "/root"
@@ -518,8 +541,12 @@ if !Sys.iswindows()
518541
@test stat(file).gid == 0
519542
@test stat(file).uid == 0
520543
else
521-
@test_throws Base._UVError("chown($(repr(file)), -2, -1)", Base.UV_EPERM) chown(file, -2, -1) # Non-root user cannot change ownership to another user
522-
@test_throws Base._UVError("chown($(repr(file)), -1, -2)", Base.UV_EPERM) chown(file, -1, -2) # Non-root user cannot change group to a group they are not a member of (eg: nogroup)
544+
@test throws_matching_uv_error("chown($(repr(file)), -2, -1)", [Base.UV_EPERM, Base.UV_EINVAL]) do
545+
chown(file, -2, -1) # Non-root user cannot change ownership to another user
546+
end
547+
@test throws_matching_uv_error("chown($(repr(file)), -1, -2)", [Base.UV_EPERM, Base.UV_EINVAL]) do
548+
chown(file, -1, -2) # Non-root user cannot change group to a group they are not a member of (eg: nogroup)
549+
end
523550
end
524551
else
525552
# test that chown doesn't cause any errors for Windows

0 commit comments

Comments
 (0)