From a716009caa6ebadd9f7540ca3911e80d6cdbabfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Gamb=C3=B6ck?= Date: Tue, 1 Aug 2017 20:01:46 +0200 Subject: [PATCH 1/2] 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. --- spec/awful/completion_spec.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/spec/awful/completion_spec.lua b/spec/awful/completion_spec.lua index f97e42ec8..0a087c5c1 100644 --- a/spec/awful/completion_spec.lua +++ b/spec/awful/completion_spec.lua @@ -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 From 829e887806a93fc31a47cfb60a9dd8e1ed57009e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Gamb=C3=B6ck?= Date: Tue, 1 Aug 2017 20:23:53 +0200 Subject: [PATCH 2/2] Create and destroy temp PATH in tests Since busted seems to spawn isolated child processes for every test suite, the temporary PATH directories have to be created at the beginning and destroyed at the end of a test suite. --- spec/awful/completion_spec.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/awful/completion_spec.lua b/spec/awful/completion_spec.lua index 0a087c5c1..3fdc8471b 100644 --- a/spec/awful/completion_spec.lua +++ b/spec/awful/completion_spec.lua @@ -46,6 +46,7 @@ local function remove_test_path_dir(test_path) end local test_dir +local test_path --- Get and create a temporary test dir based on `pat`, where %d gets replaced by -- the current PID. @@ -65,11 +66,13 @@ describe("awful.completion.shell in empty directory", function() io.popen = function(...) --luacheck: ignore return orig_popen(string.format('cd %s && ', test_dir) .. ...) end + test_path = get_test_path_dir() end) teardown(function() assert.True(os.remove(test_dir)) io.popen = orig_popen --luacheck: ignore + remove_test_path_dir(test_path) end) if has_bash then @@ -97,12 +100,14 @@ describe("awful.completion.shell", function() io.popen = function(...) --luacheck: ignore return orig_popen(string.format('cd %s && ', test_dir) .. ...) end + test_path = get_test_path_dir() end) teardown(function() assert.True(os.remove(test_dir .. '/localcommand')) assert.True(os.remove(test_dir)) io.popen = orig_popen --luacheck: ignore + remove_test_path_dir(test_path) end) if has_bash then @@ -159,11 +164,14 @@ describe("awful.completion.shell handles $SHELL", function() gdebug.print_warning = function(message) print_warning_message = message end + + test_path = get_test_path_dir() end) teardown(function() os.getenv = orig_getenv --luacheck: ignore gdebug.print_warning = orig_print_warning + remove_test_path_dir(test_path) end) before_each(function()