Skip to content

Commit

Permalink
Make IO::FileDescriptor#tty? return false for NUL on Windows (#14509)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Apr 23, 2024
1 parent ae2aa52 commit 5b8662e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions spec/std/io/file_descriptor_spec.cr
Expand Up @@ -22,6 +22,21 @@ describe IO::FileDescriptor do
end
end

describe "#tty?" do
it "returns false for null device" do
File.open(File::NULL) do |f|
f.tty?.should be_false
end
end

it "returns false for standard streams redirected to null device", tags: %w[slow] do
code = %q(print STDIN.tty?, ' ', STDERR.tty?)
compile_source(code) do |binpath|
`#{shell_command %(#{Process.quote(binpath)} < #{File::NULL} 2> #{File::NULL})}`.should eq("false false")
end
end
end

it "closes on finalize" do
pipes = [] of IO::FileDescriptor
assert_finalizes("fd") do
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/file_descriptor.cr
Expand Up @@ -145,7 +145,7 @@ module Crystal::System::FileDescriptor
end

private def system_tty?
LibC.GetFileType(windows_handle) == LibC::FILE_TYPE_CHAR
LibC.GetConsoleMode(windows_handle, out _) != 0
end

private def system_reopen(other : IO::FileDescriptor)
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/socket.cr
Expand Up @@ -442,7 +442,7 @@ module Crystal::System::Socket
end

private def system_tty?
LibC.GetFileType(LibC::HANDLE.new(fd)) == LibC::FILE_TYPE_CHAR
LibC.GetConsoleMode(LibC::HANDLE.new(fd), out _) != 0
end

private def unbuffered_read(slice : Bytes) : Int32
Expand Down

0 comments on commit 5b8662e

Please sign in to comment.