From 92e3fb82266f7d82a917541fb2e08372a22f0b79 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Thu, 23 Jan 2025 03:33:11 -0800 Subject: [PATCH] WIP --- .github/workflows/{tests.yaml => ci.yaml} | 12 +++++-- Makefile | 37 ++++++++++---------- scripts/make_cli.lua | 23 +++++++++++++ scripts/minimal_init.lua | 9 ----- tests/file/ui_spec.lua | 42 +++++++++++++++++++++++ 5 files changed, 93 insertions(+), 30 deletions(-) rename .github/workflows/{tests.yaml => ci.yaml} (83%) create mode 100644 scripts/make_cli.lua create mode 100644 tests/file/ui_spec.lua diff --git a/.github/workflows/tests.yaml b/.github/workflows/ci.yaml similarity index 83% rename from .github/workflows/tests.yaml rename to .github/workflows/ci.yaml index 57991a73..cfe12052 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/ci.yaml @@ -1,4 +1,4 @@ -name: Tests +name: CI on: push: @@ -36,13 +36,19 @@ jobs: neovim: true version: ${{ matrix.rev }} - - name: Prepare + - name: Install Dependencies + shell: bash run: | ${{ matrix.install-deps }} nvim --version fzf --version rg --version - make prepare + if [ "$RUNNER_OS" == "Linux" ]; then + fdfind --version + else + fd --version + fi + make deps - name: Run tests run: | diff --git a/Makefile b/Makefile index 71142441..ca5d1171 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,31 @@ NVIM_EXEC ?= nvim -# Run all test files -# assuming `nv` is linked to nightly appimage -# run with `make NVIM_EXEC="nvim nv" test` -# to test on both stable and nightly +# +# Run all tests or specic module tests +# +# Test both stable and nightly (assuming `nv` is linked to nightly): +# `make NVIM_EXEC="nvim nv" test` +# +# Test specific module(s) with `make test glob=file` +# NOTE: glob is resolved using `vim.fn.globpath` so we can also run: +# `make test glob=f +# .PHONY: test test: for nvim_exec in $(NVIM_EXEC); do \ printf "\n======\n\n" ; \ $$nvim_exec --version | head -n 1 && echo '' ; \ $$nvim_exec --headless --noplugin -u ./scripts/minimal_init.lua \ - -c "lua MiniTest.run()" ; \ + -l ./scripts/make_cli.lua ; \ done -# Run test from file at `$FILE` environment variable -# run with `make FILE=tests/init_spec.lua test-file` -.PHONY: test-file -test-file: - for nvim_exec in $(NVIM_EXEC); do \ - printf "\n======\n\n" ; \ - $$nvim_exec --version | head -n 1 && echo '' ; \ - $$nvim_exec --headless --noplugin -u ./scripts/minimal_init.lua \ - -c "lua MiniTest.run_file('$(FILE)')" ; \ - done - -# Download 'mini.nvim' to use its 'mini.test' testing module -prepare: +# +# Download 'mini.nvim' and `nvim-web-devicons` into "deps" subfolder +# only used with the CI workflow, `minimal_init` will detect the deps +# in our lazy.nvim local config +# +.PHONY: deps +deps: make clean @mkdir -p deps git clone --depth=1 --single-branch https://github.com/echasnovski/mini.nvim deps/mini.nvim @@ -33,5 +33,6 @@ prepare: deps/nvim-web-devicons # clean up +.PHONY: clean clean: rm -rf deps diff --git a/scripts/make_cli.lua b/scripts/make_cli.lua new file mode 100644 index 00000000..7a6d4678 --- /dev/null +++ b/scripts/make_cli.lua @@ -0,0 +1,23 @@ +-- NOTE: this script is called with `:help -l` +local MiniTest = require("mini.test") +local glob = vim.env.glob +local find_files + + +if glob then + -- Find both "tests/glob**/*_spec.lua" and "test/glob*_spec.lua" + find_files = function() + local ret = vim.fn.globpath("tests", glob .. "*_spec.lua", true, true) + for _, f in ipairs(vim.fn.globpath("tests", glob .. "**/*_spec.lua", true, true)) do + table.insert(ret, f) + end + return ret + end +else + -- All test files + find_files = function() + return vim.fn.globpath("tests", "**/*_spec.lua", true, true) + end +end + +MiniTest.run({ collect = { find_files = find_files } }) diff --git a/scripts/minimal_init.lua b/scripts/minimal_init.lua index 39b5ea86..cc0858c5 100644 --- a/scripts/minimal_init.lua +++ b/scripts/minimal_init.lua @@ -11,13 +11,4 @@ if #vim.api.nvim_list_uis() == 0 then -- Add fzf-lua (lazy) vim.opt.runtimepath:append(vim.fs.joinpath("deps", "fzf-lua")) vim.opt.runtimepath:append(vim.fs.joinpath(vim.fn.stdpath("data"), "lazy", "fzf-lua")) - - -- Set up 'mini.test' - require("mini.test").setup({ - collect = { - find_files = function() - return vim.fn.globpath("tests", "**/*_spec.lua", true, true) - end, - }, - }) end diff --git a/tests/file/ui_spec.lua b/tests/file/ui_spec.lua new file mode 100644 index 00000000..731b506b --- /dev/null +++ b/tests/file/ui_spec.lua @@ -0,0 +1,42 @@ +---@diagnostic disable: unused-local, unused-function +local MiniTest = require("mini.test") +local helpers = dofile("tests/helpers.lua") +local child = helpers.new_child_neovim() +local expect, eq = helpers.expect, helpers.expect.equality +local new_set = MiniTest.new_set + +-- Helpers with child processes +--stylua: ignore start +---@format disable-next +local reload = function(config) child.unload(); child.setup(config) end +local set_cursor = function(...) return child.set_cursor(...) end +local get_cursor = function(...) return child.get_cursor(...) end +local set_lines = function(...) return child.set_lines(...) end +local get_lines = function(...) return child.get_lines(...) end +local type_keys = function(...) return child.type_keys(...) end +local sleep = function(ms) helpers.sleep(ms, child) end +--stylua: ignore end + +local T = new_set({ + hooks = { + pre_case = function() + child.init() + child.setup({}) + + -- Make all showed messages full width + child.o.cmdheight = 10 + end, + post_once = child.stop, + }, + -- n_retry = helpers.get_n_retry(2), +}) + +T["setup()"] = new_set() + +T["setup()"]["setup global vars"] = function() + -- Global vars + eq(child.lua_get([[type(vim.g.fzf_lua_server)]]), "string") + eq(child.lua_get([[type(vim.g.fzf_lua_directory)]]), "string") +end + +return T