aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorSteven Arcangeli <506791+stevearc@users.noreply.github.com>2023-12-26 06:38:00 -0800
committerGitHub <noreply@github.com>2023-12-26 06:38:00 -0800
commitf245cca8ad42c9d344b53a18c3fc1a3c6724c2d4 (patch)
tree71cf24c9888024ce02706e3d1e544187729dcf09 /tests
parent7396fc0208539e2bd70e3e446f27529e28dba12b (diff)
fix(injected): handle inline injections (#251)
Diffstat (limited to 'tests')
-rwxr-xr-xtests/fake_formatter.sh26
-rw-r--r--tests/injected/block_quote.md5
-rw-r--r--tests/injected/block_quote.md.formatted5
-rw-r--r--tests/injected/combined_injections.md14
-rw-r--r--tests/injected/combined_injections.md.formatted14
-rw-r--r--tests/injected/inline.ts5
-rw-r--r--tests/injected/inline.ts.formatted5
-rw-r--r--tests/injected/simple.md5
-rw-r--r--tests/injected/simple.md.formatted5
-rw-r--r--tests/injected_spec.lua91
-rw-r--r--tests/minimal_init.lua16
-rw-r--r--tests/runner_spec.lua49
-rw-r--r--tests/test_util.lua15
13 files changed, 224 insertions, 31 deletions
diff --git a/tests/fake_formatter.sh b/tests/fake_formatter.sh
index a060a4c..75f92ff 100755
--- a/tests/fake_formatter.sh
+++ b/tests/fake_formatter.sh
@@ -2,16 +2,24 @@
set -e
-if [ -e "tests/fake_formatter_output" ]; then
- cat tests/fake_formatter_output
-else
- cat
+CODE=0
+if [ "$1" = "--fail" ]; then
+ shift
+ echo "failure" >&2
+ CODE=1
+fi
+if [ "$1" = "--timeout" ]; then
+ shift
+ echo "timeout" >&2
+ sleep 4
fi
-if [ "$1" = "--fail" ]; then
- echo "failure" >&2
- exit 1
-elif [ "$1" = "--timeout" ]; then
- sleep 4
+output_file="$1"
+
+if [ -n "$output_file" ] && [ -e "$output_file" ]; then
+ cat "$output_file"
+else
+ cat
fi
+exit $CODE
diff --git a/tests/injected/block_quote.md b/tests/injected/block_quote.md
new file mode 100644
index 0000000..cd56ae8
--- /dev/null
+++ b/tests/injected/block_quote.md
@@ -0,0 +1,5 @@
+text
+
+> ```lua
+> local foo = 'bar'
+> ```
diff --git a/tests/injected/block_quote.md.formatted b/tests/injected/block_quote.md.formatted
new file mode 100644
index 0000000..f772801
--- /dev/null
+++ b/tests/injected/block_quote.md.formatted
@@ -0,0 +1,5 @@
+text
+
+> ```lua
+> |local foo = 'bar'|
+> ```
diff --git a/tests/injected/combined_injections.md b/tests/injected/combined_injections.md
new file mode 100644
index 0000000..47aeeb4
--- /dev/null
+++ b/tests/injected/combined_injections.md
@@ -0,0 +1,14 @@
+text
+
+<!-- comment -->
+
+```lua
+local foo = 'bar'
+```
+
+
+<!-- comment -->
+
+```lua
+local foo = 'bar'
+```
diff --git a/tests/injected/combined_injections.md.formatted b/tests/injected/combined_injections.md.formatted
new file mode 100644
index 0000000..d7f46a4
--- /dev/null
+++ b/tests/injected/combined_injections.md.formatted
@@ -0,0 +1,14 @@
+text
+
+<!-- comment -->
+
+```lua
+|local foo = 'bar'|
+```
+
+
+<!-- comment -->
+
+```lua
+|local foo = 'bar'|
+```
diff --git a/tests/injected/inline.ts b/tests/injected/inline.ts
new file mode 100644
index 0000000..65e4a43
--- /dev/null
+++ b/tests/injected/inline.ts
@@ -0,0 +1,5 @@
+foo.innerHTML = `<div> hello </div>`;
+
+bar.innerHTML = `
+<div> world </div>
+`;
diff --git a/tests/injected/inline.ts.formatted b/tests/injected/inline.ts.formatted
new file mode 100644
index 0000000..c3d6a21
--- /dev/null
+++ b/tests/injected/inline.ts.formatted
@@ -0,0 +1,5 @@
+foo.innerHTML = `|<div> hello </div>|`;
+
+bar.innerHTML = `
+|<div> world </div>|
+`;
diff --git a/tests/injected/simple.md b/tests/injected/simple.md
new file mode 100644
index 0000000..5fd4b3a
--- /dev/null
+++ b/tests/injected/simple.md
@@ -0,0 +1,5 @@
+text
+
+```lua
+local foo = 'bar'
+```
diff --git a/tests/injected/simple.md.formatted b/tests/injected/simple.md.formatted
new file mode 100644
index 0000000..20800b4
--- /dev/null
+++ b/tests/injected/simple.md.formatted
@@ -0,0 +1,5 @@
+text
+
+```lua
+|local foo = 'bar'|
+```
diff --git a/tests/injected_spec.lua b/tests/injected_spec.lua
new file mode 100644
index 0000000..1f57e7f
--- /dev/null
+++ b/tests/injected_spec.lua
@@ -0,0 +1,91 @@
+require("plenary.async").tests.add_to_env()
+local conform = require("conform")
+local fs = require("conform.fs")
+local injected = require("conform.formatters.injected")
+local runner = require("conform.runner")
+local test_util = require("tests.test_util")
+
+-- injected formatter only supported on neovim 0.9+
+if vim.fn.has("nvim-0.9") == 0 then
+ return
+end
+
+---@param dir string
+---@return string[]
+local function list_test_files(dir)
+ ---@diagnostic disable-next-line: param-type-mismatch
+ local fd = vim.loop.fs_opendir(dir, nil, 32)
+ ---@diagnostic disable-next-line: param-type-mismatch
+ local entries = vim.loop.fs_readdir(fd)
+ local ret = {}
+ while entries do
+ for _, entry in ipairs(entries) do
+ if entry.type == "file" and not vim.endswith(entry.name, ".formatted") then
+ table.insert(ret, entry.name)
+ end
+ end
+ ---@diagnostic disable-next-line: param-type-mismatch
+ entries = vim.loop.fs_readdir(fd)
+ end
+ ---@diagnostic disable-next-line: param-type-mismatch
+ vim.loop.fs_closedir(fd)
+ return ret
+end
+
+describe("injected formatter", function()
+ before_each(function()
+ -- require("conform.log").level = vim.log.levels.TRACE
+ conform.formatters_by_ft = {
+ lua = { "test_mark" },
+ html = { "test_mark" },
+ }
+ -- A test formatter that bookends lines with "|" so we can check what was passed in
+ conform.formatters.test_mark = {
+ format = function(self, ctx, lines, callback)
+ local ret = {}
+ for i, line in ipairs(lines) do
+ if i == 1 and line == "" then
+ -- Simulate formatters removing starting newline
+ elseif i == #lines and line == "" then
+ -- Simulate formatters removing trailing newline
+ else
+ table.insert(ret, "|" .. line:gsub("%s+", " ") .. "|")
+ end
+ end
+ callback(nil, ret)
+ end,
+ }
+ end)
+
+ after_each(function()
+ test_util.reset_editor()
+ end)
+
+ for _, filename in ipairs(list_test_files("tests/injected")) do
+ local filepath = "./tests/injected/" .. filename
+ local formatted_file = filepath .. ".formatted"
+ it(filename, function()
+ local content = fs.read_file(filepath)
+ assert(content)
+ local lines = vim.split(content, "\n", { plain = true })
+ local bufnr = vim.fn.bufadd(filepath)
+ vim.fn.bufload(bufnr)
+ local config = assert(conform.get_formatter_config("injected", bufnr))
+ local ctx = runner.build_context(bufnr, config)
+ local err, new_lines, done
+ injected.format(injected, ctx, lines, function(e, formatted)
+ done = true
+ err = e
+ new_lines = formatted
+ end)
+ vim.wait(1000, function()
+ return done
+ end)
+ assert(err == nil, err)
+ local expected = fs.read_file(formatted_file)
+ assert(expected)
+ local expected_lines = vim.split(expected, "\n", { plain = true })
+ assert.are.same(expected_lines, new_lines)
+ end)
+ end
+end)
diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua
index 262d9ec..0afbd90 100644
--- a/tests/minimal_init.lua
+++ b/tests/minimal_init.lua
@@ -3,3 +3,19 @@ vim.cmd([[set runtimepath+=.]])
vim.o.swapfile = false
vim.bo.swapfile = false
require("tests.test_util").reset_editor()
+
+local configs = require("nvim-treesitter.configs")
+configs.setup({
+ ensure_installed = { "markdown", "markdown_inline", "lua", "typescript", "html" },
+ sync_install = true,
+})
+-- this needs to be run a second time to make tests behave
+require("nvim-treesitter").setup()
+
+vim.api.nvim_create_user_command("RunTests", function(opts)
+ local path = opts.fargs[1] or "tests"
+ require("plenary.test_harness").test_directory(
+ path,
+ { minimal_init = "./tests/minimal_init.lua" }
+ )
+end, { nargs = "?" })
diff --git a/tests/runner_spec.lua b/tests/runner_spec.lua
index c9cf75a..5ab7893 100644
--- a/tests/runner_spec.lua
+++ b/tests/runner_spec.lua
@@ -2,10 +2,35 @@ require("plenary.async").tests.add_to_env()
local conform = require("conform")
local runner = require("conform.runner")
local test_util = require("tests.test_util")
+local util = require("conform.util")
describe("runner", function()
+ local OUTPUT_FILE
+ local CLEANUP_FILES = {}
+
+ ---@param lines string[]
+ local function set_formatter_output(lines)
+ local fd, output_file = vim.loop.fs_mkstemp(".testenv/outputXXXXXXXXX")
+ assert(type(fd) == "number" and output_file, fd)
+ local content = table.concat(lines, "\n")
+ vim.loop.fs_write(fd, content)
+ -- Make sure we add the final newline
+ vim.loop.fs_write(fd, "\n")
+ vim.loop.fs_fsync(fd)
+ vim.loop.fs_close(fd)
+ OUTPUT_FILE = output_file
+ table.insert(CLEANUP_FILES, output_file)
+ end
+
after_each(function()
test_util.reset_editor()
+ OUTPUT_FILE = nil
+ for _, file in ipairs(CLEANUP_FILES) do
+ if vim.loop.fs_stat(file) then
+ vim.loop.fs_unlink(file)
+ end
+ end
+ CLEANUP_FILES = {}
end)
it("resolves config function", function()
@@ -151,6 +176,12 @@ describe("runner", function()
before_each(function()
conform.formatters.test = {
command = "tests/fake_formatter.sh",
+ args = function()
+ if OUTPUT_FILE then
+ return { OUTPUT_FILE }
+ end
+ return {}
+ end,
}
end)
@@ -165,7 +196,7 @@ describe("runner", function()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines)
vim.bo[bufnr].modified = false
local expected_lines = vim.split(expected, "\n", { plain = true })
- test_util.set_formatter_output(expected_lines)
+ set_formatter_output(expected_lines)
conform.format(vim.tbl_extend("keep", opts or {}, { formatters = { "test" }, quiet = true }))
return expected_lines
end
@@ -240,19 +271,19 @@ print("a")
end)
it("does not change output if formatter fails", function()
- conform.formatters.test.args = { "--fail" }
+ conform.formatters.test.args = util.extend_args(conform.formatters.test.args, { "--fail" })
run_formatter("hello", "goodbye")
assert.are.same({ "hello" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)
it("allows nonzero exit codes", function()
- conform.formatters.test.args = { "--fail" }
+ conform.formatters.test.args = util.extend_args(conform.formatters.test.args, { "--fail" })
conform.formatters.test.exit_codes = { 0, 1 }
run_formatter_test("hello", "goodbye")
end)
it("does not format if it times out", function()
- conform.formatters.test.args = { "--timeout" }
+ conform.formatters.test.args = util.extend_args(conform.formatters.test.args, { "--timeout" })
run_formatter("hello", "goodbye", { timeout_ms = 10 })
assert.are.same({ "hello" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)
@@ -260,7 +291,9 @@ print("a")
it("can format async", function()
run_formatter("hello", "goodbye", { async = true })
assert.are.same({ "hello" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
- vim.wait(100)
+ vim.wait(1000, function()
+ return vim.api.nvim_buf_get_lines(0, 0, -1, false)[1] == "goodbye"
+ end)
assert.are.same({ "goodbye" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)
@@ -268,7 +301,9 @@ print("a")
run_formatter("hello", "goodbye", { async = true })
assert.are.same({ "hello" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
vim.api.nvim_buf_set_lines(0, 0, -1, true, { "newcontent" })
- vim.wait(100)
+ vim.wait(1000, function()
+ return vim.api.nvim_buf_get_lines(0, 0, -1, false)[1] == "newcontent"
+ end)
assert.are.same({ "newcontent" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)
@@ -279,7 +314,7 @@ print("a")
})
vim.cmd.edit({ args = { "tests/testfile.txt" } })
vim.api.nvim_buf_set_lines(0, 0, -1, true, { "hello" })
- test_util.set_formatter_output({ "goodbye" })
+ set_formatter_output({ "goodbye" })
vim.cmd.write()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
vim.fn.delete("tests/testfile.txt")
diff --git a/tests/test_util.lua b/tests/test_util.lua
index cded23a..7e13238 100644
--- a/tests/test_util.lua
+++ b/tests/test_util.lua
@@ -3,8 +3,6 @@ local conform = require("conform")
local log = require("conform.log")
local M = {}
-local OUTPUT_FILE = "tests/fake_formatter_output"
-
M.reset_editor = function()
vim.cmd.tabonly({ mods = { silent = true } })
for i, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
@@ -19,21 +17,8 @@ M.reset_editor = function()
conform.formatters = {}
conform.formatters_by_ft = {}
pcall(vim.api.nvim_del_augroup_by_name, "Conform")
- 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[]
-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
-
return M