From 00983e343fb698debee3b9e162f47cdc31dfe1b3 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 16:16:22 -0500 Subject: [PATCH] test(refactor): improve directory layout for tests --- recipes.json | 6 ++-- tests/init.lua | 2 +- tests/poc_spec.lua | 29 ------------------- .../neogit/lib/git/branch_spec.lua} | 0 .../neogit/lib/git/cli_spec.lua} | 0 .../neogit/lib/git/index_spec.lua} | 0 .../neogit/operations_spec.lua} | 0 tests/{ => specs/neogit}/process_spec.lua | 13 ++++++--- .../neogit/status_spec.lua} | 0 tests/{ => test_files}/a.txt | 0 tests/{ => test_files}/output | 0 tests/{ => test_files}/process_test | 0 tests/util/git_harness.lua | 2 +- tests/util/util.lua | 8 +++++ 14 files changed, 22 insertions(+), 38 deletions(-) delete mode 100644 tests/poc_spec.lua rename tests/{git_branch_spec.lua => specs/neogit/lib/git/branch_spec.lua} (100%) rename tests/{git_cli_spec.lua => specs/neogit/lib/git/cli_spec.lua} (100%) rename tests/{hunks_spec.lua => specs/neogit/lib/git/index_spec.lua} (100%) rename tests/{branch_popup_spec.lua => specs/neogit/operations_spec.lua} (100%) rename tests/{ => specs/neogit}/process_spec.lua (85%) rename tests/{status_buf_spec.lua => specs/neogit/status_spec.lua} (100%) rename tests/{ => test_files}/a.txt (100%) rename tests/{ => test_files}/output (100%) rename tests/{ => test_files}/process_test (100%) diff --git a/recipes.json b/recipes.json index 3ff51a79..b48f3718 100644 --- a/recipes.json +++ b/recipes.json @@ -1,6 +1,6 @@ { "test-branches": { - "cmd": "nvim --headless -c \"lua require(\\\"plenary.test_harness\\\").test_directory('tests/branch_popup_spec.lua', {minimal_init = \\\"tests/init.lua\\\", sequential = true })\"", + "cmd": "nvim --headless -c \"lua require(\\\"plenary.test_harness\\\").test_directory('tests/specs/branch_popup_spec.lua', {minimal_init = \\\"tests/init.lua\\\", sequential = true })\"", "kind": "term", "env": { "NEOGIT_LOG_CONSOLE": true, @@ -8,7 +8,7 @@ } }, "test-process": { - "cmd": "nvim --headless -c \"lua require(\\\"plenary.test_harness\\\").test_directory('./tests/process_spec.lua', {minimal_init = \\\"tests/init.lua\\\", sequential = true })\"", + "cmd": "nvim --headless -c \"lua require(\\\"plenary.test_harness\\\").test_directory('./tests/specs/process_spec.lua', {minimal_init = \\\"tests/init.lua\\\", sequential = true })\"", "kind": "term", "env": { "NEOGIT_LOG_CONSOLE": true, @@ -16,7 +16,7 @@ } }, "test-all": { - "cmd": "nvim --headless -c \"lua require(\\\"plenary.test_harness\\\").test_directory('tests', {minimal_init = \\\"tests/init.lua\\\", sequential = true })\"", + "cmd": "nvim --headless -c \"lua require(\\\"plenary.test_harness\\\").test_directory('tests/specs', {minimal_init = \\\"tests/init.lua\\\", sequential = true })\"", "kind": "term", "env": { "NEOGIT_LOG_CONSOLE": true, diff --git a/tests/init.lua b/tests/init.lua index 62fe24df..ded718a6 100644 --- a/tests/init.lua +++ b/tests/init.lua @@ -20,7 +20,7 @@ else ensure_installed("nvim-telescope/telescope.nvim") end -require("plenary.test_harness").test_directory("tests", { +require("plenary.test_harness").test_directory("tests/specs", { minimal_init = "tests/minimal_init.lua", sequential = true, }) diff --git a/tests/poc_spec.lua b/tests/poc_spec.lua deleted file mode 100644 index 2cb1f501..00000000 --- a/tests/poc_spec.lua +++ /dev/null @@ -1,29 +0,0 @@ -local eq = assert.are.same - -describe("proof of concept", function() - it("should work", function() - assert(true) - end) - - it("should have access to vim global", function() - assert.is_not_nil(vim) - end) - - it("should be able to interact with vim", function() - vim.cmd("let g:val = v:true") - eq(true, vim.g.val) - end) - - it("has access to buffers", function() - require("neogit").setup() - vim.cmd("Neogit") - -- 1 is the neogit buffer just opened - -- 2 The hidden console buffer - for _, b in ipairs(vim.api.nvim_list_bufs()) do - local name = vim.api.nvim_buf_get_name(b) - local ft = vim.api.nvim_buf_get_option(b, "filetype") - local buftype = vim.api.nvim_buf_get_option(b, "buftype") - end - eq({ 1, 2 }, vim.api.nvim_list_bufs()) - end) -end) diff --git a/tests/git_branch_spec.lua b/tests/specs/neogit/lib/git/branch_spec.lua similarity index 100% rename from tests/git_branch_spec.lua rename to tests/specs/neogit/lib/git/branch_spec.lua diff --git a/tests/git_cli_spec.lua b/tests/specs/neogit/lib/git/cli_spec.lua similarity index 100% rename from tests/git_cli_spec.lua rename to tests/specs/neogit/lib/git/cli_spec.lua diff --git a/tests/hunks_spec.lua b/tests/specs/neogit/lib/git/index_spec.lua similarity index 100% rename from tests/hunks_spec.lua rename to tests/specs/neogit/lib/git/index_spec.lua diff --git a/tests/branch_popup_spec.lua b/tests/specs/neogit/operations_spec.lua similarity index 100% rename from tests/branch_popup_spec.lua rename to tests/specs/neogit/operations_spec.lua diff --git a/tests/process_spec.lua b/tests/specs/neogit/process_spec.lua similarity index 85% rename from tests/process_spec.lua rename to tests/specs/neogit/process_spec.lua index 7b36aa8e..d65f20d8 100644 --- a/tests/process_spec.lua +++ b/tests/specs/neogit/process_spec.lua @@ -1,11 +1,13 @@ require("plenary.async").tests.add_to_env() +local util = require("tests.util.util") local eq = assert.are.same local process = require("neogit.process") describe("process execution", function() it("basic command", function() - local result = process.new({ cmd = { "cat", "process_test" }, cwd = "./tests" }):spawn_blocking(1000) + local result = + process.new({ cmd = { "cat", "process_test" }, cwd = util.get_test_files_dir() }):spawn_blocking(1000) assert(result) assert.are.same(result.stdout, { "This is a test file", @@ -17,7 +19,8 @@ describe("process execution", function() }) end) it("can cat a file", function() - local result = process.new({ cmd = { "cat", "a.txt" }, cwd = "./tests" }):spawn_blocking(1000) + local result = + process.new({ cmd = { "cat", "a.txt" }, cwd = util.get_test_files_dir() }):spawn_blocking(1000) assert(result) assert.are.same(result.stdout, { @@ -74,8 +77,10 @@ describe("process execution", function() assert.are.same(result.stdout, input) end) it("basic command trim", function() - local result = - process.new({ cmd = { "cat", "process_test" }, cwd = "./tests" }):spawn_blocking(1000):trim() + local result = process + .new({ cmd = { "cat", "process_test" }, cwd = util.get_test_files_dir() }) + :spawn_blocking(1000) + :trim() assert(result) assert.are.same(result.stdout, { "This is a test file", diff --git a/tests/status_buf_spec.lua b/tests/specs/neogit/status_spec.lua similarity index 100% rename from tests/status_buf_spec.lua rename to tests/specs/neogit/status_spec.lua diff --git a/tests/a.txt b/tests/test_files/a.txt similarity index 100% rename from tests/a.txt rename to tests/test_files/a.txt diff --git a/tests/output b/tests/test_files/output similarity index 100% rename from tests/output rename to tests/test_files/output diff --git a/tests/process_test b/tests/test_files/process_test similarity index 100% rename from tests/process_test rename to tests/test_files/process_test diff --git a/tests/util/git_harness.lua b/tests/util/git_harness.lua index 2b31ddc4..a0da6170 100644 --- a/tests/util/git_harness.lua +++ b/tests/util/git_harness.lua @@ -2,7 +2,7 @@ local status = require("neogit.status") local a = require("plenary.async") local M = {} -local project_dir = vim.fn.getcwd() +local project_dir = require("tests.util.util").project_dir -- very naiive implementation, we only use this to generate unique folder names local function random_string(length) diff --git a/tests/util/util.lua b/tests/util/util.lua index 70db6826..5dab553b 100644 --- a/tests/util/util.lua +++ b/tests/util/util.lua @@ -1,5 +1,7 @@ local M = {} +M.project_dir = vim.fn.getcwd() + ---Checks if both lists contain the same values. This does NOT check ordering. ---@param l1 any[] ---@param l2 any[] @@ -18,4 +20,10 @@ function M.lists_equal(l1, l2) return true end +---Returns the path to the raw test files directory +---@return string +function M.get_test_files_dir() + return M.project_dir .. "/tests/test_files/" +end + return M