From 9ac1e05fc8cb8b8e36ad1807960e476d8bf81413 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 1 Feb 2025 17:22:41 +1100 Subject: [PATCH 1/6] chore: plenary tests POC --- .gitignore | 1 + .luarc.json | 3 ++- scripts/test.sh | 16 ++++++++++++++++ tests/minimal_init.lua | 1 + tests/unit/utils_spec.lua | 17 +++++++++++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100755 scripts/test.sh create mode 100644 tests/minimal_init.lua create mode 100644 tests/unit/utils_spec.lua diff --git a/.gitignore b/.gitignore index faee0c43dd9..3ecdfae2bce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /luals-out/ /luals/ +/plenary.nvim/ # backup vim files *~ diff --git a/.luarc.json b/.luarc.json index bb0a84b9687..1cef7a9b9d9 100644 --- a/.luarc.json +++ b/.luarc.json @@ -4,7 +4,8 @@ "workspace": { "library": [ "$VIMRUNTIME/lua/vim", - "${3rd}/luv/library" + "${3rd}/luv/library", + "plenary.nvim" ] }, "diagnostics": { diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 00000000000..9c2b18db001 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +set -e + +# TODO add ability for user to specify a test file or directory + +REPO_DIR="$(git rev-parse --show-toplevel)" +PLENARY_DIR="${REPO_DIR}/plenary.nvim" + +nvim --headless \ + --clean \ + --noplugin \ + -c "set runtimepath+=${PLENARY_DIR}" \ + -c "lua require('plenary.test_harness').test_directory('tests/', { minimal_init = './tests/minimal_init.lua' })" \ + -c "qa!" + diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua new file mode 100644 index 00000000000..0678145cdaf --- /dev/null +++ b/tests/minimal_init.lua @@ -0,0 +1 @@ +-- this file is necessary for the minimal_init option which directs the spawned nvim test instances be run with --noplugin diff --git a/tests/unit/utils_spec.lua b/tests/unit/utils_spec.lua new file mode 100644 index 00000000000..51fb50d4c4c --- /dev/null +++ b/tests/unit/utils_spec.lua @@ -0,0 +1,17 @@ +---@type Luassert +local assert = require("luassert") + +local utils = require("nvim-tree.utils") + +describe("utils.path_add_trailing", function() + before_each(function() + end) + + it("trailing added", function() + assert.equals(utils.path_add_trailing("foo"), "foo/") + end) + + it("trailing already present", function() + assert.equals(utils.path_add_trailing("foo/"), "foo/") + end) +end) From 2dcf249d497041fb2043d1c41cedbf75afed9eab Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 1 Feb 2025 18:05:23 +1100 Subject: [PATCH 2/6] chore: plenary tests POC --- lua/nvim-tree/utils.lua | 6 +++++- scripts/test.sh | 5 ++++- tests/minimal_init.lua | 7 +++++++ tests/unit/utils_spec.lua | 24 ++++++++++++++++++++++-- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 194845f5c1f..f2bed188e83 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -11,6 +11,10 @@ M.is_wsl = vim.fn.has("wsl") == 1 -- false for WSL M.is_windows = vim.fn.has("win32") == 1 or vim.fn.has("win32unix") == 1 +function M._is_windows() + return vim.fn.has("win32") == 1 or vim.fn.has("win32unix") == 1 +end + ---@param haystack string ---@param needle string ---@return boolean @@ -299,7 +303,7 @@ end ---@param path string ---@return string function M.canonical_path(path) - if M.is_windows and path:match("^%a:") then + if M._is_windows() and path:match("^%a:") then return path:sub(1, 1):upper() .. path:sub(2) end return path diff --git a/scripts/test.sh b/scripts/test.sh index 9c2b18db001..4666e2ae514 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -5,12 +5,15 @@ set -e # TODO add ability for user to specify a test file or directory REPO_DIR="$(git rev-parse --show-toplevel)" +export REPO_DIR + PLENARY_DIR="${REPO_DIR}/plenary.nvim" +export PLENARY_DIR nvim --headless \ --clean \ --noplugin \ - -c "set runtimepath+=${PLENARY_DIR}" \ + -u "tests/minimal_init.lua" \ -c "lua require('plenary.test_harness').test_directory('tests/', { minimal_init = './tests/minimal_init.lua' })" \ -c "qa!" diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua index 0678145cdaf..2d9fce696bc 100644 --- a/tests/minimal_init.lua +++ b/tests/minimal_init.lua @@ -1 +1,8 @@ -- this file is necessary for the minimal_init option which directs the spawned nvim test instances be run with --noplugin + +-- ensure that this nvim-tree is not overridden by installed versions in .local/share/nvim/site etc. +vim.o.runtimepath = vim.env.REPO_DIR .. "," .. vim.o.runtimepath + +-- plenary will append ,.,$HOME/src/nvim-tree/master/plenary.nvim in the spawned test instances, however we want this here to prevent overrides +vim.o.runtimepath = vim.env.PLENARY_DIR .. "," .. vim.o.runtimepath + diff --git a/tests/unit/utils_spec.lua b/tests/unit/utils_spec.lua index 51fb50d4c4c..924834f8b84 100644 --- a/tests/unit/utils_spec.lua +++ b/tests/unit/utils_spec.lua @@ -1,5 +1,6 @@ ---@type Luassert local assert = require("luassert") +local stub = require("luassert.stub") local utils = require("nvim-tree.utils") @@ -8,10 +9,29 @@ describe("utils.path_add_trailing", function() end) it("trailing added", function() - assert.equals(utils.path_add_trailing("foo"), "foo/") + assert.equals("foo/", utils.path_add_trailing("foo")) end) it("trailing already present", function() - assert.equals(utils.path_add_trailing("foo/"), "foo/") + assert.equals("foo/", utils.path_add_trailing("foo/")) + end) +end) + +describe("utils.canonical_path", function() + + before_each(function() + stub(vim.fn, "has") + end) + + after_each(function() + end) + + it("is windows", function() + vim.fn.has.on_call_with("win32unix").returns(1) + assert.equals("C:\\foo\\bar", utils.canonical_path("c:\\foo\\bar"), "should be uppercase drive") + end) + + it("not windows", function() + assert.equals("c:\\foo\\bar", utils.canonical_path("c:\\foo\\bar")) end) end) From f9dc2948292adfb288c7dda8104f73d1a99d98c3 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 3 Feb 2025 14:45:26 +1100 Subject: [PATCH 3/6] chore: plenary tests POC: harden directories --- scripts/test.sh | 15 ++++++++------- tests/minimal_init.lua | 16 ++++++++-------- tests/test_init.lua | 7 +++++++ 3 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 tests/test_init.lua diff --git a/scripts/test.sh b/scripts/test.sh index 4666e2ae514..34e0ad7e005 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -4,16 +4,17 @@ set -e # TODO add ability for user to specify a test file or directory -REPO_DIR="$(git rev-parse --show-toplevel)" -export REPO_DIR +# TODO clone plenary -PLENARY_DIR="${REPO_DIR}/plenary.nvim" -export PLENARY_DIR +DIR_REPO="$(git rev-parse --show-toplevel)" +export DIR_REPO + +DIR_PLENARY="${DIR_REPO}/plenary.nvim" +export DIR_PLENARY nvim --headless \ --clean \ - --noplugin \ - -u "tests/minimal_init.lua" \ - -c "lua require('plenary.test_harness').test_directory('tests/', { minimal_init = './tests/minimal_init.lua' })" \ + -u "${DIR_REPO}/tests/minimal_init.lua" \ + -l "${DIR_REPO}/tests/test_init.lua" \ -c "qa!" diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua index 2d9fce696bc..ac5785f011e 100644 --- a/tests/minimal_init.lua +++ b/tests/minimal_init.lua @@ -1,8 +1,8 @@ --- this file is necessary for the minimal_init option which directs the spawned nvim test instances be run with --noplugin - --- ensure that this nvim-tree is not overridden by installed versions in .local/share/nvim/site etc. -vim.o.runtimepath = vim.env.REPO_DIR .. "," .. vim.o.runtimepath - --- plenary will append ,.,$HOME/src/nvim-tree/master/plenary.nvim in the spawned test instances, however we want this here to prevent overrides -vim.o.runtimepath = vim.env.PLENARY_DIR .. "," .. vim.o.runtimepath - +-- Prepend these as plenary appends a "." and plenary directory +-- The spawned processes don't specify --clean so contain the full ~/.local runtime path +vim.o.runtimepath = string.format( + "%s,%s,%s", + vim.env.DIR_REPO, + vim.env.DIR_PLENARY, + vim.o.runtimepath +) diff --git a/tests/test_init.lua b/tests/test_init.lua new file mode 100644 index 00000000000..b050ea18bc2 --- /dev/null +++ b/tests/test_init.lua @@ -0,0 +1,7 @@ +local test_harness = require('plenary.test_harness') + +local DIR_TEST = vim.env.DIR_REPO .. '/tests' +local FILE_MINIMAL_INIT = DIR_TEST .. '/minimal_init.lua' + +test_harness.test_directory(DIR_TEST, { minimal_init = FILE_MINIMAL_INIT }) + From 63cd226c3e16fe8c26cc32b376d7136317702fd8 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 3 Feb 2025 15:31:33 +1100 Subject: [PATCH 4/6] chore: plenary tests POC: add make entry, specify tests --- Makefile | 13 +++++++++++++ scripts/test.sh | 11 +++++++---- tests/test_init.lua | 14 ++++++++------ tests/unit/utils_spec.lua | 1 - 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index b5e829d8ddc..e412d9a987d 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,19 @@ help-update: help-check: help-update git diff --exit-code doc/nvim-tree-lua.txt +# +# test +# +test: plenary.nvim + scripts/test.sh $$TEST_NAME + +# +# Dependencies +# +# no plenary tags or releases available +plenary.nvim: + git clone git@github.com:nvim-lua/plenary.nvim.git + .PHONY: all lint style check luacheck style-check style-doc luals style-fix help-update help-check diff --git a/scripts/test.sh b/scripts/test.sh index 34e0ad7e005..3ff47543e86 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -2,16 +2,19 @@ set -e -# TODO add ability for user to specify a test file or directory - -# TODO clone plenary - DIR_REPO="$(git rev-parse --show-toplevel)" export DIR_REPO DIR_PLENARY="${DIR_REPO}/plenary.nvim" export DIR_PLENARY +if [ "${#}" -eq 1 ]; then + TEST_NAME="${1}" +else + TEST_NAME="tests" +fi +export TEST_NAME + nvim --headless \ --clean \ -u "${DIR_REPO}/tests/minimal_init.lua" \ diff --git a/tests/test_init.lua b/tests/test_init.lua index b050ea18bc2..47a8e3de826 100644 --- a/tests/test_init.lua +++ b/tests/test_init.lua @@ -1,7 +1,9 @@ -local test_harness = require('plenary.test_harness') - -local DIR_TEST = vim.env.DIR_REPO .. '/tests' -local FILE_MINIMAL_INIT = DIR_TEST .. '/minimal_init.lua' - -test_harness.test_directory(DIR_TEST, { minimal_init = FILE_MINIMAL_INIT }) +local test_harness = require("plenary.test_harness") +test_harness.test_directory( + vim.env.TEST_NAME, + { + minimal_init = vim.env.DIR_REPO .. "/tests/minimal_init.lua", + sequential = true, + } +) diff --git a/tests/unit/utils_spec.lua b/tests/unit/utils_spec.lua index 924834f8b84..820030e2a3c 100644 --- a/tests/unit/utils_spec.lua +++ b/tests/unit/utils_spec.lua @@ -18,7 +18,6 @@ describe("utils.path_add_trailing", function() end) describe("utils.canonical_path", function() - before_each(function() stub(vim.fn, "has") end) From 006b27dc0ad12f528536db4131bd3c9b29a5a1e0 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 3 Feb 2025 15:33:01 +1100 Subject: [PATCH 5/6] chore: plenary tests POC: add make entry, specify tests --- tests/unit/utils_spec.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/unit/utils_spec.lua b/tests/unit/utils_spec.lua index 820030e2a3c..0e03d451028 100644 --- a/tests/unit/utils_spec.lua +++ b/tests/unit/utils_spec.lua @@ -5,9 +5,6 @@ local stub = require("luassert.stub") local utils = require("nvim-tree.utils") describe("utils.path_add_trailing", function() - before_each(function() - end) - it("trailing added", function() assert.equals("foo/", utils.path_add_trailing("foo")) end) @@ -22,9 +19,6 @@ describe("utils.canonical_path", function() stub(vim.fn, "has") end) - after_each(function() - end) - it("is windows", function() vim.fn.has.on_call_with("win32unix").returns(1) assert.equals("C:\\foo\\bar", utils.canonical_path("c:\\foo\\bar"), "should be uppercase drive") From 91e6b978e003fb03fd76c1697dd6b5be7eaafa12 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 3 Feb 2025 16:01:13 +1100 Subject: [PATCH 6/6] chore: plenary tests POC: add make entry, specify tests --- Makefile | 2 +- scripts/test.sh | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e412d9a987d..0f164c70b5f 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ help-check: help-update # test # test: plenary.nvim - scripts/test.sh $$TEST_NAME + scripts/test.sh # # Dependencies diff --git a/scripts/test.sh b/scripts/test.sh index 3ff47543e86..2694968cd43 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -10,11 +10,13 @@ export DIR_PLENARY if [ "${#}" -eq 1 ]; then TEST_NAME="${1}" -else +elif [ -z "${TEST_NAME}" ]; then TEST_NAME="tests" fi export TEST_NAME +echo "testing: ${TEST_NAME}" + nvim --headless \ --clean \ -u "${DIR_REPO}/tests/minimal_init.lua" \