Skip to content

Commit 3e8eb9e

Browse files
DilumAluthgestaticfloat
authored andcommitted
Update the file.jl tests to allow for both EPERM and EINVAL in the non-root CHOWN tests (#41682)
Co-authored-by: Elliot Saba <[email protected]> Co-authored-by: Elliot Saba <[email protected]> (cherry picked from commit 114ee17)
1 parent d32c44d commit 3e8eb9e

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
@@ -491,6 +491,29 @@ rm(c_tmpdir, recursive=true)
491491
@test_throws Base._UVError("unlink($(repr(c_tmpdir)))", Base.UV_ENOENT) rm(c_tmpdir, recursive=true)
492492
@test rm(c_tmpdir, force=true, recursive=true) === nothing
493493

494+
# Some operations can return multiple different error codes depending on the system environment.
495+
function throws_matching_exception(f::Function, acceptable_exceptions::AbstractVector)
496+
try
497+
f()
498+
@error "No exception was thrown."
499+
return false
500+
catch ex
501+
if ex in acceptable_exceptions
502+
return true
503+
else
504+
@error "The thrown exception is not in the list of acceptable exceptions" acceptable_exceptions exception=(ex, catch_backtrace())
505+
return false
506+
end
507+
end
508+
end
509+
function throws_matching_uv_error(f::Function, pfx::AbstractString, codes::AbstractVector{<:Integer})
510+
acceptable_exceptions = multiple_uv_errors(pfx, codes)
511+
return throws_matching_exception(f, acceptable_exceptions)
512+
end
513+
function multiple_uv_errors(pfx::AbstractString, codes::AbstractVector{<:Integer})
514+
return [Base._UVError(pfx, code) for code in codes]
515+
end
516+
494517
if !Sys.iswindows()
495518
# chown will give an error if the user does not have permissions to change files
496519
if get(ENV, "USER", "") == "root" || get(ENV, "HOME", "") == "/root"
@@ -503,8 +526,12 @@ if !Sys.iswindows()
503526
@test stat(file).gid == 0
504527
@test stat(file).uid == 0
505528
else
506-
@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
507-
@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)
529+
@test throws_matching_uv_error("chown($(repr(file)), -2, -1)", [Base.UV_EPERM, Base.UV_EINVAL]) do
530+
chown(file, -2, -1) # Non-root user cannot change ownership to another user
531+
end
532+
@test throws_matching_uv_error("chown($(repr(file)), -1, -2)", [Base.UV_EPERM, Base.UV_EINVAL]) do
533+
chown(file, -1, -2) # Non-root user cannot change group to a group they are not a member of (eg: nogroup)
534+
end
508535
end
509536
else
510537
# test that chown doesn't cause any errors for Windows

0 commit comments

Comments
 (0)