Skip to content

Commit

Permalink
Expand variables in keys of nested tables in configurations
Browse files Browse the repository at this point in the history
Closes #374
  • Loading branch information
mfussenegger committed Dec 9, 2021
1 parent b264bb2 commit b17d513
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lua/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ local function expand_config_variables(option)
option = option()
end
if type(option) == "table" then
return vim.tbl_map(expand_config_variables, option)
local result = {}
for k, v in pairs(option) do
result[expand_config_variables(k)] = expand_config_variables(v)
end
return result
end
if type(option) ~= "string" then
return option
Expand Down
16 changes: 11 additions & 5 deletions tests/integration_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ describe('dap', function()
name = 'Launch file',
program = program,
dummy_payload = {
cwd = '${workspaceFolder}'
cwd = '${workspaceFolder}',
['key_with_${workspaceFolder}'] = 'value',
numbers = {1, 2, 3, 4},
strings = {'a', 'b', 'c'},
}
}
local bp_lnum = 8
local bufnr = vim.fn.bufadd(program)
breakpoints.set({}, bufnr, bp_lnum)
local events = {}
local dummy_value = nil
local dummy_payload = nil
dap.listeners.after.event_initialized['dap.tests'] = function(session)
events.initialized = true
dummy_value = session.config.dummy_payload.cwd
dummy_payload = session.config.dummy_payload
end
dap.listeners.after.setBreakpoints['dap.tests'] = function(_, _, resp)
events.setBreakpoints = resp
Expand Down Expand Up @@ -70,10 +73,13 @@ describe('dap', function()
}, events)

-- variable must expand to concrete value
assert.are_not.equals(dummy_value, '${workspaceFolder}')
assert.are_not.equals(dummy_payload.cwd, '${workspaceFolder}')
assert.are.same(dummy_payload.numbers, {1, 2, 3, 4})
assert.are.same(dummy_payload.strings, {'a', 'b', 'c'})
assert.are.equals(dummy_payload['key_with_' .. vim.fn.getcwd()], 'value')

-- ensure `called_with` below passes
config.dummy_payload.cwd = dummy_value
config.dummy_payload = dummy_payload

it('passed cwd to adapter process', function()
luassert.spy(launch).was.called_with(dap.adapters.python, config, { cwd = venv_dir })
Expand Down

0 comments on commit b17d513

Please sign in to comment.