22
33use foundry_compilers_artifacts:: Remapping ;
44use foundry_compilers_core:: error:: { Result , SolcError } ;
5- use rand:: {
6- distributions:: { Distribution , Uniform } ,
7- seq:: SliceRandom ,
8- Rng ,
9- } ;
5+ use rand:: { seq:: SliceRandom , Rng } ;
106use serde:: { Deserialize , Serialize } ;
117use std:: {
128 collections:: { BTreeSet , HashMap , HashSet , VecDeque } ,
@@ -69,11 +65,11 @@ impl MockProjectGenerator {
6965 }
7066
7167 let graph = Graph :: < MultiCompilerParser > :: resolve ( paths) ?;
72- let mut gen = Self :: default ( ) ;
68+ let mut generated = Self :: default ( ) ;
7369 let ( _, edges) = graph. into_sources ( ) ;
7470
7571 // add all files as source files
76- gen . add_sources ( edges. files ( ) . count ( ) ) ;
72+ generated . add_sources ( edges. files ( ) . count ( ) ) ;
7773
7874 // stores libs and their files
7975 let libs = get_libs (
@@ -84,25 +80,25 @@ impl MockProjectGenerator {
8480
8581 // mark all files as libs
8682 for ( lib_id, lib_files) in libs. into_values ( ) . enumerate ( ) {
87- let lib_name = gen . name_strategy . new_lib_name ( lib_id) ;
88- let offset = gen . inner . files . len ( ) ;
83+ let lib_name = generated . name_strategy . new_lib_name ( lib_id) ;
84+ let offset = generated . inner . files . len ( ) ;
8985 let lib = MockLib { name : lib_name, id : lib_id, num_files : lib_files. len ( ) , offset } ;
9086 for lib_file in lib_files {
91- let file = & mut gen . inner . files [ lib_file] ;
87+ let file = & mut generated . inner . files [ lib_file] ;
9288 file. lib_id = Some ( lib_id) ;
93- file. name = gen . name_strategy . new_lib_name ( file. id ) ;
89+ file. name = generated . name_strategy . new_lib_name ( file. id ) ;
9490 }
95- gen . inner . libraries . push ( lib) ;
91+ generated . inner . libraries . push ( lib) ;
9692 }
9793
9894 for id in edges. files ( ) {
9995 for import in edges. imported_nodes ( id) . iter ( ) . copied ( ) {
100- let import = gen . get_import ( import) ;
101- gen . inner . files [ id] . imports . insert ( import) ;
96+ let import = generated . get_import ( import) ;
97+ generated . inner . files [ id] . imports . insert ( import) ;
10298 }
10399 }
104100
105- Ok ( gen )
101+ Ok ( generated )
106102 }
107103
108104 /// Consumes the type and returns the underlying skeleton
@@ -242,34 +238,36 @@ impl MockProjectGenerator {
242238 self
243239 }
244240
245- /// randomly assign empty file status so that mocked files don't emit artifacts
241+ /// Randomly assign empty file status so that mocked files don't emit artifacts.
246242 pub fn assign_empty_files ( & mut self ) -> & mut Self {
247- let mut rng = rand:: thread_rng ( ) ;
248- let die = Uniform :: from ( 0 ..self . inner . files . len ( ) ) ;
243+ let mut rng = rand:: rng ( ) ;
244+ let n = self . inner . files . len ( ) ;
245+
249246 for file in self . inner . files . iter_mut ( ) {
250- let throw = die . sample ( & mut rng ) ;
247+ let throw = rng . random_range ( 0 ..n ) ;
251248 if throw == 0 {
252- // give it a 1 in num(files) chance that the file will be empty
249+ // 1 in n chance that the file will be empty
253250 file. emit_artifacts = false ;
254251 }
255252 }
253+
256254 self
257255 }
258256
259257 /// Populates the imports of the project
260258 pub fn populate_imports ( & mut self , settings : & MockProjectSettings ) -> & mut Self {
261- let mut rng = rand:: thread_rng ( ) ;
259+ let mut rng = rand:: rng ( ) ;
262260
263261 // populate imports
264262 for id in 0 ..self . inner . files . len ( ) {
265263 let imports = if let Some ( lib) = self . inner . files [ id] . lib_id {
266264 let num_imports = rng
267- . gen_range ( settings. min_imports ..=settings. max_imports )
265+ . random_range ( settings. min_imports ..=settings. max_imports )
268266 . min ( self . inner . libraries [ lib] . num_files . saturating_sub ( 1 ) ) ;
269267 self . unique_imports_for_lib ( & mut rng, lib, id, num_imports)
270268 } else {
271269 let num_imports = rng
272- . gen_range ( settings. min_imports ..=settings. max_imports )
270+ . random_range ( settings. min_imports ..=settings. max_imports )
273271 . min ( self . inner . files . len ( ) . saturating_sub ( 1 ) ) ;
274272 self . unique_imports_for_source ( & mut rng, id, num_imports)
275273 } ;
@@ -436,11 +434,16 @@ impl MockFile {
436434
437435 pub fn target_path < L : Language > (
438436 & self ,
439- gen : & MockProjectGenerator ,
437+ generated : & MockProjectGenerator ,
440438 paths : & ProjectPathsConfig < L > ,
441439 ) -> PathBuf {
442440 let mut target = if let Some ( lib) = self . lib_id {
443- paths. root . join ( "lib" ) . join ( & gen. inner . libraries [ lib] . name ) . join ( "src" ) . join ( & self . name )
441+ paths
442+ . root
443+ . join ( "lib" )
444+ . join ( & generated. inner . libraries [ lib] . name )
445+ . join ( "src" )
446+ . join ( & self . name )
444447 } else {
445448 paths. sources . join ( & self . name )
446449 } ;
@@ -551,14 +554,14 @@ pub struct MockProjectSettings {
551554impl MockProjectSettings {
552555 /// Generates a new instance with random settings within an arbitrary range
553556 pub fn random ( ) -> Self {
554- let mut rng = rand:: thread_rng ( ) ;
557+ let mut rng = rand:: rng ( ) ;
555558 // arbitrary thresholds
556559 Self {
557- num_sources : rng. gen_range ( 2 ..25 ) ,
558- num_libs : rng. gen_range ( 0 ..5 ) ,
559- num_lib_files : rng. gen_range ( 1 ..10 ) ,
560- min_imports : rng. gen_range ( 0 ..3 ) ,
561- max_imports : rng. gen_range ( 4 ..10 ) ,
560+ num_sources : rng. random_range ( 2 ..25 ) ,
561+ num_libs : rng. random_range ( 0 ..5 ) ,
562+ num_lib_files : rng. random_range ( 1 ..10 ) ,
563+ min_imports : rng. random_range ( 0 ..3 ) ,
564+ max_imports : rng. random_range ( 4 ..10 ) ,
562565 allow_no_artifacts_files : true ,
563566 }
564567 }
0 commit comments