From 1f3e8794254d32d44c510e3868378b2f969b0ba6 Mon Sep 17 00:00:00 2001 From: Sewbacca Date: Mon, 4 Mar 2024 12:57:12 -0300 Subject: tests: add tests for fs.execute_env using variables with spaces Co-Authored-By: Hisham Muhammad --- spec/unit/fs_spec.lua | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ spec/util/test_env.lua | 10 ++++++++++ 2 files changed, 61 insertions(+) diff --git a/spec/unit/fs_spec.lua b/spec/unit/fs_spec.lua index 5718aea4..621a7727 100644 --- a/spec/unit/fs_spec.lua +++ b/spec/unit/fs_spec.lua @@ -1541,4 +1541,55 @@ describe("luarocks.fs #unit", function() end) end) + describe("fs.execute_env", function() + local tmpname + local tmplua + local LUA = "lua" + + local function readfile(pathname) + local file = assert(io.open(pathname, "rb")) + local data = file:read "*a" + file:close() + return data + end + + lazy_setup(function() + tmpname = os.tmpname() + + tmplua = os.tmpname() + local f = assert(io.open(tmplua, 'wb')) + f:write [[ + local out = io.open((...), 'wb') + out:write(os.getenv 'FOO') + out:close() + ]] + f:close() + LUA = test_env.testing_paths.lua + end) + + after_each(function() + os.remove(tmpname) + end) + + lazy_teardown(function() + os.remove(tmpname) + end) + + it("passes variables w/o spaces correctly", function() + fs.execute_env({ + FOO = "BAR", + }, LUA, tmplua, tmpname) + local data = readfile(tmpname) + assert.same("BAR", data) + end) + + it("passes variables w/ spaces correctly", function() + fs.execute_env({ + FOO = "BAR with spaces", + }, LUA, tmplua, tmpname) + local data = readfile(tmpname) + assert.same("BAR with spaces", data) + end) + end) + end) diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua index be75b4ec..2255d141 100644 --- a/spec/util/test_env.lua +++ b/spec/util/test_env.lua @@ -118,6 +118,16 @@ os.rename = function(a, b) -- luacheck: ignore return os_rename(V(a), V(b)) end +-- Monkeypatch incorrect tmpname's on some Lua distributions for Windows +local os_tmpname = os.tmpname +os.tmpname = function() -- luacheck:ignore + local name = os_tmpname() + if name:sub(1, 1) == '\\' then + name = os.getenv "TEMP"..name + end + return name +end + local lfs_chdir = lfs.chdir lfs.chdir = function(d) -- luacheck: ignore return lfs_chdir(V(d)) -- cgit v1.2.3-55-g6feb