From 3b14f80d1a972c736b2b848bbe3440f8ba962813 Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Fri, 28 Apr 2023 16:23:30 +0800 Subject: [PATCH] Windows: open standard streams in binary mode (#13397) --- spec/std/io/file_descriptor_spec.cr | 28 +++++++++++++++++++++ src/crystal/system/win32/file_descriptor.cr | 1 + 2 files changed, 29 insertions(+) diff --git a/spec/std/io/file_descriptor_spec.cr b/spec/std/io/file_descriptor_spec.cr index cec34b13310f..2391d97c03c9 100644 --- a/spec/std/io/file_descriptor_spec.cr +++ b/spec/std/io/file_descriptor_spec.cr @@ -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 diff --git a/src/crystal/system/win32/file_descriptor.cr b/src/crystal/system/win32/file_descriptor.cr index f4f20472d9bd..d99dd471f240 100644 --- a/src/crystal/system/win32/file_descriptor.cr +++ b/src/crystal/system/win32/file_descriptor.cr @@ -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