Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file permission specs on Windows #13465

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,26 @@ describe "File" do
end
end

pending_win32 "raises when reading a file with no permission" do
# Crystal does not expose ways to make a file unreadable on Windows
{% unless flag?(:win32) %}
it "raises when reading a file with no permission" do
with_tempfile("file.txt") do |path|
File.touch(path)
File.chmod(path, File::Permissions::None)
{% if flag?(:unix) %}
# TODO: Find a better way to execute this spec when running as privileged
# user. Compiling a program and running a separate process would be a
# lot of overhead.
if LibC.getuid == 0
pending! "Spec cannot run as superuser"
end
{% end %}
expect_raises(File::AccessDeniedError) { File.read(path) }
end
end
{% end %}

it "raises when writing to a file with no permission" do
with_tempfile("file.txt") do |path|
File.touch(path)
File.chmod(path, File::Permissions::None)
Expand All @@ -917,7 +936,7 @@ describe "File" do
pending! "Spec cannot run as superuser"
end
{% end %}
expect_raises(File::AccessDeniedError) { File.read(path) }
expect_raises(File::AccessDeniedError) { File.write(path, "foo") }
end
end

Expand Down Expand Up @@ -1390,29 +1409,29 @@ describe "File" do
end
end

pending_win32 "copies permissions" do
it "copies permissions" do
with_tempfile("cp-permissions-src.txt", "cp-permissions-out.txt") do |src_path, out_path|
File.write(src_path, "foo")
File.chmod(src_path, 0o700)
File.chmod(src_path, 0o444)

File.copy(src_path, out_path)

File.info(out_path).permissions.should eq(File::Permissions.new(0o700))
File.info(out_path).permissions.should eq(File::Permissions.new(0o444))
File.same_content?(src_path, out_path).should be_true
end
end

pending_win32 "overwrites existing destination and permissions" do
it "overwrites existing destination and permissions" do
with_tempfile("cp-permissions-src.txt", "cp-permissions-out.txt") do |src_path, out_path|
File.write(src_path, "foo")
File.chmod(src_path, 0o700)
File.chmod(src_path, 0o444)

File.write(out_path, "bar")
File.chmod(out_path, 0o777)
File.chmod(out_path, 0o666)

File.copy(src_path, out_path)

File.info(out_path).permissions.should eq(File::Permissions.new(0o700))
File.info(out_path).permissions.should eq(File::Permissions.new(0o444))
File.same_content?(src_path, out_path).should be_true
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/std/file_utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ describe "FileUtils" do
end
end

pending_win32 "copies permissions" do
it "copies permissions" do
with_tempfile("cp-permissions-src.txt", "cp-permissions-out.txt") do |src_path, out_path|
test_with_string_and_path(src_path, out_path) do |*args|
File.write(src_path, "foo")
File.chmod(src_path, 0o700)
File.write(src_path, "foo")
File.chmod(src_path, 0o444)

test_with_string_and_path(src_path, out_path) do |*args|
FileUtils.cp(*args)

File.info(out_path).permissions.should eq(File::Permissions.new(0o700))
File.info(out_path).permissions.should eq(File::Permissions.new(0o444))
FileUtils.cmp(src_path, out_path).should be_true

FileUtils.rm_rf(out_path)
Expand Down Expand Up @@ -678,7 +678,7 @@ describe "FileUtils" do
File.symlink?(path2).should be_true

expect_raises(File::NotFoundError, "Error resolving real path: '#{path2.inspect_unquoted}'") do
File.real_path(path2)
File.realpath(path2)
end
FileUtils.rm_rf(path2)
end
Expand Down