1
1
local eq = MiniTest .expect .equality
2
+ local stub = require (" tests.test_nodejs_stubs" )
2
3
3
4
local T = MiniTest .new_set ({
4
5
hooks = {
@@ -12,156 +13,170 @@ local T = MiniTest.new_set({
12
13
13
14
T [" get_node_version()" ] = MiniTest .new_set ()
14
15
15
- local function stub_process (stdout , code , fail , callback )
16
- local captured_args = nil
17
- local original_vim_system = vim .system
18
- vim .system = function (cmd , opts )
19
- captured_args = cmd
20
- if fail then
21
- error (" Command failed" )
22
- end
23
- return {
24
- wait = function ()
25
- return {
26
- stdout = stdout .. " \n " ,
27
- code = code
28
- }
29
- end
30
- }
31
- end
32
- callback ()
33
- vim .system = original_vim_system
34
- return captured_args
35
- end
36
-
37
16
T [" get_node_version()" ][" default node command" ] = function ()
38
- captured_args = stub_process ( " v20.10.0 " , 0 , false , function ()
17
+ local captured_args = stub . valid_node ( function ()
39
18
local nodejs = require (" copilot.lsp.nodejs" )
40
19
nodejs .setup ()
41
20
42
21
local version , error = nodejs .get_node_version ()
43
22
44
- eq (version , " 20.10.0 " )
23
+ eq (version , stub . valid_node_version )
45
24
eq (error , nil )
46
25
end )
47
26
eq (captured_args , { " node" , " --version" })
48
27
end
49
28
50
29
T [" get_node_version()" ][" custom node command as string" ] = function ()
51
- local captured_args = stub_process ( " v20.10.0 " , 0 , false , function ()
30
+ local captured_args = stub . valid_node ( function ()
52
31
local nodejs = require (" copilot.lsp.nodejs" )
53
32
nodejs .setup (" /usr/local/bin/node" )
54
33
55
34
local version , error = nodejs .get_node_version ()
56
35
57
- eq (version , " 20.10.0 " )
36
+ eq (version , stub . valid_node_version )
58
37
eq (error , nil )
59
38
end )
60
39
eq (captured_args , { " /usr/local/bin/node" , " --version" })
61
40
end
62
41
63
42
T [" get_node_version()" ][" custom node command as string with spaces" ] = function ()
64
- local captured_args = stub_process ( " v20.10.0 " , 0 , false , function ()
43
+ local captured_args = stub . valid_node ( function ()
65
44
local nodejs = require (" copilot.lsp.nodejs" )
66
45
nodejs .setup (" /path to/node" )
67
46
68
47
local version , error = nodejs .get_node_version ()
69
48
70
- eq (version , " 20.10.0 " )
49
+ eq (version , stub . valid_node_version )
71
50
eq (error , nil )
72
51
end )
73
52
eq (captured_args , { " /path to/node" , " --version" })
74
53
end
75
54
76
55
T [" get_node_version()" ][" custom node command as table" ] = function ()
77
- local captured_args = stub_process ( " v20.10.0 " , 0 , false , function ()
56
+ local captured_args = stub . valid_node ( function ()
78
57
local nodejs = require (" copilot.lsp.nodejs" )
79
58
nodejs .setup ({ " mise" , " x" , " node@lts" , " --" , " node" })
80
59
81
60
local version , error = nodejs .get_node_version ()
82
61
83
- eq (version , " 20.10.0 " )
62
+ eq (version , stub . valid_node_version )
84
63
eq (error , nil )
85
64
end )
86
65
eq (captured_args , { " mise" , " x" , " node@lts" , " --" , " node" , " --version" })
87
66
end
88
67
89
68
T [" get_node_version()" ][" handles vim.system failure" ] = function ()
90
- local captured_args = stub_process (" " , - 1 , true , function ()
69
+ local captured_args = stub . process (" " , - 1 , true , function ()
91
70
local nodejs = require (" copilot.lsp.nodejs" )
92
71
nodejs .setup (" node" )
93
72
94
- local version , error = nodejs .get_node_version ()
73
+ local _ , error = nodejs .get_node_version ()
95
74
96
- eq (version , " " )
97
- -- Error should contain failure information
98
- local expected_error_pattern = " Could not determine Node%.js version"
99
- eq (type (error ), " string" )
100
- eq (error :find (expected_error_pattern ) ~= nil , true )
75
+ eq (error :find (" Could not determine Node.js version" ) ~= nil , true )
101
76
end )
77
+ eq (captured_args , { " node" , " --version" })
102
78
end
103
79
104
80
T [" get_node_version()" ][" handles process with non-zero exit code" ] = function ()
105
- local captured_args = stub_process (" " , 127 , false , function ()
81
+ local captured_args = stub . process (" " , 127 , false , function ()
106
82
local nodejs = require (" copilot.lsp.nodejs" )
107
83
nodejs .setup (" nonexistent-node" )
108
84
109
- local version , error = nodejs .get_node_version ()
85
+ local _ , error = nodejs .get_node_version ()
110
86
111
- eq (version , " " )
112
- -- Error should contain failure information with exit code
113
- local expected_error_pattern = " Could not determine Node%.js version"
114
- eq (type (error ), " string" )
115
- eq (error :find (expected_error_pattern ) ~= nil , true )
116
- eq (error :find (" 127" ) ~= nil , true )
87
+ eq (error :find (" Could not determine Node.js version" ) ~= nil , true )
117
88
end )
118
89
eq (captured_args , { " nonexistent-node" , " --version" })
119
90
end
120
91
121
92
T [" get_node_version()" ][" validates node version requirement" ] = function ()
122
- local captured_args = stub_process ( " v18.17.0 " , 0 , false , function ()
93
+ local captured_args = stub . invalid_node ( function ()
123
94
local nodejs = require (" copilot.lsp.nodejs" )
124
95
nodejs .setup (" node" )
125
96
126
- local version , error = nodejs .get_node_version ()
97
+ local _ , error = nodejs .get_node_version ()
127
98
128
- eq (version , " 18.17.0" )
129
- -- Error should indicate version requirement not met
130
- eq (type (error ), " string" )
131
- eq (error :find (" Node%.js version 20 or newer required" ) ~= nil , true )
132
- eq (error :find (" 18%.17%.0" ) ~= nil , true )
99
+ eq (error :find (" Node.js version 20 or newer required" ) ~= nil , true )
133
100
end )
134
101
eq (captured_args , { " node" , " --version" })
135
102
end
136
103
137
104
T [" get_execute_command()" ] = MiniTest .new_set ()
138
105
139
- T [" get_execute_command()" ][" default node command" ] = function ()
140
- local nodejs = require (" copilot.lsp.nodejs" )
141
- nodejs .setup ()
142
- local cmd = nodejs .get_execute_command ()
143
- eq (cmd , { " node" , nodejs .server_path , " --stdio" })
106
+ T [" get_execute_command()" ][" default node command, default server path" ] = function ()
107
+ local captured_path = stub .get_runtime_server_path (function ()
108
+ local nodejs = require (" copilot.lsp.nodejs" )
109
+ eq (nodejs .setup (), true )
110
+ local cmd = nodejs .get_execute_command ()
111
+ eq (cmd , { " node" , stub .default_server_path , " --stdio" })
112
+ end )
113
+ eq (captured_path , stub .default_server_path )
114
+ end
115
+
116
+ T [" get_execute_command()" ][" default node command, custom server path" ] = function ()
117
+ stub .get_runtime_server_path (function ()
118
+ local nodejs = require (" copilot.lsp.nodejs" )
119
+ eq (nodejs .setup (nil , stub .custom_server_path ), true )
120
+ local cmd = nodejs .get_execute_command ()
121
+ eq (cmd , { " node" , stub .custom_server_path , " --stdio" })
122
+ end )
123
+ end
124
+
125
+ T [" get_execute_command()" ][" custom node command as string, default server path" ] = function ()
126
+ local captured_path = stub .get_runtime_server_path (function ()
127
+ local nodejs = require (" copilot.lsp.nodejs" )
128
+ eq (nodejs .setup (" /usr/local/bin/node" ), true )
129
+ local cmd = nodejs .get_execute_command ()
130
+ eq (cmd , { " /usr/local/bin/node" , stub .default_server_path , " --stdio" })
131
+ end )
132
+ eq (captured_path , stub .default_server_path )
133
+ end
134
+
135
+ T [" get_execute_command()" ][" custom node command as string, custom server path" ] = function ()
136
+ stub .get_runtime_server_path (function ()
137
+ local nodejs = require (" copilot.lsp.nodejs" )
138
+ nodejs .setup (" /usr/local/bin/node" , stub .custom_server_path )
139
+ local cmd = nodejs .get_execute_command ()
140
+ eq (cmd , { " /usr/local/bin/node" , stub .custom_server_path , " --stdio" })
141
+ end )
142
+ end
143
+
144
+ T [" get_execute_command()" ][" custom node command as string with spaces, default server path" ] = function ()
145
+ local captured_path = stub .get_runtime_server_path (function ()
146
+ local nodejs = require (" copilot.lsp.nodejs" )
147
+ nodejs .setup (" /path to/node" )
148
+ local cmd = nodejs .get_execute_command ()
149
+ eq (cmd , { " /path to/node" , stub .default_server_path , " --stdio" })
150
+ end )
151
+ eq (captured_path , stub .default_server_path )
144
152
end
145
153
146
- T [" get_execute_command()" ][" custom node command as string" ] = function ()
147
- local nodejs = require (" copilot.lsp.nodejs" )
148
- nodejs .setup (" /usr/local/bin/node" )
149
- local cmd = nodejs .get_execute_command ()
150
- eq (cmd , { " /usr/local/bin/node" , nodejs .server_path , " --stdio" })
154
+ T [" get_execute_command()" ][" custom node command as string with spaces, custom server path" ] = function ()
155
+ stub .get_runtime_server_path (function ()
156
+ local nodejs = require (" copilot.lsp.nodejs" )
157
+ nodejs .setup (" /path to/node" , stub .custom_server_path )
158
+ local cmd = nodejs .get_execute_command ()
159
+ eq (cmd , { " /path to/node" , stub .custom_server_path , " --stdio" })
160
+ end )
151
161
end
152
162
153
- T [" get_execute_command()" ][" custom node command as string with spaces" ] = function ()
154
- local nodejs = require (" copilot.lsp.nodejs" )
155
- nodejs .setup (" /path to/node" )
156
- local cmd = nodejs .get_execute_command ()
157
- eq (cmd , { " /path to/node" , nodejs .server_path , " --stdio" })
163
+ T [" get_execute_command()" ][" custom node command as table, default server path" ] = function ()
164
+ local captured_path = stub .get_runtime_server_path (function ()
165
+ local nodejs = require (" copilot.lsp.nodejs" )
166
+ nodejs .setup ({ " mise" , " x" , " node@lts" , " --" , " node" })
167
+ local cmd = nodejs .get_execute_command ()
168
+ eq (cmd , { " mise" , " x" , " node@lts" , " --" , " node" , stub .default_server_path , " --stdio" })
169
+ end )
170
+ eq (captured_path , stub .default_server_path )
158
171
end
159
172
160
- T [" get_execute_command()" ][" custom node command as table" ] = function ()
161
- local nodejs = require (" copilot.lsp.nodejs" )
162
- nodejs .setup ({ " mise" , " x" , " node@lts" , " --" , " node" })
163
- local cmd = nodejs .get_execute_command ()
164
- eq (cmd , { " mise" , " x" , " node@lts" , " --" , " node" , nodejs .server_path , " --stdio" })
173
+ T [" get_execute_command()" ][" custom node command as table, custom server path" ] = function ()
174
+ stub .get_runtime_server_path (function ()
175
+ local nodejs = require (" copilot.lsp.nodejs" )
176
+ nodejs .setup ({ " mise" , " x" , " node@lts" , " --" , " node" }, stub .custom_server_path )
177
+ local cmd = nodejs .get_execute_command ()
178
+ eq (cmd , { " mise" , " x" , " node@lts" , " --" , " node" , stub .custom_server_path , " --stdio" })
179
+ end )
165
180
end
166
181
167
182
return T
0 commit comments