Skip to content

Commit 2cd99fb

Browse files
committed
tests: initial round of tests using mini.test
1 parent 784f4eb commit 2cd99fb

24 files changed

+7052
-107
lines changed

.luarc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"_workspace.ignoreDir": ["deps"]
3+
}

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Run all test files
2+
# test: deps/mini.nvim
3+
test:
4+
nvim --headless --noplugin -u ./scripts/minimal_init.lua -c "lua MiniTest.run()"
5+
6+
# Run test from file at `$FILE` environment variable
7+
test_file: deps/mini.nvim
8+
nvim --headless --noplugin -u ./scripts/minimal_init.lua -c "lua MiniTest.run_file('$(FILE)')"
9+
10+
# Download 'mini.nvim' to use its 'mini.test' testing module
11+
# deps/mini.nvim:
12+
# @mkdir deps
13+
# git clone --filter=blob:none https://github.com/echasnovski/mini.nvim $@

lua/copilot/client/config.lua

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,7 @@ function M.prepare_client_config(overrides, client)
1616

1717
client.startup_error = nil
1818

19-
local server_path = nil
20-
local cmd = nil
21-
22-
if config.server.custom_server_filepath and vim.fn.filereadable(config.server.custom_server_filepath) then
23-
server_path = config.server.custom_server_filepath
24-
end
25-
26-
if config.server.type == "nodejs" then
27-
cmd = {
28-
lsp.nodejs.node_command,
29-
server_path or lsp.nodejs.get_server_path(),
30-
"--stdio",
31-
}
32-
elseif config.server.type == "binary" then
33-
cmd = {
34-
server_path or lsp.binary.get_server_path(),
35-
"--stdio",
36-
}
37-
end
19+
local cmd = lsp.get_execute_command()
3820

3921
if not cmd then
4022
logger.error("copilot server type not supported")
@@ -84,7 +66,7 @@ function M.prepare_client_config(overrides, client)
8466
}
8567

8668
if proxy_uri then
87-
vim.tbl_extend("force", settings, {
69+
settings = vim.tbl_extend("force", settings, {
8870
http = { ---@type copilot_settings_http
8971
proxy = proxy_uri,
9072
proxyStrictSSL = vim.g.copilot_proxy_strict_ssl or false,
@@ -94,7 +76,7 @@ function M.prepare_client_config(overrides, client)
9476
end
9577

9678
if provider_url then
97-
vim.tbl_extend("force", settings, {
79+
settings = vim.tbl_extend("force", settings, {
9880
["github-enterprise"] = { ---@type copilot_settings_github-enterprise
9981
uri = provider_url,
10082
},

lua/copilot/client/filetypes.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
local M = {}
1+
local M = {
2+
internal_filetypes = {
3+
yaml = false,
4+
markdown = false,
5+
help = false,
6+
gitcommit = false,
7+
gitrebase = false,
8+
hgcommit = false,
9+
svn = false,
10+
cvs = false,
11+
["."] = false,
12+
},
13+
}
214

315
local language_normalization_map = {
416
bash = "shellscript",
@@ -27,18 +39,6 @@ function M.language_for_file_type(filetype)
2739
return language_normalization_map[ft] or ft
2840
end
2941

30-
local internal_filetypes = {
31-
yaml = false,
32-
markdown = false,
33-
help = false,
34-
gitcommit = false,
35-
gitrebase = false,
36-
hgcommit = false,
37-
svn = false,
38-
cvs = false,
39-
["."] = false,
40-
}
41-
4242
---@param filetype_enabled boolean|fun():boolean
4343
local function resolve_filetype_enabled(filetype_enabled)
4444
if type(filetype_enabled) == "function" then
@@ -69,8 +69,8 @@ function M.is_ft_disabled(ft, filetypes)
6969
string.format("'filetype' %s rejected by config filetypes[%s]", ft, "*")
7070
end
7171

72-
if internal_filetypes[short_ft] ~= nil then
73-
return not internal_filetypes[short_ft],
72+
if M.internal_filetypes[short_ft] ~= nil then
73+
return not M.internal_filetypes[short_ft],
7474
string.format("'filetype' %s rejected by internal_filetypes[%s]", ft, short_ft)
7575
end
7676

lua/copilot/client/init.lua

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ end
3838

3939
---@param force? boolean
4040
function M.buf_attach(force)
41-
if lsp.binary.initialization_failed then
41+
if lsp.initialization_failed() then
42+
logger.error("copilot-language-server failed to initialize")
4243
M.startup_error = "initialization of copilot-language-server failed"
4344
return
4445
end
@@ -48,10 +49,7 @@ function M.buf_attach(force)
4849
return
4950
end
5051

51-
local bufnr = vim.api.nvim_get_current_buf()
52-
local bufname = vim.api.nvim_buf_get_name(bufnr)
53-
54-
if not (force or (config.should_attach(bufnr, bufname) and util.should_attach())) then
52+
if not (force or util.should_attach()) then
5553
logger.debug("not attaching to buffer based on force and should_attach criteria")
5654
return
5755
end
@@ -64,6 +62,7 @@ function M.buf_attach(force)
6462
-- In case it has changed, we update it
6563
M.config.root_dir = utils.get_root_dir(config.root_dir)
6664

65+
logger.trace("attaching to buffer")
6766
local ok, client_id_or_err = pcall(vim.lsp.start, M.config)
6867
if not ok then
6968
logger.error(string.format("failed to start LSP client: %s", client_id_or_err))
@@ -75,6 +74,7 @@ function M.buf_attach(force)
7574
else
7675
logger.error("LSP client failed to start (no client ID returned)")
7776
end
77+
logger.trace("buffer attached")
7878
end
7979

8080
function M.buf_detach()
@@ -83,7 +83,7 @@ function M.buf_detach()
8383
end
8484
end
8585

86-
---@return nil|vim.lsp.Client
86+
---@return vim.lsp.Client|nil
8787
function M.get()
8888
return vim.lsp.get_client_by_id(M.id)
8989
end
@@ -95,11 +95,11 @@ end
9595
---@param callback fun(client:table):nil
9696
function M.use_client(callback)
9797
if is_disabled then
98-
logger.warn("copilot is offline")
98+
logger.notify("copilot is offline")
9999
return
100100
end
101101

102-
local client = M.get() --[[@as table]]
102+
local client = M.get()
103103

104104
if not client then
105105
if not M.config then
@@ -124,36 +124,37 @@ function M.use_client(callback)
124124
return
125125
end
126126

127-
local timer, err, _ = vim.uv.new_timer()
128-
129-
if not timer then
130-
logger.error(string.format("error creating timer: %s", err))
131-
return
132-
end
133-
134-
timer:start(
135-
0,
136-
100,
137-
vim.schedule_wrap(function()
138-
if client.initialized and not timer:is_closing() then
139-
timer:stop()
140-
timer:close()
141-
callback(client)
142-
end
143-
end)
144-
)
127+
logger.error("client is not initialized yet")
128+
-- Following code is commented out for now because:
129+
-- 1) I am hoping it is not needed anymore and
130+
-- 2) It causes issues with testing >_<
131+
--
132+
-- local timer, err, _ = vim.uv.new_timer()
133+
--
134+
-- if not timer then
135+
-- logger.error(string.format("error creating timer: %s", err))
136+
-- return
137+
-- end
138+
--
139+
-- timer:start(
140+
-- 0,
141+
-- 100,
142+
-- vim.schedule_wrap(function()
143+
-- if client.initialized and not timer:is_closing() then
144+
-- timer:stop()
145+
-- timer:close()
146+
-- callback(client)
147+
-- else
148+
-- logger.error("client not initialized yet")
149+
-- end
150+
-- end)
151+
-- )
145152
end
146153

147154
function M.setup()
155+
logger.trace("setting up client")
148156
local node_command = config.copilot_node_command
149-
150-
--TODO: merge the two types into an indirection
151-
if config.server.type == "nodejs" then
152-
lsp.nodejs.setup(node_command, config.server.custom_server_filepath)
153-
elseif config.server.type == "binary" then
154-
lsp.binary.setup(config.server.custom_server_filepath)
155-
end
156-
157+
lsp.setup(config.server, node_command)
157158
M.config = require("copilot.client.config").prepare_client_config(config.server_opts_overrides, M)
158159

159160
if not M.config then

lua/copilot/client/utils.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local config = require("copilot.config")
22
local logger = require("copilot.logger")
3+
local client_ft = require("copilot.client.filetypes")
34
local M = {}
45

56
---@param config_root_dir RootDirFuncOrString
@@ -25,7 +26,7 @@ function M.get_workspace_configurations()
2526
local filetypes = vim.deepcopy(config.filetypes) --[[@as table<string, boolean>]]
2627

2728
if filetypes["*"] == nil then
28-
filetypes = vim.tbl_deep_extend("keep", filetypes, require("copilot.client.filetypes").internal_filetypes)
29+
filetypes = vim.tbl_deep_extend("keep", filetypes, client_ft.internal_filetypes)
2930
end
3031

3132
local copilot_model = config and config.copilot_model ~= "" and config.copilot_model or ""

lua/copilot/command.lua

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,38 @@
1-
local a = require("copilot.api")
21
local c = require("copilot.client")
32
local u = require("copilot.util")
3+
local logger = require("copilot.logger")
4+
local lsp = require("copilot.lsp")
45

56
local M = {}
67

78
function M.version()
89
local info = u.get_editor_info()
910

10-
---@type (string|table)[]
11+
---@type string
1112
local lines = {
1213
info.editorInfo.name .. " " .. info.editorInfo.version,
13-
"copilot.vim" .. " " .. info.editorPluginInfo.version,
14+
"copilot language server" .. " " .. info.editorPluginInfo.version,
1415
"copilot.lua" .. " " .. u.get_copilot_lua_version(),
1516
}
1617

1718
local client = c.get()
1819

1920
coroutine.wrap(function()
20-
-- TODO: this is now in lsp/*
21-
local copilot_server_info = u.get_copilot_server_info()
22-
if client then
23-
local _, data = a.get_version(client)
24-
lines[#lines + 1] = copilot_server_info.path .. "/" .. copilot_server_info().filename .. " " .. data.version
25-
else
26-
lines[#lines + 1] = copilot_server_info.path .. "/" .. copilot_server_info().filename .. " " .. "not running"
27-
end
28-
29-
local chunks = {}
30-
for _, line in ipairs(lines) do
31-
chunks[#chunks + 1] = type(line) == "table" and line or { line }
32-
chunks[#chunks + 1] = { "\n", "NONE" }
33-
end
34-
35-
vim.api.nvim_echo(chunks, true, {})
21+
local server_info = lsp.get_server_info(client)
22+
logger.notify(lines .. "\n" .. server_info)
3623
end)()
3724
end
3825

3926
---@param opts? { force?: boolean }
4027
function M.attach(opts)
28+
logger.trace("attaching to buffer")
4129
opts = opts or {}
4230

4331
if not opts.force then
4432
local should_attach, no_attach_reason = u.should_attach()
45-
-- TODO: add other should_attach method here
33+
4634
if not should_attach then
47-
vim.api.nvim_echo({
48-
{ "[Copilot] " .. no_attach_reason .. "\n" },
49-
{ "[Copilot] to force attach, run ':Copilot! attach'" },
50-
}, true, {})
35+
logger.notify(no_attach_reason .. "\nto force attach, run ':Copilot! attach'")
5136
return
5237
end
5338

@@ -76,12 +61,14 @@ function M.toggle(opts)
7661
end
7762

7863
function M.enable()
64+
logger.trace("enabling Copilot")
7965
c.setup()
8066
require("copilot.panel").setup()
8167
require("copilot.suggestion").setup()
8268
end
8369

8470
function M.disable()
71+
logger.trace("disabling Copilot")
8572
c.teardown()
8673
require("copilot.panel").teardown()
8774
require("copilot.suggestion").teardown()

lua/copilot/lsp/binary.lua

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,9 @@ local function set_permissions(filename)
180180
return true
181181
end
182182

183-
-- TODO: when this fails, it will cause a couple more errors before crashing
184183
-- let's hope the naming convention does not change!!!
185184
---@return boolean
186-
function M.ensure_client_is_downloaded()
185+
function M.init()
187186
if M.initialized then
188187
return true
189188
elseif M.initialization_failed then
@@ -268,6 +267,26 @@ local function is_musl()
268267
return string.sub(ldd_output, 1, 4) == "musl"
269268
end
270269

270+
---@param client vim.lsp.Client|nil
271+
---@return string
272+
function M.get_server_info(client)
273+
local copilot_server_info = M.get_copilot_server_info()
274+
275+
if client then
276+
return copilot_server_info.path .. "/" .. copilot_server_info().filename
277+
else
278+
return copilot_server_info.path .. "/" .. copilot_server_info().filename .. " " .. "not running"
279+
end
280+
end
281+
282+
---@return table
283+
function M.get_execute_command()
284+
return {
285+
M.server_path or M.get_server_path(),
286+
"--stdio",
287+
}
288+
end
289+
271290
---@return copilot_server_info
272291
function M.get_copilot_server_info()
273292
if M.copilot_server_info then
@@ -345,8 +364,6 @@ function M.setup(custom_server_path)
345364

346365
M.initialized = true
347366
end
348-
349-
M.ensure_client_is_downloaded()
350367
end
351368

352369
return M

0 commit comments

Comments
 (0)