|
| 1 | +---@diagnostic disable: duplicate-set-field |
1 | 2 | local helpers = require('helpers') |
2 | 3 |
|
3 | 4 | return function() |
@@ -213,5 +214,120 @@ return function() |
213 | 214 |
|
214 | 215 | helpers.cleanup_test_env() |
215 | 216 | end) |
| 217 | + |
| 218 | + helpers.test("semver-like version emits warning when vim.pack.add fails", function() |
| 219 | + helpers.setup_test_env() |
| 220 | + |
| 221 | + local mock_vim_pack_add = vim.pack.add |
| 222 | + vim.pack.add = function() |
| 223 | + error("some error from vim.pack.add") |
| 224 | + end |
| 225 | + |
| 226 | + local ok = pcall(function() |
| 227 | + require('zpack').setup({ |
| 228 | + spec = { |
| 229 | + { |
| 230 | + 'test/plugin', |
| 231 | + version = '1.*', |
| 232 | + }, |
| 233 | + }, |
| 234 | + defaults = { confirm = false }, |
| 235 | + }) |
| 236 | + end) |
| 237 | + |
| 238 | + helpers.assert_equal(ok, false, "setup should fail when vim.pack.add throws") |
| 239 | + helpers.assert_equal(#_G.test_state.notifications >= 1, true, "warning should be emitted") |
| 240 | + |
| 241 | + local found_warning = false |
| 242 | + for _, notif in ipairs(_G.test_state.notifications) do |
| 243 | + if notif.msg:match('`vim%.pack%.add` failed') and notif.msg:match('sem_version') and notif.msg:match('1%.%*') then |
| 244 | + found_warning = true |
| 245 | + break |
| 246 | + end |
| 247 | + end |
| 248 | + helpers.assert_equal(found_warning, true, "warning should mention vim.pack.add failed and sem_version") |
| 249 | + |
| 250 | + vim.pack.add = mock_vim_pack_add |
| 251 | + helpers.cleanup_test_env() |
| 252 | + end) |
| 253 | + |
| 254 | + helpers.test("multiple semver-like versions emit multiple warnings", function() |
| 255 | + helpers.setup_test_env() |
| 256 | + |
| 257 | + local mock_vim_pack_add = vim.pack.add |
| 258 | + vim.pack.add = function() |
| 259 | + error("some error from vim.pack.add") |
| 260 | + end |
| 261 | + |
| 262 | + local ok = pcall(function() |
| 263 | + require('zpack').setup({ |
| 264 | + spec = { |
| 265 | + { 'test/plugin-a', version = '1.*' }, |
| 266 | + { 'test/plugin-b', version = '^2.0.0' }, |
| 267 | + { 'test/plugin-c', version = 'main' }, |
| 268 | + }, |
| 269 | + defaults = { confirm = false }, |
| 270 | + }) |
| 271 | + end) |
| 272 | + |
| 273 | + helpers.assert_equal(ok, false, "setup should fail when vim.pack.add throws") |
| 274 | + |
| 275 | + local warning_count = 0 |
| 276 | + local found_plugin_a = false |
| 277 | + local found_plugin_b = false |
| 278 | + for _, notif in ipairs(_G.test_state.notifications) do |
| 279 | + if notif.msg:match('sem_version') then |
| 280 | + warning_count = warning_count + 1 |
| 281 | + if notif.msg:match('1%.%*') and notif.msg:match('plugin%-a') then |
| 282 | + found_plugin_a = true |
| 283 | + end |
| 284 | + if notif.msg:match('%^2%.0%.0') and notif.msg:match('plugin%-b') then |
| 285 | + found_plugin_b = true |
| 286 | + end |
| 287 | + end |
| 288 | + end |
| 289 | + |
| 290 | + helpers.assert_equal(warning_count, 2, "should emit exactly 2 warnings for semver-like versions") |
| 291 | + helpers.assert_equal(found_plugin_a, true, "should warn about plugin-a with 1.*") |
| 292 | + helpers.assert_equal(found_plugin_b, true, "should warn about plugin-b with ^2.0.0") |
| 293 | + |
| 294 | + vim.pack.add = mock_vim_pack_add |
| 295 | + helpers.cleanup_test_env() |
| 296 | + end) |
| 297 | + |
| 298 | + helpers.test("no warning when vim.pack.add fails but no semver-like versions", function() |
| 299 | + helpers.setup_test_env() |
| 300 | + |
| 301 | + local mock_vim_pack_add = vim.pack.add |
| 302 | + vim.pack.add = function() |
| 303 | + error("some error from vim.pack.add") |
| 304 | + end |
| 305 | + |
| 306 | + local ok = pcall(function() |
| 307 | + require('zpack').setup({ |
| 308 | + spec = { |
| 309 | + { |
| 310 | + 'test/plugin', |
| 311 | + version = 'nonexistent-branch', |
| 312 | + }, |
| 313 | + }, |
| 314 | + defaults = { confirm = false }, |
| 315 | + }) |
| 316 | + end) |
| 317 | + |
| 318 | + helpers.assert_equal(ok, false, "setup should fail when vim.pack.add throws") |
| 319 | + |
| 320 | + local found_warning = false |
| 321 | + for _, notif in ipairs(_G.test_state.notifications) do |
| 322 | + if notif.msg:match('sem_version') then |
| 323 | + found_warning = true |
| 324 | + break |
| 325 | + end |
| 326 | + end |
| 327 | + helpers.assert_equal(found_warning, false, "no warning should be emitted for non-semver-like versions") |
| 328 | + |
| 329 | + vim.pack.add = mock_vim_pack_add |
| 330 | + helpers.cleanup_test_env() |
| 331 | + end) |
216 | 332 | end) |
217 | 333 | end |
0 commit comments