From 86a8fe4af719fca771d993fcce54c15cd7414cf9 Mon Sep 17 00:00:00 2001 From: Tama McGlinn Date: Wed, 5 May 2021 20:49:50 +0200 Subject: [PATCH] Added failing unit test for most common case; switching while file not in cwd --- tests/git_harness.lua | 29 +++++++++++++++++++++++++ tests/worktree_spec.lua | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/tests/git_harness.lua b/tests/git_harness.lua index bbb9490..93ec532 100644 --- a/tests/git_harness.lua +++ b/tests/git_harness.lua @@ -257,6 +257,35 @@ M.in_bare_repo_from_origin_2_worktrees = function(cb) end end +M.in_bare_repo_from_origin_3_worktrees = function(cb) + return function() + local random_id = random_string() + local origin_repo_dir = 'git_worktree_test_origin_repo_' .. random_id + local bare_repo_dir = 'git_worktree_test_repo_' .. random_id + + config_git_worktree() + cleanup_repos() + + prepare_origin_repo(origin_repo_dir) + prepare_bare_repo(bare_repo_dir, origin_repo_dir) + change_dir(bare_repo_dir) + create_worktree('featB','featB') + create_worktree('featC','featC') + create_worktree('featD','featD') + + local _, err = pcall(cb) + + reset_cwd() + + cleanup_repos() + + if err ~= nil then + error(err) + end + + end +end + M.in_repo_from_origin_2_worktrees = function(cb) return function() local random_id = random_string() diff --git a/tests/worktree_spec.lua b/tests/worktree_spec.lua index 9d3b9e4..fa09e85 100644 --- a/tests/worktree_spec.lua +++ b/tests/worktree_spec.lua @@ -9,6 +9,7 @@ local in_bare_repo_from_origin_1_worktree = harness.in_bare_repo_from_origin_1_w local in_repo_from_origin_1_worktree = harness.in_repo_from_origin_1_worktree local in_repo_from_local_no_worktrees = harness.in_repo_from_local_no_worktrees local in_bare_repo_from_origin_2_worktrees = harness.in_bare_repo_from_origin_2_worktrees +local in_bare_repo_from_origin_3_worktrees = harness.in_bare_repo_from_origin_3_worktrees local in_repo_from_origin_2_worktrees = harness.in_repo_from_origin_2_worktrees local check_git_worktree_exists = harness.check_git_worktree_exists local check_branch_upstream = harness.check_branch_upstream @@ -484,6 +485,53 @@ describe('git-worktree', function() assert.True(featC_abs_A_path == get_current_file()) end)) + it('in a featB worktree(bare) with file A inside featC open, switch to featD and switch to file A in other worktree', + in_bare_repo_from_origin_3_worktrees(function() + + local featB_path = "featB" + local featB_abs_path = git_worktree:get_root() .. Path.path.sep .. featB_path + local featB_abs_A_path = featB_abs_path .. Path.path.sep .. "A.txt" + + local featC_path = "featC" + local featC_abs_path = git_worktree:get_root() .. Path.path.sep .. featC_path + local featC_abs_A_path = featC_abs_path .. Path.path.sep .. "A.txt" + + local featD_path = "featD" + local featD_abs_path = git_worktree:get_root() .. Path.path.sep .. featD_path + local featD_abs_A_path = featD_abs_path .. Path.path.sep .. "A.txt" + + -- switch to featB worktree + git_worktree.switch_worktree(featB_path) + + vim.fn.wait( + 10000, + function() + return completed_switch + end, + 1000 + ) + + -- open A file + vim.cmd("e ../featC/A.txt") + -- make sure it is opened + assert.True(featC_abs_A_path == get_current_file()) + + -- switch to featD worktree + reset_variables() + git_worktree.switch_worktree(featD_path) + + vim.fn.wait( + 10000, + function() + return completed_switch + end, + 1000 + ) + + -- make sure it switch to file in other tree + assert.True(featD_abs_A_path == get_current_file()) + end)) + it('in a featB worktree(non bare) with file A open, switch to featC and switch to file A in other worktree', in_repo_from_origin_2_worktrees(function()