Skip to content

Commit f2b4b3b

Browse files
committed
Get explicit parser installation into test suite
Now if you're running the test suite locally, it won't rely on you having manually installed the parser. Now it checks and makes sure they're all installed. This also brings parity to local runs vs github CI runs - both ensure the same parsers.
1 parent 0786c05 commit f2b4b3b

File tree

16 files changed

+117
-3
lines changed

16 files changed

+117
-3
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ jobs:
2424
git clone https://github.com/nvim-lua/plenary.nvim.git
2525
mv plenary.nvim $HOME/.local/share/nvim/lazy/
2626
27-
- name: Install nvim-treesitter and parsers
27+
- name: Install nvim-treesitter
2828
run: |
2929
mkdir -p $HOME/.local/share/nvim/lazy/
3030
git clone https://github.com/nvim-treesitter/nvim-treesitter.git
3131
mv nvim-treesitter $HOME/.local/share/nvim/lazy/
32-
nvim --headless --noplugin -u tests/minimal_init.lua -c "TSInstall! python rust haskell html javascript c_sharp" -c "quit"
3332
3433
- name: Run luacheck type check / linter
3534
run: |

tests/fixtures/ruby.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
class BankAccount
33
attr_accessor :balance, :owner
44

5+
# This is ruby code
56
def initialize(balance = 0, owner = 'Unknown')
67
@balance = balance
78
@owner = owner
89
end
910

11+
# This does what you think
1012
def deposit(amount)
1113
if amount > 0
1214
@balance += amount
@@ -16,13 +18,16 @@ def deposit(amount)
1618
end
1719
end
1820

21+
# deposit 5
1922
deposit(5, deposit(5))
2023

24+
# allows you to withdraw
2125
def withdraw(amount)
2226
return nil unless deposit?(amount)
2327
@balance -= amount
2428
end
2529

30+
# deposit?
2631
def deposit?(amount)
2732
lambda { |x| x > 0 }[amount]
2833
end

tests/minimal_init.lua

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,29 @@ vim.opt.swapfile = false
99
vim.cmd("runtime! plugin/treewalker.nvim")
1010
vim.cmd("runtime! plugin/plenary.vim")
1111
vim.cmd("runtime! plugin/nvim-treesitter")
12-
require('nvim-treesitter').setup()
12+
require('nvim-treesitter.configs').setup {
13+
ensure_installed = {
14+
"lua",
15+
"python",
16+
"rust",
17+
"haskell",
18+
"html",
19+
"javascript",
20+
"c_sharp",
21+
"typescript",
22+
"php",
23+
"markdown",
24+
"ruby",
25+
},
26+
sync_install = true,
27+
auto_install = true,
28+
ignore_install = {},
29+
modules = {}
30+
}
31+
32+
-- These are required lest nvim not be able to tell these parsers are installed
33+
vim.treesitter.language.register('ruby', { 'rb' })
34+
-- TODO Why on earth does this break tests. Isn't the md parser supposed to come by default?
35+
-- vim.treesitter.language.register('markdown', { 'md' })
36+
1337
dofile("plugin/init.lua") -- get the Treewalker command present

tests/treewalker/c_sharp_spec.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ describe("Movement in a C Sharp file", function()
88
load_fixture("/c-sharp.cs")
99
end)
1010

11+
h.ensure_has_parser()
12+
1113
it("works", function()
1214
vim.fn.cursor(7, 5)
1315
tw.move_down()

tests/treewalker/c_spec.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
local load_fixture = require "tests.load_fixture"
22
local assert = require "luassert"
33
local tw = require 'treewalker'
4+
local h = require 'tests.treewalker.helpers'
45
local lines = require 'treewalker.lines'
56

67
describe("Swapping in a c file:", function()
78
before_each(function()
89
load_fixture("/c.c")
910
end)
1011

12+
h.ensure_has_parser()
13+
1114
it("swaps right, when cursor is inside a string, the whole string", function()
1215
vim.fn.cursor(14, 17) -- the o in one
1316
assert.same(' printf("one\\n", "two\\n");', lines.get_line(14))

tests/treewalker/haskell_spec.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ describe("Movement in a haskell file: ", function()
77
load_fixture("/haskell.hs")
88
end)
99

10+
h.ensure_has_parser()
11+
1012
it("moves out of a nested node", function()
1113
vim.fn.cursor(22, 3)
1214
tw.move_out()

tests/treewalker/helpers.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ M.feed_keys = function(keys)
2626
vim.api.nvim_feedkeys(termcodes, 'mtx', false)
2727
end
2828

29+
-- This is more for the test suite itself and ensuring that it's operating correctly.
30+
-- Makes sure there's no missing parser for the loaded file in the current buffer
31+
M.ensure_has_parser = function()
32+
it("the test suite has the parser for the requested filetype", function()
33+
local ok = pcall(vim.treesitter.get_parser)
34+
if not ok then
35+
error(string.format("Test suite is missing parser for ft [%s]", vim.bo.ft))
36+
end
37+
end)
38+
end
39+
2940
return M

tests/treewalker/html_spec.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ describe("Movement in an html file", function()
77
load_fixture("/html.html")
88
end)
99

10+
h.ensure_has_parser()
11+
1012
it("doesn't stop on footers", function()
1113
vim.fn.cursor(10, 5)
1214
tw.move_down()

tests/treewalker/lua_spec.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ describe("Movement in a regular lua file: ", function()
1010
load_fixture("/lua.lua")
1111
end)
1212

13+
h.ensure_has_parser()
14+
1315
it("moves up and down at the same pace", function()
1416
vim.fn.cursor(1, 1) -- Reset cursor
1517
tw.move_down()

tests/treewalker/lua_spec_spec.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ describe("Movement in a lua spec file: ", function()
99
load_fixture("/lua-spec.lua")
1010
end)
1111

12+
h.ensure_has_parser()
13+
1214
-- go to first describe
1315
local function go_to_describe()
1416
vim.fn.cursor(1, 1)

0 commit comments

Comments
 (0)