Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file expect_test_collector.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301openExpect_test_commonmoduleList=ListLabelsmoduleTest_outcome=structtypet={file_digest:File.Digest.t;location:File.Location.t;expectations:Expectation.Raw.tlist;uncaught_exn_expectation:Expectation.Raw.toption;saved_output:(File.Location.t*string)list;trailing_output:string;upon_unreleasable_issue:Expect_test_config_types.Upon_unreleasable_issue.t;uncaught_exn:(exn*Printexc.raw_backtrace)option}endlettests_run:Test_outcome.tlistref=ref[]letprotect~finally~f=matchf()with|x->finally();x|exceptione->finally();raisee;;moduleCurrent_file=structletcurrent=refNoneletset~absolute_filename=match!currentwith|None->current:=Someabsolute_filename|Some_->failwith"Expect_test_collector.set: already set";;letunset()=match!currentwith|Some_->current:=None|None->failwith"Expect_test_collector.unset: not set";;letget()=match!currentwith|Somefn->fn|None->failwith"Expect_test_collector.get: not set";;endmoduleMake(C:Expect_test_config_types.S)=structlet(>>=)tf=C.IO_flush.bindt~fletreturn=C.IO_flush.returnmoduleC=structincludeCletflush()=Format.pp_print_flushFormat.std_formatter();Format.pp_print_flushFormat.err_formatter();Caml.flushCaml.stdout;Caml.flushCaml.stderr;C.flush();;endmoduleInstance:sigvalsave_output:File.Location.t->unitC.IO_flush.tvalsave_and_return_output:File.Location.t->stringC.IO_flush.tvalexec:file_digest:File.Digest.t->location:File.Location.t->expectations:Expectation.Raw.tlist->uncaught_exn_expectation:Expectation.Raw.toption->f:(unit->unitC.IO_run.t)->unitend=structtypet={mutablesaved:(File.Location.t*int)list;chan:out_channel;filename:File.Name.t}externalbefore_test:output:out_channel->stdout:out_channel->stderr:out_channel->unit="expect_test_collector_before_test"externalafter_test:stdout:out_channel->stderr:out_channel->unit="expect_test_collector_after_test"externalpos_out:out_channel->int="caml_out_channel_pos_fd"letget_position()=pos_outstdoutletcreate()=letfilename=Filename.temp_file"expect-test""output"inletchan=open_out_binfilenameinbefore_test~output:chan~stdout~stderr;{chan;filename=File.Name.of_stringfilename;saved=[]};;letextract_outputiclen=lets=really_input_stringicleninifnot(Check_backtraces.contains_backtracess)thenselseExpect_test_config_types.Upon_unreleasable_issue.message_when_expectation_contains_backtraceC.upon_unreleasable_issue^s;;letrelative_filenamet=File.Name.relative_to~dir:(File.initial_dir())t.filenameletwith_icfname~f=letic=open_in_binfnameinprotect~finally:(fun()->close_inic)~f:(fun()->fic);;letget_outputs_and_cleanupt=letlast_ofs=get_position()inafter_test~stdout~stderr;close_outt.chan;letfname=relative_filenametinprotect~finally:(fun()->Sys.removefname)~f:(fun()->with_icfname~f:(funic->letofs,outputs=List.fold_left(List.revt.saved)~init:(0,[])~f:(fun(ofs,acc)(loc,next_ofs)->lets=extract_outputic(next_ofs-ofs)innext_ofs,(loc,s)::acc)inlettrailing_output=extract_outputic(last_ofs-ofs)inList.revoutputs,trailing_output));;letcurrent_test:(File.Location.t*t)optionref=refNoneletget_current()=match!current_testwith|Some(_,t)->t|None->failwith"Expect_test_collector.Instance.get_current called outside a test.";;letsave_outputlocation=lett=get_current()inC.flush()>>=fun()->letpos=get_position()int.saved<-(location,pos)::t.saved;return();;letsave_and_return_outputlocation=lett=get_current()inC.flush()>>=fun()->letpos=get_position()inletprev_pos=matcht.savedwith|[]->0|(_,prev_pos)::_->prev_posint.saved<-(location,pos)::t.saved;flusht.chan;letlen=pos-prev_posinreturn(with_ic(relative_filenamet)~f:(funic->seek_inicprev_pos;really_input_stringiclen));;let()=Caml.at_exit(fun()->match!current_testwith|None->()|Some(loc,t)->letblocks,trailing=get_outputs_and_cleanuptinPrintf.eprintf"File %S, line %d, characters %d-%d:\n\
Error: program exited while expect test was running!\n\
Output captured so far:\n\
%!"(File.Name.to_stringloc.filename)loc.line_number(loc.start_pos-loc.line_start)(loc.end_pos-loc.line_start);List.iterblocks~f:(fun(_,s)->Printf.eprintf"%s%!"s);Printf.eprintf"%s%!"trailing);;letrecfinal_flush?(count=0)k=letmax_attempts=10inC.flush()>>=fun()->ifC.flushed()thenk~append:""elseifcount=max_attemptsthenk~append:(Printf.sprintf"\n\
STOPPED COLLECTING OUTPUT AFTER %d FLUSHING ATTEMPS\n\
THERE MUST BE A BACKGROUND JOB PRINTING TO STDOUT\n"max_attempts)elsefinal_flush~count:(count+1)k;;letexec~file_digest~location~expectations~uncaught_exn_expectation~f=lett=create()incurrent_test:=Some(location,t);letfinallyuncaught_exn=C.run(fun()->C.IO_flush.to_run(final_flush(fun~append->current_test:=None;letsaved_output,trailing_output=get_outputs_and_cleanuptintests_run:={file_digest;location;expectations;uncaught_exn_expectation;saved_output;trailing_output=trailing_output^append;upon_unreleasable_issue=C.upon_unreleasable_issue;uncaught_exn}::!tests_run;return())))inmatchC.runfwith|()->finallyNone|exceptionexn->letbt=Printexc.get_raw_backtrace()infinally(Some(exn,bt));;endletsave_output=Instance.save_outputletsave_and_return_output=Instance.save_and_return_outputletrun~file_digest~(location:File.Location.t)~absolute_filename:defined_in~description~tags~expectations~uncaught_exn_expectation~inline_test_configf=Ppx_inline_test_lib.Runtime.test~config:inline_test_config~descr:(matchdescriptionwith|None->""|Somes->": "^s)~tags~filename:(File.Name.to_stringlocation.filename)~line_number:location.line_number~start_pos:(location.start_pos-location.line_start)~end_pos:(location.end_pos-location.line_start)(fun()->letregistering_tests_for=Current_file.get()inifdefined_in<>registering_tests_forthenPrintf.ksprintffailwith"Trying to run an expect test from the wrong file.\n\
- test declared at %s:%d\n\
- trying to run it from %s\n"defined_inlocation.line_numberregistering_tests_forelse((* To avoid capturing not-yet flushed data of the stdout buffer *)C.run(fun()->C.IO_flush.to_run(C.flush()));Instance.exec~file_digest~location~expectations~uncaught_exn_expectation~f;true));;end[@@inlinenever]lettests_run()=(* We prepend tests when we encounter them, so reverse the list to reinstate order *)List.rev!tests_run;;