-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtty.test.lua
More file actions
141 lines (124 loc) · 4.13 KB
/
Copy pathtty.test.lua
File metadata and controls
141 lines (124 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
local tty = require "tty"
local function tempfile()
local name = os.tmpname()
local file = assert(io.open(name, "w+"))
return file, name
end
describe("tty", function()
it("should expose correct types", function()
assert.Table(tty)
assert.Function(tty.isatty)
assert.Function(tty.size)
assert.String(tty._VERSION)
end)
describe("isatty()", function()
it("defaults to stdout", function()
local stdout_is_tty = tty.isatty()
assert.Boolean(stdout_is_tty)
assert.Equal(stdout_is_tty, tty.isatty())
assert.Equal(stdout_is_tty, tty.isatty(1))
assert.Equal(stdout_is_tty, tty.isatty(io.stdout))
end)
it("should handle stderr", function()
local stderr_is_tty = tty.isatty(2)
assert.Boolean(stderr_is_tty)
assert.Equal(stderr_is_tty, tty.isatty(io.stderr))
end)
it("should return false for a regular file handle", function()
local file, name = tempfile()
finally(function()
file:close()
os.remove(name)
end)
assert.False(tty.isatty(file))
end)
it("should reject invalid arguments", function()
local err = "bad argument #1 to 'isatty' (expected a non-negative integer file descriptor or Lua file handle)"
-- stylua: ignore start
---@diagnostic disable-next-line: param-type-mismatch
assert.Error(function() _ = tty.isatty("1") end, err)
assert.Error(function() _ = tty.isatty(-1) end, err)
assert.Error(function() _ = tty.isatty(1.5) end, err)
assert.Error(function() _ = tty.isatty({}) end, err)
-- stylua: ignore end
---@diagnostic disable-next-line: undefined-field
assert.Throw(function()
_ = tty.isatty(1000000)
---@diagnostic disable: param-type-mismatch, discard-returns
end, "invalid file descriptor")
end)
it("should error when checking a closed file", function()
local file, name = tempfile()
file:close()
finally(function()
os.remove(name)
end)
---@diagnostic disable-next-line: undefined-field
assert.Throw(function()
tty.isatty(file)
end, "closed")
end)
end)
describe("size()", function()
it("should return terminal size or error appropriately for stdout", function()
local stdout_is_tty = tty.isatty()
if stdout_is_tty then
local rows, cols = tty.size()
assert.Number(rows)
assert.Number(cols)
assert.True(rows >= 1)
assert.True(cols >= 1)
else
---@diagnostic disable-next-line: undefined-field
assert.Throw(function()
tty.size()
end, "failed to get terminal size")
end
end)
it("should return terminal size or error appropriately for stderr", function()
local stderr_is_tty = tty.isatty(2)
if stderr_is_tty then
local terminal_rows, terminal_cols = tty.size(io.stderr)
assert.Number(terminal_rows)
assert.Number(terminal_cols)
assert.True(terminal_rows >= 1)
assert.True(terminal_cols >= 1)
else
---@diagnostic disable-next-line: undefined-field
assert.Throw(function()
tty.size(io.stderr)
end, "failed to get terminal size")
end
end)
it("should error for a regular file handle", function()
local file, name = tempfile()
finally(function()
file:close()
os.remove(name)
end)
---@diagnostic disable-next-line: undefined-field
assert.Throw(function()
tty.size(file)
end, "failed to get terminal size")
end)
it("should reject invalid numeric file descriptors", function()
---@diagnostic disable-next-line: undefined-field
assert.Throw(function()
tty.size(1000000)
end, "invalid file descriptor")
end)
it("should error when querying size of a closed file", function()
local closed, closed_name = tempfile()
closed:close()
finally(function()
if closed_name then
os.remove(closed_name)
end
end)
---@diagnostic disable-next-line: undefined-field
assert.Throw(function()
tty.size(closed)
end, "closed")
end)
end)
end)