lib/awful/completion.lua: handle missing SHELL environ var (#1897)

This commit is contained in:
Daniel Hahler 2017-07-03 19:26:10 +02:00 committed by GitHub
parent 4b4318602c
commit 4eddfacf51
2 changed files with 70 additions and 1 deletions

View File

@ -18,6 +18,8 @@ local print = print
local pairs = pairs local pairs = pairs
local string = string local string = string
local gears_debug = require("gears.debug")
local completion = {} local completion = {}
-- mapping of command/completion function -- mapping of command/completion function
@ -54,6 +56,8 @@ local function bash_escape(str)
return str return str
end end
completion.default_shell = nil
--- Use shell completion system to complete commands and filenames. --- Use shell completion system to complete commands and filenames.
-- @tparam string command The command line. -- @tparam string command The command line.
-- @tparam number cur_pos The cursor position. -- @tparam number cur_pos The cursor position.
@ -98,7 +102,21 @@ function completion.shell(command, cur_pos, ncomp, shell)
end end
local shell_cmd local shell_cmd
if shell == "zsh" or (not shell and os.getenv("SHELL"):match("zsh$")) then if not shell then
if not completion.default_shell then
local env_shell = os.getenv('SHELL')
if not env_shell then
gears_debug.print_warning('SHELL not set in environment, falling back to bash.')
completion.default_shell = 'bash'
elseif env_shell:match('zsh$') then
completion.default_shell = 'zsh'
else
completion.default_shell = 'bash'
end
end
shell = completion.default_shell
end
if shell == 'zsh' then
if comptype == "file" then if comptype == "file" then
-- NOTE: ${~:-"..."} turns on GLOB_SUBST, useful for expansion of -- NOTE: ${~:-"..."} turns on GLOB_SUBST, useful for expansion of
-- "~/" ($HOME). ${:-"foo"} is the string "foo" as var. -- "~/" ($HOME). ${:-"foo"} is the string "foo" as var.

View File

@ -60,4 +60,55 @@ describe("awful.completion.shell", function()
end) end)
end) end)
describe("awful.completion.shell handles $SHELL", function()
local orig_getenv = os.getenv
local gdebug = require("gears.debug")
local orig_print_warning
local print_warning_message
local os_getenv_shell
setup(function()
os.getenv = function(name) -- luacheck: ignore
if name == 'SHELL' then return os_getenv_shell end
return orig_getenv(name)
end
orig_print_warning = gdebug.print_warning
gdebug.print_warning = function(message)
print_warning_message = message
end
end)
teardown(function()
os.getenv = orig_getenv --luacheck: ignore
gdebug.print_warning = orig_print_warning
end)
before_each(function()
compl.default_shell = nil
print_warning_message = nil
end)
it("falls back to bash if unset", function()
assert.same(shell('true', 5, 1, nil), {'true', 5, {'true'}})
assert.same(print_warning_message,
'SHELL not set in environment, falling back to bash.')
assert.same(compl.default_shell, "bash")
end)
it("uses zsh from path", function()
os_getenv_shell = '/opt/bin/zsh'
assert.same(shell('true', 5, 1, nil), {'true', 5, {'true'}})
assert.same(compl.default_shell, "zsh")
assert.is_nil(print_warning_message)
end)
it("uses bash for unknown", function()
os_getenv_shell = '/dev/null'
assert.same(shell('true', 5, 1, nil), {'true', 5, {'true'}})
assert.same(compl.default_shell, "bash")
assert.is_nil(print_warning_message)
end)
end)
-- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80 -- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80