diff options
| author | Fredrik Foss-Indrehus <fredrikfoss@fr.urbanpets.no> | 2025-05-24 21:06:53 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-24 21:06:53 +0200 |
| commit | 8024d64e1330b86044fed4c8494ef3dcd483a67c (patch) | |
| tree | de575420b901ac5dd44221c90ac51b4e75bea370 | |
| parent | docs: rework Installation & Setup sections, and other minor adjustments (#1951) (diff) | |
| download | mason-8024d64e1330b86044fed4c8494ef3dcd483a67c.tar mason-8024d64e1330b86044fed4c8494ef3dcd483a67c.tar.gz mason-8024d64e1330b86044fed4c8494ef3dcd483a67c.tar.bz2 mason-8024d64e1330b86044fed4c8494ef3dcd483a67c.tar.lz mason-8024d64e1330b86044fed4c8494ef3dcd483a67c.tar.xz mason-8024d64e1330b86044fed4c8494ef3dcd483a67c.tar.zst mason-8024d64e1330b86044fed4c8494ef3dcd483a67c.zip | |
fix(fetch): add busybox wget support (#1829)
Co-authored-by: William Boman <william@redwill.se>
| -rw-r--r-- | lua/mason-core/fetch.lua | 31 | ||||
| -rw-r--r-- | lua/mason/health.lua | 2 | ||||
| -rw-r--r-- | tests/mason-core/fetch_spec.lua | 63 |
3 files changed, 75 insertions, 21 deletions
diff --git a/lua/mason-core/fetch.lua b/lua/mason-core/fetch.lua index c1f01dc2..be79db1f 100644 --- a/lua/mason-core/fetch.lua +++ b/lua/mason-core/fetch.lua @@ -75,19 +75,36 @@ local function fetch(url, opts) end local function wget() - local headers = - _.sort_by(_.identity, _.map(_.compose(_.format "--header=%s", _.join ": "), _.to_pairs(opts.headers))) + local headers = _.sort_by( + _.nth(2), + _.map( + _.compose(function(header) + return { "--header", header } + end, _.join ": "), + _.to_pairs(opts.headers) + ) + ) + + if opts.data and opts.method ~= "POST" then + return Result.failure(("fetch: data provided but method is not POST (was %s)"):format(opts.method or "-")) + end + + if not _.any(_.equals(opts.method), { "GET", "POST" }) then + -- Note: --spider can be used for HEAD support, if ever needed + return Result.failure(("fetch: wget doesn't support HTTP method %s"):format(opts.method)) + end + return spawn.wget { headers, - "-nv", "-o", "/dev/null", "-O", opts.out_file or "-", - ("--timeout=%s"):format(TIMEOUT_SECONDS), - ("--method=%s"):format(opts.method), - opts.data and { - ("--body-data=%s"):format(opts.data) or vim.NIL, + "-T", + TIMEOUT_SECONDS, + opts.data and opts.method == "POST" and { + "--post-data", + opts.data, } or vim.NIL, url, } diff --git a/lua/mason/health.lua b/lua/mason/health.lua index fcef1832..b105940d 100644 --- a/lua/mason/health.lua +++ b/lua/mason/health.lua @@ -94,7 +94,7 @@ local function check_core_utils() check { name = "unzip", cmd = "unzip", args = { "-v" }, relaxed = true } -- wget is used interchangeably with curl, but with lower priority, so we mark wget as relaxed - check { cmd = "wget", args = { "--version" }, name = "wget", relaxed = true } + check { cmd = "wget", args = { "--help" }, name = "wget", relaxed = true } check { cmd = "curl", args = { "--version" }, name = "curl" } check { cmd = "gzip", diff --git a/tests/mason-core/fetch_spec.lua b/tests/mason-core/fetch_spec.lua index 98697563..667967b0 100644 --- a/tests/mason-core/fetch_spec.lua +++ b/tests/mason-core/fetch_spec.lua @@ -30,18 +30,21 @@ describe("fetch", function() assert.spy(spawn.curl).was_called(1) assert.spy(spawn.wget).was_called_with { { - ("--header=User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format( - version.VERSION - ), - "--header=X-Custom-Header: here", + { + "--header", + ("User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format(version.VERSION), + }, + { + "--header", + "X-Custom-Header: here", + }, }, - "-nv", "-o", "/dev/null", "-O", "-", - "--timeout=30", - "--method=GET", + "-T", + 30, vim.NIL, -- body-data "https://api.github.com", } @@ -86,17 +89,17 @@ describe("fetch", function() assert.spy(spawn.wget).was_called_with { { - ("--header=User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format( - version.VERSION - ), + { + "--header", + ("User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format(version.VERSION), + }, }, - "-nv", "-o", "/dev/null", "-O", "/test.json", - "--timeout=30", - "--method=GET", + "-T", + 30, vim.NIL, -- body-data "https://api.github.com/data", } @@ -118,3 +121,37 @@ describe("fetch", function() }) end) end) + +describe("fetch :: wget", function() + it("should reject non-supported HTTP methods", function() + stub(spawn, "wget") + stub(spawn, "curl") + spawn.wget.returns(Result.failure "wget failure") + spawn.curl.returns(Result.failure "curl failure") + local PATCH_ERR = assert.has_error(function() + fetch("https://api.github.com/data", { method = "PATCH" }):get_or_throw() + end) + local DELETE_ERR = assert.has_error(function() + fetch("https://api.github.com/data", { method = "DELETE" }):get_or_throw() + end) + local PUT_ERR = assert.has_error(function() + fetch("https://api.github.com/data", { method = "PUT" }):get_or_throw() + end) + + assert.equals("fetch: wget doesn't support HTTP method PATCH", PATCH_ERR) + assert.equals("fetch: wget doesn't support HTTP method DELETE", DELETE_ERR) + assert.equals("fetch: wget doesn't support HTTP method PUT", PUT_ERR) + end) + + it("should reject requests with opts.data if not opts.method is not POST", function() + stub(spawn, "wget") + stub(spawn, "curl") + spawn.wget.returns(Result.failure "wget failure") + spawn.curl.returns(Result.failure "curl failure") + local err = assert.has_error(function() + fetch("https://api.github.com/data", { data = "data" }):get_or_throw() + end) + + assert.equals("fetch: data provided but method is not POST (was GET)", err) + end) +end) |
