Skip to content

Commit d852675

Browse files
committed
add test
1 parent fe057aa commit d852675

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

lua/neo-tree/git/init.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ M._parse_porcelain = function(
7272
batch_size,
7373
skip_bubbling
7474
)
75-
local git_root_dir = utils.normalize_path(git_root) .. utils.path_separator
75+
local git_root_dir = utils.normalize_path(git_root)
76+
if not vim.endswith(git_root_dir, utils.path_separator) then
77+
git_root_dir = git_root_dir .. utils.path_separator
78+
end
79+
7680
local num_in_batch = 0
7781
git_status = git_status or {}
7882
if not batch_size or batch_size <= 0 then
@@ -218,7 +222,7 @@ M._parse_porcelain = function(
218222
local s1 = status:sub(1, 1)
219223
local s2 = status:sub(2, 2)
220224
for parent in utils.path_parents(dir, true) do
221-
if parent == git_root then
225+
if parent == git_root or #parent < #git_root then
222226
-- bubble only up to the children of the git root
223227
break
224228
end

tests/neo-tree/git/git_spec.lua

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
local git = require("neo-tree.git")
2+
local utils = require("neo-tree.utils")
3+
local test_utils = require("tests.utils")
4+
local gsplit_plain = vim.fn.has("nvim-0.9") == 1 and { plain = true } or true
5+
describe("git parser", function()
6+
describe("parses v2 output", function()
7+
local porcelain_v2_status = {
8+
"1 D. N... 100644 000000 000000 ade2881afa1dcb156a3aa576024aa0fecf789191 0000000000000000000000000000000000000000 deleted_staged.txt",
9+
"1 .D N... 100644 100644 000000 9c13483e67ceff219800303ec7af39c4f0301a5b 9c13483e67ceff219800303ec7af39c4f0301a5b deleted_unstaged.txt",
10+
"1 MM N... 100644 100644 100644 4417f3aca512ffdf247662e2c611ee03ff9255cc 29c0e9846cd6410a44c4ca3fdaf5623818bd2838 modified_mixed.txt",
11+
"1 M. N... 100644 100644 100644 f784736eecdd43cd8eb665615163cfc6506fca5f 8d6fad5bd11ac45c7c9e62d4db1c427889ed515b modified_staged.txt",
12+
"1 .M N... 100644 100644 100644 c9e1e027aa9430cb4ffccccf45844286d10285c1 c9e1e027aa9430cb4ffccccf45844286d10285c1 modified_unstaged.txt",
13+
"1 A. N... 000000 100644 100644 0000000000000000000000000000000000000000 89cae60d74c222609086441e29985f959b6ec546 new_staged_file.txt",
14+
"2 R. N... 100644 100644 100644 3454a7dc6b93d1098e3c3f3ec369589412abdf99 3454a7dc6b93d1098e3c3f3ec369589412abdf99 R100 renamed_staged_new.txt",
15+
"renamed_staged_old.txt",
16+
"1 .T N... 100644 100644 120000 192f10ed8c11efb70155e8eb4cae6ec677347623 192f10ed8c11efb70155e8eb4cae6ec677347623 type_change.txt",
17+
"? .gitignore",
18+
"? untracked.txt",
19+
}
20+
21+
local test = function()
22+
local old = utils.is_windows
23+
local iter = coroutine.wrap(function()
24+
for i, s in ipairs(porcelain_v2_status) do
25+
coroutine.yield(s)
26+
end
27+
end)
28+
local git_root = utils.is_windows and "C:\\" or "/asdf"
29+
local status = git._parse_porcelain(2, git_root, iter, {})
30+
assert.are.same({
31+
[utils.path_join(git_root, ".gitignore")] = "?",
32+
[utils.path_join(git_root, "deleted_staged.txt")] = "D.",
33+
[utils.path_join(git_root, "deleted_unstaged.txt")] = ".D",
34+
[utils.path_join(git_root, "modified_mixed.txt")] = "MM",
35+
[utils.path_join(git_root, "modified_staged.txt")] = "M.",
36+
[utils.path_join(git_root, "modified_unstaged.txt")] = ".M",
37+
[utils.path_join(git_root, "new_staged_file.txt")] = "A.",
38+
[utils.path_join(git_root, "renamed_staged_new.txt")] = "R100",
39+
[utils.path_join(git_root, "type_change.txt")] = ".T",
40+
[utils.path_join(git_root, "untracked.txt")] = "?",
41+
}, status)
42+
end
43+
44+
local old = utils.on_windows
45+
utils.on_windows = false
46+
it("on unix", test)
47+
utils.on_windows = true
48+
it("on windows", test)
49+
utils.on_windows = old
50+
end)
51+
52+
describe("parses v1 output", function()
53+
local porcelain_v1_status = {
54+
"1 D. N... 100644 000000 000000 ade2881afa1dcb156a3aa576024aa0fecf789191 0000000000000000000000000000000000000000 deleted_staged.txt",
55+
"1 .D N... 100644 100644 000000 9c13483e67ceff219800303ec7af39c4f0301a5b 9c13483e67ceff219800303ec7af39c4f0301a5b deleted_unstaged.txt",
56+
"1 MM N... 100644 100644 100644 4417f3aca512ffdf247662e2c611ee03ff9255cc 29c0e9846cd6410a44c4ca3fdaf5623818bd2838 modified_mixed.txt",
57+
"1 M. N... 100644 100644 100644 f784736eecdd43cd8eb665615163cfc6506fca5f 8d6fad5bd11ac45c7c9e62d4db1c427889ed515b modified_staged.txt",
58+
"1 .M N... 100644 100644 100644 c9e1e027aa9430cb4ffccccf45844286d10285c1 c9e1e027aa9430cb4ffccccf45844286d10285c1 modified_unstaged.txt",
59+
"1 A. N... 000000 100644 100644 0000000000000000000000000000000000000000 89cae60d74c222609086441e29985f959b6ec546 new_staged_file.txt",
60+
"2 R. N... 100644 100644 100644 3454a7dc6b93d1098e3c3f3ec369589412abdf99 3454a7dc6b93d1098e3c3f3ec369589412abdf99 R100 renamed_staged_new.txt",
61+
"renamed_staged_old.txt",
62+
"1 .T N... 100644 100644 120000 192f10ed8c11efb70155e8eb4cae6ec677347623 192f10ed8c11efb70155e8eb4cae6ec677347623 type_change.txt",
63+
"? .gitignore",
64+
"? untracked.txt",
65+
}
66+
local test = function()
67+
local iter = coroutine.wrap(function()
68+
for i, s in ipairs(porcelain_v1_status) do
69+
coroutine.yield(s)
70+
end
71+
end)
72+
local git_root = utils.is_windows and "C:\\" or "/asdf"
73+
local status = git._parse_porcelain(2, git_root, iter, {})
74+
assert.are.same({
75+
[utils.path_join(git_root, ".gitignore")] = "?",
76+
[utils.path_join(git_root, "deleted_staged.txt")] = "D.",
77+
[utils.path_join(git_root, "deleted_unstaged.txt")] = ".D",
78+
[utils.path_join(git_root, "modified_mixed.txt")] = "MM",
79+
[utils.path_join(git_root, "modified_staged.txt")] = "M.",
80+
[utils.path_join(git_root, "modified_unstaged.txt")] = ".M",
81+
[utils.path_join(git_root, "new_staged_file.txt")] = "A.",
82+
[utils.path_join(git_root, "renamed_staged_new.txt")] = "R100",
83+
[utils.path_join(git_root, "type_change.txt")] = ".T",
84+
[utils.path_join(git_root, "untracked.txt")] = "?",
85+
}, status)
86+
end
87+
local restore = test_utils.os_to_windows(false)
88+
it("on unix", test)
89+
test_utils.os_to_windows(true)
90+
it("on windows", test)
91+
restore()
92+
end)
93+
end)

tests/utils/init.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,20 @@ function mod.buflines(bufnr)
214214
return vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
215215
end
216216

217+
function mod.os_to_windows(is_windows)
218+
local utils = require("neo-tree.utils")
219+
local old = {
220+
path_separator = utils.path_separator,
221+
is_windows = utils.is_windows,
222+
}
223+
utils.is_windows = is_windows
224+
utils.path_separator = is_windows and "\\" or "/"
225+
local restore = function()
226+
for k, v in pairs(old) do
227+
utils[k] = v
228+
end
229+
end
230+
return restore
231+
end
232+
217233
return mod

0 commit comments

Comments
 (0)