Skip to content
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
60 changes: 34 additions & 26 deletions spec/std/io/file_descriptor_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class IO::FileDescriptor
include FinalizeCounter
end

private CLOSE_ON_EXEC_AVAILABLE = {{ !flag?(:win32) }}

private def shell_command(command)
{% if flag?(:win32) %}
"cmd.exe /c #{Process.quote(command)}"
Expand Down Expand Up @@ -130,45 +132,51 @@ describe IO::FileDescriptor do
end
end

{% unless flag?(:win32) %}
describe "close_on_exec" do
it "sets close on exec on the reopened standard descriptors" do
unless STDIN.fd == Crystal::System::FileDescriptor::STDIN_HANDLE
STDIN.close_on_exec?.should be_true
end
describe "close_on_exec" do
it "sets close on exec on the reopened standard descriptors" do
unless STDIN.fd == Crystal::System::FileDescriptor::STDIN_HANDLE
STDIN.close_on_exec?.should be_true
end

unless STDOUT.fd == Crystal::System::FileDescriptor::STDOUT_HANDLE
STDOUT.close_on_exec?.should be_true
end
unless STDOUT.fd == Crystal::System::FileDescriptor::STDOUT_HANDLE
STDOUT.close_on_exec?.should be_true
end

unless STDERR.fd == Crystal::System::FileDescriptor::STDERR_HANDLE
STDERR.close_on_exec?.should be_true
end
unless STDERR.fd == Crystal::System::FileDescriptor::STDERR_HANDLE
STDERR.close_on_exec?.should be_true
end
end

it "is enabled by default (open)" do
File.open(datapath("test_file.txt")) do |file|
file.close_on_exec?.should be_true
end
it "is enabled by default (open)" do
File.open(datapath("test_file.txt")) do |file|
file.close_on_exec?.should eq CLOSE_ON_EXEC_AVAILABLE
end
end

it "is enabled by default (pipe)" do
IO::FileDescriptor.pipe.each do |fd|
fd.close_on_exec?.should be_true
fd.close_on_exec?.should be_true
end
it "is enabled by default (pipe)" do
IO::FileDescriptor.pipe.each do |fd|
fd.close_on_exec?.should eq CLOSE_ON_EXEC_AVAILABLE
fd.close_on_exec?.should eq CLOSE_ON_EXEC_AVAILABLE
end
end

it "can be disabled and reenabled" do
File.open(datapath("test_file.txt")) do |file|
file.close_on_exec = false
file.close_on_exec?.should be_false
it "can be disabled and reenabled" do
File.open(datapath("test_file.txt")) do |file|
file.close_on_exec = false
file.close_on_exec?.should be_false

if CLOSE_ON_EXEC_AVAILABLE
file.close_on_exec = true
file.close_on_exec?.should be_true
else
expect_raises(NotImplementedError) do
file.close_on_exec = true
end
end
end
end

if CLOSE_ON_EXEC_AVAILABLE
it "is copied on reopen" do
File.open(datapath("test_file.txt")) do |file1|
file1.close_on_exec = true
Expand All @@ -187,7 +195,7 @@ describe IO::FileDescriptor do
end
end
end
{% end %}
end

it ".set_blocking and .get_blocking" do
File.open(datapath("test_file.txt"), "r") do |file|
Expand Down
14 changes: 5 additions & 9 deletions spec/std/socket/socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ describe Socket, tags: "network" do
client.family.should eq(Socket::Family::INET)
client.type.should eq(Socket::Type::STREAM)
client.protocol.should eq(Socket::Protocol::TCP)
{% unless flag?(:win32) %}
client.close_on_exec?.should be_true
{% end %}
client.close_on_exec?.should eq CLOSE_ON_EXEC_AVAILABLE
ensure
client.close
end
Expand Down Expand Up @@ -168,12 +166,10 @@ describe Socket, tags: "network" do
end
end

{% unless flag?(:win32) %}
it "closes on exec by default" do
socket = Socket.new(Socket::Family::INET, Socket::Type::STREAM, Socket::Protocol::TCP)
socket.close_on_exec?.should be_true
end
{% end %}
it "closes on exec by default" do
socket = Socket.new(Socket::Family::INET, Socket::Type::STREAM, Socket::Protocol::TCP)
socket.close_on_exec?.should eq CLOSE_ON_EXEC_AVAILABLE
end

it ".set_blocking and .get_blocking" do
socket = Socket.tcp(Socket::Family::INET)
Expand Down
2 changes: 2 additions & 0 deletions spec/std/socket/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "spec"
require "socket"

CLOSE_ON_EXEC_AVAILABLE = {{ !flag?(:win32) }}

module SocketSpecHelper
class_getter?(supports_ipv6 : Bool) { detect_supports_ipv6? }

Expand Down
14 changes: 6 additions & 8 deletions spec/std/socket/tcp_server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,14 @@ describe TCPServer, tags: "network" do
{% end %}

describe "accept" do
{% unless flag?(:win32) %}
it "sets close on exec flag" do
TCPServer.open("localhost", 0) do |server|
TCPSocket.open("localhost", server.local_address.port) do |client|
server.accept? do |sock|
sock.close_on_exec?.should be_true
end
it "sets close on exec flag" do
TCPServer.open("localhost", 0) do |server|
TCPSocket.open("localhost", server.local_address.port) do |client|
server.accept? do |sock|
sock.close_on_exec?.should eq CLOSE_ON_EXEC_AVAILABLE
end
end
end
{% end %}
end
end
end
16 changes: 7 additions & 9 deletions spec/std/socket/unix_server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,17 @@ describe UNIXServer do
end
end

{% unless flag?(:win32) %}
it "sets close on exec flag" do
with_tempfile("unix_socket-accept.sock") do |path|
UNIXServer.open(path) do |server|
UNIXSocket.open(path) do |client|
server.accept? do |sock|
sock.close_on_exec?.should be_true
end
it "sets close on exec flag" do
with_tempfile("unix_socket-accept.sock") do |path|
UNIXServer.open(path) do |server|
UNIXSocket.open(path) do |client|
server.accept? do |sock|
sock.close_on_exec?.should eq CLOSE_ON_EXEC_AVAILABLE
end
end
end
end
{% end %}
end
end

# Datagram socket type is not supported on Windows yet:
Expand Down
Loading