main.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. # testing special comment on first line
  2. -- $Id: testes/main.lua $
  3. -- See Copyright Notice in file all.lua
  4. -- most (all?) tests here assume a reasonable "Unix-like" shell
  5. if _port then return end
  6. -- use only "double quotes" inside shell scripts (better change to
  7. -- run on Windows)
  8. print ("testing stand-alone interpreter")
  9. assert(os.execute()) -- machine has a system command
  10. local arg = arg or ARG
  11. local prog = os.tmpname()
  12. local otherprog = os.tmpname()
  13. local out = os.tmpname()
  14. local progname
  15. do
  16. local i = 0
  17. while arg[i] do i=i-1 end
  18. progname = arg[i+1]
  19. end
  20. print("progname: "..progname)
  21. local prepfile = function (s, p)
  22. p = p or prog
  23. io.output(p)
  24. io.write(s)
  25. assert(io.close())
  26. end
  27. local function getoutput ()
  28. io.input(out)
  29. local t = io.read("a")
  30. io.input():close()
  31. assert(os.remove(out))
  32. return t
  33. end
  34. local function checkprogout (s)
  35. local t = getoutput()
  36. for line in string.gmatch(s, ".-\n") do
  37. assert(string.find(t, line, 1, true))
  38. end
  39. end
  40. local function checkout (s)
  41. local t = getoutput()
  42. if s ~= t then print(string.format("'%s' - '%s'\n", s, t)) end
  43. assert(s == t)
  44. return t
  45. end
  46. local function RUN (p, ...)
  47. p = string.gsub(p, "lua", '"'..progname..'"', 1)
  48. local s = string.format(p, ...)
  49. assert(os.execute(s))
  50. end
  51. local function NoRun (msg, p, ...)
  52. p = string.gsub(p, "lua", '"'..progname..'"', 1)
  53. local s = string.format(p, ...)
  54. s = string.format("%s 2> %s", s, out) -- will send error to 'out'
  55. assert(not os.execute(s))
  56. assert(string.find(getoutput(), msg, 1, true)) -- check error message
  57. end
  58. RUN('lua -v')
  59. print(string.format("(temporary program file used in these tests: %s)", prog))
  60. -- running stdin as a file
  61. prepfile""
  62. RUN('lua - < %s > %s', prog, out)
  63. checkout("")
  64. prepfile[[
  65. print(
  66. 1, a
  67. )
  68. ]]
  69. RUN('lua - < %s > %s', prog, out)
  70. checkout("1\tnil\n")
  71. RUN('echo "print(10)\nprint(2)\n" | lua > %s', out)
  72. checkout("10\n2\n")
  73. -- test option '-'
  74. RUN('echo "print(arg[1])" | lua - -h > %s', out)
  75. checkout("-h\n")
  76. -- test environment variables used by Lua
  77. prepfile("print(package.path)")
  78. -- test LUA_PATH
  79. RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out)
  80. checkout("x\n")
  81. -- test LUA_PATH_version
  82. RUN('env LUA_INIT= LUA_PATH_5_4=y LUA_PATH=x lua %s > %s', prog, out)
  83. checkout("y\n")
  84. -- test LUA_CPATH
  85. prepfile("print(package.cpath)")
  86. RUN('env LUA_INIT= LUA_CPATH=xuxu lua %s > %s', prog, out)
  87. checkout("xuxu\n")
  88. -- test LUA_CPATH_version
  89. RUN('env LUA_INIT= LUA_CPATH_5_4=yacc LUA_CPATH=x lua %s > %s', prog, out)
  90. checkout("yacc\n")
  91. -- test LUA_INIT (and its access to 'arg' table)
  92. prepfile("print(X)")
  93. RUN('env LUA_INIT="X=tonumber(arg[1])" lua %s 3.2 > %s', prog, out)
  94. checkout("3.2\n")
  95. -- test LUA_INIT_version
  96. prepfile("print(X)")
  97. RUN('env LUA_INIT_5_4="X=10" LUA_INIT="X=3" lua %s > %s', prog, out)
  98. checkout("10\n")
  99. -- test LUA_INIT for files
  100. prepfile("x = x or 10; print(x); x = x + 1")
  101. RUN('env LUA_INIT="@%s" lua %s > %s', prog, prog, out)
  102. checkout("10\n11\n")
  103. -- test errors in LUA_INIT
  104. NoRun('LUA_INIT:1: msg', 'env LUA_INIT="error(\'msg\')" lua')
  105. -- test option '-E'
  106. local defaultpath, defaultCpath
  107. do
  108. prepfile("print(package.path, package.cpath)")
  109. RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s',
  110. prog, out)
  111. local output = getoutput()
  112. defaultpath = string.match(output, "^(.-)\t")
  113. defaultCpath = string.match(output, "\t(.-)$")
  114. -- running with an empty environment
  115. RUN('env -i lua %s > %s', prog, out)
  116. local out = getoutput()
  117. assert(defaultpath == string.match(output, "^(.-)\t"))
  118. assert(defaultCpath == string.match(output, "\t(.-)$"))
  119. end
  120. -- paths did not change
  121. assert(not string.find(defaultpath, "xxx") and
  122. string.find(defaultpath, "lua") and
  123. not string.find(defaultCpath, "xxx") and
  124. string.find(defaultCpath, "lua"))
  125. -- test replacement of ';;' to default path
  126. local function convert (p)
  127. prepfile("print(package.path)")
  128. RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out)
  129. local expected = getoutput()
  130. expected = string.sub(expected, 1, -2) -- cut final end of line
  131. if string.find(p, ";;") then
  132. p = string.gsub(p, ";;", ";"..defaultpath..";")
  133. p = string.gsub(p, "^;", "") -- remove ';' at the beginning
  134. p = string.gsub(p, ";$", "") -- remove ';' at the end
  135. end
  136. assert(p == expected)
  137. end
  138. convert(";")
  139. convert(";;")
  140. convert("a;;b")
  141. convert(";;b")
  142. convert("a;;")
  143. convert("a;b;;c")
  144. -- test -l over multiple libraries
  145. prepfile("print(1); a=2; return {x=15}")
  146. prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog)
  147. RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
  148. checkout("1\n2\n15\n2\n15\n")
  149. -- test 'arg' table
  150. local a = [[
  151. assert(#arg == 3 and arg[1] == 'a' and
  152. arg[2] == 'b' and arg[3] == 'c')
  153. assert(arg[-1] == '--' and arg[-2] == "-e " and arg[-3] == '%s')
  154. assert(arg[4] == undef and arg[-4] == undef)
  155. local a, b, c = ...
  156. assert(... == 'a' and a == 'a' and b == 'b' and c == 'c')
  157. ]]
  158. a = string.format(a, progname)
  159. prepfile(a)
  160. RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command
  161. -- test 'arg' availability in libraries
  162. prepfile"assert(arg)"
  163. prepfile("assert(arg)", otherprog)
  164. RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog)
  165. -- test messing up the 'arg' table
  166. RUN('echo "print(...)" | lua -e "arg[1] = 100" - > %s', out)
  167. checkout("100\n")
  168. NoRun("'arg' is not a table", 'echo "" | lua -e "arg = 1" -')
  169. -- test error in 'print'
  170. RUN('echo 10 | lua -e "print=nil" -i > /dev/null 2> %s', out)
  171. assert(string.find(getoutput(), "error calling 'print'"))
  172. -- test 'debug.debug'
  173. RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
  174. checkout("lua_debug> 1000lua_debug> ")
  175. -- test many arguments
  176. prepfile[[print(({...})[30])]]
  177. RUN('lua %s %s > %s', prog, string.rep(" a", 30), out)
  178. checkout("a\n")
  179. RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out)
  180. checkout("1\n3\n")
  181. -- test iteractive mode
  182. prepfile[[
  183. (6*2-6) -- ===
  184. a =
  185. 10
  186. print(a)
  187. a]]
  188. RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
  189. checkprogout("6\n10\n10\n\n")
  190. prepfile("a = [[b\nc\nd\ne]]\n=a")
  191. RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
  192. checkprogout("b\nc\nd\ne\n\n")
  193. prompt = "alo"
  194. prepfile[[ --
  195. a = 2
  196. ]]
  197. RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out)
  198. local t = getoutput()
  199. assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt))
  200. -- test for error objects
  201. prepfile[[
  202. debug = require "debug"
  203. m = {x=0}
  204. setmetatable(m, {__tostring = function(x)
  205. return tostring(debug.getinfo(4).currentline + x.x)
  206. end})
  207. error(m)
  208. ]]
  209. NoRun(progname .. ": 6\n", [[lua %s]], prog)
  210. prepfile("error{}")
  211. NoRun("error object is a table value", [[lua %s]], prog)
  212. -- chunk broken in many lines
  213. s = [=[ --
  214. function f ( x )
  215. local a = [[
  216. xuxu
  217. ]]
  218. local b = "\
  219. xuxu\n"
  220. if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]]
  221. return x + 1
  222. --\\
  223. end
  224. return( f( 100 ) )
  225. assert( a == b )
  226. do return f( 11 ) end ]=]
  227. s = string.gsub(s, ' ', '\n\n') -- change all spaces for newlines
  228. prepfile(s)
  229. RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
  230. checkprogout("101\n13\t22\n\n")
  231. prepfile[[#comment in 1st line without \n at the end]]
  232. RUN('lua %s', prog)
  233. prepfile[[#test line number when file starts with comment line
  234. debug = require"debug"
  235. print(debug.getinfo(1).currentline)
  236. ]]
  237. RUN('lua %s > %s', prog, out)
  238. checkprogout('3')
  239. -- close Lua with an open file
  240. prepfile(string.format([[io.output(%q); io.write('alo')]], out))
  241. RUN('lua %s', prog)
  242. checkout('alo')
  243. -- bug in 5.2 beta (extra \0 after version line)
  244. RUN([[lua -v -e"print'hello'" > %s]], out)
  245. t = getoutput()
  246. assert(string.find(t, "PUC%-Rio\nhello"))
  247. -- testing os.exit
  248. prepfile("os.exit(nil, true)")
  249. RUN('lua %s', prog)
  250. prepfile("os.exit(0, true)")
  251. RUN('lua %s', prog)
  252. prepfile("os.exit(true, true)")
  253. RUN('lua %s', prog)
  254. prepfile("os.exit(1, true)")
  255. NoRun("", "lua %s", prog) -- no message
  256. prepfile("os.exit(false, true)")
  257. NoRun("", "lua %s", prog) -- no message
  258. -- to-be-closed variables in main chunk
  259. prepfile[[
  260. local <toclose> x = function (err)
  261. assert(err == 120)
  262. print("Ok")
  263. end
  264. local <toclose> e1 = function () error(120) end
  265. os.exit(true, true)
  266. ]]
  267. RUN('lua %s > %s', prog, out)
  268. checkprogout("Ok")
  269. -- remove temporary files
  270. assert(os.remove(prog))
  271. assert(os.remove(otherprog))
  272. assert(not os.remove(out))
  273. -- invalid options
  274. NoRun("unrecognized option '-h'", "lua -h")
  275. NoRun("unrecognized option '---'", "lua ---")
  276. NoRun("unrecognized option '-Ex'", "lua -Ex")
  277. NoRun("unrecognized option '-vv'", "lua -vv")
  278. NoRun("unrecognized option '-iv'", "lua -iv")
  279. NoRun("'-e' needs argument", "lua -e")
  280. NoRun("syntax error", "lua -e a")
  281. NoRun("'-l' needs argument", "lua -l")
  282. if T then -- test library?
  283. print("testing 'not enough memory' to create a state")
  284. NoRun("not enough memory", "env MEMLIMIT=100 lua")
  285. -- testing 'warn'
  286. warn("@123", "456", "789")
  287. assert(_WARN == "@123456789")
  288. end
  289. do
  290. -- 'warn' must get at least one argument
  291. local st, msg = pcall(warn)
  292. assert(string.find(msg, "string expected"))
  293. -- 'warn' does not leave unfinished warning in case of errors
  294. -- (message would appear in next warning)
  295. st, msg = pcall(warn, "SHOULD NOT APPEAR", {})
  296. assert(string.find(msg, "string expected"))
  297. end
  298. print('+')
  299. print('testing Ctrl C')
  300. do
  301. -- interrupt a script
  302. local function kill (pid)
  303. return os.execute(string.format('kill -INT %s 2> /dev/null', pid))
  304. end
  305. -- function to run a script in background, returning its output file
  306. -- descriptor and its pid
  307. local function runback (luaprg)
  308. -- shell script to run 'luaprg' in background and echo its pid
  309. local shellprg = string.format('%s -e "%s" & echo $!', progname, luaprg)
  310. local f = io.popen(shellprg, "r") -- run shell script
  311. local pid = f:read() -- get pid for Lua script
  312. print("(if test fails now, it may leave a Lua script running in \z
  313. background, pid " .. pid .. ")")
  314. return f, pid
  315. end
  316. -- Lua script that runs protected infinite loop and then prints '42'
  317. local f, pid = runback[[
  318. pcall(function () print(12); while true do end end); print(42)]]
  319. -- wait until script is inside 'pcall'
  320. assert(f:read() == "12")
  321. kill(pid) -- send INT signal to Lua script
  322. -- check that 'pcall' captured the exception and script continued running
  323. assert(f:read() == "42") -- expected output
  324. assert(f:close())
  325. print("done")
  326. -- Lua script in a long unbreakable search
  327. local f, pid = runback[[
  328. print(15); string.find(string.rep('a', 100000), '.*b')]]
  329. -- wait (so script can reach the loop)
  330. assert(f:read() == "15")
  331. assert(os.execute("sleep 1"))
  332. -- must send at least two INT signals to stop this Lua script
  333. local n = 100
  334. for i = 0, 100 do -- keep sending signals
  335. if not kill(pid) then -- until it fails
  336. n = i -- number of non-failed kills
  337. break
  338. end
  339. end
  340. assert(f:close())
  341. assert(n >= 2)
  342. print(string.format("done (with %d kills)", n))
  343. end
  344. print("OK")