Skip to content

Commit

Permalink
Merge pull request #616 from treatybreaker/test/git-branch-lib
Browse files Browse the repository at this point in the history
Test overhauls
  • Loading branch information
ten3roberts authored Jul 16, 2023
2 parents 700e38d + 7ce8680 commit 6ee2a96
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 70 deletions.
31 changes: 31 additions & 0 deletions lua/neogit/lib/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

local function pad_right(s, len)
return s .. string.rep(" ", math.max(len - #s, 0))
end
Expand Down Expand Up @@ -280,5 +309,7 @@ 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,
pad_right = pad_right,
}
6 changes: 3 additions & 3 deletions recipes.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"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,
"NEOGIT_LOG_LEVEL": "debug"
}
},
"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,
"NEOGIT_LOG_LEVEL": "debug"
}
},
"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,
Expand Down
6 changes: 4 additions & 2 deletions tests/init.lua
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -20,7 +22,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,
})
29 changes: 0 additions & 29 deletions tests/poc_spec.lua

This file was deleted.

59 changes: 59 additions & 0 deletions tests/specs/neogit/lib/git/branch_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 neogit_util = require("neogit.lib.util")

describe("git branch lib", function()
describe("local branches", function()
local branches = {}

local function setup_local_git_branches()
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")
end

before_each(function()
git_harness.prepare_repository()
plenary_async.util.block_on(status.reset)
setup_local_git_branches()
require("neogit").setup()
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(neogit_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
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(neogit_util.lists_equal(branches, branches_detected))
end)
end)
end)
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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

Expand Down
13 changes: 9 additions & 4 deletions tests/process_spec.lua → tests/specs/neogit/process_spec.lua
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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, {
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
68 changes: 39 additions & 29 deletions tests/git_harness.lua → tests/util/git_harness.lua
Original file line number Diff line number Diff line change
@@ -1,51 +1,61 @@
local status = require("neogit.status")
local a = require("plenary.async")
local M = {}
local util = require("tests.util.util")

local project_dir = vim.fn.getcwd()
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)
function M.setup_bare_repo()
if bare_repo_path ~= nil then
return bare_repo_path
end
return res
end

local function prepare_repository(dir)
local workspace_dir = util.create_temp_dir("base-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)
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 "[email protected]"
git config user.name "Neogit Test"
git add .
git commit -m "temp commit to be soft unstaged later"
]])

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))

return bare_repo_path
end

local function cleanup_repository(dir)
vim.api.nvim_set_current_dir(project_dir)
function M.prepare_repository()
M.setup_bare_repo()

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")
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)
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)
if err ~= nil then
error(err)
end
Expand Down
48 changes: 48 additions & 0 deletions tests/util/util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
local M = {}

M.project_dir = vim.fn.getcwd()

---Returns the path to the raw test files directory
---@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 | 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
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

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

0 comments on commit 6ee2a96

Please sign in to comment.