-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #616 from treatybreaker/test/git-branch-lib
Test overhauls
- Loading branch information
Showing
15 changed files
with
196 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
2 changes: 1 addition & 1 deletion
2
tests/git_cli_spec.lua → tests/specs/neogit/lib/git/cli_spec.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
tests/branch_popup_spec.lua → tests/specs/neogit/operations_spec.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |