main.lua 12 KB

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