fixup! add vim.net.config

This commit is contained in:
TheLeoP 2024-06-15 22:38:52 -05:00
parent c36118adc0
commit 0041ee2a3e
2 changed files with 70 additions and 5 deletions

View File

@ -31,7 +31,7 @@ local separator = '__SEPARATOR__'
---Proxy credentials with the format `username:password`
---@field credentials? string
---@class vim.net.download.Opts
---@class vim.net.Opts
---@inlinedoc
---Path to write the downloaded file to. If not provided, the one inferred form the URL will be used. Defaults to `nil`
---@field as? string
@ -128,8 +128,8 @@ local separator = '__SEPARATOR__'
---@field url_effective? string
---@field xfer_id? number (Added in 8.2.0)
---@type vim.net.download.Opts
local download_defaults = {
---@type vim.net.Opts
local global_net_opts = {
as = nil,
try_suggested_remote_name = false,
credentials = nil,
@ -158,9 +158,36 @@ local download_defaults = {
end,
}
---Configure net options globally
---
---Configuration can be specified globally, or ephemerally (i.e. only for
---a single call to |vim.net.download()|). Ephemeral configuration has highest
---priority, followed by global configuration.
---
---When omitted or `nil`, retrieve the current configuration. Otherwise,
---a configuration table (see |vim.net.Opts|).
---@param opts vim.net.Opts
---: Current net config if {opts} is omitted.
---@return vim.net.Opts?
function M.config(opts)
vim.validate({
opts = { opts, 'table', true },
})
if not opts then
return vim.deepcopy(global_net_opts, true)
end
for k, v in
pairs(opts --[[@as table<any,any>]])
do
global_net_opts[k] = v
end
end
---Asynchronously download a file
---@param url string Request URL
---@param opts? vim.net.download.Opts Additional options
---@param opts? vim.net.Opts Additional options
---
---Example:
--- ```lua
@ -201,7 +228,7 @@ function M.download(url, opts)
url = { url, 'string' },
opts = { opts, 'table', true },
}
opts = vim.tbl_extend('force', download_defaults, opts or {}) --[[@as vim.net.download.Opts]]
opts = vim.tbl_extend('force', global_net_opts, opts or {}) --[[@as vim.net.Opts]]
curl_v = curl_v or _curl_v()

View File

@ -194,4 +194,42 @@ describe('vim.net', function()
eq('', read_file(path))
end)
end)
describe('download()', function()
it('works with global options', function()
local different_path = './different_path'
eq(nil, read_file(path))
eq(nil, read_file(different_path))
local filename = exec_lua(
[[
local path,different_path = ...
local done, metadata, err
vim.net.config({as = path})
vim.net.download("https://httpbingo.org/anything", {
as = different_path,
on_exit = function(err_, metadata_)
done = true
metadata = metadata_
err = err_
end
})
local _, interrupted = vim.wait(10000, function()
return done
end)
assert(done, 'file was not downloaded')
assert(not err, err)
assert(metadata, 'no metadata was received')
return metadata.filename_effective
]],
path,
different_path
)
eq(different_path, filename)
eq(nil, read_file(path))
local data = read_file(different_path)
eq('https://httpbingo.org/anything', vim.json.decode(data).url)
end)
end)
end)