From 00a528818463b10d84699b2e0f4a960d5a4aeb5c Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Tue, 12 Sep 2023 20:10:05 -0700 Subject: fix: modify diff calculation to handle end-of-file newlines better (#35) --- tests/fuzzer_spec.lua | 13 +++++++------ tests/runner_spec.lua | 15 ++++++--------- tests/test_util.lua | 5 +++++ 3 files changed, 18 insertions(+), 15 deletions(-) (limited to 'tests') diff --git a/tests/fuzzer_spec.lua b/tests/fuzzer_spec.lua index 81c032f..639cfa9 100644 --- a/tests/fuzzer_spec.lua +++ b/tests/fuzzer_spec.lua @@ -1,6 +1,7 @@ require("plenary.async").tests.add_to_env() local test_util = require("tests.test_util") local conform = require("conform") +local log = require("conform.log") local runner = require("conform.runner") describe("fuzzer", function() @@ -25,12 +26,6 @@ describe("fuzzer", function() vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, buf_content) vim.bo[bufnr].modified = false runner.apply_format(0, buf_content, expected, nil, false) - -- We expect the last newline to be effectively "swallowed" by the formatter - -- because vim will use that as the EOL at the end of the file. The exception is that we always - -- expect at least one line in the output - if #expected > 1 and expected[#expected] == "" then - table.remove(expected) - end assert.are.same(expected, vim.api.nvim_buf_get_lines(0, 0, -1, false)) end @@ -95,6 +90,10 @@ describe("fuzzer", function() for _ = 1, num_lines do table.remove(lines, idx) end + -- vim will never let the lines be empty. An empty file has a single blank line. + if #lines == 0 then + table.insert(lines, "") + end end local function make_edits(lines) @@ -112,8 +111,10 @@ describe("fuzzer", function() end it("formats correctly", function() + -- log.level = vim.log.levels.TRACE for i = 1, 50000 do math.randomseed(i) + log.info("Fuzz testing with seed %d", i) local content = make_file(20) local formatted = make_edits(content) run_formatter(content, formatted) diff --git a/tests/runner_spec.lua b/tests/runner_spec.lua index 8807d2d..10ea075 100644 --- a/tests/runner_spec.lua +++ b/tests/runner_spec.lua @@ -125,12 +125,6 @@ describe("runner", function() local expected_lines = vim.split(expected, "\n", { plain = true }) test_util.set_formatter_output(expected_lines) conform.format(vim.tbl_extend("force", opts or {}, { formatters = { "test" }, quiet = true })) - -- We expect the last newline to be effectively "swallowed" by the formatter - -- because vim will use that as the EOL at the end of the file. The exception is that we always - -- expect at least one line in the output - if #expected_lines > 1 and expected_lines[#expected_lines] == "" then - table.remove(expected_lines) - end return expected_lines end @@ -193,11 +187,14 @@ print("a") run_formatter_test("\nfoo", "\nhello\nfoo") run_formatter_test("hello", "hello\n") run_formatter_test("hello", "hello\n\n") - run_formatter_test("hello", "hello\n") - -- This should generate no changes to the buffer - assert.falsy(vim.bo.modified) run_formatter_test("hello\n", "hello") run_formatter_test("hello\n ", "hello") + + -- These should generate no changes to the buffer + run_formatter_test("hello\n", "hello\n") + assert.falsy(vim.bo.modified) + run_formatter_test("hello", "hello") + assert.falsy(vim.bo.modified) end) it("does not change output if formatter fails", function() diff --git a/tests/test_util.lua b/tests/test_util.lua index a225aec..cded23a 100644 --- a/tests/test_util.lua +++ b/tests/test_util.lua @@ -1,5 +1,6 @@ require("plenary.async").tests.add_to_env() local conform = require("conform") +local log = require("conform.log") local M = {} local OUTPUT_FILE = "tests/fake_formatter_output" @@ -21,6 +22,8 @@ M.reset_editor = function() if vim.fn.filereadable(OUTPUT_FILE) == 1 then vim.fn.delete(OUTPUT_FILE) end + log.level = vim.log.levels.ERROR + log.set_handler(print) end ---@param lines string[] @@ -28,6 +31,8 @@ M.set_formatter_output = function(lines) local content = table.concat(lines, "\n") local fd = assert(vim.loop.fs_open(OUTPUT_FILE, "w", 420)) -- 0644 vim.loop.fs_write(fd, content) + -- Make sure we add the final newline + vim.loop.fs_write(fd, "\n") vim.loop.fs_close(fd) end -- cgit v1.2.3-70-g09d2