diff options
| author | Saikari <[email protected]> | 2026-07-28 00:04:16 +0300 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-07-30 10:17:32 +0800 |
| commit | 2127d0cef9be4eeab3e0192ab9bcd7aa36f9d658 (patch) | |
| tree | 2fa9a9804ad13aa9423d52ce5a51671913899125 /tests/plugins | |
| parent | 235b82edd8b262a7080a9f9ee518567f5a54b836 (diff) | |
Refactor plugin installation logic to support custom plugin URLs and improve error handling during installation
Diffstat (limited to 'tests/plugins')
| -rw-r--r-- | tests/plugins/repository/test.lua | 114 |
1 files changed, 48 insertions, 66 deletions
diff --git a/tests/plugins/repository/test.lua b/tests/plugins/repository/test.lua index e0c67058b..c4c6c4c20 100644 --- a/tests/plugins/repository/test.lua +++ b/tests/plugins/repository/test.lua @@ -1,3 +1,5 @@ +local global = import("core.base.global") + function _write_plugin(dir, name, text) io.writefile(path.join(dir, "xmake.lua"), string.format([[ task("%s") @@ -8,8 +10,7 @@ task("%s") io.writefile(path.join(dir, "main.lua"), string.format([[function main() print("%s") end]], text)) end -function main() - local global = import("core.base.global") +function main(t) local suffix = path.filename(os.tmpfile()):gsub("[^%w]", "") local reponame = "plugin-test-repository-" .. suffix local hello_name = "plugin-test-hello-" .. suffix @@ -20,85 +21,66 @@ function main() local localdir = path.join(os.tmpfile() .. ".local-plugin", local_name) local plugindir = path.join(global.directory(), "plugins") local cachefile = path.join(global.cachedir(), "repository") - local cachebackup = os.tmpfile() .. ".repository" - - if os.isfile(cachefile) then - os.cp(cachefile, cachebackup) - end + -- reset test state local function cleanup() for _, name in ipairs({hello_name, formatter_name, available_name, local_name}) do os.tryrm(path.join(plugindir, name)) end - if os.isfile(cachebackup) then - os.cp(cachebackup, cachefile) - else - os.tryrm(cachefile) - end - os.tryrm(cachebackup) + local cache = os.isfile(cachefile) and io.load(cachefile) or {} + cache.repositories = cache.repositories or {} + cache.repositories[reponame] = nil + io.save(cachefile, cache) os.tryrm(repodir) os.tryrm(path.directory(localdir)) end + cleanup() + + -- mock repository with installed and available plugins + _write_plugin(path.join(repodir, "plugins", hello_name), hello_name, "repo-ok") + _write_plugin(path.join(repodir, "plugins", formatter_name), formatter_name, "format-ok") + _write_plugin(path.join(repodir, "plugins", available_name), available_name, "available-ok") + os.mkdir(path.directory(cachefile)) + local cache = os.isfile(cachefile) and io.load(cachefile) or {} + cache.repositories = cache.repositories or {} + cache.repositories[reponame] = {repodir} + io.save(cachefile, cache) + -- Feature: install by plain name from repository + os.runv("xmake", {"plugin", "--install", hello_name}) + os.runv("xmake", {hello_name}) - try - { - function () - -- mock repository with installed and available plugins - _write_plugin(path.join(repodir, "plugins", hello_name), hello_name, "repo-ok") - _write_plugin(path.join(repodir, "plugins", formatter_name), formatter_name, "format-ok") - _write_plugin(path.join(repodir, "plugins", available_name), available_name, "available-ok") - os.mkdir(path.directory(cachefile)) - local cache = os.isfile(cachefile) and io.load(cachefile) or {} - cache.repositories = cache.repositories or {} - cache.repositories[reponame] = {repodir} - io.save(cachefile, cache) + -- Feature: install by repo@name format + os.runv("xmake", {"plugin", "--install", reponame .. "@" .. formatter_name}) + os.runv("xmake", {formatter_name}) - -- Feature: install by plain name and repo@name in one invocation - os.exec("xmake plugin --install " .. hello_name .. " " .. reponame .. "@" .. formatter_name) - os.exec("xmake " .. hello_name) - os.exec("xmake " .. formatter_name) + -- Feature: --list shows installed and available repository plugins + local out = os.iorun("xmake plugin --list") + t:require(out:find(hello_name, 1, true)) + t:require(out:find(formatter_name, 1, true)) + t:require(out:find(available_name, 1, true)) + t:require(out:find("xmake plugin --install " .. available_name, 1, true)) - -- Feature: --list shows installed and available repository plugins - local out = os.iorun("xmake plugin --list") - assert(out:find(hello_name, 1, true)) - assert(out:find(formatter_name, 1, true)) - assert(out:find(available_name, 1, true)) - assert(out:find("xmake plugin --install " .. available_name, 1, true)) + -- Feature: install from local directory + _write_plugin(localdir, local_name, "local-ok") + os.runv("xmake", {"plugin", "--install", localdir}) + os.runv("xmake", {local_name}) - -- Feature: install from local directory - _write_plugin(localdir, local_name, "local-ok") - os.exec("xmake plugin --install " .. os.args(localdir)) - os.exec("xmake " .. local_name) + out = os.iorun("xmake plugin --list") + t:require(out:find(local_name, 1, true)) - out = os.iorun("xmake plugin --list") - assert(out:find(local_name, 1, true)) + -- Feature: remove plugin + os.runv("xmake", {"plugin", "--remove", local_name}) + out = os.iorun("xmake plugin --list") + t:require_not(out:find(local_name, 1, true)) - -- Feature: remove plugin - os.exec("xmake plugin --remove " .. local_name) - out = os.iorun("xmake plugin --list") - assert(not out:find(local_name, 1, true)) + -- Feature: install non-existent plugin fails gracefully + local ok = try { function () os.runv("xmake", {"plugin", "--install", "plugin-test-missing-" .. suffix}) return true end } + t:require_not(ok) - -- Feature: install non-existent plugin fails gracefully - local ok = try { function () os.exec("xmake plugin --install plugin-test-missing-" .. suffix) end } - assert(not ok) - -- Feature: reject plugin name traversal - ok = try { function () os.exec("xmake plugin --install " .. reponame .. "@..") end } - assert(not ok) + -- Feature: reject plugin name traversal + ok = try { function () os.runv("xmake", {"plugin", "--install", reponame .. "@.."}) return true end } + t:require_not(ok) - end, - catch - { - function (errors) - cleanup() - raise(errors) - end - }, - finally - { - function () - cleanup() - end - } - } + cleanup() end |
