Skip to content

Commit 86a8fe4

Browse files
committed
Added failing unit test for most common case; switching while file not in cwd
1 parent 4b990cc commit 86a8fe4

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

tests/git_harness.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,35 @@ M.in_bare_repo_from_origin_2_worktrees = function(cb)
257257
end
258258
end
259259

260+
M.in_bare_repo_from_origin_3_worktrees = function(cb)
261+
return function()
262+
local random_id = random_string()
263+
local origin_repo_dir = 'git_worktree_test_origin_repo_' .. random_id
264+
local bare_repo_dir = 'git_worktree_test_repo_' .. random_id
265+
266+
config_git_worktree()
267+
cleanup_repos()
268+
269+
prepare_origin_repo(origin_repo_dir)
270+
prepare_bare_repo(bare_repo_dir, origin_repo_dir)
271+
change_dir(bare_repo_dir)
272+
create_worktree('featB','featB')
273+
create_worktree('featC','featC')
274+
create_worktree('featD','featD')
275+
276+
local _, err = pcall(cb)
277+
278+
reset_cwd()
279+
280+
cleanup_repos()
281+
282+
if err ~= nil then
283+
error(err)
284+
end
285+
286+
end
287+
end
288+
260289
M.in_repo_from_origin_2_worktrees = function(cb)
261290
return function()
262291
local random_id = random_string()

tests/worktree_spec.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local in_bare_repo_from_origin_1_worktree = harness.in_bare_repo_from_origin_1_w
99
local in_repo_from_origin_1_worktree = harness.in_repo_from_origin_1_worktree
1010
local in_repo_from_local_no_worktrees = harness.in_repo_from_local_no_worktrees
1111
local in_bare_repo_from_origin_2_worktrees = harness.in_bare_repo_from_origin_2_worktrees
12+
local in_bare_repo_from_origin_3_worktrees = harness.in_bare_repo_from_origin_3_worktrees
1213
local in_repo_from_origin_2_worktrees = harness.in_repo_from_origin_2_worktrees
1314
local check_git_worktree_exists = harness.check_git_worktree_exists
1415
local check_branch_upstream = harness.check_branch_upstream
@@ -484,6 +485,53 @@ describe('git-worktree', function()
484485
assert.True(featC_abs_A_path == get_current_file())
485486
end))
486487

488+
it('in a featB worktree(bare) with file A inside featC open, switch to featD and switch to file A in other worktree',
489+
in_bare_repo_from_origin_3_worktrees(function()
490+
491+
local featB_path = "featB"
492+
local featB_abs_path = git_worktree:get_root() .. Path.path.sep .. featB_path
493+
local featB_abs_A_path = featB_abs_path .. Path.path.sep .. "A.txt"
494+
495+
local featC_path = "featC"
496+
local featC_abs_path = git_worktree:get_root() .. Path.path.sep .. featC_path
497+
local featC_abs_A_path = featC_abs_path .. Path.path.sep .. "A.txt"
498+
499+
local featD_path = "featD"
500+
local featD_abs_path = git_worktree:get_root() .. Path.path.sep .. featD_path
501+
local featD_abs_A_path = featD_abs_path .. Path.path.sep .. "A.txt"
502+
503+
-- switch to featB worktree
504+
git_worktree.switch_worktree(featB_path)
505+
506+
vim.fn.wait(
507+
10000,
508+
function()
509+
return completed_switch
510+
end,
511+
1000
512+
)
513+
514+
-- open A file
515+
vim.cmd("e ../featC/A.txt")
516+
-- make sure it is opened
517+
assert.True(featC_abs_A_path == get_current_file())
518+
519+
-- switch to featD worktree
520+
reset_variables()
521+
git_worktree.switch_worktree(featD_path)
522+
523+
vim.fn.wait(
524+
10000,
525+
function()
526+
return completed_switch
527+
end,
528+
1000
529+
)
530+
531+
-- make sure it switch to file in other tree
532+
assert.True(featD_abs_A_path == get_current_file())
533+
end))
534+
487535
it('in a featB worktree(non bare) with file A open, switch to featC and switch to file A in other worktree',
488536
in_repo_from_origin_2_worktrees(function()
489537

0 commit comments

Comments
 (0)