Skip to content

Commit 8024d64

Browse files
Fredrik Foss-Indrehuswilliamboman
andauthored
fix(fetch): add busybox wget support (#1829)
Co-authored-by: William Boman <[email protected]>
1 parent 9eaedb8 commit 8024d64

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

lua/mason-core/fetch.lua

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,36 @@ local function fetch(url, opts)
7575
end
7676

7777
local function wget()
78-
local headers =
79-
_.sort_by(_.identity, _.map(_.compose(_.format "--header=%s", _.join ": "), _.to_pairs(opts.headers)))
78+
local headers = _.sort_by(
79+
_.nth(2),
80+
_.map(
81+
_.compose(function(header)
82+
return { "--header", header }
83+
end, _.join ": "),
84+
_.to_pairs(opts.headers)
85+
)
86+
)
87+
88+
if opts.data and opts.method ~= "POST" then
89+
return Result.failure(("fetch: data provided but method is not POST (was %s)"):format(opts.method or "-"))
90+
end
91+
92+
if not _.any(_.equals(opts.method), { "GET", "POST" }) then
93+
-- Note: --spider can be used for HEAD support, if ever needed
94+
return Result.failure(("fetch: wget doesn't support HTTP method %s"):format(opts.method))
95+
end
96+
8097
return spawn.wget {
8198
headers,
82-
"-nv",
8399
"-o",
84100
"/dev/null",
85101
"-O",
86102
opts.out_file or "-",
87-
("--timeout=%s"):format(TIMEOUT_SECONDS),
88-
("--method=%s"):format(opts.method),
89-
opts.data and {
90-
("--body-data=%s"):format(opts.data) or vim.NIL,
103+
"-T",
104+
TIMEOUT_SECONDS,
105+
opts.data and opts.method == "POST" and {
106+
"--post-data",
107+
opts.data,
91108
} or vim.NIL,
92109
url,
93110
}

lua/mason/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ local function check_core_utils()
9494
check { name = "unzip", cmd = "unzip", args = { "-v" }, relaxed = true }
9595

9696
-- wget is used interchangeably with curl, but with lower priority, so we mark wget as relaxed
97-
check { cmd = "wget", args = { "--version" }, name = "wget", relaxed = true }
97+
check { cmd = "wget", args = { "--help" }, name = "wget", relaxed = true }
9898
check { cmd = "curl", args = { "--version" }, name = "curl" }
9999
check {
100100
cmd = "gzip",

tests/mason-core/fetch_spec.lua

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@ describe("fetch", function()
3030
assert.spy(spawn.curl).was_called(1)
3131
assert.spy(spawn.wget).was_called_with {
3232
{
33-
("--header=User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format(
34-
version.VERSION
35-
),
36-
"--header=X-Custom-Header: here",
33+
{
34+
"--header",
35+
("User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format(version.VERSION),
36+
},
37+
{
38+
"--header",
39+
"X-Custom-Header: here",
40+
},
3741
},
38-
"-nv",
3942
"-o",
4043
"/dev/null",
4144
"-O",
4245
"-",
43-
"--timeout=30",
44-
"--method=GET",
46+
"-T",
47+
30,
4548
vim.NIL, -- body-data
4649
"https://api.github.com",
4750
}
@@ -86,17 +89,17 @@ describe("fetch", function()
8689

8790
assert.spy(spawn.wget).was_called_with {
8891
{
89-
("--header=User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format(
90-
version.VERSION
91-
),
92+
{
93+
"--header",
94+
("User-Agent: mason.nvim %s (+https://github.com/mason-org/mason.nvim)"):format(version.VERSION),
95+
},
9296
},
93-
"-nv",
9497
"-o",
9598
"/dev/null",
9699
"-O",
97100
"/test.json",
98-
"--timeout=30",
99-
"--method=GET",
101+
"-T",
102+
30,
100103
vim.NIL, -- body-data
101104
"https://api.github.com/data",
102105
}
@@ -118,3 +121,37 @@ describe("fetch", function()
118121
})
119122
end)
120123
end)
124+
125+
describe("fetch :: wget", function()
126+
it("should reject non-supported HTTP methods", function()
127+
stub(spawn, "wget")
128+
stub(spawn, "curl")
129+
spawn.wget.returns(Result.failure "wget failure")
130+
spawn.curl.returns(Result.failure "curl failure")
131+
local PATCH_ERR = assert.has_error(function()
132+
fetch("https://api.github.com/data", { method = "PATCH" }):get_or_throw()
133+
end)
134+
local DELETE_ERR = assert.has_error(function()
135+
fetch("https://api.github.com/data", { method = "DELETE" }):get_or_throw()
136+
end)
137+
local PUT_ERR = assert.has_error(function()
138+
fetch("https://api.github.com/data", { method = "PUT" }):get_or_throw()
139+
end)
140+
141+
assert.equals("fetch: wget doesn't support HTTP method PATCH", PATCH_ERR)
142+
assert.equals("fetch: wget doesn't support HTTP method DELETE", DELETE_ERR)
143+
assert.equals("fetch: wget doesn't support HTTP method PUT", PUT_ERR)
144+
end)
145+
146+
it("should reject requests with opts.data if not opts.method is not POST", function()
147+
stub(spawn, "wget")
148+
stub(spawn, "curl")
149+
spawn.wget.returns(Result.failure "wget failure")
150+
spawn.curl.returns(Result.failure "curl failure")
151+
local err = assert.has_error(function()
152+
fetch("https://api.github.com/data", { data = "data" }):get_or_throw()
153+
end)
154+
155+
assert.equals("fetch: data provided but method is not POST (was GET)", err)
156+
end)
157+
end)

0 commit comments

Comments
 (0)