Skip to content

Commit

Permalink
feat: support read vars.env file from test folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Oct 31, 2023
1 parent ba171f1 commit aea1ca5
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
6 changes: 5 additions & 1 deletion hurl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ buymeacoffee
docstrings
sweepai
pygithub
sandboxed
sandboxed
filereadable
getcwd
infile
isdirectory
69 changes: 66 additions & 3 deletions lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,20 @@ end
---@param callback? function The callback function
local function request(opts, callback)
vim.notify('hurl: running request', vim.log.levels.INFO)

-- Check vars.env exist on the current file buffer
-- Then inject the command with --variables-file vars.env
local env_file = vim.fn.expand('%:p:h') .. '/vars.env'
if vim.fn.filereadable(env_file) == 1 then
utils.log_info('hurl: found vars.env at ' .. env_file)
table.insert(opts, '--variables-file')
table.insert(opts, env_file)
end

local cmd = vim.list_extend({ 'hurl', '-i', '--no-color' }, opts)
response = {}

if _HURL_GLOBAL_CONFIG.debug then
vim.fn.setqflist({ { filename = vim.inspect(cmd), text = vim.inspect(opts) } })
end
utils.log_info('hurl: running command' .. vim.inspect(cmd))

vim.fn.jobstart(cmd, {
on_stdout = on_output,
Expand Down Expand Up @@ -118,6 +126,46 @@ local function run_current_file(opts)
request(opts)
end

-- NOTE: Refactor this later if there is a better way
-- Copy vars.env from current directory or tests directory to nvim cache directory
---@return table
local function get_env_file_in_folders()
local root_dir = vim.fn.expand('%:p:h')
local cache_dir = vim.fn.stdpath('cache')
local env_files = {
{ path = root_dir .. '/vars.env', dest = cache_dir .. '/vars.env' },
}
local scan_dir = {
{
dir = '/src',
},
{
dir = '/test',
},
{
dir = '/tests',
},
{
dir = '/server',
},
{
dir = '/src/tests',
},
{
dir = '/server/tests',
},
}

for _, s in ipairs(scan_dir) do
local dir = root_dir .. s.dir
if vim.fn.isdirectory(dir) == 1 then
table.insert(env_files, { path = dir .. '/vars.env', dest = cache_dir .. '/vars.env' })
end
end

return scan_dir
end

--- Run selection
---@param opts table The options
local function run_selection(opts)
Expand All @@ -133,6 +181,21 @@ local function run_selection(opts)
return
end

local env_files = get_env_file_in_folders()
for _, env in ipairs(env_files) do
if vim.fn.filereadable(env.path) == 1 then
local success = utils.copy_file(env.path, env.dest)
if not success then
utils.log_error('hurl: copy vars.env failed', vim.log.levels.WARN)
else
utils.log_info('hurl: copy vars.env success ' .. env.dest)
table.insert(opts, '--variables-file')
table.insert(opts, env.dest)
end
break
end
end

table.insert(opts, fname)
request(opts)

Expand Down
25 changes: 25 additions & 0 deletions lua/hurl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,29 @@ util.is_html_response = function(content_type)
return string.find(content_type, 'text/html') ~= nil
end

--- Copy file
---@param src string
---@param dest string
---@return boolean
util.copy_file = function(src, dest)
local infile = io.open(src, 'rb')
if not infile then
return false
end

local outfile = io.open(dest, 'wb')
if not outfile then
infile:close()
return false
end

local data = infile:read('*a') -- Read the whole file
outfile:write(data)

infile:close()
outfile:close()

return true
end

return util
2 changes: 1 addition & 1 deletion test/example.hurl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GET https://api.mangadex.org/statistics/manga/8b34f37a-0181-4f0b-8ce3-01217e9a602c
GET https://api.mangadex.org/statistics/manga/{{manga_id}}

GET https://example.org

Expand Down
1 change: 1 addition & 0 deletions test/vars.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
manga_id=8b34f37a-0181-4f0b-8ce3-01217e9a602c

0 comments on commit aea1ca5

Please sign in to comment.