robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
baabb7ab6f
master
1--- @alias test.reporter.SummaryStatus 'skipped' | 'failure' | 'error' 2 3--- @class test.reporter.ColorMap 4--- @field bright fun(s: any): string 5--- @field green fun(s: any): string 6--- @field yellow fun(s: any): string 7--- @field magenta fun(s: any): string 8--- @field red fun(s: any): string 9--- @field cyan fun(s: any): string 10--- @field dim fun(s: any): string 11 12--- @class test.reporter.FileElement 13--- @field name string 14--- @field duration? number 15 16--- @class test.reporter.Options 17--- @field paths string[] 18--- @field verbose boolean 19--- @field summary_file string 20 21--- @class test.base_reporter 22--- @field new fun(opts: test.reporter.Options): test.base_reporter 23--- @field suite_start fun(self: test.base_reporter, repeat_index?: integer, repeat_count?: integer) 24--- @field file_start fun(self: test.base_reporter, file: test.reporter.FileElement) 25--- @field test_start fun(self: test.base_reporter, name: string) 26--- @field test_end fun(self: test.base_reporter, record: test.harness.Record) 27--- @field file_end fun(self: test.base_reporter, file: test.reporter.FileElement, test_count: integer) 28--- @field suite_end fun(self: test.base_reporter, duration: number, run_summary: test.harness.RunSummary, failure_output?: string) 29 30--- @class test.reporter : test.base_reporter 31--- @field opts test.reporter.Options 32--- @field colors test.reporter.ColorMap 33local M ={} 34M . __index =M 35 36--- @return test.reporter.ColorMap 37local function identity_colors () 38return setmetatable ( {} , { 39__index =function () 40return function ( s ) 41return s == nil and '' or tostring ( s ) 42end 43end , 44} ) 45end 46 47--- @param code string 48--- @return fun(s: any): string 49local function ansi_color ( code ) 50return function ( s ) 51return ( '\27[%sm%s\27[0m' ): format ( code , s == nil and '' or tostring ( s )) 52end 53end 54 55local ansi_colors ={ 56bright =ansi_color ( '1' ), 57green =ansi_color ( '32' ), 58yellow =ansi_color ( '33' ), 59magenta =ansi_color ( '35' ), 60red =ansi_color ( '31' ), 61cyan =ansi_color ( '36' ), 62dim =ansi_color ( '2' ), 63} 64 65--- @return boolean 66local function use_colors () 67local test_colors =os . getenv ( 'TEST_COLORS' ) 68if not test_colors then 69return true 70end 71 72local value =test_colors : lower () 73return not ( value == 'false' or value == '0' or value == 'no' or value == 'off' ) 74end 75 76--- @param path? string 77--- @return file 78local function open_summary_file ( path ) 79if type ( path ) ~= 'string' or path == '-' then 80return io . stdout 81end 82 83return ( assert ( io . open ( path , 'w' ))) 84end 85 86--- @param opts test.reporter.Options 87--- @return test.reporter 88function M . new ( opts ) 89local colors =identity_colors () 90if use_colors () then 91colors =ansi_colors 92end 93 94--- @type test.reporter 95local self =setmetatable ( { 96opts =opts , 97colors =colors , 98} , M ) 99 100return self 101end 102 103--- @param element? { duration?: number } 104--- @return number 105local function get_elapsed_time_ms ( element ) 106if element and element . duration then 107return element . duration * 1000 108end 109 110return tonumber ( 'nan' ) 111end 112 113--- @private 114--- @param s any 115--- @return string 116function M : succ ( s ) 117return self . colors . bright ( self . colors . green ( s )) 118end 119 120--- @private 121--- @param s any 122--- @return string 123function M : skip ( s ) 124return self . colors . bright ( self . colors . yellow ( s )) 125end 126 127--- @private 128--- @param s any 129--- @return string 130function M : fail ( s ) 131return self . colors . bright ( self . colors . magenta ( s )) 132end 133 134--- @private 135--- @param s any 136--- @return string 137function M : errr ( s ) 138return self . colors . bright ( self . colors . red ( s )) 139end 140 141--- @private 142--- @param s any 143--- @return string 144function M : fpath ( s ) 145return self . colors . cyan ( s ) 146end 147 148--- @private 149--- @param s any 150--- @return string 151function M : time ( s ) 152return self . colors . dim ( s ) 153end 154 155--- @private 156--- @param s any 157--- @return string 158function M : sect ( s ) 159return self . colors . green ( self . colors . dim ( s )) 160end 161 162--- @private 163--- @param s any 164--- @return string 165function M : nmbr ( s ) 166return self . colors . bright ( s ) 167end 168 169--- @private 170--- @param status test.harness.ResultStatus 171--- @return string 172function M : result_text ( status ) 173if status == 'success' then 174return ( '%s\n' ): format ( self : succ ( 'OK' )) 175elseif status == 'pending' then 176return ( '%s\n' ): format ( self : skip ( 'SKIP' )) 177elseif status == 'failure' then 178return ( '%s\n' ): format ( self : fail ( 'FAIL' )) 179end 180 181return ( '%s\n' ): format ( self : errr ( 'ERR' )) 182end 183 184--- @private 185--- @param status test.reporter.SummaryStatus 186--- @return string 187function M : summary_label ( status ) 188if status == 'skipped' then 189return self : skip ( 'SKIPPED ' ) 190elseif status == 'failure' then 191return self : fail ( 'FAILED ' ) 192end 193 194return self : errr ( 'ERROR ' ) 195end 196 197--- @private 198--- @param status test.reporter.SummaryStatus 199--- @param count integer 200--- @return string 201function M : summary_header_noun ( status , count ) 202if status == 'error' then 203return count == 1 and 'error' or 'errors' 204end 205 206return count == 1 and 'test' or 'tests' 207end 208 209--- @private 210--- @param status test.reporter.SummaryStatus 211--- @param count integer 212--- @return string 213function M : summary_footer_noun ( status , count ) 214if status == 'skipped' then 215return count == 1 and 'SKIPPED TEST' or 'SKIPPED TESTS' 216elseif status == 'failure' then 217return count == 1 and 'FAILED TEST' or 'FAILED TESTS' 218elseif status == 'error' then 219return count == 1 and 'ERROR' or 'ERRORS' 220end 221 222return count == 1 and 'TEST' or 'TESTS' 223end 224 225--- @param message any 226--- @return string 227local function stringify_message ( message ) 228if type ( message ) == 'string' then 229return message 230elseif message == nil then 231return '' 232end 233 234return vim . inspect ( message ) 235end 236 237--- True if `pending` starts with "<token>:". Used to bucket skips: 238--- - 'N/A' for non-actionable skips. 239--- - 'NVIM_TEST_INTEG' for integration tests. 240--- 241--- @private 242--- @param pending test.harness.Record 243--- @param token string 244--- @return boolean 245function M : has_skip_token ( pending , token ) 246if pending . name and vim . list_contains ( vim . split ( pending . name , '[ :]' ), token ) then 247return true 248end 249 250if type ( pending . message ) == 'string' then 251return vim . list_contains ( vim . split ( pending . message , '[ :]' ), token ) 252end 253 254return false 255end 256 257--- @private 258--- @param pending test.harness.Record 259--- @return string 260function M : pending_description ( pending ) 261local message =stringify_message ( pending . message ) 262if message == '' then 263return '' 264end 265 266return table . concat ( { message , '\n' } ) 267end 268 269--- @private 270--- @param failure test.harness.Record 271--- @return string 272function M : failure_description ( failure ) 273local message =stringify_message ( failure . message ) 274if message == '' then 275message ='Nil error' 276end 277 278local parts ={ message , '\n' } 279if self . opts . verbose and failure . traceback then 280parts [ # parts + 1 ] =failure . traceback 281parts [ # parts + 1 ] ='\n' 282end 283 284return table . concat ( parts ) 285end 286 287--- @private 288--- @param trace? test.harness.Trace 289--- @return string 290function M : get_file_line ( trace ) 291if not trace or not trace . short_src then 292return '' 293end 294 295local source =vim . fs . normalize ( trace . short_src ) 296local line =trace . currentline or 0 297return self : fpath ( source ) .. ' @ ' .. self : fpath ( line ) .. ': ' 298end 299 300--- @private 301--- @param status test.reporter.SummaryStatus 302--- @param count integer 303--- @param list test.harness.Record[] 304--- @param describe fun(self: test.reporter, item: test.harness.Record): string 305--- @return string 306function M : get_test_list ( status , count , list , describe ) 307if count == 0 then 308return '' 309end 310 311local label =self : summary_label ( status ) 312local parts ={ 313( '%s %s %s, listed below:\n' ): format ( 314label , 315self : nmbr ( count ), 316self : summary_header_noun ( status , count ) 317), 318} 319local na_count =0 320local integ_count =0 321 322for _ , item in ipairs ( list ) do 323if status == 'skipped' and self : has_skip_token ( item , 'NVIM_TEST_INTEG' ) then 324integ_count =integ_count + 1 325elseif status == 'skipped' and self : has_skip_token ( item , 'N/A' ) then 326na_count =na_count + 1 327else 328local fullname =self : get_file_line ( item . trace ) .. self : nmbr ( item . name ) 329parts [ # parts + 1 ] =( '%s %s\n' ): format ( label , fullname ) 330parts [ # parts + 1 ] =describe ( self , item ) 331end 332end 333 334if na_count > 0 then 335parts [ # parts + 1 ] =self : nmbr (( 'Skipped %d N/A test(s) (not listed)\n' ): format ( na_count )) 336end 337if integ_count > 0 then 338parts [ # parts + 1 ] = 339self : nmbr (( 'Skipped %d NVIM_TEST_INTEG test(s) (not listed)\n' ): format ( integ_count )) 340end 341 342return table . concat ( parts ) 343end 344 345--- @private 346--- @param status test.reporter.SummaryStatus 347--- @param count integer 348--- @return string 349function M : get_summary ( status , count ) 350if count == 0 then 351return '' 352end 353 354return ( ' %s %s\n' ): format ( self : nmbr ( count ), self : summary_footer_noun ( status , count )) 355end 356 357--- @private 358--- @return string 359--- @param summary test.harness.RunSummary 360function M : get_summary_string ( summary ) 361local tests =summary . success_count == 1 and 'test' or 'tests' 362local parts ={ 363( '%s %s %s.\n' ): format ( self : succ ( 'PASSED ' ), self : nmbr ( summary . success_count ), tests ), 364self : get_test_list ( 'skipped' , summary . skipped_count , summary . pendings , M . pending_description ), 365self : get_test_list ( 'failure' , summary . failure_count , summary . failures , M . failure_description ), 366self : get_test_list ( 'error' , summary . error_count , summary . errors , M . failure_description ), 367} 368 369if ( summary . skipped_count + summary . failure_count + summary . error_count ) > 0 then 370parts [ # parts + 1 ] ='\n' 371end 372 373parts [ # parts + 1 ] =self : get_summary ( 'skipped' , summary . skipped_count ) 374parts [ # parts + 1 ] =self : get_summary ( 'failure' , summary . failure_count ) 375parts [ # parts + 1 ] =self : get_summary ( 'error' , summary . error_count ) 376return table . concat ( parts ) 377end 378 379--- @return nil 380--- @param repeat_index? integer 381--- @param repeat_count? integer 382function M : suite_start ( repeat_index , repeat_count ) 383if repeat_count and repeat_count > 1 and repeat_index then 384io . write (( '\nRepeating all tests (run %d of %d) . . .\n\n' ): format ( repeat_index , repeat_count )) 385end 386io . write (( '%s Global test environment setup.\n' ): format ( self : sect ( '--------' ))) 387io . flush () 388end 389 390--- @param file test.reporter.FileElement 391function M : file_start ( file ) 392io . write ( 393( '%s Running tests from %s\n' ): format ( 394self : sect ( '--------' ), 395self : fpath ( vim . fs . normalize ( file . name )) 396) 397) 398io . flush () 399end 400 401--- @param name string 402function M : test_start ( name ) 403local desc =( '%s %s' ): format ( _G . _nvim_test_id or '' , name ) 404io . write (( '%s %s: ' ): format ( self : sect ( 'RUN ' ), desc )) 405io . flush () 406end 407 408--- @private 409--- @param record { duration?: number } 410--- @param text string 411function M : write_status ( record , text ) 412io . write (( '%s %s' ): format ( self : time (( '%.2f ms' ): format ( get_elapsed_time_ms ( record ))), text )) 413io . flush () 414end 415 416--- @param record test.harness.Record 417function M : test_end ( record ) 418local text =self : result_text ( record . status ) 419if record . status == 'failure' or record . status == 'error' then 420text =text .. self : failure_description ( record ) 421end 422 423self : write_status ( record , text ) 424end 425 426--- @param file test.reporter.FileElement 427--- @param test_count integer 428function M : file_end ( file , test_count ) 429io . write ( 430( '%s %s %s from %s %s\n\n' ): format ( 431self : sect ( '--------' ), 432self : nmbr ( test_count ), 433test_count == 1 and 'test' or 'tests' , 434self : fpath ( vim . fs . normalize ( file . name )), 435self : time (( '(%.2f ms total)' ): format ( get_elapsed_time_ms ( file ))) 436) 437) 438io . flush () 439end 440 441--- @param duration number 442--- @param run_summary test.harness.RunSummary 443--- @param failure_output? string 444function M : suite_end ( duration , run_summary , failure_output ) 445local tests =run_summary . test_count == 1 and 'test' or 'tests' 446local files =run_summary . file_count == 1 and 'file' or 'files' 447 448io . write (( '%s Global test environment teardown.\n' ): format ( self : sect ( '--------' ))) 449io . flush () 450 451local fpath =function ( s ) 452return self : fpath ( s ) 453end 454 455local summary_file =open_summary_file ( self . opts . summary_file ) 456summary_file : write ( '\n' ) 457summary_file : write ( 458( '%s %s %s from %s test %s of %s ran. %s\n' ): format ( 459self : sect ( '========' ), 460self : nmbr ( run_summary . test_count ), 461tests , 462self : nmbr ( run_summary . file_count ), 463files , 464vim . iter ( self . opts . paths ): map ( fpath ): join ( ';' ), 465self : time (( '(%.2f ms total)' ): format ( duration * 1000 )) 466) 467) 468summary_file : write ( self : get_summary_string ( run_summary )) 469if failure_output then 470summary_file : write ( failure_output ) 471end 472summary_file : flush () 473if summary_file ~= io . stdout then 474summary_file : close () 475end 476end 477 478return M