Skip to content
Closed
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
20 changes: 13 additions & 7 deletions spec/std/dir_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe "Dir" do
end

it "tests empty? on nonexistent directory" do
expect_raises Errno do
expect_raises OSError::FileNotFound do
Dir.empty?(File.join([__DIR__, "/foo/bar/"]))
end
end
Expand All @@ -57,7 +57,7 @@ describe "Dir" do
end

it "tests mkdir with an existing path" do
expect_raises Errno do
expect_raises OSError::FileExists do
Dir.mkdir(__DIR__, 0o700)
end
end
Expand All @@ -73,19 +73,19 @@ describe "Dir" do

it "tests mkdir_p with an existing path" do
Dir.mkdir_p(__DIR__).should eq(0)
expect_raises Errno do
expect_raises OSError::FileExists do
Dir.mkdir_p(__FILE__)
end
end

it "tests rmdir with an nonexistent path" do
expect_raises Errno do
expect_raises OSError::FileNotFound do
Dir.rmdir("/tmp/crystal_mkdir_test_#{Process.pid}/")
end
end

it "tests rmdir with a path that cannot be removed" do
expect_raises Errno do
expect_raises OSError do
Dir.rmdir(__DIR__)
end
end
Expand Down Expand Up @@ -213,12 +213,18 @@ describe "Dir" do
Dir.current.should eq(cwd)
end

it "raises" do
expect_raises do
it "raises for nonexistent directory" do
expect_raises OSError::FileNotFound do
Dir.cd("/nope")
end
end

it "raises for file" do
expect_raises OSError::NotADirectory do
Dir.cd("/usr/bin/env")
end
end

it "accepts a block" do
cwd = Dir.current

Expand Down
36 changes: 21 additions & 15 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe "File" do

it "raises an error when the file does not exist" do
filename = "#{__DIR__}/data/non_existing_file.txt"
expect_raises Errno do
expect_raises OSError::FileNotFound do
File.empty?(filename)
end
end
Expand Down Expand Up @@ -286,7 +286,7 @@ describe "File" do
end

it "raises when destination doesn't exist" do
expect_raises(Errno) do
expect_raises(OSError::FileNotFound) do
File.chmod("#{__DIR__}/data/unknown_chmod_path.txt", 0o664)
end
end
Expand Down Expand Up @@ -353,7 +353,7 @@ describe "File" do
end

it "gets stat for non-existent file and raises" do
expect_raises Errno do
expect_raises OSError::FileNotFound do
File.stat("non-existent")
end
end
Expand Down Expand Up @@ -387,9 +387,16 @@ describe "File" do
File.exists?(filename).should be_false
end

it "raises errno when file doesn't exist" do
it "raises when file doesn't exist" do
filename = "#{__DIR__}/data/temp1.txt"
expect_raises Errno do
expect_raises OSError::FileNotFound do
File.delete(filename)
end
end

it "raises when it's a directory" do
filename = "#{__DIR__}/data/dir/subdir2"
expect_raises (OSError::IsADirectory | OSError::PermissionError) do
File.delete(filename)
end
end
Expand All @@ -409,7 +416,7 @@ describe "File" do

it "raises if old file doesn't exist" do
filename = "#{__DIR__}/data/temp1.txt"
expect_raises Errno do
expect_raises OSError::FileNotFound do
File.rename(filename, "#{filename}.new")
end
end
Expand Down Expand Up @@ -519,8 +526,8 @@ describe "File" do
File.real_path("/usr/share/..").should eq("/usr")
end

it "raises Errno if file doesn't exist" do
expect_raises Errno do
it "raises if file doesn't exist" do
expect_raises OSError::FileNotFound do
File.real_path("/usr/share/foo/bar")
end
end
Expand Down Expand Up @@ -724,7 +731,7 @@ describe "File" do
filename = "#{__DIR__}/data/temp_write.txt"
File.write(filename, "0123456789")
File.open(filename, "r") do |f|
expect_raises(Errno) do
expect_raises(OSError) do # TODO: this should be an IO error
f.truncate(4)
end
end
Expand All @@ -733,12 +740,11 @@ describe "File" do
end

describe "flock" do
it "exlusively locks a file" do
it "exclusively locks a file" do
File.open(__FILE__) do |file1|
File.open(__FILE__) do |file2|
file1.flock_exclusive do
# BUG: check for EWOULDBLOCK when exception filters are implemented
expect_raises(Errno) do
expect_raises(OSError::BlockingIO) do
file2.flock_exclusive(blocking: false) { }
end
end
Expand Down Expand Up @@ -974,7 +980,7 @@ describe "File" do
atime = Time.new(2000, 1, 2)
mtime = Time.new(2000, 3, 4)

expect_raises Errno, "Error setting time to file" do
expect_raises OSError::FileNotFound, "Error setting time to file" do
File.utime(atime, mtime, "#{__DIR__}/nonexistent_file")
end
end
Expand Down Expand Up @@ -1021,13 +1027,13 @@ describe "File" do
end

it "raises if path contains non-existent directory" do
expect_raises Errno, "Error opening file" do
expect_raises OSError::FileNotFound, "Error opening file" do
File.touch("/tmp/non/existent/directory/test.tmp")
end
end

it "raises if file cannot be accessed" do
expect_raises Errno, "Operation not permitted" do
expect_raises OSError::PermissionError, "Operation not permitted" do
File.touch("/bin/ls")
end
end
Expand Down
24 changes: 12 additions & 12 deletions spec/std/file_utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe "FileUtils" do
end

it "raises an error if non correct arguments" do
expect_raises Errno do
expect_raises OSError::FileNotFound do
FileUtils.mv("/tmp/crystal_mv_test/a", "/tmp/crystal_mv_test/b")
end
end
Expand Down Expand Up @@ -363,16 +363,16 @@ describe "FileUtils" do
end

it "tests mkdir with an existing path" do
expect_raises Errno do
expect_raises OSError::FileExists do
Dir.mkdir(__DIR__, 0o700)
end
end

it "tests mkdir with multiples existing paths" do
expect_raises Errno do
expect_raises OSError::FileExists do
FileUtils.mkdir([__DIR__, __DIR__], 0o700)
end
expect_raises Errno do
expect_raises OSError::FileExists do
FileUtils.mkdir(["/tmp/crystal_mkdir_test_#{Process.pid}/", __DIR__], 0o700)
end
end
Expand Down Expand Up @@ -401,38 +401,38 @@ describe "FileUtils" do

it "tests mkdir_p with an existing path" do
FileUtils.mkdir_p(__DIR__).should be_nil
expect_raises Errno do
expect_raises OSError::FileExists do
FileUtils.mkdir_p(__FILE__)
end
end

it "tests mkdir_p with multiple existing path" do
FileUtils.mkdir_p([__DIR__, __DIR__]).should be_nil
expect_raises Errno do
expect_raises OSError::FileExists do
FileUtils.mkdir_p([__FILE__, "/tmp/crystal_mkdir_ptest_#{Process.pid}/"])
end
end

it "tests rmdir with an non existing path" do
expect_raises Errno do
expect_raises OSError::FileNotFound do
FileUtils.rmdir("/tmp/crystal_mkdir_test_#{Process.pid}/tmp/")
end
end

it "tests rmdir with multiple non existing path" do
expect_raises Errno do
expect_raises OSError::FileNotFound do
FileUtils.rmdir(["/tmp/crystal_mkdir_test_#{Process.pid}/tmp/", "/tmp/crystal_mkdir_test_#{Process.pid + 1}/tmp/"])
end
end

it "tests rmdir with a path that cannot be removed" do
expect_raises Errno do
expect_raises OSError do
FileUtils.rmdir(__DIR__)
end
end

it "tests rmdir with multiple path that cannot be removed" do
expect_raises Errno do
expect_raises OSError do
FileUtils.rmdir([__DIR__, __DIR__])
end
end
Expand All @@ -445,7 +445,7 @@ describe "FileUtils" do
end

it "tests rm with non existing path" do
expect_raises Errno do
expect_raises OSError::FileNotFound do
FileUtils.rm("/tmp/crystal_rm_test_#{Process.pid}")
end
end
Expand All @@ -461,7 +461,7 @@ describe "FileUtils" do
end

it "tests rm with some non existing paths" do
expect_raises Errno do
expect_raises OSError::FileNotFound do
path1 = "/tmp/crystal_rm_test_#{Process.pid}"
path2 = "/tmp/crystal_rm_test_#{Process.pid + 1}"
File.write(path1, "")
Expand Down
8 changes: 4 additions & 4 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ private class RaiseErrno
include IO

def read(slice : Bytes)
Errno.value = @value
raise Errno.new "..."
OSError.errno = @value
raise OSError.create "..."
end

def write(slice : Bytes) : Nil
Expand Down Expand Up @@ -294,9 +294,9 @@ module HTTP
))
end

it "handles Errno" do
it "handles OSError" do
processor = HTTP::Server::RequestProcessor.new { }
input = RaiseErrno.new(Errno::ECONNRESET)
input = RaiseErrno.new(OSError::ECONNRESET)
output = IO::Memory.new
processor.process(input, output)
output.rewind.gets_to_end.empty?.should be_true
Expand Down
10 changes: 5 additions & 5 deletions spec/std/socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe UNIXServer do
server = UNIXServer.new(path)

begin
expect_raises(Errno) { UNIXServer.new(path) }
expect_raises(OSError) { UNIXServer.new(path) }
ensure
server.close
end
Expand All @@ -235,7 +235,7 @@ describe UNIXServer do
File.exists?(path).should be_true

begin
expect_raises Errno, /(already|Address) in use/ do
expect_raises OSError, /(already|Address) in use/ do
UNIXServer.new(path)
end

Expand Down Expand Up @@ -416,7 +416,7 @@ describe TCPServer do
it "fails when port is in use" do
port = free_udp_socket_port

expect_raises Errno, /(already|Address) in use/ do
expect_raises OSError, /(already|Address) in use/ do
sock = Socket.tcp(Socket::Family::INET6)
sock.bind(Socket::IPAddress.new("::1", port))

Expand All @@ -426,7 +426,7 @@ describe TCPServer do

it "doesn't reuse the TCP port by default (SO_REUSEPORT)" do
TCPServer.open("::", 0) do |server|
expect_raises(Errno) do
expect_raises(OSError) do
TCPServer.open("::", server.local_address.port) { }
end
end
Expand Down Expand Up @@ -532,7 +532,7 @@ describe TCPSocket do
server.local_address.port
end

expect_raises(Errno, "Error connecting to 'localhost:#{port}': Connection refused") do
expect_raises(ConnectionError::ConnectionRefused, "Error connecting to 'localhost:#{port}': Connection refused") do
TCPSocket.new("localhost", port)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/cache_dir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module Crystal
begin
Dir.mkdir_p(candidate)
return @dir = candidate
rescue Errno
rescue OSError
# Try next one
end
end
Expand Down
47 changes: 47 additions & 0 deletions src/crystal/system/unix/errno.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{% skip_file() unless flag?(:unix) %}

require "c/errno"
require "c/string"

lib LibC
{% if flag?(:linux) %}
{% if flag?(:musl) %}
fun __errno_location : Int*
{% else %}
@[ThreadLocal]
$errno : Int
{% end %}
{% elsif flag?(:darwin) || flag?(:freebsd) %}
fun __error : Int*
{% elsif flag?(:openbsd) %}
fun __error = __errno : Int*
{% end %}
end

module Crystal::System::Errno
# Returns the value of libc's errno.
def self.value : LibC::Int
{% if flag?(:linux) %}
{% if flag?(:musl) %}
LibC.__errno_location.value
{% else %}
LibC.errno
{% end %}
{% elsif flag?(:darwin) || flag?(:freebsd) || flag?(:openbsd) %}
LibC.__error.value
{% end %}
end

# Sets the value of libc's errno.
def self.value=(value)
{% if flag?(:linux) %}
{% if flag?(:musl) %}
LibC.__errno_location.value = value
{% else %}
LibC.errno = value
{% end %}
{% elsif flag?(:darwin) || flag?(:freebsd) || flag?(:openbsd) %}
LibC.__error.value = value
{% end %}
end
end
Loading