Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic lua tests #7

Merged
merged 16 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ jobs:
if: runner.os == 'Linux' && matrix.app_version == 'stable'
env:
NEOVIM_PATH: ${{ steps.vim.outputs.executable }}
- name: Set up Lua
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
uses: leafo/gh-actions-lua@v9
with:
luaVersion: "luajit-2.1.0-beta3"
if: runner.os == 'Linux' && matrix.app_version == 'stable'
- name: Set up Luarocks
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
uses: leafo/gh-actions-luarocks@v4
with:
luaVersion: "5.1"
if: runner.os == 'Linux' && matrix.app_version == 'stable'
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
- name: Install Busted
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
run: |
luarocks install busted
luarocks install luafilesystem
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
if: runner.os == 'Linux' && matrix.app_version == 'stable'
- name: Run cursorless.nvim busted lua tests
run: |
cd cursorless.nvim
busted --run unit
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
if: runner.os == 'Linux' && matrix.app_version == 'stable'
id: neovimLuaTests
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
- name: Create vscode dist that can be installed locally
run: pnpm -F @cursorless/cursorless-vscode populate-dist --local-install
if: runner.os == 'Linux' && matrix.app_version == 'stable'
Expand Down
8 changes: 8 additions & 0 deletions cursorless.nvim/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
return {
_all = {
lua = './test/nvim-shim.sh'
},
unit = {
ROOT = {'./test/unit/'},
},
}
11 changes: 3 additions & 8 deletions cursorless.nvim/lua/cursorless/cursorless.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,9 @@ function M.buffer_get_selection()
end

-- https://github.com/nvim-treesitter/nvim-treesitter/blob/master/lua/nvim-treesitter/ts_utils.lua#L278
-- luacheck:ignore 631
-- https://github.com/nvim-treesitter/nvim-treesitter-textobjects/blob/master/lua/nvim-treesitter/textobjects/select.lua#L114
-- as an example if you put that in a vim buffer and do the following you can do a selection:
-- :w c:\work\tmp\test.lua
-- :so %
-- :lua select_range(5, 12, 5, 30)
-- for example it will highlight the last function name (nvim_win_set_cursor).
-- another example is :tmap <c-b> <Cmd>lua require("talon.cursorless").select_range(4, 0, 4, 38)<Cr>
-- If you have a buffer with the line: "hello world"
-- :lua require("cursorless.cursorless").select_range(1, 2, 1, 4)
-- will highlight "llo"
-- NOTE: works for any mode (n,i,v,nt) except in t mode
function M.select_range(start_line, start_col, end_line, end_col)
vim.cmd([[normal! :noh]])
Expand Down
16 changes: 16 additions & 0 deletions cursorless.nvim/test/helpers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- This file gets linked into plugin/helpers.lua of busted nvim config
-- Functions that are exposed to all tests

function _G.get_selected_text()
local _, ls, cs = unpack(vim.fn.getpos("v"))
local _, le, ce = unpack(vim.fn.getpos("."))
return vim.api.nvim_buf_get_text(0, ls - 1, cs - 1, le - 1, ce, {})
end

function _G.convert_table_entries(tbl, func)
local mapped = {}
for k, v in pairs(tbl) do
mapped[k] = func(v)
end
return mapped
end
28 changes: 28 additions & 0 deletions cursorless.nvim/test/nvim-shim.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
set -euo pipefail

if ! [[ "${PWD}" == *"cursorless.nvim" ]]; then
echo "ERROR: This script must be run from inside cursorless.nvim/ directory"
exit 1
fi

test_folder=$(mktemp -d "${TMPDIR-/tmp}"/cursorless-busted-test-XXXXX)
export XDG_CONFIG_HOME="${test_folder}/xdg/config/"
export XDG_STATE_HOME="${test_folder}/xdg/local/state/"
export XDG_DATA_HOME="${test_folder}/xdg/local/share/"
dependency_folder="${XDG_DATA_HOME}/nvim/site/pack/testing/start/"
plugin_folder="${XDG_CONFIG_HOME}/nvim/plugin/"

mkdir -p "${plugin_folder}" "${XDG_STATE_HOME}" "${dependency_folder}"
ln -sf "${PWD}" "${dependency_folder}/cursorless.nvim"

# Link in standalone helper functions we want all tests to be able to call
ln -sf "${PWD}/test/helpers.lua" "${plugin_folder}/helpers.lua"

# shellcheck disable=SC2068
command nvim --cmd 'set loadplugins' -l $@
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
exit_code=$?

rm -rf "${test_folder}"

exit $exit_code
88 changes: 88 additions & 0 deletions cursorless.nvim/test/unit/cursorless_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
describe("cursorless.nvim tests", function()
local cursorless = require("cursorless.cursorless")

describe("window_get_visible_lines()", function()
it("Can read one visible line", function()
local pos = vim.api.nvim_win_get_cursor(0)[2]
local line = vim.api.nvim_get_current_line()
local nline = line:sub(0, pos) .. "hello" .. line:sub(pos + 1)
vim.api.nvim_set_current_line(nline)

local visible = cursorless.window_get_visible_lines()
assert(table.concat(visible) == table.concat({ 1, 1 }))
end)

it("Can read all lines visible on the window", function()
local maxlines = vim.api.nvim_win_get_height(0)
local lines = {}
for _ = 1, (maxlines + 1) do
table.insert(lines, "hello ")
end
vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
local visible = cursorless.window_get_visible_lines()
assert(table.concat(visible) == table.concat({ 1, maxlines }))
end)
end)
describe("select_range()", function()
it("Selects the specified range", function()
local lines = "hello world"
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n"))
cursorless.select_range(1, 2, 1, 4)

assert(table.concat(_G.get_selected_text()) == "llo")
end)
end)
describe("buffer_get_selection()", function()
it(
"Can get the forward selection in a format expected by cursorless",
function()
local lines = "hello world"
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n"))
cursorless.select_range(1, 2, 1, 4)
assert(
table.concat(
_G.convert_table_entries(
cursorless.buffer_get_selection(),
tostring
),
", "
)
== table.concat(
_G.convert_table_entries({ 1, 3, 1, 5, false }, tostring),
", "
)
)
end
)
it(
"Can get the backward selection in a format expected by cursorless",
function()
local lines = "hello world"
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n"))
cursorless.select_range(1, 4, 1, 2)
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
print(
table.concat(
_G.convert_table_entries(
cursorless.buffer_get_selection(),
tostring
),
", "
)
)
assert(
table.concat(
_G.convert_table_entries(
cursorless.buffer_get_selection(),
tostring
),
", "
)
== table.concat(
_G.convert_table_entries({ 1, 3, 1, 5, true }, tostring),
", "
)
)
end
)
end)
end)
10 changes: 8 additions & 2 deletions docs/contributing/cursorless-in-neovim.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Follow the steps in [CONTRIBUTING.md](./CONTRIBUTING.md#initial-setup).

Follow the installation steps in [cursorless.nvim](https://github.com/hands-free-vim/cursorless.nvim/tree/main#prerequisites).

Confirm that production cursorless.nvim is working in neovim, eg say `"take first paint"` in a nonempty document.
Confirm that production cursorless.nvim is working in neovim, eg say `"take first paint"` in a non-empty document.

### 3. Add nvim executable path to your PATH

Expand Down Expand Up @@ -40,7 +40,13 @@ debug mode. To do so you need to run the `workbench.action.debug.selectandstart`

The debug logs are written in `C:\path\to\cursorless\packages\cursorless-neovim\out\nvim_node.log`.

NOTE: This will spawn a standalone nvim instance that is independent of VSCode. Consequently after you're done debugging, you need to close nvim.
NOTE: This will spawn a standalone nvim instance that is independent of VSCode. Consequently after you're done
debugging, you need to close nvim.

### Running lua tests

Their are separate cursorless and lua tests. You can run the lua tests by entering the `cursorless.nvim` folder and
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
running: `busted --run unit`. These tests currently only work on Linux.
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved

## Sending pull requests

Expand Down
3 changes: 3 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@
'';
}))
python

pkgs.neovim
pkgs.luajitPackages.busted # for lua testing
pkgs.luarocks # pre-commit doesn't auto-install luarocks
];
# To prevent weird broken non-interactive bash terminal
buildInputs = [ pkgs.bashInteractive ];
Expand Down
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if not (vim.uv or vim.loop).fs_stat(lazypath) then
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
vim.opt.runtimepath:prepend(lazypath)
require("lazy").setup({
-- Allows title detection by neovim-talon while testing
"hands-free-vim/talon.nvim",
Expand Down
Loading