Regenerate nvim config
This commit is contained in:
@ -0,0 +1,98 @@
|
||||
require("plenary.async").tests.add_to_env()
|
||||
local conform = require("conform")
|
||||
local test_util = require("tests.test_util")
|
||||
|
||||
describe("api", function()
|
||||
after_each(function()
|
||||
test_util.reset_editor()
|
||||
end)
|
||||
|
||||
it("retrieves info about a formatter", function()
|
||||
local info = conform.get_formatter_info("stylua")
|
||||
assert.equal("stylua", info.name)
|
||||
assert.equal("stylua", info.command)
|
||||
assert.equal("boolean", type(info.available))
|
||||
end)
|
||||
|
||||
it("retrieves unavailable info if formatter does not exist", function()
|
||||
local info = conform.get_formatter_info("asdf")
|
||||
assert.equal("asdf", info.name)
|
||||
assert.equal("asdf", info.command)
|
||||
assert.falsy(info.available)
|
||||
end)
|
||||
|
||||
describe("list_formatters", function()
|
||||
local get_formatter_info = conform.get_formatter_info
|
||||
before_each(function()
|
||||
conform.get_formatter_info = function(...)
|
||||
local info = get_formatter_info(...)
|
||||
info.available = true
|
||||
return info
|
||||
end
|
||||
end)
|
||||
after_each(function()
|
||||
conform.get_formatter_info = get_formatter_info
|
||||
end)
|
||||
|
||||
it("lists all formatters configured for buffer", function()
|
||||
conform.formatters_by_ft.lua = { "stylua", "lua-format" }
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_set_current_buf(bufnr)
|
||||
vim.bo[bufnr].filetype = "lua"
|
||||
local formatters = conform.list_formatters()
|
||||
local formatter_names = vim.tbl_map(function(f)
|
||||
return f.name
|
||||
end, formatters)
|
||||
assert.are.same({ "stylua", "lua-format" }, formatter_names)
|
||||
end)
|
||||
|
||||
it("merges formatters from mixed filetypes", function()
|
||||
conform.formatters_by_ft.lua = { "stylua", "lua-format" }
|
||||
conform.formatters_by_ft["*"] = { "trim_whitespace" }
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_set_current_buf(bufnr)
|
||||
vim.bo[bufnr].filetype = "lua"
|
||||
local formatters = conform.list_formatters()
|
||||
local formatter_names = vim.tbl_map(function(f)
|
||||
return f.name
|
||||
end, formatters)
|
||||
assert.are.same({ "stylua", "lua-format", "trim_whitespace" }, formatter_names)
|
||||
end)
|
||||
|
||||
it("flattens formatters in alternation groups", function()
|
||||
conform.formatters_by_ft.lua = { { "stylua", "lua-format" }, "trim_whitespace" }
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_set_current_buf(bufnr)
|
||||
vim.bo[bufnr].filetype = "lua"
|
||||
local formatters = conform.list_formatters()
|
||||
local formatter_names = vim.tbl_map(function(f)
|
||||
return f.name
|
||||
end, formatters)
|
||||
assert.are.same({ "stylua", "trim_whitespace" }, formatter_names)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("list_all_formatters", function()
|
||||
it("lists all formatters configured for all buffers", function()
|
||||
conform.formatters_by_ft.lua = { "stylua", "lua-format" }
|
||||
conform.formatters_by_ft["*"] = { "trim_whitespace" }
|
||||
local formatters = conform.list_all_formatters()
|
||||
local formatter_names = vim.tbl_map(function(f)
|
||||
return f.name
|
||||
end, formatters)
|
||||
table.sort(formatter_names)
|
||||
assert.are.same({ "lua-format", "stylua", "trim_whitespace" }, formatter_names)
|
||||
end)
|
||||
|
||||
it("flattens formatters in alternation groups", function()
|
||||
conform.formatters_by_ft.lua = { { "stylua", "lua-format" } }
|
||||
conform.formatters_by_ft["*"] = { "trim_whitespace" }
|
||||
local formatters = conform.list_all_formatters()
|
||||
local formatter_names = vim.tbl_map(function(f)
|
||||
return f.name
|
||||
end, formatters)
|
||||
table.sort(formatter_names)
|
||||
assert.are.same({ "lua-format", "stylua", "trim_whitespace" }, formatter_names)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
25
config/neovim/store/lazy-plugins/conform.nvim/tests/fake_formatter.sh
Executable file
25
config/neovim/store/lazy-plugins/conform.nvim/tests/fake_formatter.sh
Executable file
@ -0,0 +1,25 @@
|
||||
#!/nix/store/306znyj77fv49kwnkpxmb0j2znqpa8bj-bash-5.2p26/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
CODE=0
|
||||
if [ "$1" = "--fail" ]; then
|
||||
shift
|
||||
echo "failure" >&2
|
||||
CODE=1
|
||||
fi
|
||||
if [ "$1" = "--timeout" ]; then
|
||||
shift
|
||||
echo "timeout" >&2
|
||||
sleep 4
|
||||
fi
|
||||
|
||||
output_file="$1"
|
||||
|
||||
if [ -n "$output_file" ] && [ -e "$output_file" ]; then
|
||||
cat "$output_file"
|
||||
else
|
||||
cat
|
||||
fi
|
||||
|
||||
exit $CODE
|
||||
@ -0,0 +1,22 @@
|
||||
local fs = require("conform.fs")
|
||||
|
||||
describe("fs", function()
|
||||
local relative_paths = {
|
||||
{ "/home", "/home/file.txt", "file.txt" },
|
||||
{ "/home/", "/home/file.txt", "file.txt" },
|
||||
{ "/home", "/foo/file.txt", "../foo/file.txt" },
|
||||
{ "/home/foo", "/home/bar/file.txt", "../bar/file.txt" },
|
||||
{ "/home", "/file.txt", "../file.txt" },
|
||||
{ "/home", "/home/foo/file.txt", "foo/file.txt" },
|
||||
{ ".", "foo/file.txt", "foo/file.txt" },
|
||||
{ "home", "home/file.txt", "file.txt" },
|
||||
{ "home", "file.txt", "../file.txt" },
|
||||
}
|
||||
|
||||
it("relative_path", function()
|
||||
for _, paths in ipairs(relative_paths) do
|
||||
local source, target, expected = unpack(paths)
|
||||
assert.are.same(fs.relative_path(source, target), expected)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
@ -0,0 +1,130 @@
|
||||
require("plenary.async").tests.add_to_env()
|
||||
local conform = require("conform")
|
||||
local log = require("conform.log")
|
||||
local runner = require("conform.runner")
|
||||
local test_util = require("tests.test_util")
|
||||
|
||||
describe("fuzzer", function()
|
||||
before_each(function()
|
||||
conform.formatters.test = {
|
||||
meta = { url = "", description = "" },
|
||||
command = "tests/fake_formatter.sh",
|
||||
}
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
test_util.reset_editor()
|
||||
end)
|
||||
|
||||
---@param buf_content string[]
|
||||
---@param expected string[]
|
||||
---@param opts? table
|
||||
local function run_formatter(buf_content, expected, opts)
|
||||
local bufnr = vim.fn.bufadd("testfile")
|
||||
vim.fn.bufload(bufnr)
|
||||
vim.api.nvim_set_current_buf(bufnr)
|
||||
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)
|
||||
assert.are.same(expected, vim.api.nvim_buf_get_lines(0, 0, -1, false))
|
||||
end
|
||||
|
||||
local function make_word()
|
||||
local chars = {}
|
||||
for _ = 1, math.random(1, 10) do
|
||||
table.insert(chars, string.char(math.random(97, 122)))
|
||||
end
|
||||
return table.concat(chars, "")
|
||||
end
|
||||
|
||||
local function make_line()
|
||||
local words = {}
|
||||
for _ = 1, math.random(0, 6) do
|
||||
table.insert(words, make_word())
|
||||
end
|
||||
return table.concat(words, " ")
|
||||
end
|
||||
|
||||
local function make_file(num_lines)
|
||||
local lines = {}
|
||||
for _ = 1, math.random(1, num_lines) do
|
||||
table.insert(lines, make_line())
|
||||
end
|
||||
return lines
|
||||
end
|
||||
|
||||
local function do_insert(lines)
|
||||
local idx = math.random(1, #lines + 1)
|
||||
for _ = 1, math.random(1, 3) do
|
||||
table.insert(lines, idx, make_line())
|
||||
end
|
||||
end
|
||||
|
||||
local function do_replace(lines)
|
||||
local num_lines = math.random(1, math.min(3, #lines))
|
||||
local idx = math.random(1, #lines - num_lines + 1)
|
||||
local replacement = {}
|
||||
local num_replace = math.random(1, 5)
|
||||
for _ = 1, num_replace do
|
||||
table.insert(replacement, make_line())
|
||||
end
|
||||
local col = math.random(1, lines[idx]:len())
|
||||
replacement[1] = lines[idx]:sub(1, col) .. replacement[1]
|
||||
col = math.random(1, lines[idx + num_lines - 1]:len())
|
||||
replacement[#replacement] = replacement[#replacement] .. lines[idx + num_lines - 1]:sub(col)
|
||||
|
||||
for _ = 1, num_lines - num_replace do
|
||||
table.remove(lines, idx)
|
||||
end
|
||||
for _ = 1, num_replace - num_lines do
|
||||
table.insert(lines, idx, "")
|
||||
end
|
||||
for i = 1, num_replace do
|
||||
lines[idx + i - 1] = replacement[i]
|
||||
end
|
||||
end
|
||||
|
||||
local function do_delete(lines)
|
||||
local num_lines = math.random(1, 3)
|
||||
local idx = math.random(1, #lines - num_lines)
|
||||
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)
|
||||
local was_empty = table.concat(lines):match("^%s*$")
|
||||
lines = vim.deepcopy(lines)
|
||||
for _ = 1, math.random(0, 3) do
|
||||
do_insert(lines)
|
||||
end
|
||||
for _ = 1, math.random(0, 3) do
|
||||
do_replace(lines)
|
||||
end
|
||||
for _ = 1, math.random(0, 3) do
|
||||
do_delete(lines)
|
||||
end
|
||||
-- avoid blank output (whitepsace only) which is ignored when applying formatting
|
||||
if not was_empty then
|
||||
while table.concat(lines):match("^%s*$") do
|
||||
do_replace(lines)
|
||||
end
|
||||
end
|
||||
return lines
|
||||
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)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
@ -0,0 +1,5 @@
|
||||
text
|
||||
|
||||
> ```lua
|
||||
> local foo = 'bar'
|
||||
> ```
|
||||
@ -0,0 +1,5 @@
|
||||
text
|
||||
|
||||
> ```lua
|
||||
> |local foo = 'bar'|
|
||||
> ```
|
||||
@ -0,0 +1,14 @@
|
||||
text
|
||||
|
||||
<!-- comment -->
|
||||
|
||||
```lua
|
||||
local foo = 'bar'
|
||||
```
|
||||
|
||||
|
||||
<!-- comment -->
|
||||
|
||||
```lua
|
||||
local foo = 'bar'
|
||||
```
|
||||
@ -0,0 +1,14 @@
|
||||
text
|
||||
|
||||
|<!-- comment -->|
|
||||
|
||||
```lua
|
||||
|local foo = 'bar'|
|
||||
```
|
||||
|
||||
|
||||
|<!-- comment -->|
|
||||
|
||||
```lua
|
||||
|local foo = 'bar'|
|
||||
```
|
||||
@ -0,0 +1,9 @@
|
||||
foo.innerHTML = `<div> hello </div>`;
|
||||
|
||||
bar.innerHTML = `
|
||||
<div> world </div>
|
||||
`;
|
||||
|
||||
baz.innerHTML = `
|
||||
<div> world </div>
|
||||
`;
|
||||
@ -0,0 +1,9 @@
|
||||
foo.innerHTML = `|<div> hello </div>|`;
|
||||
|
||||
bar.innerHTML = `
|
||||
|<div> world </div>|
|
||||
`;
|
||||
|
||||
baz.innerHTML = `
|
||||
|<div> world </div>|
|
||||
`;
|
||||
@ -0,0 +1,5 @@
|
||||
text
|
||||
|
||||
```lua
|
||||
local foo = "bar"
|
||||
```
|
||||
@ -0,0 +1,5 @@
|
||||
text
|
||||
|
||||
```lua
|
||||
|local foo = "bar"|
|
||||
```
|
||||
@ -0,0 +1,88 @@
|
||||
require("plenary.async").tests.add_to_env()
|
||||
local conform = require("conform")
|
||||
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 .. "|")
|
||||
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 bufnr = vim.fn.bufadd(filepath)
|
||||
vim.fn.bufload(bufnr)
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
|
||||
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_bufnr = vim.fn.bufadd(formatted_file)
|
||||
vim.fn.bufload(expected_bufnr)
|
||||
local expected_lines = vim.api.nvim_buf_get_lines(expected_bufnr, 0, -1, true)
|
||||
assert.are.same(expected_lines, new_lines)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
@ -0,0 +1,21 @@
|
||||
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 = "?" })
|
||||
@ -0,0 +1,389 @@
|
||||
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()
|
||||
conform.formatters.test = function()
|
||||
return {
|
||||
meta = { url = "", description = "" },
|
||||
command = "echo",
|
||||
}
|
||||
end
|
||||
local config = assert(conform.get_formatter_config("test"))
|
||||
assert.are.same({
|
||||
meta = { url = "", description = "" },
|
||||
command = "echo",
|
||||
stdin = true,
|
||||
}, config)
|
||||
end)
|
||||
|
||||
describe("build_context", function()
|
||||
it("sets the filename and dirname", function()
|
||||
vim.cmd.edit({ args = { "README.md" } })
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
conform.formatters.test = {
|
||||
meta = { url = "", description = "" },
|
||||
command = "echo",
|
||||
}
|
||||
local config = assert(conform.get_formatter_config("test"))
|
||||
local ctx = runner.build_context(0, config)
|
||||
local filename = vim.api.nvim_buf_get_name(bufnr)
|
||||
assert.are.same({
|
||||
buf = bufnr,
|
||||
filename = filename,
|
||||
dirname = vim.fs.dirname(filename),
|
||||
}, ctx)
|
||||
end)
|
||||
|
||||
it("sets temp file when stdin = false", function()
|
||||
vim.cmd.edit({ args = { "README.md" } })
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
conform.formatters.test = {
|
||||
meta = { url = "", description = "" },
|
||||
command = "echo",
|
||||
stdin = false,
|
||||
}
|
||||
local config = assert(conform.get_formatter_config("test"))
|
||||
local ctx = runner.build_context(0, config)
|
||||
local bufname = vim.api.nvim_buf_get_name(bufnr)
|
||||
local dirname = vim.fs.dirname(bufname)
|
||||
assert.equal(bufnr, ctx.buf)
|
||||
assert.equal(dirname, ctx.dirname)
|
||||
assert.truthy(ctx.filename:match(dirname .. "/.conform.%d+.README.md$"))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("build_cmd", function()
|
||||
it("replaces $FILENAME in args", function()
|
||||
vim.cmd.edit({ args = { "README.md" } })
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
conform.formatters.test = {
|
||||
meta = { url = "", description = "" },
|
||||
command = "echo",
|
||||
args = { "$FILENAME" },
|
||||
}
|
||||
local config = assert(conform.get_formatter_config("test"))
|
||||
local ctx = runner.build_context(0, config)
|
||||
local cmd = runner.build_cmd("", ctx, config)
|
||||
assert.are.same({ "echo", vim.api.nvim_buf_get_name(bufnr) }, cmd)
|
||||
end)
|
||||
|
||||
it("replaces $DIRNAME in args", function()
|
||||
vim.cmd.edit({ args = { "README.md" } })
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
conform.formatters.test = {
|
||||
meta = { url = "", description = "" },
|
||||
command = "echo",
|
||||
args = { "$DIRNAME" },
|
||||
}
|
||||
local config = assert(conform.get_formatter_config("test"))
|
||||
local ctx = runner.build_context(0, config)
|
||||
local cmd = runner.build_cmd("", ctx, config)
|
||||
assert.are.same({ "echo", vim.fs.dirname(vim.api.nvim_buf_get_name(bufnr)) }, cmd)
|
||||
end)
|
||||
|
||||
it("resolves arg function", function()
|
||||
vim.cmd.edit({ args = { "README.md" } })
|
||||
conform.formatters.test = {
|
||||
meta = { url = "", description = "" },
|
||||
command = "echo",
|
||||
args = function()
|
||||
return { "--stdin" }
|
||||
end,
|
||||
}
|
||||
local config = assert(conform.get_formatter_config("test"))
|
||||
local ctx = runner.build_context(0, config)
|
||||
local cmd = runner.build_cmd("", ctx, config)
|
||||
assert.are.same({ "echo", "--stdin" }, cmd)
|
||||
end)
|
||||
|
||||
it("replaces $FILENAME in string args", function()
|
||||
vim.cmd.edit({ args = { "README.md" } })
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
conform.formatters.test = {
|
||||
meta = { url = "", description = "" },
|
||||
command = "echo",
|
||||
args = "$FILENAME | patch",
|
||||
}
|
||||
local config = assert(conform.get_formatter_config("test"))
|
||||
local ctx = runner.build_context(0, config)
|
||||
local cmd = runner.build_cmd("", ctx, config)
|
||||
assert.equal("echo " .. vim.api.nvim_buf_get_name(bufnr) .. " | patch", cmd)
|
||||
end)
|
||||
|
||||
it("replaces $DIRNAME in string args", function()
|
||||
vim.cmd.edit({ args = { "README.md" } })
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
conform.formatters.test = {
|
||||
meta = { url = "", description = "" },
|
||||
command = "echo",
|
||||
args = "$DIRNAME | patch",
|
||||
}
|
||||
local config = assert(conform.get_formatter_config("test"))
|
||||
local ctx = runner.build_context(0, config)
|
||||
local cmd = runner.build_cmd("", ctx, config)
|
||||
assert.equal("echo " .. vim.fs.dirname(vim.api.nvim_buf_get_name(bufnr)) .. " | patch", cmd)
|
||||
end)
|
||||
|
||||
it("resolves arg function with string results", function()
|
||||
vim.cmd.edit({ args = { "README.md" } })
|
||||
conform.formatters.test = {
|
||||
meta = { url = "", description = "" },
|
||||
command = "echo",
|
||||
args = function()
|
||||
return "| patch"
|
||||
end,
|
||||
}
|
||||
local config = assert(conform.get_formatter_config("test"))
|
||||
local ctx = runner.build_context(0, config)
|
||||
local cmd = runner.build_cmd("", ctx, config)
|
||||
assert.equal("echo | patch", cmd)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("e2e", 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)
|
||||
|
||||
---@param buf_content string
|
||||
---@param expected string
|
||||
---@param opts? table
|
||||
local function run_formatter(buf_content, expected, opts)
|
||||
local bufnr = vim.fn.bufadd("testfile")
|
||||
vim.fn.bufload(bufnr)
|
||||
vim.api.nvim_set_current_buf(bufnr)
|
||||
local lines = vim.split(buf_content, "\n", { plain = true })
|
||||
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 })
|
||||
set_formatter_output(expected_lines)
|
||||
conform.format(vim.tbl_extend("keep", opts or {}, { formatters = { "test" }, quiet = true }))
|
||||
return expected_lines
|
||||
end
|
||||
|
||||
---@param buf_content string
|
||||
---@param new_content string
|
||||
local function run_formatter_test(buf_content, new_content)
|
||||
local lines = run_formatter(buf_content, new_content)
|
||||
assert.are.same(lines, vim.api.nvim_buf_get_lines(0, 0, -1, false))
|
||||
end
|
||||
|
||||
it("sets the correct output", function()
|
||||
run_formatter_test(
|
||||
[[
|
||||
if true {
|
||||
print("hello")
|
||||
}]],
|
||||
[[
|
||||
if true {
|
||||
print("hello")
|
||||
}]]
|
||||
)
|
||||
run_formatter_test(
|
||||
[[
|
||||
if true {
|
||||
print("hello")
|
||||
}]],
|
||||
[[
|
||||
if true {
|
||||
print("goodbye")
|
||||
}]]
|
||||
)
|
||||
run_formatter_test(
|
||||
[[
|
||||
if true {
|
||||
print("hello")
|
||||
}]],
|
||||
[[
|
||||
if true {
|
||||
print("hello world")
|
||||
print("hello world")
|
||||
print("hello world")
|
||||
}]]
|
||||
)
|
||||
run_formatter_test(
|
||||
[[
|
||||
print("a")
|
||||
print("b")
|
||||
print("c")
|
||||
]],
|
||||
[[
|
||||
print("c")
|
||||
print("b")
|
||||
print("a")
|
||||
]]
|
||||
)
|
||||
run_formatter_test("hello\ngoodbye", "hello\n\n\ngoodbye")
|
||||
run_formatter_test("hello", "hello\ngoodbye")
|
||||
run_formatter_test("hello\ngoodbye", "hello")
|
||||
run_formatter_test("", "hello")
|
||||
run_formatter_test("\nfoo", "\nhello\nfoo")
|
||||
run_formatter_test("hello", "hello\n")
|
||||
run_formatter_test("hello", "hello\n\n")
|
||||
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()
|
||||
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 = 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 = 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)
|
||||
|
||||
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(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)
|
||||
|
||||
it("discards formatting changes if buffer has been concurrently modified", function()
|
||||
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(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)
|
||||
|
||||
it("discards formatting changes if formatter output is empty /w non-empty input", function()
|
||||
local bufnr = vim.fn.bufadd("testfile")
|
||||
vim.fn.bufload(bufnr)
|
||||
vim.api.nvim_set_current_buf(bufnr)
|
||||
local original_lines = { "line one", "line two" }
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, original_lines)
|
||||
vim.bo[bufnr].modified = false
|
||||
set_formatter_output({ "" })
|
||||
conform.format({ formatters = { "test" }, quiet = true })
|
||||
local output_lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
||||
assert.are.same(original_lines, output_lines)
|
||||
end)
|
||||
|
||||
it("formats on save", function()
|
||||
conform.setup({
|
||||
formatters_by_ft = { ["*"] = { "test" } },
|
||||
format_on_save = true,
|
||||
})
|
||||
vim.cmd.edit({ args = { "tests/testfile.txt" } })
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, true, { "hello" })
|
||||
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")
|
||||
assert.are.same({ "goodbye" }, lines)
|
||||
end)
|
||||
|
||||
it("formats file even if one formatter errors", function()
|
||||
conform.formatters.test2 = {
|
||||
command = "tests/fake_formatter.sh",
|
||||
args = { "--fail" },
|
||||
}
|
||||
local lines = run_formatter("hello", "goodbye", { formatters = { "test2", "test" } })
|
||||
assert.are.same(lines, vim.api.nvim_buf_get_lines(0, 0, -1, false))
|
||||
end)
|
||||
|
||||
it("does not change output if dry_run is true", function()
|
||||
run_formatter("hello", "foo", { dry_run = true })
|
||||
assert.are.same({ "hello" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
|
||||
end)
|
||||
|
||||
describe("range formatting", function()
|
||||
it("applies edits that overlap the range start", function()
|
||||
run_formatter(
|
||||
"a\nb\nc",
|
||||
"d\nb\nd",
|
||||
{ range = {
|
||||
start = { 1, 0 },
|
||||
["end"] = { 2, 0 },
|
||||
} }
|
||||
)
|
||||
assert.are.same({ "d", "b", "c" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
|
||||
end)
|
||||
|
||||
it("applies edits that overlap the range end", function()
|
||||
run_formatter(
|
||||
"a\nb\nc",
|
||||
"d\nb\nd",
|
||||
{ range = {
|
||||
start = { 3, 0 },
|
||||
["end"] = { 3, 1 },
|
||||
} }
|
||||
)
|
||||
assert.are.same({ "a", "b", "d" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
|
||||
end)
|
||||
|
||||
it("applies edits that are completely contained by the range", function()
|
||||
run_formatter(
|
||||
"a\nb\nc",
|
||||
"a\nd\nc",
|
||||
{ range = {
|
||||
start = { 1, 0 },
|
||||
["end"] = { 3, 0 },
|
||||
} }
|
||||
)
|
||||
assert.are.same({ "a", "d", "c" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
@ -0,0 +1,24 @@
|
||||
require("plenary.async").tests.add_to_env()
|
||||
local conform = require("conform")
|
||||
local log = require("conform.log")
|
||||
local M = {}
|
||||
|
||||
M.reset_editor = function()
|
||||
vim.cmd.tabonly({ mods = { silent = true } })
|
||||
for i, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
|
||||
if i > 1 then
|
||||
vim.api.nvim_win_close(winid, true)
|
||||
end
|
||||
end
|
||||
vim.api.nvim_win_set_buf(0, vim.api.nvim_create_buf(false, true))
|
||||
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
||||
vim.api.nvim_buf_delete(bufnr, { force = true })
|
||||
end
|
||||
conform.formatters = {}
|
||||
conform.formatters_by_ft = {}
|
||||
pcall(vim.api.nvim_del_augroup_by_name, "Conform")
|
||||
log.level = vim.log.levels.ERROR
|
||||
log.set_handler(print)
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user