@@ -249,6 +249,7 @@ impl Module {
249249 config : & Config ,
250250 emission_location : & Location ,
251251 top_level_prefix : impl fmt:: Display ,
252+ emit_tests : bool ,
252253 ) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync + ' static > > {
253254 if self . submodules . is_empty ( ) && !emission_location. is_top_level ( ) {
254255 // Only output a single file
@@ -257,14 +258,18 @@ impl Module {
257258 self . contents ( config, emission_location, top_level_prefix)
258259 . to_string ( ) ,
259260 ) ?;
260- fs:: write (
261- test_path. with_extension ( "rs" ) ,
262- self . tests ( config) . to_string ( ) ,
263- ) ?;
261+ if emit_tests {
262+ fs:: write (
263+ test_path. with_extension ( "rs" ) ,
264+ self . tests ( config) . to_string ( ) ,
265+ ) ?;
266+ }
264267 } else {
265268 // Output an entire module
266269 fs:: create_dir_all ( path) ?;
267- fs:: create_dir_all ( test_path) ?;
270+ if emit_tests {
271+ fs:: create_dir_all ( test_path) ?;
272+ }
268273
269274 // TODO: Fix this
270275 let mut expected_files: Vec < OsString > = vec ! [ ] ;
@@ -278,6 +283,7 @@ impl Module {
278283 config,
279284 & emission_location. add_module ( & name) ,
280285 "//! This file has been automatically generated by `objc2`'s `header-translator`.\n //! DO NOT EDIT\n " ,
286+ emit_tests,
281287 ) ?;
282288 if module. submodules . is_empty ( ) {
283289 expected_files. push ( format ! ( "{name}.rs" ) . into ( ) ) ;
@@ -291,24 +297,28 @@ impl Module {
291297 self . contents ( config, emission_location, top_level_prefix)
292298 . to_string ( ) ,
293299 ) ?;
294- fs:: write ( test_path. join ( "mod.rs" ) , self . tests ( config) . to_string ( ) ) ?;
300+ if emit_tests {
301+ fs:: write ( test_path. join ( "mod.rs" ) , self . tests ( config) . to_string ( ) ) ?;
302+ }
295303 expected_files. push ( "mod.rs" . into ( ) ) ;
296304
297305 // Remove previously generated files
298- for file in path. read_dir ( ) ?. chain ( test_path. read_dir ( ) ?) {
299- let file = file?;
300- if expected_files. contains ( & file. file_name ( ) ) {
301- continue ;
302- }
303- error ! ( "removing previous file {:?}" , file. path( ) ) ;
304- if file. path ( ) . is_dir ( ) {
305- fs:: remove_dir_all ( file. path ( ) ) ?;
306- } else {
307- fs:: remove_file ( file. path ( ) ) ?;
306+ if let Ok ( test_dir) = test_path. read_dir ( ) {
307+ for file in path. read_dir ( ) ?. chain ( test_dir) {
308+ let file = file?;
309+ if expected_files. contains ( & file. file_name ( ) ) {
310+ continue ;
311+ }
312+ error ! ( "removing previous file {:?}" , file. path( ) ) ;
313+ if file. path ( ) . is_dir ( ) {
314+ fs:: remove_dir_all ( file. path ( ) ) ?;
315+ } else {
316+ fs:: remove_file ( file. path ( ) ) ?;
317+ }
308318 }
309319 }
310320
311- if emission_location. is_top_level ( ) {
321+ if emission_location. is_top_level ( ) && emit_tests {
312322 let data = config. library ( emission_location) ;
313323 let mut s = String :: new ( ) ;
314324 writeln ! ( & mut s, "#![cfg(feature = \" test-frameworks\" )]" ) ?;
0 commit comments