@@ -22,6 +22,7 @@ use deno_core::ModuleSpecifier;
2222use deno_runtime:: web_worker:: WebWorkerOptions ;
2323use deno_runtime:: ops:: worker_host:: CreateWebWorkerCb ;
2424use deno_runtime:: web_worker:: WebWorker ;
25+ use clarity_repl:: clarity:: coverage:: CoverageReporter ;
2526
2627mod sessions {
2728 use std:: sync:: Mutex ;
@@ -35,10 +36,10 @@ mod sessions {
3536 use super :: TransactionArgs ;
3637
3738 lazy_static ! {
38- static ref SESSIONS : Mutex <HashMap <u32 , Session >> = Mutex :: new( HashMap :: new( ) ) ;
39+ pub static ref SESSIONS : Mutex <HashMap <u32 , ( String , Session ) >> = Mutex :: new( HashMap :: new( ) ) ;
3940 }
4041
41- pub fn handle_setup_chain ( transactions : Vec < TransactionArgs > ) -> Result < ( u32 , Vec < Account > ) , AnyError > {
42+ pub fn handle_setup_chain ( name : String , transactions : Vec < TransactionArgs > ) -> Result < ( u32 , Vec < Account > ) , AnyError > {
4243 let mut sessions = SESSIONS . lock ( ) . unwrap ( ) ;
4344 let session_id = sessions. len ( ) as u32 ;
4445
@@ -81,11 +82,11 @@ mod sessions {
8182 . initial_contracts
8283 . push ( repl:: settings:: InitialContract {
8384 code : deploy_contract. code . clone ( ) ,
85+ path : "" . into ( ) ,
8486 name : Some ( deploy_contract. name . clone ( ) ) ,
8587 deployer,
8688 } ) ;
8789 }
88-
8990 // if let Some(ref contract_call) tx.contract_call {
9091 // TODO: initial_tx_sender
9192 // let code = format!("(contract-call? '{}.{} {} {})", initial_tx_sender, contract_call.contract, contract_call.method, contract_call.args.join(" "));
@@ -99,6 +100,7 @@ mod sessions {
99100 // }
100101 }
101102
103+
102104 for ( name, config) in project_config. ordered_contracts ( ) . iter ( ) {
103105 let mut contract_path = root_path. clone ( ) ;
104106 contract_path. push ( & config. path ) ;
@@ -109,33 +111,33 @@ mod sessions {
109111 . initial_contracts
110112 . push ( repl:: settings:: InitialContract {
111113 code : code,
114+ path : contract_path. to_str ( ) . unwrap ( ) . into ( ) ,
112115 name : Some ( name. clone ( ) ) ,
113116 deployer : deployer_address. clone ( ) ,
114117 } ) ;
115118 }
116119 settings. initial_deployer = initial_deployer;
117120 settings. include_boot_contracts = vec ! [ "pox" . to_string( ) , "costs" . to_string( ) , "bns" . to_string( ) ] ;
118-
119121 let mut session = Session :: new ( settings. clone ( ) ) ;
120122 session. start ( ) ;
121123 session. advance_chain_tip ( 1 ) ;
122- sessions. insert ( session_id, session) ;
124+ sessions. insert ( session_id, ( name , session) ) ;
123125 Ok ( ( session_id, settings. initial_accounts ) )
124126 }
125127
126- pub fn perform_block < F , R > ( session_id : u32 , handler : F ) -> Result < R , AnyError > where F : FnOnce ( & mut Session ) -> Result < R , AnyError > {
128+ pub fn perform_block < F , R > ( session_id : u32 , handler : F ) -> Result < R , AnyError > where F : FnOnce ( & str , & mut Session ) -> Result < R , AnyError > {
127129 let mut sessions = SESSIONS . lock ( ) . unwrap ( ) ;
128130 match sessions. get_mut ( & session_id) {
129131 None => {
130132 println ! ( "Error: unable to retrieve session" ) ;
131133 unreachable ! ( )
132134 }
133- Some ( ref mut session) => handler ( session) ,
135+ Some ( ( name , ref mut session) ) => handler ( name . as_str ( ) , session) ,
134136 }
135137 }
136138}
137139
138- pub async fn run_tests ( files : Vec < String > ) -> Result < ( ) , AnyError > {
140+ pub async fn run_tests ( files : Vec < String > , include_coverage : bool ) -> Result < ( ) , AnyError > {
139141
140142 let fail_fast = true ;
141143 let quiet = false ;
@@ -210,6 +212,24 @@ pub async fn run_tests(files: Vec<String>) -> Result<(), AnyError> {
210212 return Err ( e) ;
211213 }
212214
215+ if include_coverage {
216+ let mut coverage_reporter = CoverageReporter :: new ( ) ;
217+ let sessions = sessions:: SESSIONS . lock ( ) . unwrap ( ) ;
218+ for ( session_id, ( name, session) ) in sessions. iter ( ) {
219+
220+ for contract in session. settings . initial_contracts . iter ( ) {
221+ if let Some ( ref name) = contract. name {
222+ if contract. path != "" {
223+ coverage_reporter. register_contract ( name. clone ( ) , contract. path . clone ( ) ) ;
224+ }
225+ }
226+ }
227+ coverage_reporter. add_reports ( & session. coverage_reports ) ;
228+ coverage_reporter. add_asts ( & session. asts ) ;
229+ }
230+
231+ coverage_reporter. write_lcov_file ( "coverage.lcov" ) ;
232+ }
213233 Ok ( ( ) )
214234}
215235
@@ -368,11 +388,12 @@ where
368388#[ derive( Debug , Deserialize ) ]
369389#[ serde( rename_all = "camelCase" ) ]
370390struct SetupChainArgs {
391+ name : String ,
371392 transactions : Vec < TransactionArgs >
372393}
373394
374395fn setup_chain ( args : SetupChainArgs ) -> Result < Value , AnyError > {
375- let ( session_id, accounts) = sessions:: handle_setup_chain ( args. transactions ) ?;
396+ let ( session_id, accounts) = sessions:: handle_setup_chain ( args. name , args . transactions ) ?;
376397
377398 Ok ( json ! ( {
378399 "session_id" : session_id,
@@ -419,7 +440,7 @@ struct TransferSTXArgs {
419440}
420441
421442fn mine_block ( args : MineBlockArgs ) -> Result < Value , AnyError > {
422- let ( block_height, receipts) = sessions:: perform_block ( args. session_id , |session| {
443+ let ( block_height, receipts) = sessions:: perform_block ( args. session_id , |name , session| {
423444 let initial_tx_sender = session. get_tx_sender ( ) ;
424445 let mut receipts = vec ! [ ] ;
425446 for tx in args. transactions . iter ( ) {
@@ -433,12 +454,12 @@ fn mine_block(args: MineBlockArgs) -> Result<Value, AnyError> {
433454 } else {
434455 format ! ( "(contract-call? '{}.{} {} {})" , initial_tx_sender, args. contract, args. method, args. args. join( " " ) )
435456 } ;
436- let execution = session. interpret ( snippet, None , true ) . unwrap ( ) ; // todo(ludo)
457+ let execution = session. interpret ( snippet, None , true , Some ( name . into ( ) ) ) . unwrap ( ) ; // todo(ludo)
437458 receipts. push ( ( execution. result , execution. events ) ) ;
438459 }
439460
440461 if let Some ( ref args) = tx. deploy_contract {
441- let execution = session. interpret ( args. code . clone ( ) , Some ( args. name . clone ( ) ) , true ) . unwrap ( ) ; // todo(ludo)
462+ let execution = session. interpret ( args. code . clone ( ) , Some ( args. name . clone ( ) ) , true , Some ( name . into ( ) ) ) . unwrap ( ) ; // todo(ludo)
442463 receipts. push ( ( execution. result , execution. events ) ) ;
443464 }
444465 }
@@ -466,7 +487,7 @@ struct MineEmptyBlocksArgs {
466487}
467488
468489fn mine_empty_blocks ( args : MineEmptyBlocksArgs ) -> Result < Value , AnyError > {
469- let block_height = sessions:: perform_block ( args. session_id , |session| {
490+ let block_height = sessions:: perform_block ( args. session_id , |name , session| {
470491 let block_height = session. advance_chain_tip ( args. count ) ;
471492 Ok ( block_height)
472493 } ) ?;
@@ -488,7 +509,7 @@ struct CallReadOnlyFnArgs {
488509}
489510
490511fn call_read_only_fn ( args : CallReadOnlyFnArgs ) -> Result < Value , AnyError > {
491- let ( result, events) = sessions:: perform_block ( args. session_id , |session| {
512+ let ( result, events) = sessions:: perform_block ( args. session_id , |name , session| {
492513 let initial_tx_sender = session. get_tx_sender ( ) ;
493514 session. set_tx_sender ( args. sender . clone ( ) ) ;
494515
@@ -500,7 +521,7 @@ fn call_read_only_fn(args: CallReadOnlyFnArgs) -> Result<Value, AnyError> {
500521 format ! ( "(contract-call? '{}.{} {} {})" , initial_tx_sender, args. contract, args. method, args. args. join( " " ) )
501522 } ;
502523
503- let execution = session. interpret ( snippet, None , true ) . unwrap ( ) ; // todo(ludo)
524+ let execution = session. interpret ( snippet, None , true , Some ( name . into ( ) ) ) . unwrap ( ) ; // todo(ludo)
504525 session. set_tx_sender ( initial_tx_sender) ;
505526 Ok ( ( execution. result , execution. events ) )
506527 } ) ?;
@@ -519,7 +540,7 @@ struct GetAssetsMapsArgs {
519540}
520541
521542fn get_assets_maps ( args : GetAssetsMapsArgs ) -> Result < Value , AnyError > {
522- let assets_maps = sessions:: perform_block ( args. session_id , |session| {
543+ let assets_maps = sessions:: perform_block ( args. session_id , |name , session| {
523544 let assets_maps = session. get_assets_maps ( ) ;
524545 Ok ( assets_maps)
525546 } ) ?;
0 commit comments