2017-04-18 00:05:06 +02:00
|
|
|
local compl = require("awful.completion")
|
|
|
|
local shell = function(...)
|
|
|
|
local command, pos, matches = compl.shell(...)
|
|
|
|
return {command, pos, matches}
|
|
|
|
end
|
|
|
|
local gfs = require("gears.filesystem")
|
2017-07-03 21:28:13 +02:00
|
|
|
local Gio = require("lgi").Gio
|
|
|
|
local GLib = require("lgi").GLib
|
|
|
|
|
|
|
|
local has_bash = GLib.find_program_in_path("bash")
|
|
|
|
local has_zsh = GLib.find_program_in_path("zsh")
|
|
|
|
|
|
|
|
if not has_bash and not has_zsh then
|
|
|
|
print('Skipping spec/awful/completion_spec.lua: bash and zsh are not installed.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-08-01 20:01:46 +02:00
|
|
|
local orig_path_env = GLib.getenv("PATH")
|
|
|
|
local required_commands = {"bash", "zsh", "sort"}
|
|
|
|
|
|
|
|
-- Change PATH
|
|
|
|
local function get_test_path_dir()
|
|
|
|
local test_path = Gio.File.new_tmp("awesome-tests-path-XXXXXX")
|
|
|
|
test_path:delete()
|
|
|
|
test_path:make_directory()
|
|
|
|
|
|
|
|
for _, cmd in ipairs(required_commands) do
|
|
|
|
local target = GLib.find_program_in_path(cmd)
|
|
|
|
if target then
|
|
|
|
test_path:get_child(cmd):make_symbolic_link(target)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test_path = test_path:get_path()
|
|
|
|
GLib.setenv("PATH", test_path, true)
|
|
|
|
return test_path
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Reset PATH
|
|
|
|
local function remove_test_path_dir(test_path)
|
|
|
|
GLib.setenv("PATH", orig_path_env, true)
|
|
|
|
for _, cmd in ipairs(required_commands) do
|
|
|
|
os.remove(test_path .. "/" .. cmd)
|
|
|
|
end
|
|
|
|
os.remove(test_path)
|
|
|
|
end
|
|
|
|
|
2017-07-03 21:28:13 +02:00
|
|
|
local test_dir
|
2017-08-01 20:23:53 +02:00
|
|
|
local test_path
|
2017-07-03 21:28:13 +02:00
|
|
|
|
|
|
|
--- Get and create a temporary test dir based on `pat`, where %d gets replaced by
|
|
|
|
-- the current PID.
|
|
|
|
local function get_test_dir()
|
|
|
|
local tfile = Gio.File.new_tmp("awesome-tests-XXXXXX")
|
|
|
|
local path = tfile:get_path()
|
|
|
|
tfile:delete()
|
|
|
|
gfs.make_directories(path)
|
|
|
|
return path
|
|
|
|
end
|
2017-04-18 00:05:06 +02:00
|
|
|
|
|
|
|
describe("awful.completion.shell in empty directory", function()
|
|
|
|
local orig_popen = io.popen
|
|
|
|
|
|
|
|
setup(function()
|
2017-07-03 21:28:13 +02:00
|
|
|
test_dir = get_test_dir()
|
2017-04-18 00:05:06 +02:00
|
|
|
io.popen = function(...) --luacheck: ignore
|
2017-07-03 21:28:13 +02:00
|
|
|
return orig_popen(string.format('cd %s && ', test_dir) .. ...)
|
2017-04-18 00:05:06 +02:00
|
|
|
end
|
2017-08-01 20:23:53 +02:00
|
|
|
test_path = get_test_path_dir()
|
2017-04-18 00:05:06 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
teardown(function()
|
2017-07-03 21:28:13 +02:00
|
|
|
assert.True(os.remove(test_dir))
|
2017-04-18 00:05:06 +02:00
|
|
|
io.popen = orig_popen --luacheck: ignore
|
2017-08-01 20:23:53 +02:00
|
|
|
remove_test_path_dir(test_path)
|
2017-04-18 00:05:06 +02:00
|
|
|
end)
|
|
|
|
|
2017-07-03 21:28:13 +02:00
|
|
|
if has_bash then
|
|
|
|
it("handles completion of true (bash)", function()
|
|
|
|
assert.same(shell('true', 5, 1, 'bash'), {'true', 5, {'true'}})
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
if has_zsh then
|
|
|
|
it("handles completion of true (zsh)", function()
|
|
|
|
assert.same(shell('true', 5, 1, 'zsh'), {'true', 5, {'true'}})
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
it("handles completion of true (nil)", function()
|
2017-04-18 00:05:06 +02:00
|
|
|
assert.same(shell('true', 5, 1, nil), {'true', 5, {'true'}})
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe("awful.completion.shell", function()
|
|
|
|
local orig_popen = io.popen
|
|
|
|
|
|
|
|
setup(function()
|
2017-07-03 21:28:13 +02:00
|
|
|
test_dir = get_test_dir()
|
|
|
|
os.execute(string.format(
|
|
|
|
'cd %s && touch localcommand && chmod +x localcommand', test_dir))
|
2017-04-18 00:05:06 +02:00
|
|
|
io.popen = function(...) --luacheck: ignore
|
2017-07-03 21:28:13 +02:00
|
|
|
return orig_popen(string.format('cd %s && ', test_dir) .. ...)
|
2017-04-18 00:05:06 +02:00
|
|
|
end
|
2017-08-01 20:23:53 +02:00
|
|
|
test_path = get_test_path_dir()
|
2017-04-18 00:05:06 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
teardown(function()
|
2017-07-03 21:28:13 +02:00
|
|
|
assert.True(os.remove(test_dir .. '/localcommand'))
|
|
|
|
assert.True(os.remove(test_dir))
|
2017-04-18 00:05:06 +02:00
|
|
|
io.popen = orig_popen --luacheck: ignore
|
2017-08-01 20:23:53 +02:00
|
|
|
remove_test_path_dir(test_path)
|
2017-04-18 00:05:06 +02:00
|
|
|
end)
|
|
|
|
|
2017-07-03 21:28:13 +02:00
|
|
|
if has_bash then
|
|
|
|
it("handles empty input (bash)", function()
|
|
|
|
assert.same(shell('', 1, 1, 'bash'), {'', 1})
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
if has_zsh then
|
|
|
|
it("handles empty input (zsh)", function()
|
|
|
|
assert.same(shell('', 1, 1, 'zsh'), {'', 1})
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
it("handles empty input (nil)", function()
|
2017-04-18 00:05:06 +02:00
|
|
|
assert.same(shell('', 1, 1, nil), {'', 1})
|
|
|
|
end)
|
|
|
|
|
2017-07-03 21:28:13 +02:00
|
|
|
if has_bash then
|
|
|
|
it("completes local command (bash)", function()
|
|
|
|
assert.same(shell('./localcomm', 12, 1, 'bash'), {'./localcommand', 15, {'./localcommand'}})
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
if has_zsh then
|
|
|
|
it("completes local command (zsh)", function()
|
|
|
|
assert.same(shell('./localcomm', 12, 1, 'zsh'), {'./localcommand', 15, {'./localcommand'}})
|
|
|
|
end)
|
|
|
|
end
|
2017-04-18 00:05:06 +02:00
|
|
|
|
2017-07-03 21:28:13 +02:00
|
|
|
if has_bash then
|
|
|
|
it("completes local file (bash)", function()
|
2017-07-02 16:06:54 +02:00
|
|
|
assert.same(shell('ls l', 5, 1, 'bash'), {'ls localcommand', 16, {'localcommand'}})
|
2017-07-03 21:28:13 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
if has_zsh then
|
|
|
|
it("completes local file (zsh)", function()
|
2017-07-02 16:06:54 +02:00
|
|
|
assert.same(shell('ls l', 5, 1, 'zsh'), {'ls localcommand', 16, {'localcommand'}})
|
2017-07-03 21:28:13 +02:00
|
|
|
end)
|
|
|
|
end
|
2017-04-18 00:05:06 +02:00
|
|
|
end)
|
|
|
|
|
2017-07-03 19:26:10 +02:00
|
|
|
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
|
2017-08-01 20:23:53 +02:00
|
|
|
|
|
|
|
test_path = get_test_path_dir()
|
2017-07-03 19:26:10 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
teardown(function()
|
|
|
|
os.getenv = orig_getenv --luacheck: ignore
|
|
|
|
gdebug.print_warning = orig_print_warning
|
2017-08-01 20:23:53 +02:00
|
|
|
remove_test_path_dir(test_path)
|
2017-07-03 19:26:10 +02:00
|
|
|
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)
|
|
|
|
|
2017-04-18 00:05:06 +02:00
|
|
|
-- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80
|