Skip to content

Commit

Permalink
Moved 'default' search path to be searched first (#357).
Browse files Browse the repository at this point in the history
  • Loading branch information
pkulchenko committed Sep 21, 2014
1 parent 4ba15eb commit b048b57
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ ide = {
oshome = os.getenv("HOME") or (iswindows and os.getenv('HOMEDRIVE') and os.getenv('HOMEPATH')
and (os.getenv('HOMEDRIVE')..os.getenv('HOMEPATH'))),
wxver = string.match(wx.wxVERSION_STRING, "[%d%.]+"),

test = {}, -- local functions used for testing
}

-- add wx.wxMOD_RAW_CONTROL as it's missing in wxlua 2.8.12.3;
Expand Down Expand Up @@ -218,11 +220,11 @@ local function setLuaPaths(mainpath, osname)
local luadev_path = (luadev
and ('LUA_DEV/?.lua;LUA_DEV/?/init.lua;LUA_DEV/lua/?.lua;LUA_DEV/lua/?/init.lua')
:gsub('LUA_DEV', (luadev:gsub('[\\/]$','')))
or "")
or nil)
local luadev_cpath = (luadev
and ('LUA_DEV/?.dll;LUA_DEV/?51.dll;LUA_DEV/clibs/?.dll;LUA_DEV/clibs/?51.dll')
:gsub('LUA_DEV', (luadev:gsub('[\\/]$','')))
or "")
or nil)

if luadev then
local path, clibs = os.getenv('PATH'), luadev:gsub('[\\/]$','')..'\\clibs'
Expand All @@ -235,12 +237,14 @@ local function setLuaPaths(mainpath, osname)
-- if the path has an excamation mark, allow Lua to expand it as this
-- expansion happens only once.
if osname == "Windows" and mainpath:find('%!') then mainpath = "!/../" end

-- if LUA_PATH or LUA_CPATH is not specified, then add ;;
-- ;; will be replaced with the default (c)path by the Lua interpreter
wx.wxSetEnv("LUA_PATH",
(os.getenv("LUA_PATH") or "") .. ';'
(os.getenv("LUA_PATH") or ';') .. ';'
.. "./?.lua;./?/init.lua;./lua/?.lua;./lua/?/init.lua" .. ';'
.. mainpath.."lualibs/?/?.lua;"..mainpath.."lualibs/?.lua" .. ';;'
.. luadev_path)
.. mainpath.."lualibs/?/?.lua;"..mainpath.."lualibs/?.lua"
.. (luadev_path and (';' .. luadev_path) or ''))

ide.osclibs = -- keep the list to use for other Lua versions
osname == "Windows" and mainpath.."bin/?.dll;"..mainpath.."bin/clibs/?.dll" or
Expand All @@ -250,9 +254,12 @@ local function setLuaPaths(mainpath, osname)
assert(false, "Unexpected OS name")

wx.wxSetEnv("LUA_CPATH",
(os.getenv("LUA_CPATH") or "") .. ';' .. ide.osclibs .. ';;' .. luadev_cpath)
(os.getenv("LUA_CPATH") or ';') .. ';' .. ide.osclibs
.. (luadev_cpath and (';' .. luadev_cpath) or ''))
end

ide.test.setLuaPaths = setLuaPaths

---------------
-- process args
local filenames = {}
Expand Down
84 changes: 84 additions & 0 deletions t/6-path.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
local LPATH = os.getenv('LUA_PATH')
local LCPATH = os.getenv('LUA_CPATH')
local LCPATH52 = os.getenv('LUA_CPATH_5_2')
local mainpath = GetPathWithSep(ide.editorFilename)

-- LUA_(C)PATH is specified, no ;; is added
wx.wxSetEnv('LUA_PATH', 'foo')
wx.wxSetEnv('LUA_CPATH', 'bar')
ide.test.setLuaPaths(mainpath, 'Windows')
ok(os.getenv('LUA_PATH'), "LUA_PATH is set.")
ok(os.getenv('LUA_CPATH'), "LUA_CPATH is set.")

ok(not (os.getenv('LUA_PATH') or ""):find(';;'),
"No ;; is added when LUA_PATH is specified.")
ok(not (os.getenv('LUA_CPATH') or ""):find(';;'),
"No ;; is added when LUA_CPATH is specified.")

-- LUA_(C)PATH is not specified, ;; is added at the beginning
wx.wxSetEnv('LUA_PATH', '')
wx.wxSetEnv('LUA_CPATH', '')
ide.test.setLuaPaths(mainpath, 'Windows')
ok((os.getenv('LUA_PATH') or ""):find(';;'),
";; is added when LUA_PATH is not specified.")
ok((os.getenv('LUA_CPATH') or ""):find(';;'),
";; is added when LUA_CPATH is not specified.")

-- ide.osclibs are added
ok((os.getenv('LUA_CPATH') or ""):find(ide.osclibs, 1, true),
"OS clibs is included in LUA_CPATH.")
ok(ide.osclibs:find('/clibs/'),
"OS clibs includes '/clibs/' folder.")

local luadev = MergeFullPath(GetPathWithSep(ide.editorFilename), '../'):gsub('[\\/]$','')

-- LUA_DEV is not used on non-Windows
wx.wxSetEnv('LUA_PATH', 'foo')
wx.wxSetEnv('LUA_CPATH', 'bar')
ide.test.setLuaPaths(mainpath, 'Unix')
ok(not os.getenv('LUA_PATH'):find(luadev..'/?.lua', 1, true),
"LUA_DEV is not used in LUA_PATH on non-Windows.")
ok(not os.getenv('LUA_CPATH'):find(luadev..'/?51.dll', 1, true),
"LUA_DEV is not used in LUA_CPATH on non-Windows.")
wx.wxSetEnv('LUA_DEV', '')

-- LUA_DEV is used on Windows
wx.wxSetEnv('LUA_PATH', 'foo')
wx.wxSetEnv('LUA_CPATH', 'bar')
wx.wxSetEnv('LUA_DEV', luadev)
ide.test.setLuaPaths(mainpath, 'Windows')
ok(os.getenv('LUA_PATH'):find(luadev..'/?.lua', 1, true), "LUA_DEV is used in LUA_PATH on Windows.")
ok(os.getenv('LUA_CPATH'):find(luadev..'/?51.dll', 1, true), "LUA_DEV is used in LUA_CPATH on Windows.")

-- stub CommandLineRun and check if interpreters set paths correctly
local CLR = CommandLineRun
local lp, lcp
local fn = wx.wxFileName("foo")
_G.CommandLineRun = function(cmd,wdir,tooutput,nohide,stringcallback,uid,endcallback)
lp, lcp, lcp52 = os.getenv('LUA_PATH'), os.getenv('LUA_CPATH'), os.getenv('LUA_CPATH_5_2')
if endcallback then endcallback() end
return
end
ide.interpreters.luadeb:frun(fn, "")
ok(lcp:find(ide.osclibs, 1, true) == 1,
"Prepend clibs to LUA_CPATH if path.lua is not set.")

local CPL = ide.config.path.lua
ide.config.path.lua = "foo"
ide.interpreters.luadeb:frun(fn, "")
ok(lcp:find(ide.osclibs, 1, true) ~= 1,
"Don't prepend clibs to LUA_CPATH if path.lua is set.")
ide.config.path.lua = CPL

-- LUA_CPATH_5_2 modified if it's already set
wx.wxSetEnv('LUA_CPATH_5_2', 'foo')
ide.interpreters.luadeb52:frun(fn, "")
ok(lcp:find(ide.osclibs, 1, true) ~= 1,
"LUA_CPATH_5_2 is modified if it is already set.")

_G.CommandLineRun = CLR

wx.wxSetEnv('LUA_PATH', LPATH or "")
wx.wxSetEnv('LUA_CPATH', LCPATH or "")
wx.wxSetEnv('LUA_CPATH_5_2', LCPATH52 or "")
ide.test.setLuaPaths(mainpath, ide.osname)

0 comments on commit b048b57

Please sign in to comment.