aboutsummaryrefslogtreecommitdiffstats
path: root/tests/runner_spec.lua
blob: f054d9ebc050b4669c5852948cbee3a44a80ad10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
require("plenary.async").tests.add_to_env()
local test_util = require("tests.test_util")
local conform = require("conform")
local runner = require("conform.runner")

describe("runner", function()
  after_each(function()
    test_util.reset_editor()
  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)
  end)

  describe("e2e", function()
    before_each(function()
      conform.formatters.test = {
        meta = { url = "", description = "" },
        command = "tests/fake_formatter.sh",
      }
    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 })
      test_util.set_formatter_output(expected_lines)
      conform.format(vim.tbl_extend("force", opts or {}, { formatters = { "test" } }))
      return expected_lines
    end

    ---@param buf_content string
    ---@param new_content string
    ---@param expected? string[]
    local function run_formatter_test(buf_content, new_content, expected)
      local lines = run_formatter(buf_content, new_content)
      assert.are.same(expected or 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", { "hello", "", "", "goodbye" })
      run_formatter_test("hello", "hello\ngoodbye", { "hello", "goodbye" })
      run_formatter_test("", "hello", { "hello" })
      run_formatter_test("\nfoo", "\nhello\nfoo", { "", "hello", "foo" })
      run_formatter_test("hello", "hello\n\n", { "hello", "" })
      run_formatter_test("hello", "hello\n", { "hello" })
      assert.falsy(vim.bo.modified)
      run_formatter_test("hello\n", "hello", { "hello" })
      run_formatter_test("hello\n ", "hello", { "hello" })
    end)

    it("does not change output if formatter fails", function()
      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.exit_codes = { 0, 1 }
      run_formatter_test("hello", "goodbye")
    end)

    it("does not format if it times out", function()
      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(100)
      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(100)
      assert.are.same({ "newcontent" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
    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" })
      test_util.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)
  end)
end)