url_parsing_spec.lua

  1-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
  2--
  3-- SPDX-License-Identifier: GPL-3.0-or-later
  4
  5-- Load main.lua as a module (it exports functions when required)
  6package.path = package.path .. ";./?.lua"
  7local wt = dofile("src/main.lua")
  8
  9describe("extract_project_name", function()
 10	describe("standard git URLs", function()
 11		it("extracts from SSH URL with .git suffix", function()
 12			assert.are.equal("project", wt.extract_project_name("git@github.com:user/project.git"))
 13		end)
 14
 15		it("extracts from HTTPS URL with .git suffix", function()
 16			assert.are.equal("project", wt.extract_project_name("https://github.com/user/project.git"))
 17		end)
 18
 19		it("extracts from HTTPS URL without .git suffix", function()
 20			assert.are.equal("project", wt.extract_project_name("https://github.com/user/project"))
 21		end)
 22
 23		it("extracts from SSH URL without .git suffix", function()
 24			assert.are.equal("project", wt.extract_project_name("git@github.com:user/project"))
 25		end)
 26
 27		it("extracts from ssh:// protocol URL", function()
 28			assert.are.equal("repo", wt.extract_project_name("ssh://git@host:2222/user/repo.git"))
 29		end)
 30
 31		it("extracts from gitlab-style nested groups", function()
 32			assert.are.equal("repo", wt.extract_project_name("git@gitlab.com:group/subgroup/repo.git"))
 33		end)
 34	end)
 35
 36	describe("SCP-style URLs", function()
 37		it("extracts from SCP URL with path", function()
 38			assert.are.equal("project", wt.extract_project_name("git@host:user/project.git"))
 39		end)
 40
 41		it("extracts from SCP URL without path slash", function()
 42			assert.are.equal("repo", wt.extract_project_name("git@host:repo.git"))
 43		end)
 44	end)
 45
 46	describe("edge cases", function()
 47		it("returns nil for empty string", function()
 48			assert.is_nil(wt.extract_project_name(""))
 49		end)
 50
 51		it("returns nil for just a slash", function()
 52			assert.is_nil(wt.extract_project_name("/"))
 53		end)
 54
 55		it("extracts bare project name with .git suffix", function()
 56			assert.are.equal("repo", wt.extract_project_name("repo.git"))
 57		end)
 58
 59		it("extracts bare project name without suffix", function()
 60			assert.are.equal("repo", wt.extract_project_name("repo"))
 61		end)
 62
 63		it("extracts project from URL with trailing slash", function()
 64			assert.are.equal("repo", wt.extract_project_name("https://github.com/user/repo.git/"))
 65		end)
 66
 67		it("extracts project from URL with query string", function()
 68			assert.are.equal("repo", wt.extract_project_name("https://github.com/user/repo.git?ref=main"))
 69		end)
 70
 71		it("extracts project from URL with fragment", function()
 72			assert.are.equal("repo", wt.extract_project_name("https://github.com/user/repo#readme"))
 73		end)
 74	end)
 75
 76	describe("special characters in project names", function()
 77		it("extracts project with dots", function()
 78			assert.are.equal("my.project", wt.extract_project_name("git@github.com:user/my.project.git"))
 79		end)
 80
 81		it("extracts project with hyphens", function()
 82			assert.are.equal("my-project", wt.extract_project_name("git@github.com:user/my-project.git"))
 83		end)
 84
 85		it("extracts project with underscores", function()
 86			assert.are.equal("my_project", wt.extract_project_name("git@github.com:user/my_project.git"))
 87		end)
 88
 89		it("extracts project with numbers", function()
 90			assert.are.equal("project123", wt.extract_project_name("git@github.com:user/project123.git"))
 91		end)
 92	end)
 93end)
 94
 95describe("resolve_url_template", function()
 96	describe("basic substitution", function()
 97		it("substitutes ${project} in SSH template", function()
 98			local result = wt.resolve_url_template("git@github.com:myuser/${project}.git", "myrepo")
 99			assert.are.equal("git@github.com:myuser/myrepo.git", result)
100		end)
101
102		it("substitutes ${project} in HTTPS template", function()
103			local result = wt.resolve_url_template("https://github.com/myuser/${project}.git", "myrepo")
104			assert.are.equal("https://github.com/myuser/myrepo.git", result)
105		end)
106
107		it("handles multiple ${project} occurrences", function()
108			local result = wt.resolve_url_template("${project}/${project}", "x")
109			assert.are.equal("x/x", result)
110		end)
111
112		it("returns unchanged template with no placeholder", function()
113			local result = wt.resolve_url_template("git@host:static.git", "ignored")
114			assert.are.equal("git@host:static.git", result)
115		end)
116	end)
117
118	describe("special characters in project name", function()
119		it("handles hyphens in project name", function()
120			local result = wt.resolve_url_template("git@host:${project}.git", "my-repo")
121			assert.are.equal("git@host:my-repo.git", result)
122		end)
123
124		it("handles dots in project name", function()
125			local result = wt.resolve_url_template("git@host:${project}.git", "my.repo")
126			assert.are.equal("git@host:my.repo.git", result)
127		end)
128
129		it("handles percent in project name", function()
130			assert.are.equal("git@host:weird%name.git", wt.resolve_url_template("git@host:${project}.git", "weird%name"))
131		end)
132	end)
133end)