From 2dcf249d497041fb2043d1c41cedbf75afed9eab Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 1 Feb 2025 18:05:23 +1100 Subject: [PATCH] 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 194845f5..f2bed188 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 9c2b18db..4666e2ae 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 0678145c..2d9fce69 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 51fb50d4..924834f8 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)