forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_binary.lua
More file actions
185 lines (164 loc) · 5.72 KB
/
test_binary.lua
File metadata and controls
185 lines (164 loc) · 5.72 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
local eq = MiniTest.expect.equality
local child_helper = require("tests.child_helper")
local child = child_helper.new_child_neovim("test_binary")
local T = MiniTest.new_set({
hooks = {
pre_once = function() end,
pre_case = function()
child.run_pre_case(false)
end,
post_once = child.stop,
},
})
T["binary()"] = MiniTest.new_set()
T["binary()"]["get_copilot_server_info returns valid info for current OS"] = function()
local result = child.lua([[
local binary = require("copilot.lsp.binary")
binary.copilot_server_info = nil
local info = binary.get_copilot_server_info()
return {
has_path = info.path ~= nil and info.path ~= "",
has_filename = info.filename ~= nil and info.filename ~= "",
has_absolute_path = info.absolute_path ~= nil,
}
]])
eq(result.has_path, true)
eq(result.has_filename, true)
eq(result.has_absolute_path, true)
end
T["binary()"]["get_copilot_server_info caches result"] = function()
local result = child.lua([[
local binary = require("copilot.lsp.binary")
binary.copilot_server_info = nil
local info1 = binary.get_copilot_server_info()
local info2 = binary.get_copilot_server_info()
return info1.path == info2.path and info1.filename == info2.filename
]])
eq(result, true)
end
T["binary()"]["get_server_path returns absolute filepath"] = function()
local result = child.lua([[
local binary = require("copilot.lsp.binary")
binary.copilot_server_info = nil
local path = binary.get_server_path()
return type(path) == "string" and #path > 0
]])
eq(result, true)
end
T["binary()"]["setup with non-existent custom path returns early"] = function()
local result = child.lua([[
local binary = require("copilot.lsp.binary")
local ok = pcall(binary.setup, "/nonexistent/path/to/server")
return ok
]])
eq(result, true)
end
T["binary()"]["get_server_info returns path/filename when client is provided"] = function()
local result = child.lua([[
local binary = require("copilot.lsp.binary")
binary.copilot_server_info = nil
local info = binary.get_copilot_server_info()
local server_info = binary.get_server_info({})
return {
server_info = server_info,
expected = info.path .. "/" .. info.filename,
}
]])
eq(result.server_info, result.expected)
end
T["binary()"]["get_server_info returns not running when client is nil"] = function()
local result = child.lua([[
local binary = require("copilot.lsp.binary")
binary.copilot_server_info = nil
local info = binary.get_copilot_server_info()
local server_info = binary.get_server_info(nil)
return {
server_info = server_info,
expected = info.path .. "/" .. info.filename .. " " .. "not running",
}
]])
eq(result.server_info, result.expected)
end
T["binary()"]["download_file rejects when neither curl nor wget is available"] = function()
local result = child.lua([[
local binary = require("copilot.lsp.binary")
-- Mock vim.fn.executable to return 0 for both curl and wget
local orig_executable = vim.fn.executable
vim.fn.executable = function(cmd)
if cmd == "curl" or cmd == "wget" then
return 0
end
return orig_executable(cmd)
end
-- Clean up any leftover files from previous runs
vim.fn.delete("/tmp/copilot-test", "rf")
-- Access download_file indirectly via init() which calls download_file
-- We need to reset state and set up server info so init() reaches download_file
binary.initialized = false
binary.initialization_failed = false
binary.copilot_server_info = {
path = "linux-x64",
filename = "copilot-language-server-test",
absolute_path = "/tmp/copilot-test",
absolute_filepath = "/tmp/copilot-test/copilot-language-server-test",
extracted_filename = "copilot-language-server",
}
local captured_error = nil
local logger = require("copilot.logger")
local orig_error = logger.error
logger.error = function(msg, ...)
captured_error = msg
end
local ok = binary.init()
logger.error = orig_error
vim.fn.executable = orig_executable
return {
ok = ok,
has_error = captured_error ~= nil,
error_msg = captured_error or "",
}
]])
eq(result.ok, false)
eq(result.has_error, true)
end
T["binary()"]["download_file does not reject when wget is available but curl is not"] = function()
local result = child.lua([[
local binary = require("copilot.lsp.binary")
-- Mock vim.fn.executable: curl unavailable, wget available
local orig_executable = vim.fn.executable
vim.fn.executable = function(cmd)
if cmd == "curl" then return 0 end
if cmd == "wget" then return 1 end
return orig_executable(cmd)
end
-- Clean up any leftover files from previous runs
vim.fn.delete("/tmp/copilot-test", "rf")
binary.initialized = false
binary.initialization_failed = false
binary.copilot_server_info = {
path = "linux-x64",
filename = "copilot-language-server-test",
absolute_path = "/tmp/copilot-test",
absolute_filepath = "/tmp/copilot-test/copilot-language-server-test",
extracted_filename = "copilot-language-server",
}
local neither_error = false
local logger = require("copilot.logger")
local orig_error = logger.error
local orig_notify = logger.notify
logger.notify = function() end
logger.error = function(msg, ...)
if type(msg) == "string" and msg:find("neither") then
neither_error = true
end
end
binary.init()
logger.error = orig_error
logger.notify = orig_notify
vim.fn.executable = orig_executable
return neither_error
]])
-- Should NOT get the "neither curl nor wget" error when wget IS available
eq(result, false)
end
return T