|
1 | 1 | local utils = require("pytrize.utils") |
2 | 2 |
|
3 | | -describe("min", function() |
4 | | - it("returns the smaller value", function() |
5 | | - assert.are.equal(1, utils.min(1, 2)) |
6 | | - assert.are.equal(1, utils.min(2, 1)) |
7 | | - end) |
8 | | - |
9 | | - it("returns the value when equal", function() |
10 | | - assert.are.equal(5, utils.min(5, 5)) |
11 | | - end) |
12 | | - |
13 | | - it("works with negative numbers", function() |
14 | | - assert.are.equal(-3, utils.min(-3, -1)) |
15 | | - end) |
16 | | -end) |
| 3 | +describe("with_buf", function() |
| 4 | + it("loads a file into a buffer, calls fn, and cleans up", function() |
| 5 | + local tmp = vim.fn.tempname() .. ".py" |
| 6 | + local f = io.open(tmp, "w") |
| 7 | + f:write("x = 1\n") |
| 8 | + f:close() |
| 9 | + |
| 10 | + local result = utils.with_buf(tmp, function(bufnr) |
| 11 | + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) |
| 12 | + return lines[1] |
| 13 | + end, { force_delete = true }) |
| 14 | + |
| 15 | + assert.are.equal("x = 1", result) |
| 16 | + |
| 17 | + -- buffer should have been cleaned up |
| 18 | + assert.are.equal(-1, vim.fn.bufnr(tmp)) |
| 19 | + vim.fn.delete(tmp) |
| 20 | + end) |
| 21 | + |
| 22 | + it("suppresses file-info messages by loading via vim.fn.execute", function() |
| 23 | + local tmp = vim.fn.tempname() .. ".py" |
| 24 | + local f = io.open(tmp, "w") |
| 25 | + f:write("x = 1\n") |
| 26 | + f:close() |
| 27 | + |
| 28 | + -- The C-level file-info message ("path" NL, NB) that bufload() |
| 29 | + -- emits does not appear in :messages or headless output, so we |
| 30 | + -- cannot assert on captured text. Instead we verify the |
| 31 | + -- suppression mechanism: with_buf must route through |
| 32 | + -- vim.fn.execute() which uses :redir to swallow all output. |
| 33 | + local original_execute = vim.fn.execute |
| 34 | + local execute_calls = {} |
| 35 | + vim.fn.execute = function(cmd) |
| 36 | + table.insert(execute_calls, cmd) |
| 37 | + return original_execute(cmd) |
| 38 | + end |
| 39 | + |
| 40 | + utils.with_buf(tmp, function(_) end, { force_delete = true }) |
| 41 | + |
| 42 | + vim.fn.execute = original_execute |
| 43 | + |
| 44 | + -- At least one call should be the bufload wrapper |
| 45 | + local found = false |
| 46 | + for _, cmd in ipairs(execute_calls) do |
| 47 | + if type(cmd) == "string" and cmd:find("bufload") then |
| 48 | + found = true |
| 49 | + break |
| 50 | + end |
| 51 | + end |
| 52 | + assert.is_true(found, "with_buf should call vim.fn.execute() with bufload to suppress file-info messages") |
| 53 | + |
| 54 | + vim.fn.delete(tmp) |
| 55 | + end) |
17 | 56 |
|
18 | | -describe("max", function() |
19 | | - it("returns the larger value", function() |
20 | | - assert.are.equal(2, utils.max(1, 2)) |
21 | | - assert.are.equal(2, utils.max(2, 1)) |
22 | | - end) |
| 57 | + it("propagates errors from fn", function() |
| 58 | + local tmp = vim.fn.tempname() .. ".py" |
| 59 | + local f = io.open(tmp, "w") |
| 60 | + f:write("x = 1\n") |
| 61 | + f:close() |
23 | 62 |
|
24 | | - it("returns the value when equal", function() |
25 | | - assert.are.equal(5, utils.max(5, 5)) |
26 | | - end) |
| 63 | + assert.has_error(function() |
| 64 | + utils.with_buf(tmp, function(_) |
| 65 | + error("test error") |
| 66 | + end, { force_delete = true }) |
| 67 | + end) |
27 | 68 |
|
28 | | - it("works with negative numbers", function() |
29 | | - assert.are.equal(-1, utils.max(-3, -1)) |
30 | | - end) |
| 69 | + vim.fn.delete(tmp) |
| 70 | + end) |
31 | 71 | end) |
0 commit comments