diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/runner_spec.lua | 47 | ||||
-rw-r--r-- | tests/util_spec.lua | 72 |
2 files changed, 87 insertions, 32 deletions
diff --git a/tests/runner_spec.lua b/tests/runner_spec.lua index 5d4fea4..f52ca48 100644 --- a/tests/runner_spec.lua +++ b/tests/runner_spec.lua @@ -121,7 +121,7 @@ describe("runner", function() 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) + assert.are.same({ vim.fn.exepath("echo"), vim.api.nvim_buf_get_name(bufnr) }, cmd) end) it("replaces $DIRNAME in args", function() @@ -135,7 +135,10 @@ describe("runner", function() 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) + assert.are.same( + { vim.fn.exepath("echo"), vim.fs.dirname(vim.api.nvim_buf_get_name(bufnr)) }, + cmd + ) end) it("resolves arg function", function() @@ -150,35 +153,7 @@ describe("runner", function() 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) + assert.are.same({ vim.fn.exepath("echo"), "--stdin" }, cmd) end) it("resolves arg function with string results", function() @@ -193,7 +168,7 @@ describe("runner", function() 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) + assert.are.same(util.shell_build_argv(vim.fn.exepath("echo") .. " | patch"), cmd) end) end) @@ -410,5 +385,13 @@ print("a") assert.are.same({ "a", "d", "c" }, vim.api.nvim_buf_get_lines(0, 0, -1, false)) end) end) + + it("can run the format command in the shell", function() + conform.formatters.test = { + command = "echo", + args = '-e "world\nhello" | sort', + } + run_formatter_test("", "hello\nworld") + end) end) end) diff --git a/tests/util_spec.lua b/tests/util_spec.lua new file mode 100644 index 0000000..9f1456c --- /dev/null +++ b/tests/util_spec.lua @@ -0,0 +1,72 @@ +local test_util = require("tests.test_util") +local util = require("conform.util") + +describe("util", function() + local shell = vim.o.shell + local shellcmdflag = vim.o.shellcmdflag + local shellxescape = vim.o.shellxescape + local shellxquote = vim.o.shellxquote + after_each(function() + test_util.reset_editor() + vim.o.shell = shell + vim.o.shellcmdflag = shellcmdflag + vim.o.shellxescape = shellxescape + vim.o.shellxquote = shellxquote + end) + + describe("shell_build_argv", function() + it("builds simple command", function() + vim.o.shell = "/bin/bash" + vim.o.shellcmdflag = "-c" + vim.o.shellxescape = "" + vim.o.shellxquote = "" + local argv = util.shell_build_argv("echo hello") + assert.are_same({ "/bin/bash", "-c", "echo hello" }, argv) + end) + + it("handles shell arguments", function() + vim.o.shell = "/bin/bash -f" + vim.o.shellcmdflag = "-c" + vim.o.shellxescape = "" + vim.o.shellxquote = "" + local argv = util.shell_build_argv("echo hello") + assert.are_same({ "/bin/bash", "-f", "-c", "echo hello" }, argv) + end) + + it("handles shell with spaces", function() + vim.o.shell = '"c:\\program files\\unix\\sh.exe"' + vim.o.shellcmdflag = "-c" + vim.o.shellxescape = "" + vim.o.shellxquote = "" + local argv = util.shell_build_argv("echo hello") + assert.are_same({ "c:\\program files\\unix\\sh.exe", "-c", "echo hello" }, argv) + end) + + it("handles shell with spaces and args", function() + vim.o.shell = '"c:\\program files\\unix\\sh.exe" -f' + vim.o.shellcmdflag = "-c" + vim.o.shellxescape = "" + vim.o.shellxquote = "" + local argv = util.shell_build_argv("echo hello") + assert.are_same({ "c:\\program files\\unix\\sh.exe", "-f", "-c", "echo hello" }, argv) + end) + + it("applies shellxquote", function() + vim.o.shell = "/bin/bash" + vim.o.shellcmdflag = "-c" + vim.o.shellxescape = "" + vim.o.shellxquote = "'" + local argv = util.shell_build_argv("echo hello") + assert.are_same({ "/bin/bash", "-c", "'echo hello'" }, argv) + end) + + it("uses shellxescape", function() + vim.o.shell = "/bin/bash" + vim.o.shellcmdflag = "-c" + vim.o.shellxescape = "el" + vim.o.shellxquote = "(" + local argv = util.shell_build_argv("echo hello") + assert.are_same({ "/bin/bash", "-c", "(^echo h^e^l^lo)" }, argv) + end) + end) +end) |