Set up isolated PATH directory for testing

We cannot possibly know what is in the PATH of a user. The completion
test cases however assume that there is nothing else in the PATH that
starts with `true` other than `true` itself.

These two methods create or respectively destroy a temporary PATH
directory especially for testing. We can put commands in there via
symlinks and verify the correct functionality of the complete commands.
The PATH environment variable is set and reset via GLib.
This commit is contained in:
Florian Gamböck 2017-08-01 20:01:46 +02:00
parent 828eefed79
commit a716009caa

View file

@ -15,6 +15,36 @@ if not has_bash and not has_zsh then
return
end
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
local test_dir
--- Get and create a temporary test dir based on `pat`, where %d gets replaced by