robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1-- Black-box tests for the local Lua harness itself. These spawn a separate 2-- Nvim process instead of driving an embedded instance via testnvim. 3local t =require ( 'test.testutil' ) 4local describe , it , pending , finally =t . describe , t . it , t . pending , t . finally 5local uv =vim . uv 6 7local eq =t . eq 8local matches =t . matches 9local not_matches =t . not_matches 10 11local root =t . paths . test_source_path 12local build_dir =t . paths . test_build_dir 13local nvim_prog =build_dir .. '/bin/nvim' 14local runner =root .. '/test/runner.lua' 15 16---@param path string 17local function mkdir ( path ) 18assert ( t . mkdir ( path ), ( 'failed to create directory: %s' ): format ( path )) 19end 20 21-- Prepended to each *_spec.lua fixture so it can use the test framework. 22local spec_preamble ="local h = require('test.harness'); local eq = require('test.assert').eq; " 23.. 'local describe, it, before_each, after_each, setup, teardown, pending, finally = ' 24.. 'h.describe, h.it, h.before_each, h.after_each, h.setup, h.teardown, h.pending, h.finally; ' 25 26---@param suite_files table<string, string> 27---@return string 28local function write_suite ( suite_files ) 29local dir =t . tmpname ( false ) 30mkdir ( dir ) 31 32for name , contents in pairs ( suite_files ) do 33if name : match ( '_spec%.lua$' ) then 34contents =spec_preamble .. contents 35end 36t . write_file ( dir .. '/' .. name , contents ) 37end 38 39return dir 40end 41 42---@param overrides table<string, string?> 43---@return string[] 44local function make_env ( overrides ) 45local env =uv . os_environ () 46for k , v in pairs ( overrides ) do 47env [ k ] =v 48end 49 50local items ={} 51for k , v in pairs ( env ) do 52if v ~= nil then 53items [ # items + 1 ] =k .. '=' .. v 54end 55end 56 57return items 58end 59 60---@param suite_dir string 61---@param extra_args? string[] 62---@return integer, string 63local function run_harness ( suite_dir , extra_args ) 64local env_root =t . tmpname ( false ) 65mkdir ( env_root ) 66mkdir ( env_root .. '/config' ) 67mkdir ( env_root .. '/share' ) 68mkdir ( env_root .. '/state' ) 69mkdir ( env_root .. '/tmp' ) 70 71local stdout =assert ( uv . new_pipe ( false )) 72local stderr =assert ( uv . new_pipe ( false )) 73local out ={} 74local err ={} 75local exit_code --- @type integer? 76 77local args ={ 78'-l' , 79runner , 80'-v' , 81'--lpath=' .. build_dir .. '/?.lua' , 82'--lpath=' .. root .. '/src/?.lua' , 83'--lpath=' .. root .. '/runtime/lua/?.lua' , 84'--lpath=' .. suite_dir .. '/?.lua' , 85'--lpath=?.lua' , 86} 87for _ , arg in ipairs ( extra_args or {} ) do 88args [ # args + 1 ] =arg 89end 90args [ # args + 1 ] =suite_dir 91 92local handle =assert ( uv . spawn ( nvim_prog , { 93args =args , 94env =make_env ( { 95NVIM_TEST ='1' , 96TEST_COLORS ='0' , 97VIMRUNTIME =root .. '/runtime' , 98XDG_CONFIG_HOME =env_root .. '/config' , 99XDG_DATA_HOME =env_root .. '/share' , 100XDG_STATE_HOME =env_root .. '/state' , 101NVIM_RPLUGIN_MANIFEST =env_root .. '/rplugin_manifest' , 102NVIM_LOG_FILE =env_root .. '/nvim.log' , 103TMPDIR =env_root .. '/tmp' , 104SYSTEM_NAME =os . getenv ( 'SYSTEM_NAME' ) or uv . os_uname (). sysname , 105SHELL =os . getenv ( 'SHELL' ) or 'sh' , 106} ), 107stdio ={ nil , stdout , stderr } , 108hide =true , 109} , function ( code , _signal ) 110exit_code =code 111end )) 112 113local function read_all ( pipe , chunks ) 114pipe : read_start ( function ( read_err , chunk ) 115assert ( not read_err , read_err ) 116if chunk == nil then 117pipe : read_stop () 118pipe : close () 119else 120chunks [ # chunks + 1 ] =chunk 121end 122end ) 123end 124 125read_all ( stdout , out ) 126read_all ( stderr , err ) 127 128while exit_code == nil or not stdout : is_closing () or not stderr : is_closing () do 129uv . run ( 'once' ) 130end 131 132handle : close () 133while not handle : is_closing () do 134uv . run ( 'once' ) 135end 136 137return exit_code , table . concat ( out ) .. table . concat ( err ) 138end 139 140---@param suite_dir string 141---@param extra_args? string[] 142local function assert_harness_passes ( suite_dir , extra_args ) 143local code , output =run_harness ( suite_dir , extra_args ) 144eq ( 0 , code ) 145matches ( 'PASSED' , output , true ) 146end 147 148describe ( 'test harness' , function () 149it ( 'restores package.preload between files' , function () 150local suite_dir =write_suite ( { 151[ 'one_spec.lua' ] =[[ 152package.preload.leak_mod = function() 153return 'leak' 154end 155 156describe('one', function() 157it('defines a preload entry', function() 158eq('leak', require('leak_mod')) 159end) 160end) 161]] , 162[ 'two_spec.lua' ] =[[ 163describe('two', function() 164it('does not see the preload entry from another file', function() 165local ok = pcall(require, 'leak_mod') 166assert(not ok) 167end) 168end) 169]] , 170} ) 171 172assert_harness_passes ( suite_dir ) 173end ) 174 175it ( 'restores package.loaded between files' , function () 176local suite_dir =write_suite ( { 177[ 'loaded_mod.lua' ] =[[ 178return { source = 'file' } 179]] , 180[ 'one_spec.lua' ] =[[ 181describe('one', function() 182it('mutates package.loaded', function() 183eq('file', require('loaded_mod').source) 184package.loaded.loaded_mod = 'leak' 185eq('leak', require('loaded_mod')) 186end) 187end) 188]] , 189[ 'two_spec.lua' ] =[[ 190describe('two', function() 191it('does not see package.loaded leaks from another file', function() 192local mod = require('loaded_mod') 193eq('file', mod.source) 194end) 195end) 196]] , 197} ) 198 199assert_harness_passes ( suite_dir ) 200end ) 201 202it ( 'restores environment variables between files' , function () 203local env_name ='__NVIM_HARNESS_TEST_ENV__' 204local suite_dir =write_suite ( { 205[ 'one_spec.lua' ] =string . format ( 206[[ 207describe('one', function() 208it('mutates the environment', function() 209vim.uv.os_setenv(%q, 'leak') 210eq('leak', vim.uv.os_getenv(%q)) 211end) 212end) 213]] , 214env_name , 215env_name 216), 217[ 'two_spec.lua' ] =string . format ( 218[[ 219describe('two', function() 220it('does not see environment leaks from another file', function() 221eq(nil, vim.uv.os_getenv(%q)) 222end) 223end) 224]] , 225env_name 226), 227} ) 228 229assert_harness_passes ( suite_dir ) 230end ) 231 232it ( 'restores globals between files' , function () 233local suite_dir =write_suite ( { 234[ 'one_spec.lua' ] =[[ 235describe('one', function() 236it('mutates a global', function() 237_G.__harness_leak = 'leak' 238eq('leak', _G.__harness_leak) 239end) 240end) 241]] , 242[ 'two_spec.lua' ] =[[ 243describe('two', function() 244it('does not see global leaks from another file', function() 245eq(nil, _G.__harness_leak) 246end) 247end) 248]] , 249} ) 250 251assert_harness_passes ( suite_dir ) 252end ) 253 254it ( 'restores arg between files' , function () 255local suite_dir =write_suite ( { 256[ 'one_spec.lua' ] =[[ 257describe('one', function() 258it('mutates arg', function() 259_G.arg.__leak = 'leak' 260eq('leak', _G.arg.__leak) 261end) 262end) 263]] , 264[ 'two_spec.lua' ] =[[ 265describe('two', function() 266it('does not see arg leaks from another file', function() 267eq(nil, _G.arg.__leak) 268end) 269end) 270]] , 271} ) 272 273assert_harness_passes ( suite_dir ) 274end ) 275 276it ( 'restores helper-provided globals to their baseline between files' , function () 277local suite_dir =write_suite ( { 278[ 'helper.lua' ] =[[ 279_G.helper_value = 'baseline' 280]] , 281[ 'one_spec.lua' ] =[[ 282describe('one', function() 283it('mutates a helper-provided global', function() 284eq('baseline', _G.helper_value) 285_G.helper_value = 'leak' 286eq('leak', _G.helper_value) 287end) 288end) 289]] , 290[ 'two_spec.lua' ] =[[ 291describe('two', function() 292it('restores the helper baseline between files', function() 293eq('baseline', _G.helper_value) 294end) 295end) 296]] , 297} ) 298 299assert_harness_passes ( suite_dir , { 300'--helper=' .. suite_dir .. '/helper.lua' , 301} ) 302end ) 303 304it ( 'restores helper-provided globals shallowly between files' , function () 305local suite_dir =write_suite ( { 306[ 'helper.lua' ] =[[ 307_G.helper_value = { nested = 'baseline' } 308]] , 309[ 'one_spec.lua' ] =[[ 310describe('one', function() 311it('mutates nested helper state in place', function() 312eq('baseline', _G.helper_value.nested) 313_G.helper_value.nested = 'leak' 314eq('leak', _G.helper_value.nested) 315end) 316end) 317]] , 318[ 'two_spec.lua' ] =[[ 319describe('two', function() 320it('keeps the same nested helper table', function() 321eq('leak', _G.helper_value.nested) 322end) 323end) 324]] , 325} ) 326 327assert_harness_passes ( suite_dir , { 328'--helper=' .. suite_dir .. '/helper.lua' , 329} ) 330end ) 331 332it ( 'keeps helper-loaded modules across files' , function () 333local suite_dir =write_suite ( { 334[ 'helper_mod.lua' ] =[[ 335_G.helper_module_loads = (_G.helper_module_loads or 0) + 1 336return { loads = _G.helper_module_loads } 337]] , 338[ 'helper.lua' ] =[[ 339require('helper_mod') 340]] , 341[ 'one_spec.lua' ] =[[ 342describe('one', function() 343it('uses the helper-loaded module', function() 344eq(1, require('helper_mod').loads) 345end) 346end) 347]] , 348[ 'two_spec.lua' ] =[[ 349describe('two', function() 350it('does not reload the helper module', function() 351eq(1, require('helper_mod').loads) 352end) 353end) 354]] , 355} ) 356 357assert_harness_passes ( suite_dir , { 358'--helper=' .. suite_dir .. '/helper.lua' , 359} ) 360end ) 361 362it ( 'rejects helpers that register hooks' , function () 363local suite_dir =write_suite ( { 364[ 'helper.lua' ] =[[ 365before_each(function() end) 366]] , 367[ 'one_spec.lua' ] =[[ 368describe('real', function() 369it('passes', function() end) 370end) 371]] , 372} ) 373 374local code , output =run_harness ( suite_dir , { 375'--helper=' .. suite_dir .. '/helper.lua' , 376} ) 377 378eq ( 1 , code ) 379matches ( "attempt to call global 'before_each'" , output , true ) 380end ) 381 382it ( 'rejects helpers that define suites or tests' , function () 383local suite_dir =write_suite ( { 384[ 'helper.lua' ] =[[ 385describe('helper suite', function() 386it('should fail', function() end) 387end) 388]] , 389[ 'one_spec.lua' ] =[[ 390describe('real', function() 391it('passes', function() end) 392end) 393]] , 394} ) 395 396local code , output =run_harness ( suite_dir , { 397'--helper=' .. suite_dir .. '/helper.lua' , 398} ) 399 400eq ( 1 , code ) 401matches ( "attempt to call global 'describe'" , output , true ) 402end ) 403 404it ( 'deduplicates suite-end callbacks registered from the same module' , function () 405local marker =t . tmpname ( false ) 406local suite_dir =write_suite ( { 407[ 'cbmod.lua' ] =string . format ( 408[[ 409local harness = require('test.harness') 410 411harness.on_suite_end(function() 412local file = assert(io.open(%q, 'ab')) 413file:write('hit\n') 414file:close() 415end) 416 417return true 418]] , 419marker 420), 421[ 'one_spec.lua' ] =[[ 422require('cbmod') 423 424describe('one', function() 425it('works', function() 426assert(true) 427end) 428end) 429]] , 430[ 'two_spec.lua' ] =[[ 431require('cbmod') 432 433describe('two', function() 434it('works', function() 435assert(true) 436end) 437end) 438]] , 439} ) 440 441assert_harness_passes ( suite_dir ) 442eq ( 'hit\n' , t . read_file ( marker )) 443end ) 444 445it ( 446'keeps distinct suite-end callbacks from long paths with the same truncated short_src tail' , 447function () 448local marker =t . tmpname ( false ) 449local suite_dir =t . tmpname ( false ) 450local left =suite_dir 451.. '/' 452.. string . rep ( 'L' , 100 ) 453.. '/common/common/common/common/common/same_suffix' 454local right =suite_dir 455.. '/' 456.. string . rep ( 'R' , 100 ) 457.. '/common/common/common/common/common/same_suffix' 458 459mkdir ( suite_dir ) 460for _ , path in ipairs ( { left , right } ) do 461local parents ={} 462local dir =path 463while dir ~= suite_dir and not uv . fs_stat ( dir ) do 464table . insert ( parents , 1 , dir ) 465dir =vim . fs . dirname ( dir ) 466end 467for _ , parent in ipairs ( parents ) do 468if not uv . fs_stat ( parent ) then 469mkdir ( parent ) 470end 471end 472end 473 474t . write_file ( left .. '/same_spec.lua' , spec_preamble .. string . format ( 475[[ 476local harness = require('test.harness') 477 478harness.on_suite_end(function() 479local file = assert(io.open(%q, 'ab')) 480file:write('left\n') 481file:close() 482end) 483 484describe('left', function() 485it('works', function() end) 486end) 487]] , 488marker 489)) 490t . write_file ( right .. '/same_spec.lua' , spec_preamble .. string . format ( 491[[ 492local harness = require('test.harness') 493 494harness.on_suite_end(function() 495local file = assert(io.open(%q, 'ab')) 496file:write('right\n') 497file:close() 498end) 499 500describe('right', function() 501it('works', function() end) 502end) 503]] , 504marker 505)) 506 507assert_harness_passes ( suite_dir ) 508eq ( 'left\nright\n' , t . read_file ( marker )) 509end 510) 511 512it ( 'reports suite-end callback failures without crashing the runner' , function () 513local suite_dir =write_suite ( { 514[ 'cbmod.lua' ] =[[ 515local harness = require('test.harness') 516 517harness.on_suite_end(function() 518error('boom from suite_end') 519end) 520 521return true 522]] , 523[ 'one_spec.lua' ] =[[ 524require('cbmod') 525 526describe('one', function() 527it('works', function() 528assert(true) 529end) 530end) 531]] , 532} ) 533 534local code , output =run_harness ( suite_dir ) 535 536eq ( 1 , code ) 537matches ( '[suite_end 1]' , output , true ) 538matches ( 'suite_end' , output , true ) 539matches ( 'boom from suite_end' , output , true ) 540matches ( 'ERROR ' , output , true ) 541not_matches ( 'FAILED ' , output , true ) 542matches ( '1 test from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 543matches ( 'Global test environment teardown.' , output , true ) 544end ) 545 546it ( 'reports wrapped suite-end callback failures at the callback definition line' , function () 547local suite_dir =write_suite ( { 548[ 'wrapper.lua' ] =[[ 549local M = {} 550 551function M.fail() 552error('boom from wrapper') 553end 554 555return M 556]] , 557[ 'cbmod.lua' ] =[[ 558local harness = require('test.harness') 559local wrapper = require('wrapper') 560 561harness.on_suite_end(function() 562wrapper.fail() 563end) 564 565return true 566]] , 567[ 'one_spec.lua' ] =[[ 568require('cbmod') 569 570describe('one', function() 571it('works', function() 572assert(true) 573end) 574end) 575]] , 576} ) 577 578local code , output =run_harness ( suite_dir ) 579 580eq ( 1 , code ) 581matches ( 'cbmod.lua @ 4: [suite_end 1]' , output , true ) 582not_matches ( 'wrapper.lua @ 4:' , output , true ) 583end ) 584 585it ( 'reloads helper suite-end callbacks between repeats' , function () 586local marker =t . tmpname ( false ) 587local suite_dir =write_suite ( { 588[ 'helper.lua' ] =string . format ( 589[[ 590local harness = require('test.harness') 591local hits = 0 592 593harness.on_suite_end(function() 594hits = hits + 1 595local file = assert(io.open(%q, 'ab')) 596file:write(hits .. '\n') 597file:close() 598end) 599]] , 600marker 601), 602[ 'one_spec.lua' ] =[[ 603describe('one', function() 604it('works', function() 605assert(true) 606end) 607end) 608]] , 609} ) 610 611assert_harness_passes ( suite_dir , { 612'--helper=' .. suite_dir .. '/helper.lua' , 613'--repeat=2' , 614} ) 615eq ( '1\n1\n' , t . read_file ( marker )) 616end ) 617 618it ( 'restores process state before each repeat' , function () 619local suite_dir =write_suite ( { 620[ 'one_spec.lua' ] =[[ 621local harness = require('test.harness') 622 623harness.on_suite_end(function() 624_G.__repeat_leak = 'leak' 625end) 626 627describe('one', function() 628it('starts clean', function() 629eq(nil, _G.__repeat_leak) 630end) 631end) 632]] , 633} ) 634 635assert_harness_passes ( suite_dir , { 636'--repeat=2' , 637} ) 638end ) 639 640it ( 'filters tests by tags across files' , function () 641local suite_dir =write_suite ( { 642[ 'one_spec.lua' ] =[[ 643describe('one', function() 644it('fast #fast', function() end) 645end) 646]] , 647[ 'two_spec.lua' ] =[[ 648describe('two', function() 649it('slow #slow', function() end) 650end) 651]] , 652} ) 653 654local code , output =run_harness ( suite_dir , { 655'--tags=fast' , 656} ) 657 658eq ( 0 , code ) 659matches ( '1 test from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 660matches ( 'one fast #fast' , output , true ) 661not_matches ( 'two slow #slow' , output , true ) 662end ) 663 664it ( 'filters tests by suite tags across files' , function () 665local suite_dir =write_suite ( { 666[ 'one_spec.lua' ] =[[ 667describe('one #fast', function() 668it('works', function() end) 669end) 670]] , 671[ 'two_spec.lua' ] =[[ 672describe('two #slow', function() 673it('works', function() end) 674end) 675]] , 676} ) 677 678local code , output =run_harness ( suite_dir , { 679'--tags=fast' , 680} ) 681 682eq ( 0 , code ) 683matches ( '1 test from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 684matches ( 'one #fast works' , output , true ) 685not_matches ( 'two #slow works' , output , true ) 686end ) 687 688it ( 'filters tests by name across files' , function () 689local suite_dir =write_suite ( { 690[ 'one_spec.lua' ] =[[ 691describe('one', function() 692it('chosen', function() end) 693end) 694]] , 695[ 'two_spec.lua' ] =[[ 696describe('two', function() 697it('skipped', function() end) 698end) 699]] , 700} ) 701 702local code , output =run_harness ( suite_dir , { 703'--filter=chosen' , 704} ) 705 706eq ( 0 , code ) 707matches ( '1 test from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 708matches ( 'one chosen' , output , true ) 709not_matches ( 'two skipped' , output , true ) 710end ) 711 712it ( 'filters tests by suite name across files' , function () 713local suite_dir =write_suite ( { 714[ 'one_spec.lua' ] =[[ 715describe('chosen suite', function() 716it('works', function() end) 717end) 718]] , 719[ 'two_spec.lua' ] =[[ 720describe('skipped suite', function() 721it('works', function() end) 722end) 723]] , 724} ) 725 726local code , output =run_harness ( suite_dir , { 727'--filter' , 728'chosen suite' , 729} ) 730 731eq ( 0 , code ) 732matches ( '1 test from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 733matches ( 'chosen suite works' , output , true ) 734not_matches ( 'skipped suite works' , output , true ) 735end ) 736 737it ( 'does not keep suite-end callbacks from filtered-out files' , function () 738local marker =t . tmpname ( false ) 739local suite_dir =write_suite ( { 740[ 'one_spec.lua' ] =string . format ( 741[[ 742local harness = require('test.harness') 743 744harness.on_suite_end(function() 745local file = assert(io.open(%q, 'ab')) 746file:write('filtered\n') 747file:close() 748end) 749 750describe('one', function() 751it('skipped', function() 752assert(true) 753end) 754end) 755]] , 756marker 757), 758[ 'two_spec.lua' ] =[[ 759describe('two', function() 760it('chosen', function() 761assert(true) 762end) 763end) 764]] , 765} ) 766 767local code , output =run_harness ( suite_dir , { 768'--filter=chosen' , 769} ) 770 771eq ( 0 , code ) 772eq ( nil , uv . fs_stat ( marker )) 773matches ( '1 test from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 774end ) 775 776it ( 'filters tests out by name across files' , function () 777local suite_dir =write_suite ( { 778[ 'one_spec.lua' ] =[[ 779describe('one', function() 780it('chosen', function() end) 781end) 782]] , 783[ 'two_spec.lua' ] =[[ 784describe('two', function() 785it('skipped', function() end) 786end) 787]] , 788} ) 789 790local code , output =run_harness ( suite_dir , { 791'--filter-out=skipped' , 792} ) 793 794eq ( 0 , code ) 795matches ( '1 test from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 796matches ( 'one chosen' , output , true ) 797not_matches ( 'two skipped' , output , true ) 798end ) 799 800it ( 'filters tests out by suite name across files' , function () 801local suite_dir =write_suite ( { 802[ 'one_spec.lua' ] =[[ 803describe('chosen suite', function() 804it('works', function() end) 805end) 806]] , 807[ 'two_spec.lua' ] =[[ 808describe('skipped suite', function() 809it('works', function() end) 810end) 811]] , 812} ) 813 814local code , output =run_harness ( suite_dir , { 815'--filter-out' , 816'skipped suite' , 817} ) 818 819eq ( 0 , code ) 820matches ( '1 test from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 821matches ( 'chosen suite works' , output , true ) 822not_matches ( 'skipped suite works' , output , true ) 823end ) 824 825it ( 'filters tests out by multiple repeated option values' , function () 826local suite_dir =write_suite ( { 827[ 'one_spec.lua' ] =[[ 828describe('chosen suite', function() 829it('works', function() end) 830end) 831]] , 832[ 'two_spec.lua' ] =[[ 833describe('skipped suite', function() 834it('works', function() end) 835end) 836]] , 837[ 'three_spec.lua' ] =[[ 838describe('three', function() 839it('skipped test', function() end) 840end) 841]] , 842} ) 843 844local code , output =run_harness ( suite_dir , { 845'--filter-out=skipped suite' , 846'--filter-out=skipped test' , 847} ) 848 849eq ( 0 , code ) 850matches ( '1 test from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 851matches ( 'chosen suite works' , output , true ) 852not_matches ( 'skipped suite works' , output , true ) 853not_matches ( 'three skipped test' , output , true ) 854end ) 855 856it ( 'reports when filters exclude all tests' , function () 857local suite_dir =write_suite ( { 858[ 'one_spec.lua' ] =[[ 859describe('one', function() 860it('skipped', function() end) 861end) 862]] , 863} ) 864 865local code , output =run_harness ( suite_dir , { 866'--filter-out=.' , 867} ) 868 869eq ( 1 , code ) 870matches ( 'No tests matched the current selection.' , output , true ) 871not_matches ( 'Running tests from' , output , true ) 872end ) 873 874it ( 'reports malformed filter patterns clearly before selection runs' , function () 875local suite_dir =write_suite ( { 876[ 'one_spec.lua' ] =[[ 877describe('one', function() 878it('works', function() end) 879end) 880]] , 881} ) 882 883for _ , case in ipairs ( { 884{ '--filter=[' , 'invalid value for --filter: malformed pattern' } , 885{ '--filter-out=[' , 'invalid value for --filter-out: malformed pattern' } , 886} ) do 887local code , output =run_harness ( suite_dir , { case [ 1 ] } ) 888 889eq ( 1 , code ) 890matches ( case [ 2 ], output , true ) 891not_matches ( 'test_selected' , output , true ) 892not_matches ( 'Running tests from' , output , true ) 893not_matches ( 'test harness failed with exit code' , output , true ) 894end 895end ) 896 897it ( 'skips unreadable test directories and keeps running readable files' , function () 898if t . is_os ( 'win' ) then 899pending ( 'N/A: permission denied directory scan depends on POSIX chmod' ) 900end 901 902local suite_dir =t . tmpname ( false ) 903mkdir ( suite_dir ) 904t . write_file ( suite_dir .. '/one_spec.lua' , spec_preamble .. [[ 905describe('one', function() 906it('works', function() end) 907end) 908]] ) 909local blocked =suite_dir .. '/blocked' 910 911mkdir ( blocked ) 912t . write_file ( blocked .. '/two_spec.lua' , spec_preamble .. [[ 913describe('two', function() 914it('is hidden behind permissions', function() end) 915end) 916]] ) 917finally ( function () 918assert ( uv . fs_chmod ( blocked , 448 )) 919end ) 920assert ( uv . fs_chmod ( blocked , 0 )) 921 922local code , output =run_harness ( suite_dir ) 923 924eq ( 0 , code ) 925matches ( 'Running tests from' , output , true ) 926matches ( 'one works' , output , true ) 927not_matches ( 'two is hidden behind permissions' , output , true ) 928matches ( '1 test from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 929end ) 930 931it ( 'reports missing test paths clearly before loading suites' , function () 932local suite_dir =write_suite ( { 933[ 'one_spec.lua' ] =[[ 934describe('one', function() 935it('works', function() end) 936end) 937]] , 938} ) 939local missing =suite_dir .. '/missing' 940 941local code , output =run_harness ( suite_dir , { missing } ) 942 943eq ( 1 , code ) 944matches ( 'test path not found: ' .. missing , output , true ) 945not_matches ( 'collect_test_files' , output , true ) 946not_matches ( 'Running tests from' , output , true ) 947not_matches ( 'test harness failed with exit code' , output , true ) 948end ) 949 950it ( 'reports missing helpers clearly before running suites' , function () 951local suite_dir =write_suite ( { 952[ 'one_spec.lua' ] =[[ 953describe('one', function() 954it('works', function() end) 955end) 956]] , 957} ) 958local missing =suite_dir .. '/missing.lua' 959 960local code , output =run_harness ( suite_dir , { 961'--helper=' .. missing , 962} ) 963 964eq ( 1 , code ) 965matches ( 'cannot open ' .. missing , output , true ) 966not_matches ( 'load_helper' , output , true ) 967not_matches ( 'Running tests from' , output , true ) 968not_matches ( 'test harness failed with exit code' , output , true ) 969end ) 970 971it ( 'reports empty value options before running suites' , function () 972local suite_dir =write_suite ( { 973[ 'one_spec.lua' ] =[[ 974describe('one', function() 975it('works', function() end) 976end) 977]] , 978} ) 979 980for _ , case in ipairs ( { 981{ '--helper=' , 'missing value for --helper' } , 982{ '--summary-file=' , 'missing value for --summary-file' } , 983} ) do 984local code , output =run_harness ( suite_dir , { 985case [ 1 ], 986} ) 987 988eq ( 1 , code ) 989matches ( case [ 2 ], output , true ) 990not_matches ( 'open_summary_file' , output , true ) 991not_matches ( 'Running tests from' , output , true ) 992not_matches ( 'test harness failed with exit code' , output , true ) 993end 994end ) 995 996it ( 'treats the next argv item as the value even when it starts with dashes' , function () 997local suite_dir =write_suite ( { 998[ 'one_spec.lua' ] =[[ 999describe('one', function() 1000it('works', function() end) 1001end) 1002]] , 1003} ) 1004 1005local code , output =run_harness ( suite_dir , { '--filter' , '--verbose' } ) 1006 1007eq ( 1 , code ) 1008matches ( 'No tests matched the current selection.' , output , true ) 1009not_matches ( 'missing value for --filter' , output , true ) 1010 1011code , output =run_harness ( suite_dir , { '--repeat' , '-1' } ) 1012 1013eq ( 1 , code ) 1014matches ( 'invalid value for --repeat: -1' , output , true ) 1015not_matches ( 'missing value for --repeat' , output , true ) 1016end ) 1017 1018it ( 'it() rejects a non-table, non-function second argument' , function () 1019eq ( 1020'.../harness.lua:0: it() arg 2 must be an opts table or a function' , 1021t . pcall_err ( it , 'nope' , 'not opts' , function () end ) 1022) 1023end ) 1024 1025it ( '{retries=n} reruns before_each/after_each/finally, only for the retried test' , function () 1026local log =t . tmpname ( false ) 1027local suite_dir =write_suite ( { 1028[ 'one_spec.lua' ] =( [[ 1029local function log(s) 1030vim.fn.writefile({ s }, '%s', 'a') 1031end 1032describe('retry', function() 1033before_each(function() log('d-before') end) 1034after_each(function() log('d-after') end) 1035it('succeeds on the second attempt', { retries = 2 }, function(ctx) 1036finally(function() log('t1-finally' .. ctx.retry) end) 1037eq(true, ctx.retry >= 1) 1038end) 1039it('runs once', function(ctx) 1040log('t2-next' .. ctx.retry) 1041end) 1042end) 1043]] ): format ( log ), 1044} ) 1045 1046local code , output =run_harness ( suite_dir ) 1047 1048eq ( 0 , code ) 1049matches ( 'PASSED ' , output , true ) 1050not_matches ( 'FAILED ' , output , true ) 1051-- Attempt 2 succeeded so attempt 3 never ran, and the next test starts back at retry 0. 1052eq ( 1053'd-before t1-finally0 d-after d-before t1-finally1 d-after d-before t2-next0 d-after' , 1054table . concat ( vim . fn . readfile ( log ), ' ' ) 1055) 1056end ) 1057 1058it ( '{retries=n} reports one failure after all retries are exhausted' , function () 1059local suite_dir =write_suite ( { 1060[ 'one_spec.lua' ] =[[ 1061describe('retry', function() 1062local attempts = 0 1063before_each(function() attempts = attempts + 1 end) 1064it('never succeeds', { retries = 2 }, function() 1065error('boom on attempt ' .. attempts) 1066end) 1067end) 1068]] , 1069} ) 1070 1071local code , output =run_harness ( suite_dir ) 1072 1073eq ( 1 , code ) 1074matches ( 'boom on attempt 3' , output , true ) 1075not_matches ( 'boom on attempt 1' , output , true ) 1076matches ( '1 test, listed below' , output , true ) -- One failure, not one per attempt. 1077end ) 1078 1079it ( 'reports test-body errors as failures' , function () 1080local suite_dir =write_suite ( { 1081[ 'one_spec.lua' ] =[[ 1082describe('one', function() 1083it('fails', function() 1084error('boom from test') 1085end) 1086end) 1087]] , 1088} ) 1089 1090local code , output =run_harness ( suite_dir ) 1091 1092eq ( 1 , code ) 1093matches ( 'boom from test' , output , true ) 1094matches ( 'FAILED ' , output , true ) 1095not_matches ( 'ERROR ' , output , true ) 1096end ) 1097 1098it ( 'reports test-body failures at the failing line' , function () 1099local suite_dir =write_suite ( { 1100[ 'one_spec.lua' ] =[[ 1101describe('one', function() 1102it('fails', function() 1103error('boom from test') 1104end) 1105end) 1106]] , 1107} ) 1108 1109local code , output =run_harness ( suite_dir ) 1110 1111eq ( 1 , code ) 1112matches ( 'one_spec.lua @ 3: one fails' , output , true ) 1113end ) 1114 1115it ( 'ignores fake trace lines embedded in multiline error messages' , function () 1116local suite_dir =write_suite ( { 1117[ 'one_spec.lua' ] =[[ 1118describe('one', function() 1119it('fake trace', function() 1120error('boom\n' .. debug.getinfo(1, 'S').source:sub(2) .. ':999: fake trace', 0) 1121end) 1122end) 1123]] , 1124} ) 1125 1126local code , output =run_harness ( suite_dir ) 1127 1128eq ( 1 , code ) 1129matches ( 'one_spec.lua @ 3: one fake trace' , output , true ) 1130not_matches ( 'one_spec.lua @ 999: one fake trace' , output , true ) 1131end ) 1132 1133it ( 'reports wrapped assertion failures at the test definition line' , function () 1134local suite_dir =write_suite ( { 1135[ 'one_spec.lua' ] =[[ 1136describe('one', function() 1137it('equal fail', function() 1138eq(1, 2) 1139end) 1140end) 1141]] , 1142} ) 1143 1144local code , output =run_harness ( suite_dir ) 1145 1146eq ( 1 , code ) 1147matches ( 'one_spec.lua @ 2: one equal fail' , output , true ) 1148not_matches ( root .. '/test/assert.lua @' , output , true ) 1149end ) 1150 1151it ( 'reports local wrapper failures at the test definition line' , function () 1152local suite_dir =write_suite ( { 1153[ 'wrapper.lua' ] =[[ 1154local M = {} 1155 1156function M.fail() 1157error('boom from wrapper') 1158end 1159 1160return M 1161]] , 1162[ 'one_spec.lua' ] =[[ 1163local wrapper = require('wrapper') 1164 1165describe('one', function() 1166it('wrapper fail', function() 1167wrapper.fail() 1168end) 1169end) 1170]] , 1171} ) 1172 1173local code , output =run_harness ( suite_dir ) 1174 1175eq ( 1 , code ) 1176matches ( 'one_spec.lua @ 4: one wrapper fail' , output , true ) 1177not_matches ( 'wrapper.lua @ 4:' , output , true ) 1178end ) 1179 1180it ( 'reports failing finally cleanup at the cleanup line' , function () 1181local suite_dir =write_suite ( { 1182[ 'one_spec.lua' ] =[[ 1183describe('one', function() 1184it('finfail', function() 1185finally(function() 1186error('boom fin') 1187end) 1188pending('later') 1189end) 1190end) 1191]] , 1192} ) 1193 1194local code , output =run_harness ( suite_dir ) 1195 1196eq ( 1 , code ) 1197matches ( 'one_spec.lua @ 4: one finfail' , output , true ) 1198end ) 1199 1200it ( 'rejects finally in setup hooks' , function () 1201local suite_dir =write_suite ( { 1202[ 'one_spec.lua' ] =[[ 1203describe('one', function() 1204setup(function() 1205finally(function() end) 1206end) 1207 1208it('works', function() end) 1209end) 1210]] , 1211} ) 1212 1213local code , output =run_harness ( suite_dir ) 1214 1215eq ( 1 , code ) 1216matches ( 'one_spec.lua @ 2: one [setup]' , output , true ) 1217matches ( 'finally() must be called while a test body is running' , output , true ) 1218end ) 1219 1220it ( 'rejects finally in teardown hooks' , function () 1221local suite_dir =write_suite ( { 1222[ 'one_spec.lua' ] =[[ 1223describe('one', function() 1224teardown(function() 1225finally(function() end) 1226end) 1227 1228it('works', function() end) 1229end) 1230]] , 1231} ) 1232 1233local code , output =run_harness ( suite_dir ) 1234 1235eq ( 1 , code ) 1236matches ( 'one_spec.lua @ 2: one [teardown]' , output , true ) 1237matches ( 'finally() must be called while a test body is running' , output , true ) 1238end ) 1239 1240it ( 'reports failing after_each cleanup at the cleanup line' , function () 1241local suite_dir =write_suite ( { 1242[ 'one_spec.lua' ] =[[ 1243describe('one', function() 1244after_each(function() 1245error('boom after') 1246end) 1247 1248it('afterfail', function() 1249pending('later') 1250end) 1251end) 1252]] , 1253} ) 1254 1255local code , output =run_harness ( suite_dir ) 1256 1257eq ( 1 , code ) 1258matches ( 'one_spec.lua @ 3: one afterfail' , output , true ) 1259end ) 1260 1261it ( 'reports cross-file after_each wrapper failures at the hook definition line' , function () 1262local suite_dir =write_suite ( { 1263[ 'wrapper.lua' ] =[[ 1264local M = {} 1265 1266function M.fail() 1267error('boom from wrapper') 1268end 1269 1270return M 1271]] , 1272[ 'one_spec.lua' ] =[[ 1273local wrapper = require('wrapper') 1274 1275describe('one', function() 1276after_each(function() 1277wrapper.fail() 1278end) 1279 1280it('afterwrap', function() 1281assert(true) 1282end) 1283end) 1284]] , 1285} ) 1286 1287local code , output =run_harness ( suite_dir ) 1288 1289eq ( 1 , code ) 1290matches ( 'one_spec.lua @ 4: one afterwrap' , output , true ) 1291not_matches ( 'one_spec.lua @ 8: one afterwrap' , output , true ) 1292not_matches ( 'wrapper.lua @ 4:' , output , true ) 1293end ) 1294 1295it ( 1296'reports pending cross-file after_each wrapper failures at the hook definition line' , 1297function () 1298local suite_dir =write_suite ( { 1299[ 'wrapper.lua' ] =[[ 1300local M = {} 1301 1302function M.fail() 1303error('boom from wrapper') 1304end 1305 1306return M 1307]] , 1308[ 'one_spec.lua' ] =[[ 1309local wrapper = require('wrapper') 1310 1311describe('one', function() 1312after_each(function() 1313wrapper.fail() 1314end) 1315 1316it('afterwrap pending', function() 1317pending('later') 1318end) 1319end) 1320]] , 1321} ) 1322 1323local code , output =run_harness ( suite_dir ) 1324 1325eq ( 1 , code ) 1326matches ( 'one_spec.lua @ 4: one afterwrap pending' , output , true ) 1327not_matches ( 'one_spec.lua @ 8: one afterwrap pending' , output , true ) 1328not_matches ( 'wrapper.lua @ 4:' , output , true ) 1329end 1330) 1331 1332it ( 'reports cross-file finally wrapper failures at the cleanup definition line' , function () 1333local suite_dir =write_suite ( { 1334[ 'wrapper.lua' ] =[[ 1335local M = {} 1336 1337function M.fail() 1338error('boom from wrapper') 1339end 1340 1341return M 1342]] , 1343[ 'one_spec.lua' ] =[[ 1344local wrapper = require('wrapper') 1345 1346describe('one', function() 1347it('finwrap', function() 1348finally(function() 1349wrapper.fail() 1350end) 1351end) 1352end) 1353]] , 1354} ) 1355 1356local code , output =run_harness ( suite_dir ) 1357 1358eq ( 1 , code ) 1359matches ( 'one_spec.lua @ 5: one finwrap' , output , true ) 1360not_matches ( 'one_spec.lua @ 4: one finwrap' , output , true ) 1361not_matches ( 'wrapper.lua @ 4:' , output , true ) 1362end ) 1363 1364it ( 'does not count synthetic hook failures as executed tests' , function () 1365local suite_dir =write_suite ( { 1366[ 'one_spec.lua' ] =[[ 1367describe('trace', function() 1368setup(function() 1369error('boom setup') 1370end) 1371 1372it('passes', function() end) 1373end) 1374]] , 1375} ) 1376 1377local code , output =run_harness ( suite_dir ) 1378 1379eq ( 1 , code ) 1380matches ( '0 tests from 1 test file of ' .. suite_dir .. ' ran.' , output , true ) 1381matches ( '1 ERROR' , output , true ) 1382end ) 1383 1384it ( 'reports setup failures at the hook definition site' , function () 1385local suite_dir =write_suite ( { 1386[ 'one_spec.lua' ] =[[ 1387describe('trace', function() 1388setup(function() 1389error('boom setup') 1390end) 1391 1392it('passes', function() end) 1393end) 1394]] , 1395} ) 1396 1397local code , output =run_harness ( suite_dir ) 1398 1399eq ( 1 , code ) 1400matches ( 'one_spec.lua @ 2: trace [setup]' , output , true ) 1401end ) 1402 1403it ( 'does not keep suite-end callbacks from load-error files' , function () 1404local marker =t . tmpname ( false ) 1405local suite_dir =write_suite ( { 1406[ 'one_spec.lua' ] =string . format ( 1407[[ 1408local harness = require('test.harness') 1409 1410harness.on_suite_end(function() 1411local file = assert(io.open(%q, 'ab')) 1412file:write('load\n') 1413file:close() 1414end) 1415 1416error('boom during load') 1417]] , 1418marker 1419), 1420} ) 1421 1422local code , output =run_harness ( suite_dir ) 1423 1424eq ( 1 , code ) 1425eq ( nil , uv . fs_stat ( marker )) 1426matches ( 'boom during load' , output , true ) 1427end ) 1428 1429it ( 'reports load failures at the failing line' , function () 1430local suite_dir =write_suite ( { 1431[ 'one_spec.lua' ] =[[ 1432local ok = true 1433 1434local broken = ) 1435]] , 1436} ) 1437 1438local code , output =run_harness ( suite_dir ) 1439 1440eq ( 1 , code ) 1441matches ( 'one_spec%.lua @ 3: .-one_spec%.lua %[load%]' , output ) 1442end ) 1443 1444it ( 'reports required-module syntax errors at the real module line' , function () 1445local suite_dir =write_suite ( { 1446[ 'broken.lua' ] =[[ 1447local ok = true 1448 1449local broken = ) 1450]] , 1451[ 'one_spec.lua' ] =[[ 1452require('broken') 1453 1454describe('one', function() 1455it('works', function() end) 1456end) 1457]] , 1458} ) 1459 1460local code , output =run_harness ( suite_dir ) 1461 1462eq ( 1 , code ) 1463matches ( 'broken%.lua @ 3: .-one_spec%.lua %[load%]' , output ) 1464end ) 1465end )