Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve unit test coverage #85

Merged
merged 12 commits into from
Oct 23, 2023
2 changes: 0 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Thanks to your contribute, while please finish below tasks:

# Regression Test

## Platforms
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
args: --config-path .stylua.toml --color always --check lua
args: --config-path .stylua.toml ./
- name: Format with stylua
if: ${{ github.ref != 'refs/heads/master' }}
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "chore(pr): format with stylua"
release:
name: Release
if: ${{ github.ref == 'refs/heads/master' }}
Expand Down
36 changes: 22 additions & 14 deletions lua/gitlinker/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ local function cmd(args, cwd)
end

--- @package
--- @return CmdResult
--- @return string[]|nil
local function _get_remote()
local result = cmd({ "git", "remote" })
logger.debug("|git._get_remote| result:%s", vim.inspect(result))
return result
if type(result.stdout) ~= "table" or #result.stdout == 0 then
result:print_err("fatal: git repo has no remote")
return nil
end
return result.stdout
end

--- @param remote string
Expand All @@ -90,7 +94,7 @@ local function get_remote_url(remote)
)
if not result:has_out() then
result:print_err(
"failed to get remote url by remote '" .. remote .. "'"
"fatal: failed to get remote url by remote '" .. remote .. "'"
)
return nil
end
Expand Down Expand Up @@ -121,7 +125,7 @@ local function _get_rev_name(revspec)
vim.inspect(result)
)
if not result:has_out() then
result:print_err("git branch has no remote")
result:print_err("fatal: git branch has no remote")
return nil
end
return result.stdout[1]
Expand All @@ -140,7 +144,11 @@ local function is_file_in_rev(file, revspec)
)
if result:has_err() then
result:print_err(
"'" .. file .. "' does not exist in remote '" .. revspec .. "'"
"fatal: '"
.. file
.. "' does not exist in remote '"
.. revspec
.. "'"
)
return false
end
Expand Down Expand Up @@ -237,7 +245,7 @@ local function get_root()
vim.inspect(result)
)
if not result:has_out() then
result:print_err("not in a git repository")
result:print_err("fatal: not in a git repository")
return nil
end
return result.stdout[1]
Expand All @@ -246,16 +254,13 @@ end
--- @return string?
local function get_branch_remote()
-- origin/upstream
--- @type CmdResult
local remote_result = _get_remote()

if type(remote_result.stdout) ~= "table" or #remote_result.stdout == 0 then
remote_result:print_err("git repo has no remote")
local remotes = _get_remote()
if not remotes then
return nil
end

if #remote_result.stdout == 1 then
return remote_result.stdout[1]
if #remotes == 1 then
return remotes[1]
end

-- origin/linrongbin16/add-rule2
Expand All @@ -278,7 +283,6 @@ local function get_branch_remote()
return nil
end

local remotes = remote_result.stdout
for _, remote in ipairs(remotes) do
if remote_from_upstream_branch == remote then
return remote
Expand All @@ -294,6 +298,10 @@ local function get_branch_remote()
end

local M = {
CmdResult = CmdResult,
_get_remote = _get_remote,
_get_rev = _get_rev,
_get_rev_name = _get_rev_name,
get_root = get_root,
get_remote_url = get_remote_url,
is_file_in_rev = is_file_in_rev,
Expand Down
101 changes: 101 additions & 0 deletions test/git_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
local cwd = vim.fn.getcwd()

describe("git", function()
local assert_eq = assert.is_equal
local assert_true = assert.is_true
local assert_false = assert.is_false

before_each(function()
vim.api.nvim_command("cd " .. cwd)
vim.opt.swapfile = false
local logger = require("gitlinker.logger")
logger.setup()
vim.cmd([[ edit lua/gitlinker.lua ]])
end)

local logger = require("gitlinker.logger")
logger.setup({
level = "DEBUG",
console_log = true,
file_log = true,
})
local git = require("gitlinker.git")
local path = require("gitlinker.path")
describe("[git]", function()
it("_get_remote", function()
local r = git._get_remote()
print(string.format("_get_remote:%s\n", vim.inspect(r)))
assert_eq(type(r), "table")
end)
it("get_remote_url", function()
local remote = git.get_branch_remote()
print(string.format("get_branch_remote:%s\n", vim.inspect(remote)))
if remote then
assert_eq(type(remote), "string")
assert_true(string.len(remote) > 0)
local r = git.get_remote_url(remote)
print(string.format("get_remote_url:%s\n", vim.inspect(r)))
assert_eq(type(r), "string")
assert_true(string.len(r) > 0)
else
assert_true(remote == nil)
end
end)
it("_get_rev(@{u})", function()
local rev = git._get_rev("@{u}")
if rev then
print(string.format("_get_rev:%s\n", vim.inspect(rev)))
assert_eq(type(rev), "string")
assert_true(string.len(rev) > 0)
else
assert_true(rev == nil)
end
end)
it("_get_rev_name(@{u})", function()
local rev = git._get_rev_name("@{u}")
if rev then
print(string.format("_get_rev_name:%s\n", vim.inspect(rev)))
assert_eq(type(rev), "string")
assert_true(string.len(rev) > 0)
else
assert_true(rev == nil)
end
end)
it("is_file_in_rev", function()
local remote = git.get_branch_remote()
if not remote then
assert_true(remote == nil)
return
end
assert_eq(type(remote), "string")
assert_true(string.len(remote) > 0)
local remote_url = git.get_remote_url(remote)
if not remote_url then
assert_true(remote_url == nil)
return
end
assert_eq(type(remote_url), "string")
assert_true(string.len(remote_url) > 0)

local rev = git.get_closest_remote_compatible_rev(remote) --[[@as string]]
if not rev then
assert_true(rev == nil)
return
end
assert_eq(type(rev), "string")
assert_true(string.len(rev) > 0)

local bufpath = path.buffer_relpath() --[[@as string]]
if not bufpath then
assert_true(bufpath == nil)
return
end
local actual = git.is_file_in_rev(bufpath, rev)
if actual ~= nil then
print(string.format("is_file_in_rev:%s\n", vim.inspect(actual)))
else
assert_true(actual == nil)
end
end)
end)
end)
54 changes: 54 additions & 0 deletions test/gitlinker_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,65 @@ describe("gitlinker", function()

before_each(function()
vim.api.nvim_command("cd " .. cwd)
vim.opt.swapfile = false
local logger = require("gitlinker.logger")
logger.setup()
vim.cmd([[ edit lua/gitlinker.lua ]])
end)

local gitlinker = require("gitlinker")
gitlinker.setup()
local Linker = require("gitlinker.linker").Linker
describe("[gitlinker]", function()
it("_make_sharable_permalinks", function()
local lk1 = Linker:make({ lstart = 10, lend = 23 }) --[[@as Linker]]
if lk1 then
local actual1 = gitlinker._make_sharable_permalinks(
"https://github.com/linrongbin16/gitlinker.nvim/blob/",
lk1
)
assert_eq(type(actual1), "string")
assert_true(string.len(actual1) > 0)
assert_eq(actual1:sub(#actual1 - 7), "#L10-L23")
print(
string.format("make permalink1:%s\n", vim.inspect(actual1))
)
else
assert_true(lk1 == nil)
end

local lk2 = Linker:make({ lstart = 17, lend = 17 }) --[[@as Linker]]
if lk2 then
local actual2 = gitlinker._make_sharable_permalinks(
"https://github.com/linrongbin16/gitlinker.nvim/blob/",
lk2
)
assert_eq(type(actual2), "string")
assert_true(string.len(actual2) > 0)
assert_eq(actual2:sub(#actual2 - 3), "#L17")
print(
string.format("make permalink2:%s\n", vim.inspect(actual2))
)
else
assert_true(lk2 == nil)
end

local lk3 = Linker:make() --[[@as Linker]]
if lk3 then
local actual3 = gitlinker._make_sharable_permalinks(
"https://github.com/linrongbin16/gitlinker.nvim/blob/",
lk3
)
print(
string.format("make permalink3:%s\n", vim.inspect(actual3))
)
assert_eq(type(actual3), "string")
assert_true(string.len(actual3) > 0)
assert_eq(actual3:sub(#actual3 - 2), "#L1")
else
assert_true(lk3 == nil)
end
end)
it("_map_remote_to_host", function()
local test_cases = {
-- [1]
Expand Down
Loading