Skip to content

Commit e99043e

Browse files
committed
test: cleanup tests, remove sleeps in favor of callbacks, ensure green
1 parent 9481fc7 commit e99043e

File tree

6 files changed

+169
-217
lines changed

6 files changed

+169
-217
lines changed

tests/child_helper.lua

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
local env = require("tests.env")
2+
local M = {}
3+
_G.attach_debugger = false
4+
5+
---@param test_name string
6+
function M.new_child_neovim(test_name)
7+
---@class MiniTest.child
8+
local child = MiniTest.new_child_neovim()
9+
local logfile = string.format("./tests/logs/%s.log", test_name)
10+
11+
-- TODO: this needs a reset, as it is reused in multiple tests
12+
-- TODO: this does not work, as the child needs a string representation of the config
13+
child.config = nil
14+
15+
if vim.fn.filereadable(logfile) == 1 then
16+
vim.fn.delete(logfile)
17+
end
18+
19+
function child.reset_config()
20+
child.config = {
21+
panel = "",
22+
suggestion = [[
23+
suggestion_notification = function(virt_text, _)
24+
if (#virt_text > 0) and (#virt_text[1] > 0) and (virt_text[1][1] == "89") then
25+
M.suggested = true
26+
end
27+
end,
28+
]],
29+
logger = string.format(
30+
[[
31+
file_log_level = vim.log.levels.TRACE,
32+
file = "%s",
33+
]],
34+
logfile
35+
),
36+
server = "",
37+
root_dir = "",
38+
should_attach = "",
39+
filetypes = [[
40+
["*"] = true,
41+
]],
42+
auth_provider_url = "",
43+
workspace_folders = "",
44+
server_opts_overrides = "",
45+
copilot_model = "",
46+
copilot_node_command = "",
47+
}
48+
end
49+
50+
function child.setup_and_wait_for_debugger()
51+
if not _G.attach_debugger then
52+
return
53+
end
54+
55+
child.lua([[
56+
local osv = require("osv")
57+
local debugger_attached = false
58+
osv.on_attach = function() debugger_attached = true end
59+
osv.launch({ port = 8086 })
60+
-- wait until a debuggee is attached, or 30 seconds
61+
vim.wait(30000, function() return debugger_attached end, 10)
62+
]])
63+
end
64+
65+
function child.run_pre_case()
66+
child.reset_config()
67+
child.restart({ "-u", "tests/scripts/minimal_init.lua" })
68+
child.fn.setenv("GITHUB_COPILOT_TOKEN", env.COPILOT_TOKEN)
69+
child.setup_and_wait_for_debugger()
70+
end
71+
72+
function child.configure_copilot()
73+
local script = ""
74+
for k, v in pairs(child.config) do
75+
if v ~= "" and v ~= nil then
76+
if type(v) == "string" then
77+
script = string.format(
78+
[[%s%s = {
79+
%s
80+
},
81+
]],
82+
script,
83+
k,
84+
v
85+
)
86+
end
87+
end
88+
end
89+
90+
script = string.format(
91+
[[
92+
M.suggested = false
93+
M.setup({ %s })
94+
]],
95+
script
96+
)
97+
98+
child.lua(script)
99+
100+
child.lua([[
101+
local copilot_is_initialized = function()
102+
local client = require("copilot.client")
103+
return client.initialized
104+
end
105+
106+
vim.wait(30000, copilot_is_initialized, 10)
107+
]])
108+
end
109+
110+
function child.wait_for_suggestion()
111+
child.lua([[
112+
vim.wait(30000, function()
113+
return M.suggested
114+
end, 10)
115+
]])
116+
end
117+
118+
return child
119+
end
120+
121+
return M

tests/test_auth.lua

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
local child = MiniTest.new_child_neovim()
1+
local child_helper = require("tests.child_helper")
2+
local child = child_helper.new_child_neovim("test_auth")
23
local u = require("tests.utils")
3-
local env = require("tests.env")
44

55
local config_path = require("copilot.auth").find_config_path()
66
local config_path_renamed = config_path .. "_temp_renamed"
77

88
local T = MiniTest.new_set({
99
hooks = {
1010
pre_once = function()
11-
if vim.fn.filereadable("./tests/logs/test_auth.log") == 1 then
12-
vim.fn.delete("./tests/logs/test_auth.log")
13-
end
14-
1511
if vim.fn.isdirectory(config_path) == 1 then
1612
vim.fn.rename(config_path, config_path_renamed)
1713
end
1814
end,
1915
pre_case = function()
20-
child.restart({ "-u", "tests/scripts/minimal_init.lua" })
16+
child.run_pre_case()
2117
child.lua("M = require('copilot')")
2218
child.lua("c = require('copilot.client')")
2319
child.lua("s = require('copilot.status')")
2420
child.lua("cmd = require('copilot.command')")
2521
child.lua("a = require('copilot.api')")
2622
child.lua("logger = require('copilot.logger')")
23+
child.fn.setenv("GITHUB_COPILOT_TOKEN", vim.NIL)
2724
end,
2825
post_once = function()
2926
child.stop()
@@ -37,43 +34,23 @@ local T = MiniTest.new_set({
3734

3835
T["auth()"] = MiniTest.new_set()
3936

40-
-- TODO: This test currently assumes you are not auth'd, so the token env var cannot be used
37+
-- TODO: callback for this too
4138
T["auth()"]["auth before attaching, should not give error"] = function()
42-
child.lua([[M.setup({
43-
logger = {
44-
file_log_level = vim.log.levels.TRACE,
45-
file = "./tests/logs/test_auth.log",
46-
trace_lsp = "verbose",
47-
log_lsp_messages = true,
48-
trace_lsp_progress = true,
49-
},
50-
})]])
51-
52-
vim.loop.sleep(500)
39+
child.configure_copilot()
5340
child.cmd("Copilot auth")
54-
vim.loop.sleep(500)
41+
vim.loop.sleep(3000)
5542
local messages = child.cmd_capture("messages")
56-
u.expect_match(messages, ".*Online.*Enabled.*")
43+
u.expect_match(messages, ".*Authenticated as GitHub user.*")
5744
end
5845

5946
T["auth()"]["auth issue replication"] = function()
60-
child.fn.setenv("GITHUB_COPILOT_TOKEN", env.COPILOT_TOKEN)
61-
child.lua([[M.setup({
62-
logger = {
63-
file_log_level = vim.log.levels.TRACE,
64-
file = "./tests/logs/test_auth.log",
65-
trace_lsp = "verbose",
66-
log_lsp_messages = true,
67-
trace_lsp_progress = true,
68-
},
69-
})]])
70-
71-
vim.loop.sleep(500)
47+
child.configure_copilot()
7248
child.cmd("Copilot auth")
73-
vim.loop.sleep(500)
49+
vim.loop.sleep(2000)
7450
child.cmd("Copilot status")
51+
vim.loop.sleep(500)
7552
local messages = child.cmd_capture("messages")
76-
u.expect_match(messages, ".*Online.*Authenticated.*")
53+
u.expect_match(messages, ".*Online.*Enabled.*")
7754
end
7855

7956
return T

tests/test_base_to_organize.lua

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,32 @@
11
local eq = MiniTest.expect.equality
2-
local child = MiniTest.new_child_neovim()
3-
local env = require("tests.env")
4-
-- local utils_debug = require("tests.utils_debug")
2+
local child_helper = require("tests.child_helper")
3+
local child = child_helper.new_child_neovim("test_base_to_organize")
54

65
local T = MiniTest.new_set({
76
hooks = {
87
pre_case = function()
9-
child.restart({ "-u", "tests/scripts/minimal_init.lua" })
8+
child.run_pre_case()
109
child.lua([[M = require('copilot')]])
11-
child.lua([[c = require('copilot.command')]])
1210
child.lua([[s = require('copilot.status')]])
1311
child.lua([[a = require('copilot.api')]])
14-
child.fn.setenv("GITHUB_COPILOT_TOKEN", env.COPILOT_TOKEN)
15-
-- utils_debug.launch_lua_debugee(child)
1612
end,
1713
post_once = child.stop,
1814
},
1915
})
2016

21-
-- TODO: find a way for autocmd or something
22-
local function run_setup()
23-
-- utils_debug.attach_to_debugee()
24-
-- vim.loop.sleep(10000)
25-
-- vim.wait(0)
26-
child.lua([[M.setup({
27-
logger = {
28-
file_log_level = vim.log.levels.TRACE,
29-
file = "./tests/logs/test_example.log",
30-
},
31-
})]])
32-
end
33-
3417
T["lua()"] = MiniTest.new_set()
3518

3619
T["lua()"]["setup not called, copilot.setup_done is false"] = function()
3720
eq(child.lua("return M.setup_done"), false)
3821
end
3922

4023
T["lua()"]["setup called, copilot.setup_done is true"] = function()
41-
run_setup()
24+
child.configure_copilot()
4225
eq(child.lua("return M.setup_done"), true)
4326
end
4427

4528
T["lua()"]["api.status reroutes to status"] = function()
46-
run_setup()
29+
child.configure_copilot()
4730
child.lua("s.data.status = 'test'")
4831
local status = child.lua("return a.status.data.status")
4932
eq(status, "test")

tests/test_client.lua

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
local child = MiniTest.new_child_neovim()
1+
local child_helper = require("tests.child_helper")
2+
local child = child_helper.new_child_neovim("test_client")
23
local u = require("tests.utils")
3-
local env = require("tests.env")
44

55
local T = MiniTest.new_set({
66
hooks = {
7-
pre_once = function()
8-
if vim.fn.filereadable("./tests/logs/test_client.log") == 1 then
9-
vim.fn.delete("./tests/logs/test_client.log")
10-
end
11-
end,
7+
pre_once = function() end,
128
pre_case = function()
13-
child.restart({ "-u", "tests/scripts/minimal_init.lua" })
14-
child.lua([[M = require('copilot')]])
15-
child.lua([[c = require('copilot.client')]])
16-
child.lua([[s = require('copilot.status')]])
17-
child.lua([[cmd = require('copilot.command')]])
18-
child.lua([[a = require('copilot.api')]])
19-
child.lua("logger = require('copilot.logger')")
20-
-- child.lua([[require("osv").launch({ port = 8086 })]])
21-
child.fn.setenv("GITHUB_COPILOT_TOKEN", env.COPILOT_TOKEN)
9+
child.run_pre_case()
10+
child.lua("M = require('copilot')")
11+
child.lua("s = require('copilot.status')")
12+
child.lua("c = require('copilot.client')")
2213
end,
2314
post_once = child.stop,
2415
},
@@ -30,40 +21,14 @@ T["client.config()"]["config, github-enterprise populated"] = function()
3021
child.lua([[M.setup({
3122
auth_provider_url = "https://someurl.com",
3223
})]])
33-
local settings = child.lua([[return vim.inspect(c.config.settings)]])
24+
local settings = child.lua("return vim.inspect(c.config.settings)")
3425
u.expect_match(settings, "{.*github%-enterprise.*{.*uri.*https://someurl%.com.*}.*}")
3526
end
3627

3728
T["client()"] = MiniTest.new_set()
3829

39-
-- T["client()"]["buf_attach"] = function()
40-
-- child.lua([[M.setup({
41-
-- logger = {
42-
-- file_log_level = vim.log.levels.TRACE,
43-
-- file = "./tests/logs/test_client.log",
44-
-- },
45-
-- })]])
46-
--
47-
-- child.lua([[cmd.enable()]])
48-
-- child.lua([[cmd.attach({ force = true })]])
49-
-- local messages = child.cmd_capture("messages")
50-
-- vim.loop.sleep(500)
51-
-- -- u.expect_match(messages, "Copilot: Copilot attached to buffer")
52-
-- print(messages)
53-
-- end
54-
55-
-- TODO: The sleep is a poor way to wait for the scheduled job to be done...
56-
-- have not found a better way yet.
5730
T["client()"]["status info"] = function()
58-
child.lua([[M.setup({
59-
logger = {
60-
file_log_level = vim.log.levels.TRACE,
61-
file = "./tests/logs/test_client.log",
62-
},
63-
})]])
64-
65-
-- child.lua("vim.wait(0)") -- does not seem to be enough to force the async job
66-
vim.loop.sleep(500)
31+
child.configure_copilot()
6732
child.cmd("Copilot status")
6833
vim.loop.sleep(500)
6934
local messages = child.cmd_capture("messages")

tests/test_panel.lua

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
local eq = MiniTest.expect.equality
2-
local child = MiniTest.new_child_neovim()
3-
local env = require("tests.env")
2+
local child_helper = require("tests.child_helper")
3+
local child = child_helper.new_child_neovim("test_client")
44

55
local T = MiniTest.new_set({
66
hooks = {
7-
pre_once = function()
8-
if vim.fn.filereadable("./tests/logs/test_suggestion.log") == 1 then
9-
vim.fn.delete("./tests/logs/test_suggestion.log")
10-
end
11-
end,
7+
pre_once = function() end,
128
pre_case = function()
13-
child.restart({ "-u", "tests/scripts/minimal_init.lua" })
9+
child.run_pre_case()
1410
child.bo.readonly = false
1511
child.lua("M = require('copilot')")
16-
child.lua("cmd = require('copilot.command')")
1712
child.lua("p = require('copilot.panel')")
18-
-- child.lua([[require("osv").launch({ port = 8086 })]])
19-
child.fn.setenv("GITHUB_COPILOT_TOKEN", env.COPILOT_TOKEN)
2013
end,
2114
post_once = child.stop,
2215
},
@@ -27,24 +20,9 @@ T["panel()"] = MiniTest.new_set()
2720
-- This test can fail if the LSP is taking more time than usual and re-running it passes
2821
T["panel()"]["panel suggestions works"] = function()
2922
child.o.lines, child.o.columns = 30, 100
30-
child.lua([[M.setup({
31-
panel = {
32-
auto_refresh = true,
33-
},
34-
suggestion = {
35-
auto_trigger = true,
36-
},
37-
logger = {
38-
file_log_level = vim.log.levels.TRACE,
39-
file = "./tests/logs/test_suggestion.log",
40-
},
41-
filetypes = {
42-
["*"] = true,
43-
},
44-
})]])
45-
46-
-- look for a synchronous way to wait for engine to be up
47-
vim.loop.sleep(500)
23+
child.config.panel = child.config.panel .. "auto_refresh = true,"
24+
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
25+
child.configure_copilot()
4826
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
4927
child.lua("p.toggle()")
5028

0 commit comments

Comments
 (0)