1
0
forked from a/repotool
repotool/test/test_git_parser.lua
2025-07-06 16:40:46 -05:00

100 lines
4.4 KiB
Lua

local git = require("git")
test.suite("git.parse_url", function()
test.case("parses HTTPS URL without .git", function()
local domain, path = git.parse_url("https://gitlab.freedesktop.org/xorg/proto")
assert_eq(domain, "gitlab.freedesktop.org", "Domain should be gitlab.freedesktop.org")
assert_eq(path, "xorg/proto", "Path should be xorg/proto")
end)
test.case("parses HTTPS URL without .git", function()
local domain, path = git.parse_url("https://gitlab.freedesktop.org/xorg/proto/xcbproto")
assert_eq(domain, "gitlab.freedesktop.org", "Domain should be gitlab.freedesktop.org")
assert_eq(path, "xorg/proto/xcbproto", "Path should be xorg/proto/xcbproto")
end)
test.case("parses HTTPS URL with .git", function()
local domain, path = git.parse_url("https://github.com/user/repo.git")
assert_eq(domain, "github.com", "Domain should be github.com")
assert_eq(path, "user/repo", "Path should be user/repo")
end)
test.case("parses HTTPS URL with authentication", function()
local domain, path = git.parse_url("https://user@gitlab.freedesktop.org/xorg/proto")
assert_eq(domain, "gitlab.freedesktop.org", "Domain should be gitlab.freedesktop.org")
assert_eq(path, "xorg/proto", "Path should be xorg/proto")
end)
test.case("parses HTTPS URL with authentication and .git", function()
local domain, path = git.parse_url("https://user:token@github.com/user/repo.git")
assert_eq(domain, "github.com", "Domain should be github.com")
assert_eq(path, "user/repo", "Path should be user/repo")
end)
test.case("parses SSH URL", function()
local domain, path = git.parse_url("git@github.com:user/repo.git")
assert_eq(domain, "github.com", "Domain should be github.com")
assert_eq(path, "user/repo", "Path should be user/repo")
end)
test.case("parses SSH URL without .git", function()
local domain, path = git.parse_url("git@gitlab.com:group/project")
assert_eq(domain, "gitlab.com", "Domain should be gitlab.com")
assert_eq(path, "group/project", "Path should be group/project")
end)
test.case("parses SSH URL with port", function()
local domain, path = git.parse_url("ssh://git@github.com/user/repo.git")
assert_eq(domain, "github.com", "Domain should be github.com")
assert_eq(path, "user/repo", "Path should be user/repo")
end)
test.case("parses bare domain path", function()
local domain, path = git.parse_url("gfx.cafe/oku/trade")
assert_eq(domain, "gfx.cafe", "Domain should be gfx.cafe")
assert_eq(path, "oku/trade", "Path should be oku/trade")
end)
test.case("returns nil for invalid URLs", function()
local domain, path = git.parse_url("not-a-url")
assert_nil(domain, "Domain should be nil for invalid URL")
assert_nil(path, "Path should be nil for invalid URL")
end)
test.case("handles complex paths", function()
local domain, path = git.parse_url("https://gitlab.com/group/subgroup/project.git")
assert_eq(domain, "gitlab.com", "Domain should be gitlab.com")
assert_eq(path, "group/subgroup/project", "Path should preserve subgroups")
end)
test.case("does not parse short SSH syntax as regular domain", function()
-- This ensures git@g:xorg/proto is treated as SSH, not misinterpreted
local domain, path = git.parse_url("git@g:xorg/proto")
assert_eq(domain, "g", "Domain should be g for SSH URL")
assert_eq(path, "xorg/proto", "Path should be xorg/proto")
end)
end)
test.suite("git.valid_url", function()
test.case("identifies HTTPS URLs", function()
assert_eq(git.valid_url("https://github.com/user/repo"), 1)
assert_eq(git.valid_url("http://example.com/path"), 1)
end)
test.case("identifies SSH URLs", function()
assert_eq(git.valid_url("git@github.com:user/repo"), 2)
assert_eq(git.valid_url("ssh://git@github.com/user/repo"), 2)
end)
test.case("identifies bare domain paths", function()
assert_eq(git.valid_url("example.com/user/repo"), 3)
assert_eq(git.valid_url("sub.example.org/path/to/repo"), 3)
end)
test.case("returns -1 for invalid URLs", function()
assert_eq(git.valid_url("not-a-url"), -1)
assert_eq(git.valid_url("/local/path"), -1)
assert_eq(git.valid_url("file:///path"), -1)
end)
end)