From c0c021508879c338636646d74d661005295b52e2 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 15:36:13 -0500 Subject: [PATCH 01/16] test(refactor): place git_harness in a test util directory --- tests/branch_popup_spec.lua | 2 +- tests/git_cli_spec.lua | 2 +- tests/status_buf_spec.lua | 28 ++++++++++++++-------------- tests/{ => util}/git_harness.lua | 15 ++++++++++----- 4 files changed, 26 insertions(+), 21 deletions(-) rename tests/{ => util}/git_harness.lua (90%) diff --git a/tests/branch_popup_spec.lua b/tests/branch_popup_spec.lua index d78321720..ebda56cf5 100644 --- a/tests/branch_popup_spec.lua +++ b/tests/branch_popup_spec.lua @@ -1,7 +1,7 @@ require("plenary.async").tests.add_to_env() local eq = assert.are.same local operations = require("neogit.operations") -local harness = require("tests.git_harness") +local harness = require("tests.util.git_harness") local in_prepared_repo = harness.in_prepared_repo local get_current_branch = harness.get_current_branch diff --git a/tests/git_cli_spec.lua b/tests/git_cli_spec.lua index 0048af269..f446b8f64 100644 --- a/tests/git_cli_spec.lua +++ b/tests/git_cli_spec.lua @@ -1,6 +1,6 @@ local eq = assert.are.same local git_cli = require("neogit.lib.git.cli") -local git_harness = require("tests.git_harness") +local git_harness = require("tests.util.git_harness") local in_prepared_repo = git_harness.in_prepared_repo describe("git cli", function() diff --git a/tests/status_buf_spec.lua b/tests/status_buf_spec.lua index 5d148c7ac..2f40633eb 100644 --- a/tests/status_buf_spec.lua +++ b/tests/status_buf_spec.lua @@ -1,6 +1,6 @@ local eq = assert.are.same local status = require("neogit.status") -local harness = require("tests.git_harness") +local harness = require("tests.util.git_harness") local _ = require("tests.mocks.input") local in_prepared_repo = harness.in_prepared_repo local get_git_status = harness.get_git_status @@ -60,8 +60,8 @@ describe("status buffer", function() -It exists so it can be manipulated by the test suite. +This is a change made to a tracked file. Here are some lines we can change during the tests. - - + + ]], get_git_diff("a.txt", "--cached") ) @@ -78,7 +78,7 @@ describe("status buffer", function() [[--- a/a.txt +++ b/a.txt @@ -7,4 +7,5 @@ Here are some lines we can change during the tests. - + This is a second block of text to create a second hunk. It also has some line we can manipulate. +Adding a new line right here! @@ -103,8 +103,8 @@ describe("status buffer", function() It exists so it can be manipulated by the test suite. +This is a change made to a tracked file. Here are some lines we can change during the tests. - - + + ]], get_git_diff("a.txt", "--cached") ) @@ -156,8 +156,8 @@ describe("status buffer", function() This is another test file. +Changes here! This way, unstaging staged changes can be tested. - - + + ]], get_git_diff("b.txt") ) @@ -205,7 +205,7 @@ describe("status buffer", function() [[--- a/a.txt +++ b/a.txt @@ -7,4 +7,5 @@ Here are some lines we can change during the tests. - + This is a second block of text to create a second hunk. It also has some line we can manipulate. +Adding a new line right here! @@ -229,10 +229,10 @@ describe("status buffer", function() This is a text file under version control. -It exists so it can be manipulated by the test suite. Here are some lines we can change during the tests. - - + + @@ -7,4 +6,5 @@ Here are some lines we can change during the tests. - + This is a second block of text to create a second hunk. It also has some line we can manipulate. +Adding a new line right here! @@ -294,8 +294,8 @@ describe("status buffer", function() This is another test file. -It will have staged changes. This way, unstaging staged changes can be tested. - - + + @@ -7,3 +6,4 @@ This way, unstaging staged changes can be tested. Some more lines down here to force a second hunk. I can't think of anything else. diff --git a/tests/git_harness.lua b/tests/util/git_harness.lua similarity index 90% rename from tests/git_harness.lua rename to tests/util/git_harness.lua index 3f5f7ab4f..2b31ddc49 100644 --- a/tests/git_harness.lua +++ b/tests/util/git_harness.lua @@ -16,7 +16,11 @@ local function random_string(length) return res end -local function prepare_repository(dir) +function M.prepare_repository(dir) + if dir == nil then + dir = "/tmp/neogit_test_" .. random_string(5) + end + vim.api.nvim_set_current_dir(project_dir) local cmd = string.format( [[ @@ -29,9 +33,11 @@ cp -r %s/.git.orig/ %s/.git/ ) vim.fn.system(cmd) vim.api.nvim_set_current_dir(dir) + + return dir end -local function cleanup_repository(dir) +function M.cleanup_repository(dir) vim.api.nvim_set_current_dir(project_dir) vim.fn.system(string.format([[ rm -rf %s ]], dir)) @@ -39,13 +45,12 @@ end function M.in_prepared_repo(cb) return function() - local dir = "/tmp/neogit_test_" .. random_string(5) - prepare_repository(dir) + local dir = M.prepare_repository() require("neogit").setup() vim.cmd("Neogit") a.util.block_on(status.reset) local _, err = pcall(cb, dir) - cleanup_repository(dir) + M.cleanup_repository(dir) if err ~= nil then error(err) end From 842cf4ea1fef9afe34ee03c3040e019cd0a69fc9 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 15:54:03 -0500 Subject: [PATCH 02/16] test(revert): undo changes to status_buf_spec whitespace --- tests/status_buf_spec.lua | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/status_buf_spec.lua b/tests/status_buf_spec.lua index 2f40633eb..00c1f45dc 100644 --- a/tests/status_buf_spec.lua +++ b/tests/status_buf_spec.lua @@ -60,8 +60,8 @@ describe("status buffer", function() -It exists so it can be manipulated by the test suite. +This is a change made to a tracked file. Here are some lines we can change during the tests. - - + + ]], get_git_diff("a.txt", "--cached") ) @@ -78,7 +78,7 @@ describe("status buffer", function() [[--- a/a.txt +++ b/a.txt @@ -7,4 +7,5 @@ Here are some lines we can change during the tests. - + This is a second block of text to create a second hunk. It also has some line we can manipulate. +Adding a new line right here! @@ -103,8 +103,8 @@ describe("status buffer", function() It exists so it can be manipulated by the test suite. +This is a change made to a tracked file. Here are some lines we can change during the tests. - - + + ]], get_git_diff("a.txt", "--cached") ) @@ -156,8 +156,8 @@ describe("status buffer", function() This is another test file. +Changes here! This way, unstaging staged changes can be tested. - - + + ]], get_git_diff("b.txt") ) @@ -205,7 +205,7 @@ describe("status buffer", function() [[--- a/a.txt +++ b/a.txt @@ -7,4 +7,5 @@ Here are some lines we can change during the tests. - + This is a second block of text to create a second hunk. It also has some line we can manipulate. +Adding a new line right here! @@ -229,10 +229,10 @@ describe("status buffer", function() This is a text file under version control. -It exists so it can be manipulated by the test suite. Here are some lines we can change during the tests. - - + + @@ -7,4 +6,5 @@ Here are some lines we can change during the tests. - + This is a second block of text to create a second hunk. It also has some line we can manipulate. +Adding a new line right here! @@ -294,8 +294,8 @@ describe("status buffer", function() This is another test file. -It will have staged changes. This way, unstaging staged changes can be tested. - - + + @@ -7,3 +6,4 @@ This way, unstaging staged changes can be tested. Some more lines down here to force a second hunk. I can't think of anything else. From 85d910e334fe71200a4c30e28c162584dd20df0a Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 16:04:33 -0500 Subject: [PATCH 03/16] test(feat): add tests for local branch detection for lib.git.branch --- tests/git_branch_spec.lua | 52 +++++++++++++++++++++++++++++++++++++++ tests/util/util.lua | 21 ++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tests/git_branch_spec.lua create mode 100644 tests/util/util.lua diff --git a/tests/git_branch_spec.lua b/tests/git_branch_spec.lua new file mode 100644 index 000000000..53ba52799 --- /dev/null +++ b/tests/git_branch_spec.lua @@ -0,0 +1,52 @@ +local gb = require("neogit.lib.git.branch") +local status = require("neogit.status") +local plenary_async = require("plenary.async") +local git_harness = require("tests.util.git_harness") +local util = require("tests.util.util") + +describe("git branch lib", function() + describe("local branches", function() + local branches = {} + local repo_dir = nil + + before_each(function() + repo_dir = git_harness.prepare_repository() + plenary_async.util.block_on(status.reset) + + branches = { + "test-branch", + "tester", + "test/some-issue", + "num-branch=123", + "deeply/nested/branch/name", + } + + for _, branch in ipairs(branches) do + vim.fn.system("git branch " .. branch) + + if vim.v.shell_error ~= 0 then + error("Unable to create testing branch: " .. branch) + end + end + + table.insert(branches, "master") + table.insert(branches, "second-branch") + + require("neogit").setup() + + print("Branches:\n " .. vim.inspect(branches)) + end) + + it("properly detects all local branches", function() + local branches_detected = gb.get_local_branches(true) + print("Branches Detected:\n " .. vim.inspect(branches_detected)) + assert.True(util.lists_equal(branches, branches_detected)) + end) + + after_each(function() + git_harness.cleanup_repository(repo_dir) + repo_dir = nil + branches = {} + end) + end) +end) diff --git a/tests/util/util.lua b/tests/util/util.lua new file mode 100644 index 000000000..70db68264 --- /dev/null +++ b/tests/util/util.lua @@ -0,0 +1,21 @@ +local M = {} + +---Checks if both lists contain the same values. This does NOT check ordering. +---@param l1 any[] +---@param l2 any[] +---@return boolean +function M.lists_equal(l1, l2) + if #l1 ~= #l2 then + return false + end + + for _, value in ipairs(l1) do + if not vim.tbl_contains(l2, value) then + return false + end + end + + return true +end + +return M From 00983e343fb698debee3b9e162f47cdc31dfe1b3 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 16:16:22 -0500 Subject: [PATCH 04/16] 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 3ff51a792..b48f3718e 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 62fe24df5..ded718a6f 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 2cb1f501a..000000000 --- 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 7b36aa8e4..d65f20d8b 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 2b31ddc49..a0da6170e 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 70db68264..5dab553b7 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 From f9373e8633492e7cb86d0fbd24292a798f91848b Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 17:12:12 -0500 Subject: [PATCH 05/16] test(feat): add utility to remove a given value from a table --- tests/util/util.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/util/util.lua b/tests/util/util.lua index 5dab553b7..faa0a91e6 100644 --- a/tests/util/util.lua +++ b/tests/util/util.lua @@ -20,6 +20,17 @@ function M.lists_equal(l1, l2) return true end +---Removes the given value from the table +---@param tbl table +---@param value any +function M.remove_item_from_table(tbl, value) + for index, t_value in ipairs(tbl) do + if vim.deep_equal(t_value, value) then + table.remove(tbl, index) + end + end +end + ---Returns the path to the raw test files directory ---@return string function M.get_test_files_dir() From 8924bc5acfaae8f5cf2b553908b25c0ea8abba94 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 17:13:00 -0500 Subject: [PATCH 06/16] test(feat): add test to detect all but current branch --- tests/specs/neogit/lib/git/branch_spec.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/specs/neogit/lib/git/branch_spec.lua b/tests/specs/neogit/lib/git/branch_spec.lua index 53ba52799..df66c156a 100644 --- a/tests/specs/neogit/lib/git/branch_spec.lua +++ b/tests/specs/neogit/lib/git/branch_spec.lua @@ -33,12 +33,24 @@ describe("git branch lib", function() table.insert(branches, "second-branch") require("neogit").setup() - - print("Branches:\n " .. vim.inspect(branches)) end) it("properly detects all local branches", function() local branches_detected = gb.get_local_branches(true) + print("Branches:\n " .. vim.inspect(branches)) + print("Branches Detected:\n " .. vim.inspect(branches_detected)) + assert.True(util.lists_equal(branches, branches_detected)) + end) + + it("properly detects all branches but the current branch", function() + vim.fn.system("git checkout master") + if vim.v.shell_error ~= 0 then + error("Failed to checkout master branch!") + end + util.remove_item_from_table(branches, "master") + + local branches_detected = gb.get_local_branches(false) + print("Branches:\n " .. vim.inspect(branches)) print("Branches Detected:\n " .. vim.inspect(branches_detected)) assert.True(util.lists_equal(branches, branches_detected)) end) From 57524eb0ccbe4244ddd84b549da45e5918035802 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 19:03:57 -0500 Subject: [PATCH 07/16] =?UTF-8?q?test(feat):=20use=20a=20bare=20repo=20?= =?UTF-8?q?=E2=9A=A1=F0=9F=94=A5=F0=9F=94=A5=F0=9F=94=A5=E2=9A=A1=20@CKolk?= =?UTF-8?q?ey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/util/git_harness.lua | 76 +++++++++++++++++++++----------------- tests/util/util.lua | 19 +++++++++- 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/tests/util/git_harness.lua b/tests/util/git_harness.lua index a0da6170e..7c28e2c8c 100644 --- a/tests/util/git_harness.lua +++ b/tests/util/git_harness.lua @@ -1,46 +1,57 @@ local status = require("neogit.status") local a = require("plenary.async") local M = {} +local util = require("tests.util.util") -local project_dir = require("tests.util.util").project_dir +local project_dir = util.project_dir +local bare_repo_path = nil --- very naiive implementation, we only use this to generate unique folder names -local function random_string(length) - math.randomseed(os.clock() ^ 5) - - local res = "" - for _ = 1, length do - local r = math.random(97, 122) - res = res .. string.char(r) - end - return res -end - -function M.prepare_repository(dir) - if dir == nil then - dir = "/tmp/neogit_test_" .. random_string(5) +function M.setup_bare_repo() + if bare_repo_path ~= nil then + return bare_repo_path end + local workspace_dir = M.create_temp_dir() + print("BASE DIR: " .. workspace_dir) vim.api.nvim_set_current_dir(project_dir) - local cmd = string.format( - [[ -cp -r tests/.repo/ %s -cp -r %s/.git.orig/ %s/.git/ -]], - dir, - dir, - dir - ) - vim.fn.system(cmd) - vim.api.nvim_set_current_dir(dir) - - return dir + util.system("cp -r tests/.repo " .. workspace_dir) + vim.api.nvim_set_current_dir(workspace_dir) + util.system([[ + mv ./.repo/.git.orig ./.git + mv ./.repo/* . + git config user.email "test@neogit-test.test" + git config user.name "Neogit Test" + git add . + git commit -m "temp commit to be soft unstaged later" + ]]) + + bare_repo_path = M.create_temp_dir() + + print("BARE DIR: " .. bare_repo_path) + util.system(string.format("git clone --bare %s %s", workspace_dir, bare_repo_path)) + + return bare_repo_path end -function M.cleanup_repository(dir) - vim.api.nvim_set_current_dir(project_dir) +function M.create_temp_dir() + local tmp_dir = vim.trim(util.system("mktemp -d")) + return tmp_dir +end + +function M.prepare_repository() + M.setup_bare_repo() + + local working_dir = M.create_temp_dir() + vim.api.nvim_set_current_dir(working_dir) + util.system(string.format("git clone %s %s", bare_repo_path, working_dir)) + util.system("git reset --soft HEAD~1") + util.system("git rm --cached untracked.txt") + util.system("git restore --staged a.txt") + util.system("git checkout second-branch") + util.system("git switch master") + print("WORKING DIRECTORY: " .. working_dir) - vim.fn.system(string.format([[ rm -rf %s ]], dir)) + return working_dir end function M.in_prepared_repo(cb) @@ -50,7 +61,6 @@ function M.in_prepared_repo(cb) vim.cmd("Neogit") a.util.block_on(status.reset) local _, err = pcall(cb, dir) - M.cleanup_repository(dir) if err ~= nil then error(err) end diff --git a/tests/util/util.lua b/tests/util/util.lua index faa0a91e6..02b257007 100644 --- a/tests/util/util.lua +++ b/tests/util/util.lua @@ -32,9 +32,26 @@ function M.remove_item_from_table(tbl, value) end ---Returns the path to the raw test files directory ----@return string +---@return string The path to the project directory function M.get_test_files_dir() return M.project_dir .. "/tests/test_files/" end +---Runs a system command and errors if it fails +---@param cmd string Command to be ran +---@param ignore_err boolean? Whether the error should be ignored +---@param error_msg string? The error message to be emitted on command failure +---@return string The output of the system command +function M.system(cmd, ignore_err, error_msg) + if ignore_err ~= nil then + ignore_err = false + end + + local output = vim.fn.system(cmd) + if vim.v.shell_error ~= 0 and not ignore_err then + error(error_msg or ("Command failed: ↓\n" .. cmd .. "\nOutput from command: ↓\n" .. output)) + end + return output +end + return M From 1efbdb5a20c38e60161158ad608fcb0286e6a284 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 19:10:16 -0500 Subject: [PATCH 08/16] test(refactor): remove invalid cleanup for branch_spec --- tests/specs/neogit/lib/git/branch_spec.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/specs/neogit/lib/git/branch_spec.lua b/tests/specs/neogit/lib/git/branch_spec.lua index df66c156a..1bba535ea 100644 --- a/tests/specs/neogit/lib/git/branch_spec.lua +++ b/tests/specs/neogit/lib/git/branch_spec.lua @@ -56,7 +56,6 @@ describe("git branch lib", function() end) after_each(function() - git_harness.cleanup_repository(repo_dir) repo_dir = nil branches = {} end) From ef1fadc61ebfc2013e46151becdb0128b8cd4109 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 19:12:43 -0500 Subject: [PATCH 09/16] test(refactor): remove useless `repo_dir` var --- tests/specs/neogit/lib/git/branch_spec.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/specs/neogit/lib/git/branch_spec.lua b/tests/specs/neogit/lib/git/branch_spec.lua index 1bba535ea..21d275471 100644 --- a/tests/specs/neogit/lib/git/branch_spec.lua +++ b/tests/specs/neogit/lib/git/branch_spec.lua @@ -7,10 +7,9 @@ local util = require("tests.util.util") describe("git branch lib", function() describe("local branches", function() local branches = {} - local repo_dir = nil before_each(function() - repo_dir = git_harness.prepare_repository() + git_harness.prepare_repository() plenary_async.util.block_on(status.reset) branches = { @@ -56,7 +55,6 @@ describe("git branch lib", function() end) after_each(function() - repo_dir = nil branches = {} end) end) From ffa6434626f236496978760b25c869909431728b Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 19:31:34 -0500 Subject: [PATCH 10/16] test(refactor): extract create_temp_dir to utils, set its base path --- tests/util/git_harness.lua | 11 +++-------- tests/util/util.lua | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/tests/util/git_harness.lua b/tests/util/git_harness.lua index 7c28e2c8c..cd95fa405 100644 --- a/tests/util/git_harness.lua +++ b/tests/util/git_harness.lua @@ -11,7 +11,7 @@ function M.setup_bare_repo() return bare_repo_path end - local workspace_dir = M.create_temp_dir() + local workspace_dir = util.create_temp_dir() print("BASE DIR: " .. workspace_dir) vim.api.nvim_set_current_dir(project_dir) util.system("cp -r tests/.repo " .. workspace_dir) @@ -25,7 +25,7 @@ function M.setup_bare_repo() git commit -m "temp commit to be soft unstaged later" ]]) - bare_repo_path = M.create_temp_dir() + bare_repo_path = util.create_temp_dir() print("BARE DIR: " .. bare_repo_path) util.system(string.format("git clone --bare %s %s", workspace_dir, bare_repo_path)) @@ -33,15 +33,10 @@ function M.setup_bare_repo() return bare_repo_path end -function M.create_temp_dir() - local tmp_dir = vim.trim(util.system("mktemp -d")) - return tmp_dir -end - function M.prepare_repository() M.setup_bare_repo() - local working_dir = M.create_temp_dir() + local working_dir = util.create_temp_dir() vim.api.nvim_set_current_dir(working_dir) util.system(string.format("git clone %s %s", bare_repo_path, working_dir)) util.system("git reset --soft HEAD~1") diff --git a/tests/util/util.lua b/tests/util/util.lua index 02b257007..7ea0dd473 100644 --- a/tests/util/util.lua +++ b/tests/util/util.lua @@ -38,7 +38,7 @@ function M.get_test_files_dir() end ---Runs a system command and errors if it fails ----@param cmd string Command to be ran +---@param cmd string | table Command to be ran ---@param ignore_err boolean? Whether the error should be ignored ---@param error_msg string? The error message to be emitted on command failure ---@return string The output of the system command @@ -54,4 +54,24 @@ function M.system(cmd, ignore_err, error_msg) return output end +M.neogit_test_base_dir = "/tmp/neogit-testing/" + +---Create a temporary directory for use +---@param suffix string? The suffix to be appended to the temp directory, ideally avoid spaces in your suffix +---@return string The path to the temporary directory +function M.create_temp_dir(suffix) + if suffix == nil then + suffix = "" + else + suffix = "-" .. suffix + end + + if not vim.loop.fs_stat("/tmp/neogit-testing") then + M.system("mkdir " .. M.neogit_test_base_dir) + end + local tmp_dir = + vim.trim(M.system(string.format("mktemp -d --suffix=%s -p %s", suffix, M.neogit_test_base_dir))) + return tmp_dir +end + return M From 2fcb791d7c216419fca056dbd9876e79474b80dc Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 19:33:00 -0500 Subject: [PATCH 11/16] test(refactor): use temp base path for dep installation --- tests/init.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/init.lua b/tests/init.lua index ded718a6f..b1c2fcea2 100644 --- a/tests/init.lua +++ b/tests/init.lua @@ -1,7 +1,9 @@ +local util = require("tests.util.util") + local function ensure_installed(repo) local name = repo:match(".+/(.+)$") - local install_path = "/tmp/neogit/" .. name + local install_path = util.neogit_test_base_dir .. name vim.opt.runtimepath:prepend(install_path) From bd47251923e98fe79c8ca3bf58e56b15c6b9a73b Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 19:36:20 -0500 Subject: [PATCH 12/16] test(refactor): set suffixes for working, base, bare dirs --- tests/util/git_harness.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/util/git_harness.lua b/tests/util/git_harness.lua index cd95fa405..4837e97d1 100644 --- a/tests/util/git_harness.lua +++ b/tests/util/git_harness.lua @@ -11,7 +11,7 @@ function M.setup_bare_repo() return bare_repo_path end - local workspace_dir = util.create_temp_dir() + local workspace_dir = util.create_temp_dir("base-dir") print("BASE DIR: " .. workspace_dir) vim.api.nvim_set_current_dir(project_dir) util.system("cp -r tests/.repo " .. workspace_dir) @@ -25,7 +25,7 @@ function M.setup_bare_repo() git commit -m "temp commit to be soft unstaged later" ]]) - bare_repo_path = util.create_temp_dir() + bare_repo_path = util.create_temp_dir("bare-dir") print("BARE DIR: " .. bare_repo_path) util.system(string.format("git clone --bare %s %s", workspace_dir, bare_repo_path)) @@ -36,7 +36,7 @@ end function M.prepare_repository() M.setup_bare_repo() - local working_dir = util.create_temp_dir() + local working_dir = util.create_temp_dir("working-dir") vim.api.nvim_set_current_dir(working_dir) util.system(string.format("git clone %s %s", bare_repo_path, working_dir)) util.system("git reset --soft HEAD~1") From 251b951fdd835f1c0553ef86b3e5b6a74944fe02 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sun, 16 Jul 2023 07:06:32 -0500 Subject: [PATCH 13/16] test(refactor): remove after function for branch_spec --- tests/specs/neogit/lib/git/branch_spec.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/specs/neogit/lib/git/branch_spec.lua b/tests/specs/neogit/lib/git/branch_spec.lua index 21d275471..908585832 100644 --- a/tests/specs/neogit/lib/git/branch_spec.lua +++ b/tests/specs/neogit/lib/git/branch_spec.lua @@ -53,9 +53,5 @@ describe("git branch lib", function() print("Branches Detected:\n " .. vim.inspect(branches_detected)) assert.True(util.lists_equal(branches, branches_detected)) end) - - after_each(function() - branches = {} - end) end) end) From 0c03aef7ba64a24a5269621283907a78fee44581 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sun, 16 Jul 2023 07:16:36 -0500 Subject: [PATCH 14/16] test(refactor): extract branch creation to small helper function This makes it a bit easier to parse the exact test setup calls. --- tests/specs/neogit/lib/git/branch_spec.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/specs/neogit/lib/git/branch_spec.lua b/tests/specs/neogit/lib/git/branch_spec.lua index 908585832..377c60b4f 100644 --- a/tests/specs/neogit/lib/git/branch_spec.lua +++ b/tests/specs/neogit/lib/git/branch_spec.lua @@ -8,10 +8,7 @@ describe("git branch lib", function() describe("local branches", function() local branches = {} - before_each(function() - git_harness.prepare_repository() - plenary_async.util.block_on(status.reset) - + local function setup_local_git_branches() branches = { "test-branch", "tester", @@ -30,7 +27,12 @@ describe("git branch lib", function() table.insert(branches, "master") table.insert(branches, "second-branch") + end + before_each(function() + git_harness.prepare_repository() + plenary_async.util.block_on(status.reset) + setup_local_git_branches() require("neogit").setup() end) From 0fdf34274e6e96c1a8aa168e5b3ba42c662aada9 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sun, 16 Jul 2023 13:55:40 -0500 Subject: [PATCH 15/16] test(fix): properly set ignore_err for M.system --- tests/util/util.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/util/util.lua b/tests/util/util.lua index 7ea0dd473..ebcfbab73 100644 --- a/tests/util/util.lua +++ b/tests/util/util.lua @@ -43,7 +43,7 @@ end ---@param error_msg string? The error message to be emitted on command failure ---@return string The output of the system command function M.system(cmd, ignore_err, error_msg) - if ignore_err ~= nil then + if ignore_err == nil then ignore_err = false end From 7e018d68e5564905f046221fb9756eb40d04a97f Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sun, 16 Jul 2023 14:09:39 -0500 Subject: [PATCH 16/16] refactor: extract some test utilites to neogit primary `neogit.lib.util` --- lua/neogit/lib/util.lua | 31 ++++++++++++++++++++++ tests/specs/neogit/lib/git/branch_spec.lua | 8 +++--- tests/util/util.lua | 29 -------------------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/lua/neogit/lib/util.lua b/lua/neogit/lib/util.lua index 42a8d418a..fe7fb3e5e 100644 --- a/lua/neogit/lib/util.lua +++ b/lua/neogit/lib/util.lua @@ -249,6 +249,35 @@ local function build_reverse_lookup(tbl) return result end +---Removes the given value from the table +---@param tbl table +---@param value any +local function remove_item_from_table(tbl, value) + for index, t_value in ipairs(tbl) do + if vim.deep_equal(t_value, value) then + table.remove(tbl, index) + end + end +end + +---Checks if both lists contain the same values. This does NOT check ordering. +---@param l1 any[] +---@param l2 any[] +---@return boolean +local function lists_equal(l1, l2) + if #l1 ~= #l2 then + return false + end + + for _, value in ipairs(l1) do + if not vim.tbl_contains(l2, value) then + return false + end + end + + return true +end + return { time = time, time_async = time_async, @@ -276,4 +305,6 @@ return { merge = merge, str_min_width = str_min_width, str_clamp = str_clamp, + remove_item_from_table = remove_item_from_table, + lists_equal = lists_equal, } diff --git a/tests/specs/neogit/lib/git/branch_spec.lua b/tests/specs/neogit/lib/git/branch_spec.lua index 377c60b4f..33bfb3ddc 100644 --- a/tests/specs/neogit/lib/git/branch_spec.lua +++ b/tests/specs/neogit/lib/git/branch_spec.lua @@ -2,7 +2,7 @@ local gb = require("neogit.lib.git.branch") local status = require("neogit.status") local plenary_async = require("plenary.async") local git_harness = require("tests.util.git_harness") -local util = require("tests.util.util") +local neogit_util = require("neogit.lib.util") describe("git branch lib", function() describe("local branches", function() @@ -40,7 +40,7 @@ describe("git branch lib", function() local branches_detected = gb.get_local_branches(true) print("Branches:\n " .. vim.inspect(branches)) print("Branches Detected:\n " .. vim.inspect(branches_detected)) - assert.True(util.lists_equal(branches, branches_detected)) + assert.True(neogit_util.lists_equal(branches, branches_detected)) end) it("properly detects all branches but the current branch", function() @@ -48,12 +48,12 @@ describe("git branch lib", function() if vim.v.shell_error ~= 0 then error("Failed to checkout master branch!") end - util.remove_item_from_table(branches, "master") + neogit_util.remove_item_from_table(branches, "master") local branches_detected = gb.get_local_branches(false) print("Branches:\n " .. vim.inspect(branches)) print("Branches Detected:\n " .. vim.inspect(branches_detected)) - assert.True(util.lists_equal(branches, branches_detected)) + assert.True(neogit_util.lists_equal(branches, branches_detected)) end) end) end) diff --git a/tests/util/util.lua b/tests/util/util.lua index ebcfbab73..729168057 100644 --- a/tests/util/util.lua +++ b/tests/util/util.lua @@ -2,35 +2,6 @@ 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[] ----@return boolean -function M.lists_equal(l1, l2) - if #l1 ~= #l2 then - return false - end - - for _, value in ipairs(l1) do - if not vim.tbl_contains(l2, value) then - return false - end - end - - return true -end - ----Removes the given value from the table ----@param tbl table ----@param value any -function M.remove_item_from_table(tbl, value) - for index, t_value in ipairs(tbl) do - if vim.deep_equal(t_value, value) then - table.remove(tbl, index) - end - end -end - ---Returns the path to the raw test files directory ---@return string The path to the project directory function M.get_test_files_dir()