helpers.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. local assert = require('luassert')
  2. local luv = require('luv')
  3. local lfs = require('lfs')
  4. local relpath = require('pl.path').relpath
  5. local quote_me = '[^.%w%+%-%@%_%/]' -- complement (needn't quote)
  6. local function shell_quote(str)
  7. if string.find(str, quote_me) or str == '' then
  8. return '"' .. str:gsub('[$%%"\\]', '\\%0') .. '"'
  9. else
  10. return str
  11. end
  12. end
  13. local function argss_to_cmd(...)
  14. local cmd = ''
  15. for i = 1, select('#', ...) do
  16. local arg = select(i, ...)
  17. if type(arg) == 'string' then
  18. cmd = cmd .. ' ' ..shell_quote(arg)
  19. else
  20. for _, subarg in ipairs(arg) do
  21. cmd = cmd .. ' ' .. shell_quote(subarg)
  22. end
  23. end
  24. end
  25. return cmd
  26. end
  27. local function popen_r(...)
  28. return io.popen(argss_to_cmd(...), 'r')
  29. end
  30. local function popen_w(...)
  31. return io.popen(argss_to_cmd(...), 'w')
  32. end
  33. -- sleeps the test runner (_not_ the nvim instance)
  34. local function sleep(ms)
  35. luv.sleep(ms)
  36. end
  37. local check_logs_useless_lines = {
  38. ['Warning: noted but unhandled ioctl']=1,
  39. ['could cause spurious value errors to appear']=2,
  40. ['See README_MISSING_SYSCALL_OR_IOCTL for guidance']=3,
  41. }
  42. local function eq(expected, actual, ctx)
  43. return assert.are.same(expected, actual, ctx)
  44. end
  45. local function neq(expected, actual)
  46. return assert.are_not.same(expected, actual)
  47. end
  48. local function ok(res)
  49. return assert.is_true(res)
  50. end
  51. local function near(actual, expected, tolerance)
  52. return assert.is.near(actual, expected, tolerance)
  53. end
  54. local function matches(pat, actual)
  55. if nil ~= string.match(actual, pat) then
  56. return true
  57. end
  58. error(string.format('Pattern does not match.\nPattern:\n%s\nActual:\n%s', pat, actual))
  59. end
  60. -- Expect an error matching pattern `pat`.
  61. local function expect_err(pat, ...)
  62. local fn = select(1, ...)
  63. local fn_args = {...}
  64. table.remove(fn_args, 1)
  65. assert.error_matches(function() return fn(unpack(fn_args)) end, pat)
  66. end
  67. -- initial_path: directory to recurse into
  68. -- re: include pattern (string)
  69. -- exc_re: exclude pattern(s) (string or table)
  70. local function glob(initial_path, re, exc_re)
  71. exc_re = type(exc_re) == 'table' and exc_re or { exc_re }
  72. local paths_to_check = {initial_path}
  73. local ret = {}
  74. local checked_files = {}
  75. local function is_excluded(path)
  76. for _, pat in pairs(exc_re) do
  77. if path:match(pat) then return true end
  78. end
  79. return false
  80. end
  81. if is_excluded(initial_path) then
  82. return ret
  83. end
  84. while #paths_to_check > 0 do
  85. local cur_path = paths_to_check[#paths_to_check]
  86. paths_to_check[#paths_to_check] = nil
  87. for e in lfs.dir(cur_path) do
  88. local full_path = cur_path .. '/' .. e
  89. local checked_path = full_path:sub(#initial_path + 1)
  90. if (not is_excluded(checked_path)) and e:sub(1, 1) ~= '.' then
  91. local attrs = lfs.attributes(full_path)
  92. if attrs then
  93. local check_key = attrs.dev .. ':' .. tostring(attrs.ino)
  94. if not checked_files[check_key] then
  95. checked_files[check_key] = true
  96. if attrs.mode == 'directory' then
  97. paths_to_check[#paths_to_check + 1] = full_path
  98. elseif not re or checked_path:match(re) then
  99. ret[#ret + 1] = full_path
  100. end
  101. end
  102. end
  103. end
  104. end
  105. end
  106. return ret
  107. end
  108. local function check_logs()
  109. local log_dir = os.getenv('LOG_DIR')
  110. local runtime_errors = 0
  111. if log_dir and lfs.attributes(log_dir, 'mode') == 'directory' then
  112. for tail in lfs.dir(log_dir) do
  113. if tail:sub(1, 30) == 'valgrind-' or tail:find('san%.') then
  114. local file = log_dir .. '/' .. tail
  115. local fd = io.open(file)
  116. local start_msg = ('='):rep(20) .. ' File ' .. file .. ' ' .. ('='):rep(20)
  117. local lines = {}
  118. local warning_line = 0
  119. for line in fd:lines() do
  120. local cur_warning_line = check_logs_useless_lines[line]
  121. if cur_warning_line == warning_line + 1 then
  122. warning_line = cur_warning_line
  123. else
  124. lines[#lines + 1] = line
  125. end
  126. end
  127. fd:close()
  128. os.remove(file)
  129. if #lines > 0 then
  130. local out = io.stdout
  131. out:write(start_msg .. '\n')
  132. out:write('= ' .. table.concat(lines, '\n= ') .. '\n')
  133. out:write(select(1, start_msg:gsub('.', '=')) .. '\n')
  134. runtime_errors = runtime_errors + 1
  135. end
  136. end
  137. end
  138. end
  139. assert(0 == runtime_errors)
  140. end
  141. -- Tries to get platform name from $SYSTEM_NAME, uname; fallback is "Windows".
  142. local uname = (function()
  143. local platform = nil
  144. return (function()
  145. if platform then
  146. return platform
  147. end
  148. platform = os.getenv("SYSTEM_NAME")
  149. if platform then
  150. return platform
  151. end
  152. local status, f = pcall(popen_r, 'uname', '-s')
  153. if status then
  154. platform = f:read("*l")
  155. f:close()
  156. else
  157. platform = 'Windows'
  158. end
  159. return platform
  160. end)
  161. end)()
  162. local function tmpdir_get()
  163. return os.getenv('TMPDIR') and os.getenv('TMPDIR') or os.getenv('TEMP')
  164. end
  165. -- Is temp directory `dir` defined local to the project workspace?
  166. local function tmpdir_is_local(dir)
  167. return not not (dir and string.find(dir, 'Xtest'))
  168. end
  169. local tmpname = (function()
  170. local seq = 0
  171. local tmpdir = tmpdir_get()
  172. return (function()
  173. if tmpdir_is_local(tmpdir) then
  174. -- Cannot control os.tmpname() dir, so hack our own tmpname() impl.
  175. seq = seq + 1
  176. local fname = tmpdir..'/nvim-test-lua-'..seq
  177. io.open(fname, 'w'):close()
  178. return fname
  179. else
  180. local fname = os.tmpname()
  181. if uname() == 'Windows' and fname:sub(1, 2) == '\\s' then
  182. -- In Windows tmpname() returns a filename starting with
  183. -- special sequence \s, prepend $TEMP path
  184. return tmpdir..fname
  185. elseif fname:match('^/tmp') and uname() == 'Darwin' then
  186. -- In OS X /tmp links to /private/tmp
  187. return '/private'..fname
  188. else
  189. return fname
  190. end
  191. end
  192. end)
  193. end)()
  194. local function map(func, tab)
  195. local rettab = {}
  196. for k, v in pairs(tab) do
  197. rettab[k] = func(v)
  198. end
  199. return rettab
  200. end
  201. local function filter(filter_func, tab)
  202. local rettab = {}
  203. for _, entry in pairs(tab) do
  204. if filter_func(entry) then
  205. table.insert(rettab, entry)
  206. end
  207. end
  208. return rettab
  209. end
  210. local function hasenv(name)
  211. local env = os.getenv(name)
  212. if env and env ~= '' then
  213. return env
  214. end
  215. return nil
  216. end
  217. local tests_skipped = 0
  218. local function check_cores(app, force)
  219. app = app or 'build/bin/nvim'
  220. local initial_path, re, exc_re
  221. local gdb_db_cmd = 'gdb -n -batch -ex "thread apply all bt full" "$_NVIM_TEST_APP" -c "$_NVIM_TEST_CORE"'
  222. local lldb_db_cmd = 'lldb -Q -o "bt all" -f "$_NVIM_TEST_APP" -c "$_NVIM_TEST_CORE"'
  223. local random_skip = false
  224. -- Workspace-local $TMPDIR, scrubbed and pattern-escaped.
  225. -- "./Xtest-tmpdir/" => "Xtest%-tmpdir"
  226. local local_tmpdir = (tmpdir_is_local(tmpdir_get())
  227. and relpath(tmpdir_get()):gsub('^[ ./]+',''):gsub('%/+$',''):gsub('([^%w])', '%%%1')
  228. or nil)
  229. local db_cmd
  230. if hasenv('NVIM_TEST_CORE_GLOB_DIRECTORY') then
  231. initial_path = os.getenv('NVIM_TEST_CORE_GLOB_DIRECTORY')
  232. re = os.getenv('NVIM_TEST_CORE_GLOB_RE')
  233. exc_re = { os.getenv('NVIM_TEST_CORE_EXC_RE'), local_tmpdir }
  234. db_cmd = os.getenv('NVIM_TEST_CORE_DB_CMD') or gdb_db_cmd
  235. random_skip = os.getenv('NVIM_TEST_CORE_RANDOM_SKIP')
  236. elseif os.getenv('TRAVIS_OS_NAME') == 'osx' then
  237. initial_path = '/cores'
  238. re = nil
  239. exc_re = { local_tmpdir }
  240. db_cmd = lldb_db_cmd
  241. else
  242. initial_path = '.'
  243. re = '/core[^/]*$'
  244. exc_re = { '^/%.deps$', local_tmpdir, '^/%node_modules$' }
  245. db_cmd = gdb_db_cmd
  246. random_skip = true
  247. end
  248. -- Finding cores takes too much time on linux
  249. if not force and random_skip and math.random() < 0.9 then
  250. tests_skipped = tests_skipped + 1
  251. return
  252. end
  253. local cores = glob(initial_path, re, exc_re)
  254. local found_cores = 0
  255. local out = io.stdout
  256. for _, core in ipairs(cores) do
  257. local len = 80 - #core - #('Core file ') - 2
  258. local esigns = ('='):rep(len / 2)
  259. out:write(('\n%s Core file %s %s\n'):format(esigns, core, esigns))
  260. out:flush()
  261. os.execute(db_cmd:gsub('%$_NVIM_TEST_APP', app):gsub('%$_NVIM_TEST_CORE', core) .. ' 2>&1')
  262. out:write('\n')
  263. found_cores = found_cores + 1
  264. os.remove(core)
  265. end
  266. if found_cores ~= 0 then
  267. out:write(('\nTests covered by this check: %u\n'):format(tests_skipped + 1))
  268. end
  269. tests_skipped = 0
  270. if found_cores > 0 then
  271. error("crash detected (see above)")
  272. end
  273. end
  274. local function which(exe)
  275. local pipe = popen_r('which', exe)
  276. local ret = pipe:read('*a')
  277. pipe:close()
  278. if ret == '' then
  279. return nil
  280. else
  281. return ret:sub(1, -2)
  282. end
  283. end
  284. local function repeated_read_cmd(...)
  285. for _ = 1, 10 do
  286. local stream = popen_r(...)
  287. local ret = stream:read('*a')
  288. stream:close()
  289. if ret then
  290. return ret
  291. end
  292. end
  293. print('ERROR: Failed to execute ' .. argss_to_cmd(...) .. ': nil return after 10 attempts')
  294. return nil
  295. end
  296. local function shallowcopy(orig)
  297. if type(orig) ~= 'table' then
  298. return orig
  299. end
  300. local copy = {}
  301. for orig_key, orig_value in pairs(orig) do
  302. copy[orig_key] = orig_value
  303. end
  304. return copy
  305. end
  306. local deepcopy
  307. local function id(v)
  308. return v
  309. end
  310. local deepcopy_funcs = {
  311. table = function(orig)
  312. local copy = {}
  313. for k, v in pairs(orig) do
  314. copy[deepcopy(k)] = deepcopy(v)
  315. end
  316. return copy
  317. end,
  318. number = id,
  319. string = id,
  320. ['nil'] = id,
  321. boolean = id,
  322. }
  323. deepcopy = function(orig)
  324. return deepcopy_funcs[type(orig)](orig)
  325. end
  326. local REMOVE_THIS = {}
  327. local function mergedicts_copy(d1, d2)
  328. local ret = shallowcopy(d1)
  329. for k, v in pairs(d2) do
  330. if d2[k] == REMOVE_THIS then
  331. ret[k] = nil
  332. elseif type(d1[k]) == 'table' and type(v) == 'table' then
  333. ret[k] = mergedicts_copy(d1[k], v)
  334. else
  335. ret[k] = v
  336. end
  337. end
  338. return ret
  339. end
  340. -- dictdiff: find a diff so that mergedicts_copy(d1, diff) is equal to d2
  341. --
  342. -- Note: does not do copies of d2 values used.
  343. local function dictdiff(d1, d2)
  344. local ret = {}
  345. local hasdiff = false
  346. for k, v in pairs(d1) do
  347. if d2[k] == nil then
  348. hasdiff = true
  349. ret[k] = REMOVE_THIS
  350. elseif type(v) == type(d2[k]) then
  351. if type(v) == 'table' then
  352. local subdiff = dictdiff(v, d2[k])
  353. if subdiff ~= nil then
  354. hasdiff = true
  355. ret[k] = subdiff
  356. end
  357. elseif v ~= d2[k] then
  358. ret[k] = d2[k]
  359. hasdiff = true
  360. end
  361. else
  362. ret[k] = d2[k]
  363. hasdiff = true
  364. end
  365. end
  366. for k, v in pairs(d2) do
  367. if d1[k] == nil then
  368. ret[k] = shallowcopy(v)
  369. hasdiff = true
  370. end
  371. end
  372. if hasdiff then
  373. return ret
  374. else
  375. return nil
  376. end
  377. end
  378. local function updated(d, d2)
  379. for k, v in pairs(d2) do
  380. d[k] = v
  381. end
  382. return d
  383. end
  384. local function concat_tables(...)
  385. local ret = {}
  386. for i = 1, select('#', ...) do
  387. local tbl = select(i, ...)
  388. if tbl then
  389. for _, v in ipairs(tbl) do
  390. ret[#ret + 1] = v
  391. end
  392. end
  393. end
  394. return ret
  395. end
  396. local function dedent(str, leave_indent)
  397. -- find minimum common indent across lines
  398. local indent = nil
  399. for line in str:gmatch('[^\n]+') do
  400. local line_indent = line:match('^%s+') or ''
  401. if indent == nil or #line_indent < #indent then
  402. indent = line_indent
  403. end
  404. end
  405. if indent == nil or #indent == 0 then
  406. -- no minimum common indent
  407. return str
  408. end
  409. local left_indent = (' '):rep(leave_indent or 0)
  410. -- create a pattern for the indent
  411. indent = indent:gsub('%s', '[ \t]')
  412. -- strip it from the first line
  413. str = str:gsub('^'..indent, left_indent)
  414. -- strip it from the remaining lines
  415. str = str:gsub('[\n]'..indent, '\n' .. left_indent)
  416. return str
  417. end
  418. local function format_float(v)
  419. -- On windows exponent appears to have three digits and not two
  420. local ret = ('%.6e'):format(v)
  421. local l, f, es, e = ret:match('^(%-?%d)%.(%d+)e([+%-])0*(%d%d+)$')
  422. return l .. '.' .. f .. 'e' .. es .. e
  423. end
  424. local SUBTBL = {
  425. '\\000', '\\001', '\\002', '\\003', '\\004',
  426. '\\005', '\\006', '\\007', '\\008', '\\t',
  427. '\\n', '\\011', '\\012', '\\r', '\\014',
  428. '\\015', '\\016', '\\017', '\\018', '\\019',
  429. '\\020', '\\021', '\\022', '\\023', '\\024',
  430. '\\025', '\\026', '\\027', '\\028', '\\029',
  431. '\\030', '\\031',
  432. }
  433. local format_luav
  434. format_luav = function(v, indent, opts)
  435. opts = opts or {}
  436. local linesep = '\n'
  437. local next_indent_arg = nil
  438. local indent_shift = opts.indent_shift or ' '
  439. local next_indent
  440. local nl = '\n'
  441. if indent == nil then
  442. indent = ''
  443. linesep = ''
  444. next_indent = ''
  445. nl = ' '
  446. else
  447. next_indent_arg = indent .. indent_shift
  448. next_indent = indent .. indent_shift
  449. end
  450. local ret = ''
  451. if type(v) == 'string' then
  452. if opts.literal_strings then
  453. ret = v
  454. else
  455. local quote = opts.dquote_strings and '"' or '\''
  456. ret = quote .. tostring(v):gsub(
  457. opts.dquote_strings and '["\\]' or '[\'\\]',
  458. '\\%0'):gsub(
  459. '[%z\1-\31]', function(match)
  460. return SUBTBL[match:byte() + 1]
  461. end) .. quote
  462. end
  463. elseif type(v) == 'table' then
  464. if v == REMOVE_THIS then
  465. ret = 'REMOVE_THIS'
  466. else
  467. local processed_keys = {}
  468. ret = '{' .. linesep
  469. local non_empty = false
  470. for i, subv in ipairs(v) do
  471. ret = ('%s%s%s,%s'):format(ret, next_indent,
  472. format_luav(subv, next_indent_arg, opts), nl)
  473. processed_keys[i] = true
  474. non_empty = true
  475. end
  476. for k, subv in pairs(v) do
  477. if not processed_keys[k] then
  478. if type(k) == 'string' and k:match('^[a-zA-Z_][a-zA-Z0-9_]*$') then
  479. ret = ret .. next_indent .. k .. ' = '
  480. else
  481. ret = ('%s%s[%s] = '):format(ret, next_indent,
  482. format_luav(k, nil, opts))
  483. end
  484. ret = ret .. format_luav(subv, next_indent_arg, opts) .. ',' .. nl
  485. non_empty = true
  486. end
  487. end
  488. if nl == ' ' and non_empty then
  489. ret = ret:sub(1, -3)
  490. end
  491. ret = ret .. indent .. '}'
  492. end
  493. elseif type(v) == 'number' then
  494. if v % 1 == 0 then
  495. ret = ('%d'):format(v)
  496. else
  497. ret = format_float(v)
  498. end
  499. elseif type(v) == 'nil' then
  500. ret = 'nil'
  501. elseif type(v) == 'boolean' then
  502. ret = (v and 'true' or 'false')
  503. else
  504. print(type(v))
  505. -- Not implemented yet
  506. assert(false)
  507. end
  508. return ret
  509. end
  510. local function format_string(fmt, ...)
  511. local i = 0
  512. local args = {...}
  513. local function getarg()
  514. i = i + 1
  515. return args[i]
  516. end
  517. local ret = fmt:gsub('%%[0-9*]*%.?[0-9*]*[cdEefgGiouXxqsr%%]', function(match)
  518. local subfmt = match:gsub('%*', function()
  519. return tostring(getarg())
  520. end)
  521. local arg = nil
  522. if subfmt:sub(-1) ~= '%' then
  523. arg = getarg()
  524. end
  525. if subfmt:sub(-1) == 'r' or subfmt:sub(-1) == 'q' then
  526. -- %r is like built-in %q, but it is supposed to single-quote strings and
  527. -- not double-quote them, and also work not only for strings.
  528. -- Builtin %q is replaced here as it gives invalid and inconsistent with
  529. -- luajit results for e.g. "\e" on lua: luajit transforms that into `\27`,
  530. -- lua leaves as-is.
  531. arg = format_luav(arg, nil, {dquote_strings = (subfmt:sub(-1) == 'q')})
  532. subfmt = subfmt:sub(1, -2) .. 's'
  533. end
  534. if subfmt == '%e' then
  535. return format_float(arg)
  536. else
  537. return subfmt:format(arg)
  538. end
  539. end)
  540. return ret
  541. end
  542. local function intchar2lua(ch)
  543. ch = tonumber(ch)
  544. return (20 <= ch and ch < 127) and ('%c'):format(ch) or ch
  545. end
  546. local fixtbl_metatable = {
  547. __newindex = function()
  548. assert(false)
  549. end,
  550. }
  551. local function fixtbl(tbl)
  552. return setmetatable(tbl, fixtbl_metatable)
  553. end
  554. local function fixtbl_rec(tbl)
  555. for _, v in pairs(tbl) do
  556. if type(v) == 'table' then
  557. fixtbl_rec(v)
  558. end
  559. end
  560. return fixtbl(tbl)
  561. end
  562. -- From https://github.com/premake/premake-core/blob/master/src/base/table.lua
  563. local function table_flatten(arr)
  564. local result = {}
  565. local function _table_flatten(_arr)
  566. local n = #_arr
  567. for i = 1, n do
  568. local v = _arr[i]
  569. if type(v) == "table" then
  570. _table_flatten(v)
  571. elseif v then
  572. table.insert(result, v)
  573. end
  574. end
  575. end
  576. _table_flatten(arr)
  577. return result
  578. end
  579. local function hexdump(str)
  580. local len = string.len(str)
  581. local dump = ""
  582. local hex = ""
  583. local asc = ""
  584. for i = 1, len do
  585. if 1 == i % 8 then
  586. dump = dump .. hex .. asc .. "\n"
  587. hex = string.format("%04x: ", i - 1)
  588. asc = ""
  589. end
  590. local ord = string.byte(str, i)
  591. hex = hex .. string.format("%02x ", ord)
  592. if ord >= 32 and ord <= 126 then
  593. asc = asc .. string.char(ord)
  594. else
  595. asc = asc .. "."
  596. end
  597. end
  598. return dump .. hex .. string.rep(" ", 8 - len % 8) .. asc
  599. end
  600. -- Reads text lines from `filename` into a table.
  601. --
  602. -- filename: path to file
  603. -- start: start line (1-indexed), negative means "lines before end" (tail)
  604. local function read_file_list(filename, start)
  605. local lnum = (start ~= nil and type(start) == 'number') and start or 1
  606. local tail = (lnum < 0)
  607. local maxlines = tail and math.abs(lnum) or nil
  608. local file = io.open(filename, 'r')
  609. if not file then
  610. return nil
  611. end
  612. local lines = {}
  613. local i = 1
  614. for line in file:lines() do
  615. if i >= start then
  616. table.insert(lines, line)
  617. if #lines > maxlines then
  618. table.remove(lines, 1)
  619. end
  620. end
  621. i = i + 1
  622. end
  623. file:close()
  624. return lines
  625. end
  626. -- Reads the entire contents of `filename` into a string.
  627. --
  628. -- filename: path to file
  629. local function read_file(filename)
  630. local file = io.open(filename, 'r')
  631. if not file then
  632. return nil
  633. end
  634. local ret = file:read('*a')
  635. file:close()
  636. return ret
  637. end
  638. -- Dedent the given text and write it to the file name.
  639. local function write_file(name, text, no_dedent, append)
  640. local file = io.open(name, (append and 'a' or 'w'))
  641. if type(text) == 'table' then
  642. -- Byte blob
  643. local bytes = text
  644. text = ''
  645. for _, char in ipairs(bytes) do
  646. text = ('%s%c'):format(text, char)
  647. end
  648. elseif not no_dedent then
  649. text = dedent(text)
  650. end
  651. file:write(text)
  652. file:flush()
  653. file:close()
  654. end
  655. local function isCI()
  656. local is_travis = nil ~= os.getenv('TRAVIS')
  657. local is_appveyor = nil ~= os.getenv('APPVEYOR')
  658. local is_quickbuild = nil ~= os.getenv('PR_NUMBER')
  659. return is_travis or is_appveyor or is_quickbuild
  660. end
  661. -- Gets the contents of $NVIM_LOG_FILE for printing to the build log.
  662. -- Also removes the file, if the current environment looks like CI.
  663. local function read_nvim_log()
  664. local logfile = os.getenv('NVIM_LOG_FILE') or '.nvimlog'
  665. local keep = isCI() and 999 or 10
  666. local lines = read_file_list(logfile, -keep) or {}
  667. local log = (('-'):rep(78)..'\n'
  668. ..string.format('$NVIM_LOG_FILE: %s\n', logfile)
  669. ..(#lines > 0 and '(last '..tostring(keep)..' lines)\n' or '(empty)\n'))
  670. for _,line in ipairs(lines) do
  671. log = log..line..'\n'
  672. end
  673. log = log..('-'):rep(78)..'\n'
  674. if isCI() then
  675. os.remove(logfile)
  676. end
  677. return log
  678. end
  679. local module = {
  680. REMOVE_THIS = REMOVE_THIS,
  681. argss_to_cmd = argss_to_cmd,
  682. check_cores = check_cores,
  683. check_logs = check_logs,
  684. concat_tables = concat_tables,
  685. dedent = dedent,
  686. deepcopy = deepcopy,
  687. dictdiff = dictdiff,
  688. eq = eq,
  689. expect_err = expect_err,
  690. filter = filter,
  691. fixtbl = fixtbl,
  692. fixtbl_rec = fixtbl_rec,
  693. format_luav = format_luav,
  694. format_string = format_string,
  695. glob = glob,
  696. hasenv = hasenv,
  697. hexdump = hexdump,
  698. intchar2lua = intchar2lua,
  699. map = map,
  700. matches = matches,
  701. mergedicts_copy = mergedicts_copy,
  702. near = near,
  703. neq = neq,
  704. ok = ok,
  705. popen_r = popen_r,
  706. popen_w = popen_w,
  707. read_file = read_file,
  708. read_file_list = read_file_list,
  709. read_nvim_log = read_nvim_log,
  710. repeated_read_cmd = repeated_read_cmd,
  711. shallowcopy = shallowcopy,
  712. sleep = sleep,
  713. table_flatten = table_flatten,
  714. tmpname = tmpname,
  715. uname = uname,
  716. updated = updated,
  717. which = which,
  718. write_file = write_file,
  719. }
  720. return module