Skip to content

Commit

Permalink
Windows: open standard streams in binary mode (#13397)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Apr 28, 2023
1 parent 35c0c73 commit 3b14f80
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions spec/std/io/file_descriptor_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ describe IO::FileDescriptor do
end
end

it "opens STDIN in binary mode" do
code = %q(print STDIN.gets_to_end.includes?('\r'))
compile_source(code) do |binpath|
io_in = IO::Memory.new("foo\r\n")
io_out = IO::Memory.new
Process.run(binpath, input: io_in, output: io_out)
io_out.to_s.should eq("true")
end
end

it "opens STDOUT in binary mode" do
code = %q(puts "foo")
compile_source(code) do |binpath|
io = IO::Memory.new
Process.run(binpath, output: io)
io.to_s.should eq("foo\n")
end
end

it "opens STDERR in binary mode" do
code = %q(STDERR.puts "foo")
compile_source(code) do |binpath|
io = IO::Memory.new
Process.run(binpath, error: io)
io.to_s.should eq("foo\n")
end
end

it "does not close if close_on_finalize is false" do
pipes = [] of IO::FileDescriptor
assert_finalizes("fd") do
Expand Down
1 change: 1 addition & 0 deletions src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ module Crystal::System::FileDescriptor
console_handle = false
handle = windows_handle(fd)
if handle != LibC::INVALID_HANDLE_VALUE
LibC._setmode fd, LibC::O_BINARY
# TODO: use `out old_mode` after implementing interpreter out closured var
old_mode = uninitialized LibC::DWORD
if LibC.GetConsoleMode(handle, pointerof(old_mode)) != 0
Expand Down

0 comments on commit 3b14f80

Please sign in to comment.