1
1
-- these are helper functions for testing the nodejs LSP integration
2
2
-- they stub out system interactions for verifying access to node and the LSP script
3
3
4
- Stub = {}
4
+ M = {}
5
5
6
- Stub .default_server_path = " copilot/js/language-server.js"
7
- Stub .custom_server_path = " custom/path/to/language-server.js"
6
+ M .default_server_path = " copilot/js/language-server.js"
7
+ M .custom_server_path = " custom/path/to/language-server.js"
8
8
9
9
--- @param stdout string the stdout that will be returned by the stubbed vim.system
10
10
--- @param code integer the exit code that will be returned by the stubbed vim.system
11
11
--- @param fail boolean if true , vim.system will error when called
12
12
--- @param callback function the function to call while vim.system is stubbed
13
13
--- @return table | nil captured_args -- the arguments vim.system was called with
14
- Stub .process = function (stdout , code , fail , callback )
14
+ function M .process (stdout , code , fail , callback )
15
15
local captured_args = nil
16
16
local original_vim_system = vim .system
17
+
18
+ --- @diagnostic disable-next-line : duplicate-set-field
17
19
vim .system = function (cmd )
18
20
captured_args = cmd
19
21
if fail then
@@ -23,57 +25,63 @@ Stub.process = function(stdout, code, fail, callback)
23
25
wait = function ()
24
26
return {
25
27
stdout = stdout .. " \n " ,
26
- code = code
28
+ code = code ,
27
29
}
28
- end
30
+ end ,
29
31
}
30
32
end
31
33
-- wrap callback in pcall to ensure vim.system is restored if callback errors
32
34
local ok , err = pcall (callback )
33
35
vim .system = original_vim_system
34
- if not ok then error (err ) end
36
+ if not ok then
37
+ error (err )
38
+ end
35
39
return captured_args
36
40
end
37
41
38
- Stub .valid_node_version = " 20.0.0"
39
- Stub .invalid_node_version = " 10.0.0"
42
+ M .valid_node_version = " 20.0.0"
43
+ M .invalid_node_version = " 10.0.0"
40
44
41
45
--- Convenience wrapper for Stub.process for a valid Node.js version (>= 20)
42
- Stub .valid_node = function (callback )
43
- return Stub .process (" v" .. Stub .valid_node_version , 0 , false , callback )
46
+ function M .valid_node (callback )
47
+ return M .process (" v" .. M .valid_node_version , 0 , false , callback )
44
48
end
45
49
46
50
--- Convenience wrapper for Stub.process for an invalid Node.js version (< 20)
47
- Stub .invalid_node = function (callback )
48
- return Stub .process (" v" .. Stub .invalid_node_version , 0 , false , callback )
51
+ function M .invalid_node (callback )
52
+ return M .process (" v" .. M .invalid_node_version , 0 , false , callback )
49
53
end
50
54
51
55
--- @param callback function the function to call while vim.api.nvim_get_runtime_file is stubbed
52
56
--- @return string | nil captured_path -- the path vim.api.nvim_get_runtime_file was called with
53
- Stub .get_runtime_server_path = function (callback )
57
+ function M .get_runtime_server_path (callback )
54
58
local captured_path = nil
55
59
56
60
local original_get_file = vim .api .nvim_get_runtime_file
61
+ --- @diagnostic disable-next-line : duplicate-set-field
57
62
vim .api .nvim_get_runtime_file = function (path )
58
63
captured_path = path
59
- return { vim .fn .expand (Stub .default_server_path ) }
64
+ return { vim .fn .expand (M .default_server_path ) }
60
65
end
61
66
62
67
local original_filereadable = vim .fn .filereadable
68
+ --- @diagnostic disable-next-line : duplicate-set-field
63
69
vim .fn .filereadable = function ()
64
70
return 1
65
71
end
66
72
67
73
-- stub valid node version for callback so setup() succeeds
68
- Stub .valid_node (function ()
74
+ M .valid_node (function ()
69
75
-- wrap callback in pcall to ensure vim.api.nvim_get_runtime_file is restored if callback errors
70
76
local ok , err = pcall (callback )
71
77
vim .api .nvim_get_runtime_file = original_get_file
72
78
vim .fn .filereadable = original_filereadable
73
- if not ok then error (err ) end
79
+ if not ok then
80
+ error (err )
81
+ end
74
82
end )
75
83
76
84
return captured_path
77
85
end
78
86
79
- return Stub
87
+ return M
0 commit comments