123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145(**************************************************************************)(* The OUnit library *)(* *)(* Copyright (C) 2002-2008 Maas-Maarten Zeeman. *)(* Copyright (C) 2010 OCamlCore SARL *)(* Copyright (C) 2013 Sylvain Le Gall *)(* *)(* The package OUnit is copyright by Maas-Maarten Zeeman, OCamlCore SARL *)(* and Sylvain Le Gall. *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining *)(* a copy of this document and the OUnit software ("the Software"), to *)(* deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, *)(* sublicense, and/or sell copies of the Software, and to permit persons *)(* to whom the Software is furnished to do so, subject to the following *)(* conditions: *)(* *)(* The above copyright notice and this permission notice shall be *)(* included in all copies or substantial portions of the Software. *)(* *)(* The Software is provided ``as is'', without warranty of any kind, *)(* express or implied, including but not limited to the warranties of *)(* merchantability, fitness for a particular purpose and noninfringement. *)(* In no event shall Maas-Maarten Zeeman be liable for any claim, damages *)(* or other liability, whether in an action of contract, tort or *)(* otherwise, arising from, out of or in connection with the Software or *)(* the use or other dealings in the software. *)(* *)(* See LICENSE.txt for details. *)(**************************************************************************)openOUnitTesttypet=(unit->unit)listletcreateset_uptear_downtest_ctxt=letfixture=set_uptest_ctxtinlettear_downtest_ctxt=tear_downfixturetest_ctxtinOUnitShared.Mutex.with_locktest_ctxt.sharedtest_ctxt.tear_down_mutex(fun()->test_ctxt.tear_down<-tear_down::test_ctxt.tear_down);fixtureletlogfloggerlvlfmt=OUnitLogger.Test.logfloggerlvlfmtletbracket_tmpfile?(prefix="ounit-")?(suffix=".txt")?modetest_ctxt=create(funtest_ctxt->letsuffix="-"^(OUnitTest.get_shard_idtest_ctxt)^suffixinlet(fn,chn)=Filename.open_temp_file?modeprefixsuffixinlogftest_ctxt.test_logger`Info"Created a temporary file: %S."fn;(fn,chn))(fun(fn,chn)test_ctxt->(tryclose_outchnwith_->());trySys.removefn;logftest_ctxt.test_logger`Info"Removed a temporary file: %S."fnwith_->())test_ctxtletbracket_tmpdir?(prefix="ounit-")?(suffix=".dir")test_ctxt=letmax_attempt=10inletrectry_hard_mkdirattempt=ifmax_attempt=attemptthenbeginOUnitUtils.failwithf"Unable to create temporary directory after %d attempts."attemptendelsebegintryletsuffix="-"^(OUnitTest.get_shard_idtest_ctxt)^suffixinlettmpdn=Filename.temp_fileprefixsuffixinSys.removetmpdn;Unix.mkdirtmpdn0o755;tmpdnwithUnix.Unix_error(Unix.EEXIST,"mkdir",_)->try_hard_mkdir(max_attempt+1)endincreate(funtest_ctxt->lettmpdn=try_hard_mkdir0inlogftest_ctxt.test_logger`Info"Create a temporary directory: %S."tmpdn;tmpdn)(funtmpdntest_ctxt->letlog_deletefn=logftest_ctxt.test_logger`Info"Delete in a temporary directory: %S."fninletsafe_runfa=tryfawith_->()inletrecrmdirfn=Array.iter(funbn->letfn'=Filename.concatfnbninletis_dir=tryletst=Unix.lstatfn'inst.Unix.st_kind=Unix.S_DIRwith_->falseinifis_dirthenbeginrmdirfn';safe_runUnix.rmdirfn';log_deletefn'endelsebeginsafe_runSys.removefn';log_deletefn'end)(trySys.readdirfnwith_->[||])inrmdirtmpdn;safe_runUnix.rmdirtmpdn;log_deletetmpdn)test_ctxtletchdir_mutex=OUnitShared.Mutex.createOUnitShared.ScopeProcessletbracket_chdirdir=create(funtest_ctxt->let()=OUnitLogger.infoftest_ctxt.logger"Change directory to %S"dir;tryOUnitShared.Mutex.locktest_ctxt.sharedchdir_mutex;withOUnitShared.Lock_failure->failwith"Trying to do a nested chdir."inletcur_pwd=Sys.getcwd()inUnix.chdirdir;cur_pwd)(funcur_pwdtest_ctxt->Unix.chdircur_pwd;OUnitShared.Mutex.unlocktest_ctxt.sharedchdir_mutex)letwith_brackettest_ctxtbracketf=section_ctxttest_ctxt(funtest_ctxt->letres=brackettest_ctxtinfrestest_ctxt)