Skip to content

Commit

Permalink
test: pass in handler_option for github/commit handlers, add case for…
Browse files Browse the repository at this point in the history
… owner detection
  • Loading branch information
sportshead committed Apr 28, 2024
1 parent 26c683c commit 7259a45
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 32 deletions.
61 changes: 39 additions & 22 deletions test/spec/gx/handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ local assert = require("luassert")

describe("test handler", function()
local activated_handlers
local handler_options = {
git_remotes = { "upstream", "origin" },
}

before_each(function()
before_mock_filetype = vim.bo.filetype
Expand All @@ -26,14 +29,17 @@ describe("test handler", function()

assert.same(
{ { ["name"] = "url", ["url"] = "https://github.com" } },
handler.get_url("v", "github.com", activated_handlers)
handler.get_url("v", "github.com", activated_handlers, handler_options)
)
assert.same(
{ { ["name"] = "url", ["url"] = "https://github.com" } },
handler.get_url("v", "https://github.com", activated_handlers)
handler.get_url("v", "https://github.com", activated_handlers, handler_options)
)
assert.same({}, handler.get_url("v", '"example_user/example_plugin"', activated_handlers))
assert.same({}, handler.get_url("v", "Fixes #22", activated_handlers))
assert.same(
{},
handler.get_url("v", '"example_user/example_plugin"', activated_handlers, handler_options)
)
assert.same({}, handler.get_url("v", "Fixes #22", activated_handlers, handler_options))
end)

it("plugin handler on", function()
Expand All @@ -44,15 +50,15 @@ describe("test handler", function()

assert.same(
{ { ["name"] = "url", ["url"] = "https://github.com" } },
handler.get_url("v", "github.com", activated_handlers)
handler.get_url("v", "github.com", activated_handlers, handler_options)
)
assert.same(
{ { ["name"] = "url", ["url"] = "https://github.com" } },
handler.get_url("v", "https://github.com", activated_handlers)
handler.get_url("v", "https://github.com", activated_handlers, handler_options)
)
assert.same(
{ { ["name"] = "nvim-plugin", ["url"] = "https://github.com/example_user/example_plugin" } },
handler.get_url("v", '"example_user/example_plugin"', activated_handlers)
handler.get_url("v", '"example_user/example_plugin"', activated_handlers, handler_options)
)
end)

Expand All @@ -65,15 +71,15 @@ describe("test handler", function()

assert.same(
{ { ["name"] = "url", ["url"] = "https://github.com" } },
handler.get_url("v", "github.com", activated_handlers)
handler.get_url("v", "github.com", activated_handlers, handler_options)
)
assert.same(
{ { ["name"] = "url", ["url"] = "https://github.com" } },
handler.get_url("v", "https://github.com", activated_handlers)
handler.get_url("v", "https://github.com", activated_handlers, handler_options)
)
assert.same(
{ { ["name"] = "nvim-plugin", ["url"] = "https://github.com/example_user/example_plugin" } },
handler.get_url("v", '"example_user/example_plugin"', activated_handlers)
handler.get_url("v", '"example_user/example_plugin"', activated_handlers, handler_options)
)
end)

Expand All @@ -85,13 +91,16 @@ describe("test handler", function()

assert.same(
{ { ["name"] = "url", ["url"] = "https://github.com" } },
handler.get_url("v", "github.com", activated_handlers)
handler.get_url("v", "github.com", activated_handlers, handler_options)
)
assert.same(
{ { ["name"] = "url", ["url"] = "https://github.com" } },
handler.get_url("v", "https://github.com", activated_handlers)
handler.get_url("v", "https://github.com", activated_handlers, handler_options)
)
assert.same(
{},
handler.get_url("v", '"example_user/example_plugin"', activated_handlers, handler_options)
)
assert.same({}, handler.get_url("v", '"example_user/example_plugin"', activated_handlers))
end)

it("github handler on", function()
Expand All @@ -102,15 +111,15 @@ describe("test handler", function()

assert.same(
{ { ["name"] = "url", ["url"] = "https://github.com" } },
handler.get_url("v", "github.com", activated_handlers)
handler.get_url("v", "github.com", activated_handlers, handler_options)
)
assert.same(
{ { ["name"] = "url", ["url"] = "https://github.com" } },
handler.get_url("v", "https://github.com", activated_handlers)
handler.get_url("v", "https://github.com", activated_handlers, handler_options)
)
assert.same(
{ { ["name"] = "github", ["url"] = "https://github.com/chrishrb/gx.nvim/issues/22" } },
handler.get_url("v", "Fixes #22", activated_handlers)
handler.get_url("v", "Fixes #22", activated_handlers, handler_options)
)
end)

Expand All @@ -123,12 +132,20 @@ describe("test handler", function()
stub(helper, "get_filename")
helper.get_filename.on_call_with().returns("package.json")

assert.same({
assert.same(
{
["name"] = "package_json",
["url"] = "https://www.npmjs.com/package/@rushstack/eslint-patch",
{
["name"] = "package_json",
["url"] = "https://www.npmjs.com/package/@rushstack/eslint-patch",
},
},
}, handler.get_url("v", '"@rushstack/eslint-patch": "^1.2.0",', activated_handlers))
handler.get_url(
"v",
'"@rushstack/eslint-patch": "^1.2.0",',
activated_handlers,
handler_options
)
)

helper.get_filename:revert()
end)
Expand All @@ -145,7 +162,7 @@ describe("test handler", function()
assert.same({
{ ["name"] = "custom", ["url"] = "https://from.user.handler" },
{ ["name"] = "commit", ["url"] = "https://github.com/chrishrb/gx.nvim/commit/1a2b3c4" },
}, handler.get_url("v", "1a2b3c4", activated_handlers))
}, handler.get_url("v", "1a2b3c4", activated_handlers, handler_options))
end)

it("user defined handler instead of builtin handler", function()
Expand All @@ -158,7 +175,7 @@ describe("test handler", function()

assert.same(
{ { ["url"] = "https://from.user.handler" } },
handler.get_url("v", "1a2b3c4", activated_handlers)
handler.get_url("v", "1a2b3c4", activated_handlers, handler_options)
)
end)
end)
16 changes: 11 additions & 5 deletions test/spec/gx/handlers/commit_handler_spec.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
local handler = require("gx.handlers.commit")
local handler_options = {
git_remotes = { "upstream", "origin" },
}

describe("commit_handler_does_work", function()
it("doesn't see a hash with < 7 characters", function()
assert.is_nil(handler.handle("v", "1a2b3c"))
assert.is_nil(handler.handle("v", "1a2b3c", handler_options))
end)

it("does see a hash with 7 characters", function()
assert.equals(
"https://github.com/chrishrb/gx.nvim/commit/1a2b3c4",
handler.handle("v", "1a2b3c4")
handler.handle("v", "1a2b3c4", handler_options)
)
end)

it("does see a hash with 10 characters", function()
assert.equals(
"https://github.com/chrishrb/gx.nvim/commit/1a2b3c4d5f",
handler.handle("v", "1a2b3c4d5f")
handler.handle("v", "1a2b3c4d5f", handler_options)
)
end)

it("does see a hash with 40 characters", function()
local hash = "1a2b3c4d5f1a2b3c4d5f1a2b3c4d5f1a2b3c4d5f"
assert.equals("https://github.com/chrishrb/gx.nvim/commit/" .. hash, handler.handle("v", hash))
assert.equals(
"https://github.com/chrishrb/gx.nvim/commit/" .. hash,
handler.handle("v", hash, handler_options)
)
end)

it("doesn't see a hash with > 40 characters", function()
assert.is_nil(handler.handle("v", "1a2b3c4d5f1a2b3c4d5f1a2b3c4d5f1a2b3c4d5fa"))
assert.is_nil(handler.handle("v", "1a2b3c4d5f1a2b3c4d5f1a2b3c4d5f1a2b3c4d5fa", handler_options))
end)
end)
25 changes: 20 additions & 5 deletions test/spec/gx/handlers/github_handler_spec.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
local handler = require("gx.handlers.github")
local handler_options = {
git_remotes = { "upstream", "origin" },
}

describe("github_handler_does_work", function()
it("with_plain_issue_number", function()
assert.equals("https://github.com/chrishrb/gx.nvim/issues/2", handler.handle("v", "#2"))
assert.equals(
"https://github.com/chrishrb/gx.nvim/issues/2",
handler.handle("v", "#2", handler_options)
)
end)
it("with_text_before_issue_number", function()
assert.equals("https://github.com/chrishrb/gx.nvim/issues/22", handler.handle("v", "Fixes #22"))
assert.equals(
"https://github.com/chrishrb/gx.nvim/issues/22",
handler.handle("v", "Fixes #22", handler_options)
)
end)
it("with_text_after_issue_number", function()
assert.equals(
"https://github.com/chrishrb/gx.nvim/issues/40",
handler.handle("v", "#40 is a related issue")
handler.handle("v", "#40 is a related issue", handler_options)
)
end)
it("with_text_all_around", function()
assert.equals(
"https://github.com/chrishrb/gx.nvim/issues/4",
handler.handle("v", "Fixes #4 once and for all")
handler.handle("v", "Fixes #4 once and for all", handler_options)
)
end)
it("with_issue_number_in_parentheses", function()
assert.equals(
"https://github.com/chrishrb/gx.nvim/issues/51",
handler.handle("v", "This is a squashed PR (#51)")
handler.handle("v", "This is a squashed PR (#51)", handler_options)
)
end)
it("github_issue owner detection", function()
assert.equals(
"https://github.com/foouser/gx.nvim/issues/42",
handler.handle("v", "See foouser#42", handler_options)
)
end)
end)

0 comments on commit 7259a45

Please sign in to comment.