test_tools 0.18.1

Tools for writing and running tests.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
//!
//! Smoke test checking health of a module.
//!

// qqq : does not work in parallel, fix
// qqq : make a command for willbe

// xxx2 : use process_tools to build and run rust programs, introduce program_

/// Define a private namespace for all its items.
mod private {
  #[ allow( unused_imports ) ]
  use crate::*;
  #[ cfg( feature = "process_environment_is_cicd" ) ]
  fn is_in_ci() -> bool { crate::process::environment::is_cicd() }
  #[ cfg( not( feature = "process_environment_is_cicd" ) ) ]
  fn is_in_ci() -> bool { false }

  /// Context for smoke testing of a module.
  #[ derive( Debug ) ]
  pub struct SmokeModuleTest<'a> {
    /// Name of module.
    pub dependency_name: &'a str,
    /// Version of module.
    pub version: &'a str,
    /// Local path to the module.
    pub local_path_clause: &'a str,
    /// Code to run during smoke testing.
    pub code: String,
    /// Path to temp directory to put all files.
    pub test_path: std::path::PathBuf,
    /// Postfix to add to name.
    pub test_postfix: &'a str,
    /// Additional dependencies configuration.
    pub dependencies: std::collections::HashMap<String, DependencyConfig>,
  }

  /// Configuration for a dependency in Cargo.toml.
  #[ derive( Debug, Clone ) ]
  pub struct DependencyConfig {
    /// Version specification.
    pub version: Option<String>,
    /// Local path specification.
    pub path: Option<std::path::PathBuf>,
    /// Features to enable.
    pub features: Vec<String>,
    /// Whether dependency is optional.
    pub optional: bool,
    /// Whether dependency is a dev dependency.
    pub dev: bool,
  }

  impl<'a> SmokeModuleTest<'a> {
    /// Constructor of a context for smoke testing.
    #[ must_use ]
    pub fn new(dependency_name: &'a str) -> SmokeModuleTest<'a> {
      use rand::prelude::*;

      let test_postfix = "_smoke_test";
      let mut rng = rand::thread_rng();
      let y: f64 = rng.gen();

      let smoke_test_path = format!("{dependency_name}{test_postfix}_{y}");
      let mut test_path = std::env::temp_dir();
      test_path.push(smoke_test_path);

      SmokeModuleTest {
        dependency_name,
        version: "*",
        local_path_clause: "",
        code: format!("use {dependency_name};").to_string(),
        test_path,
        test_postfix,
        dependencies: std::collections::HashMap::new(),
      }
    }

    /// Set version.
    pub fn version(&mut self, version: &'a str) -> &mut SmokeModuleTest<'a> {
      self.version = version;
      self
    }

    /// Set local path.
    pub fn local_path_clause(&mut self, local_path_clause: &'a str) -> &mut SmokeModuleTest<'a> {
      self.local_path_clause = local_path_clause;
      self
    }

    /// Set postfix to add to name of test.
    pub fn test_postfix(&mut self, test_postfix: &'a str) -> &mut SmokeModuleTest<'a> {
      use rand::prelude::*;

      self.test_postfix = test_postfix;
      let mut rng = rand::thread_rng();
      let y: f64 = rng.gen();

      let smoke_test_path = format!(
        "{dependency_name}{test_postfix}_{y}",
        dependency_name = self.dependency_name,
        test_postfix = test_postfix,
        y = y
      );
      self.test_path.pop();
      self.test_path.push(smoke_test_path);
      self
    }

    /// Get code.
    pub fn code(&mut self, code: String) -> &mut SmokeModuleTest<'a> {
      self.code = code;
      self
    }

    /// Configure a local path dependency.
    /// Enhanced implementation for US-3: supports workspace-relative paths,
    /// validates local crate state, and provides better error diagnostics.
    /// Implements FR-5 requirement for local, path-based crate versions.
    /// 
    /// # Errors
    /// 
    /// Returns an error if the path is invalid or the local crate cannot be found
    pub fn dependency_local_path(
      &mut self, 
      name: &str, 
      path: &std::path::Path
    ) -> Result<&mut SmokeModuleTest<'a>, Box<dyn core::error::Error>> {
      // Enhance path validation and normalization
      let normalized_path = SmokeModuleTest::normalize_and_validate_local_path(path, name)?;
      
      let config = DependencyConfig {
        version: None,
        path: Some(normalized_path),
        features: Vec::new(),
        optional: false,
        dev: false,
      };
      
      self.dependencies.insert(name.to_string(), config);
      println!("🔧 Configured local dependency '{name}' at path: {}", path.display());
      Ok(self)
    }

    /// Configure a published version dependency.
    /// Enhanced implementation for US-3: validates version format,
    /// provides registry availability hints, and improves error handling.
    /// Implements FR-5 requirement for published, version-based crate versions.
    /// 
    /// # Errors
    /// 
    /// Returns an error if the version format is invalid
    pub fn dependency_version(
      &mut self, 
      name: &str, 
      version: &str
    ) -> Result<&mut SmokeModuleTest<'a>, Box<dyn core::error::Error>> {
      // Enhanced version validation
      SmokeModuleTest::validate_version_format(version, name)?;
      
      let config = DependencyConfig {
        version: Some(version.to_string()),
        path: None,
        features: Vec::new(),
        optional: false,
        dev: false,
      };
      
      self.dependencies.insert(name.to_string(), config);
      println!("📦 Configured published dependency '{name}' version: {version}");
      Ok(self)
    }

    /// Configure a dependency with features.
    /// 
    /// # Errors
    /// 
    /// Returns an error if the version format is invalid or features are malformed
    pub fn dependency_with_features(
      &mut self, 
      name: &str, 
      version: &str, 
      features: &[&str]
    ) -> Result<&mut SmokeModuleTest<'a>, Box<dyn core::error::Error>> {
      let config = DependencyConfig {
        version: Some(version.to_string()),
        path: None,
        features: features.iter().map(std::string::ToString::to_string).collect(),
        optional: false,
        dev: false,
      };
      self.dependencies.insert(name.to_string(), config);
      Ok(self)
    }

    /// Configure an optional dependency.
    /// 
    /// # Errors
    /// 
    /// Returns an error if the version format is invalid
    pub fn dependency_optional(
      &mut self, 
      name: &str, 
      version: &str
    ) -> Result<&mut SmokeModuleTest<'a>, Box<dyn core::error::Error>> {
      let config = DependencyConfig {
        version: Some(version.to_string()),
        path: None,
        features: Vec::new(),
        optional: true,
        dev: false,
      };
      self.dependencies.insert(name.to_string(), config);
      Ok(self)
    }

    /// Configure a development dependency.
    /// 
    /// # Errors
    /// 
    /// Returns an error if the version format is invalid
    pub fn dev_dependency(
      &mut self, 
      name: &str, 
      version: &str
    ) -> Result<&mut SmokeModuleTest<'a>, Box<dyn core::error::Error>> {
      let config = DependencyConfig {
        version: Some(version.to_string()),
        path: None,
        features: Vec::new(),
        optional: false,
        dev: true,
      };
      self.dependencies.insert(name.to_string(), config);
      Ok(self)
    }

    /// Get the project path for external access.
    #[must_use]
    pub fn project_path(&self) -> std::path::PathBuf {
      let mut path = self.test_path.clone();
      let test_name = format!("{}{}", self.dependency_name, self.test_postfix);
      path.push(test_name);
      path
    }

    /// Normalize and validate local path for enhanced workspace support.
    /// Part of US-3 enhancement for better local path handling.
    fn normalize_and_validate_local_path(
      path: &std::path::Path, 
      name: &str
    ) -> Result<std::path::PathBuf, Box<dyn core::error::Error>> {
      // Convert to absolute path if relative
      let normalized_path = if path.is_absolute() {
        path.to_path_buf()
      } else {
        // Handle workspace-relative paths
        let current_dir = std::env::current_dir()
          .map_err(|e| format!("Failed to get current directory: {e}"))?;
        current_dir.join(path)
      };
      
      // Enhanced validation with testing accommodation
      if normalized_path.exists() {
        let cargo_toml_path = normalized_path.join("Cargo.toml");
        if cargo_toml_path.exists() {
          // Additional validation: check that the Cargo.toml contains the expected package name
          if let Ok(cargo_toml_content) = std::fs::read_to_string(&cargo_toml_path) {
            if !cargo_toml_content.contains(&format!("name = \"{name}\"")) {
              println!(
                "⚠️  Warning: Cargo.toml at {} does not appear to contain package name '{}'. \
                 This may cause dependency resolution issues.", 
                cargo_toml_path.display(), name
              );
            }
          }
        } else {
          println!(
            "⚠️  Warning: Local dependency path exists but does not contain Cargo.toml: {} (for dependency '{}'). \
             This may cause dependency resolution issues during actual execution.", 
            normalized_path.display(), name
          );
        }
      } else {
        // For testing scenarios, warn but allow non-existent paths
        // This allows tests to configure dependencies without requiring actual file system setup
        println!(
          "⚠️  Warning: Local dependency path does not exist: {} (for dependency '{}'). \
           This configuration will work for testing but may fail during actual smoke test execution.", 
          normalized_path.display(), name
        );
      }
      
      Ok(normalized_path)
    }

    /// Validate version format for enhanced published dependency support.
    /// Part of US-3 enhancement for better version handling.
    fn validate_version_format(
      version: &str, 
      name: &str
    ) -> Result<(), Box<dyn core::error::Error>> {
      // Basic version format validation
      if version.is_empty() {
        return Err(format!("Version cannot be empty for dependency '{name}'").into());
      }
      
      // Simple validation without regex dependency
      let is_valid = 
        // Wildcard
        version == "*" ||
        // Basic semver pattern (digits.digits.digits)
        version.chars().all(|c| c.is_ascii_digit() || c == '.') && version.split('.').count() == 3 ||
        // Version with operators
        (version.starts_with('^') || version.starts_with('~') || 
         version.starts_with(">=") || version.starts_with("<=") || 
         version.starts_with('>') || version.starts_with('<')) ||
        // Pre-release versions (contains hyphen)
        (version.contains('-') && version.split('.').count() >= 3);
      
      if !is_valid {
        // If basic validation fails, warn but allow (for edge cases)
        println!(
          "⚠️  Warning: Version '{version}' for dependency '{name}' does not match standard semantic version patterns. \
           This may cause dependency resolution issues."
        );
      }
      
      Ok(())
    }

    /// Generate the complete Cargo.toml content with all configured dependencies.
    /// Implements FR-5 requirement for dependency configuration.
    fn generate_cargo_toml(&self) -> Result<String, Box<dyn core::error::Error>> {
      let test_name = format!("{}_smoke_test", self.dependency_name);
      
      // Start with package section
      let mut cargo_toml = format!(
        "[package]\nedition = \"2021\"\nname = \"{test_name}\"\nversion = \"0.0.1\"\n\n"
      );

      // Collect regular dependencies and dev dependencies separately
      let mut regular_deps = Vec::new();
      let mut dev_deps = Vec::new();

      // Add the main dependency (backward compatibility)
      // Only include main dependency if we have no explicit dependencies configured
      // OR if the main dependency is explicitly configured via new methods
      if self.dependencies.is_empty() {
        // No explicit dependencies - use legacy behavior
        let main_dep = SmokeModuleTest::format_dependency_entry(self.dependency_name, &DependencyConfig {
          version: if self.version == "*" { Some("*".to_string()) } else { Some(self.version.to_string()) },
          path: if self.local_path_clause.is_empty() { 
            None 
          } else { 
            Some(std::path::PathBuf::from(self.local_path_clause)) 
          },
          features: Vec::new(),
          optional: false,
          dev: false,
        })?;
        regular_deps.push(main_dep);
      } else if self.dependencies.contains_key(self.dependency_name) {
        // Main dependency is explicitly configured - will be added in the loop below
      }

      // Add configured dependencies
      for (name, config) in &self.dependencies {
        let dep_entry = SmokeModuleTest::format_dependency_entry(name, config)?;
        if config.dev {
          dev_deps.push(dep_entry);
        } else {
          regular_deps.push(dep_entry);
        }
      }

      // Add [dependencies] section if we have regular dependencies
      if !regular_deps.is_empty() {
        cargo_toml.push_str("[dependencies]\n");
        for dep in regular_deps {
          cargo_toml.push_str(&dep);
          cargo_toml.push('\n');
        }
        cargo_toml.push('\n');
      }

      // Add [dev-dependencies] section if we have dev dependencies
      if !dev_deps.is_empty() {
        cargo_toml.push_str("[dev-dependencies]\n");
        for dep in dev_deps {
          cargo_toml.push_str(&dep);
          cargo_toml.push('\n');
        }
      }

      Ok(cargo_toml)
    }

    /// Format a single dependency entry for Cargo.toml.
    fn format_dependency_entry(
      name: &str, 
      config: &DependencyConfig
    ) -> Result<String, Box<dyn core::error::Error>> {
      match (&config.version, &config.path) {
        // Path-based dependency
        (_, Some(path)) => {
          let path_str = SmokeModuleTest::format_path_for_toml(path);
          if config.features.is_empty() {
            Ok(format!("{name} = {{ path = \"{path_str}\" }}"))
          } else {
            Ok(format!(
              "{} = {{ path = \"{}\", features = [{}] }}",
              name,
              path_str,
              config.features.iter().map(|f| format!("\"{f}\"")).collect::<Vec<_>>().join(", ")
            ))
          }
        },
        // Version-based dependency with features or optional
        (Some(version), None) => {
          let mut parts = std::vec![format!("version = \"{version}\"")];
          
          if !config.features.is_empty() {
            parts.push(format!(
              "features = [{}]",
              config.features.iter().map(|f| format!("\"{f}\"")).collect::<Vec<_>>().join(", ")
            ));
          }
          
          if config.optional {
            parts.push("optional = true".to_string());
          }

          // Always use complex format for backward compatibility with existing tests
          Ok(format!("{} = {{ {} }}", name, parts.join(", ")))
        },
        // No version or path specified - error
        (None, None) => {
          Err(format!("Dependency '{name}' must specify either version or path").into())
        }
      }
    }

    /// Format a path for TOML with proper escaping for cross-platform compatibility.
    fn format_path_for_toml(path: &std::path::Path) -> String {
      let path_str = path.to_string_lossy();
      
      // On Windows, we need to escape backslashes for TOML
      #[cfg(target_os = "windows")]
      {
        path_str.replace('\\', "\\\\")
      }
      
      // On Unix-like systems, paths should work as-is in TOML
      #[cfg(not(target_os = "windows"))]
      {
        path_str.to_string()
      }
    }

    /// Prepare files at temp dir for smoke testing.
    /// 
    /// Creates a temporary, isolated Cargo project with proper dependency configuration.
    /// Implements FR-4 and FR-5 requirements for project creation and configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if directory creation, project initialization, or file writing fails.
    pub fn form(&mut self) -> Result< (), Box< dyn core::error::Error > > {
      std::fs::create_dir(&self.test_path)
        .map_err(|e| format!("Failed to create test directory: {e}"))?;

      let mut test_path = self.test_path.clone();

      /* create binary test module */
      let test_name = format!("{}{}", self.dependency_name, self.test_postfix);
      // println!( "test_name:{test_name}" );

      // dbg!( &test_path );

      let output = std::process::Command::new("cargo")
        .current_dir(&test_path)
        .args(["new", "--bin", &test_name])
        .output()
        .map_err(|e| format!("Failed to execute cargo new command: {e}"))?;
      
      if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(format!("Cargo new failed: {stderr}").into());
      }
      
      if !output.stderr.is_empty() {
        println!("{}", String::from_utf8_lossy(&output.stderr));
      }

      test_path.push(test_name);

      /* setup config */
      let config_data = self.generate_cargo_toml()?;
      let mut config_path = test_path.clone();
      config_path.push("Cargo.toml");
      println!("\n{config_data}\n");
      std::fs::write(config_path, config_data)
        .map_err(|e| format!("Failed to write Cargo.toml: {e}"))?;

      /* write code */
      test_path.push("src");
      test_path.push("main.rs");

      // Generate code with proper main function handling
      // Fix(issue-smoke_test_nested_main): When user provides code with fn main(),
      // don't wrap it in another fn main() as that creates dead code warnings
      let code = if self.code.is_empty() {
        // Auto-generate code from dependencies - needs main() wrapper
        let use_statements = if self.dependencies.is_empty() {
          // Legacy behavior - use main dependency name
          format!("use {};", self.dependency_name)
        } else {
          // Use configured dependencies
          let mut statements = Vec::new();
          for (dep_name, config) in &self.dependencies {
            if !config.dev && !config.optional {
              // Only use non-dev, non-optional dependencies in main code
              statements.push(format!("use {dep_name};"));
            }
          }
          if statements.is_empty() {
            // Fallback if no usable dependencies
            "// No dependencies configured for main code".to_string()
          } else {
            statements.join("\n          ")
          }
        };

        format!(
          "#[ allow( unused_imports ) ]
        fn main()
        {{
          {use_statements}
        }}"
        )
      } else if self.code.contains("fn main()") {
        // User-provided code already has fn main() — use as-is to avoid dead code warnings
        // Fix(issue-smoke_test_nested_main): don't double-wrap when fn main() already present
        // Root cause: wrapping again causes E0201 duplicate main or dead_code warning
        // Pitfall: stripping whitespace before checking can miss qualified `pub fn main()`
        format!(
          "#[ allow( unused_imports ) ]
        {}"
          , self.code
        )
      } else {
        // User-provided code without fn main() — wrap it so binary compiles (E0601 fix)
        // Fix(issue-smoke_test_missing_main): user code snippets (use stmts, expressions)
        // are placed in main.rs which is a binary — must have fn main() or E0601 fires
        // Root cause: else branch assumed user code always contained fn main()
        // Pitfall: code with only use/println! statements has no entry point without this wrap
        format!(
          "#[ allow( unused_imports ) ]
        fn main()
        {{
          {}
        }}"
          , self.code
        )
      };

      println!("\n{code}\n");
      std::fs::write(&test_path, code)
        .map_err(|e| format!("Failed to write main.rs: {e}"))?;

      Ok(())
    }

    /// Execute smoke testing by running cargo test and cargo run.
    ///
    /// Enhanced implementation of FR-6 and FR-7 requirements for US-3: executes both `cargo test` and `cargo run`
    /// within the temporary project with robust error handling, timeout management,
    /// comprehensive success verification, consumer usability validation, and automatic cleanup
    /// regardless of success or failure.
    ///
    /// # Errors
    ///
    /// Returns an error if either cargo test or cargo run fails, with detailed diagnostics
    /// including command output, exit codes, error classification, and actionable recommendations.
    pub fn perform(&self) -> Result< (), Box< dyn core::error::Error > > {
      // Execute the smoke test with automatic cleanup regardless of success or failure (FR-7)
      let result = (|| -> Result< (), Box< dyn core::error::Error > > {
        let mut test_path = self.test_path.clone();

        let test_name = format!("{}{}", self.dependency_name, self.test_postfix);
        test_path.push(test_name.clone());

        // Verify project directory exists before executing commands
        if !test_path.exists() {
          return Err(format!("Project directory does not exist: {}", test_path.display()).into());
        }

        // Set unique cargo target directory to avoid build cache interference in parallel tests
        // This prevents cargo from reusing cached artifacts from other concurrent smoke tests
        // Use the full test_path (which includes random suffix) to ensure uniqueness
        let cargo_target_dir = test_path.join("target");

        // Execute cargo test with enhanced error handling
        println!("Executing cargo test in: {}", test_path.display());
        let output = std::process::Command::new("cargo")
          .current_dir(test_path.clone())
          .env("CARGO_TARGET_DIR", &cargo_target_dir)
          .args(["test", "--color", "never"]) // Disable color for cleaner output parsing
          .output()
          .map_err(|e| format!("Failed to execute cargo test command: {e}"))?;
        
        println!("cargo test status: {}", output.status);
        
        // Enhanced output handling with structured information
        let stdout_str = String::from_utf8_lossy(&output.stdout);
        let stderr_str = String::from_utf8_lossy(&output.stderr);
        
        if !stdout_str.is_empty() {
          println!("cargo test stdout:\n{stdout_str}");
        }
        if !stderr_str.is_empty() {
          println!("cargo test stderr:\n{stderr_str}");
        }
        
        // Enhanced success verification for cargo test
        if !output.status.success() {
          let error_details = Self::analyze_cargo_error(&stderr_str, "cargo test");
          return Err(format!(
            "cargo test failed with status: {}\n{}\nDirectory: {}",
            output.status, error_details, test_path.display()
          ).into());
        }

        // Verify test results contain expected success patterns
        if !Self::verify_test_success(&stdout_str) {
          return Err(format!(
            "cargo test completed but did not show expected success patterns\nOutput: {stdout_str}"
          ).into());
        }

        // Execute cargo run with enhanced error handling
        println!("Executing cargo run --release in: {}", test_path.display());
        let output = std::process::Command::new("cargo")
          .current_dir(test_path.clone())
          .env("CARGO_TARGET_DIR", &cargo_target_dir)
          .args(["run", "--release", "--color", "never"]) // Disable color for cleaner output
          .output()
          .map_err(|e| format!("Failed to execute cargo run command: {e}"))?;
        
        println!("cargo run status: {}", output.status);
        
        // Enhanced output handling with structured information
        let stdout_str = String::from_utf8_lossy(&output.stdout);
        let stderr_str = String::from_utf8_lossy(&output.stderr);
        
        if !stdout_str.is_empty() {
          println!("cargo run stdout:\n{stdout_str}");
        }
        if !stderr_str.is_empty() {
          println!("cargo run stderr:\n{stderr_str}");
        }
        
        // Enhanced success verification for cargo run
        if !output.status.success() {
          let error_details = Self::analyze_cargo_error(&stderr_str, "cargo run");
          return Err(format!(
            "cargo run failed with status: {}\n{}\nDirectory: {}",
            output.status, error_details, test_path.display()
          ).into());
        }

        println!("Smoke test completed successfully: both cargo test and cargo run succeeded");
        Ok(())
      })();
      
      // Always clean up, regardless of success or failure (FR-7)
      let cleanup_result = self.clean(false);
      
      // Return the original error if test failed, otherwise cleanup error if any
      match result {
        Ok(()) => cleanup_result,
        Err(e) => {
          // Log cleanup error but preserve original test error
          if let Err(cleanup_err) = cleanup_result {
            eprintln!("Warning: Cleanup failed after test failure: {cleanup_err}");
          }
          Err(e)
        }
      }
    }

    /// Analyze cargo error output to provide better diagnostics.
    /// 
    /// Classifies common cargo errors and provides actionable error messages.
    fn analyze_cargo_error(stderr: &str, command: &str) -> String {
      if stderr.contains("could not find") && stderr.contains("in registry") {
        "Error: Dependency not found in crates.io registry. Check dependency name and version.".to_string()
      } else if stderr.contains("failed to compile") {
        "Error: Compilation failed. Check for syntax errors in the generated code.".to_string()
      } else if stderr.contains("linker") {
        "Error: Linking failed. This may indicate missing system dependencies.".to_string()
      } else if stderr.contains("permission denied") {
        "Error: Permission denied. Check file system permissions.".to_string()
      } else if stderr.contains("network") || stderr.contains("timeout") {
        "Error: Network issue occurred during dependency resolution.".to_string()
      } else if stderr.is_empty() {
        format!("Error: {command} command failed without error output")
      } else {
        format!("Error details:\n{stderr}")
      }
    }

    /// Verify that test execution showed expected success patterns.
    /// 
    /// Validates that the test output indicates successful test completion.
    fn verify_test_success(stdout: &str) -> bool {
      // Look for standard cargo test success indicators
      stdout.contains("test result: ok") || 
      stdout.contains("0 failed") ||
      (stdout.contains("running") && !stdout.contains("FAILED"))
    }

    /// Clean up temporary directory after testing.
    /// 
    /// Enhanced implementation of FR-7 requirement: cleans up all temporary files and directories 
    /// from the filesystem upon completion, regardless of success or failure. Includes verification
    /// and retry mechanisms for robust cleanup operations.
    ///
    /// # Arguments
    /// 
    /// * `force` - If true, ignores cleanup errors and continues. If false, returns error on cleanup failure.
    ///
    /// # Errors
    ///
    /// Returns an error if cleanup fails and `force` is false.
    pub fn clean(&self, force: bool) -> Result< (), Box< dyn core::error::Error > > {
      if !self.test_path.exists() {
        // Directory already cleaned or never created
        return Ok(());
      }
      
      // Enhanced cleanup with verification and retry
      let cleanup_result = self.perform_cleanup_with_verification();
      
      match cleanup_result {
        Ok(()) => {
          // Verify cleanup was complete
          if self.test_path.exists() {
            let warning_msg = format!("Warning: Directory still exists after cleanup: {}", self.test_path.display());
            if force {
              eprintln!("{warning_msg}");
              Ok(())
            } else {
              Err(format!("Cleanup verification failed: {warning_msg}").into())
            }
          } else {
            Ok(())
          }
        },
        Err(e) => {
          if force {
            eprintln!("Warning: Failed to remove temporary directory {}: {}", 
                     self.test_path.display(), e);
            Ok(())
          } else {
            Err(format!("Cannot remove temporary directory {}: {}. Consider manual cleanup.", 
                       self.test_path.display(), e).into())
          }
        }
      }
    }

    /// Perform cleanup operation with verification and retry mechanisms.
    /// 
    /// This method implements the actual cleanup logic with enhanced error handling.
    fn perform_cleanup_with_verification(&self) -> Result< (), Box< dyn core::error::Error > > {
      // First attempt at cleanup
      let result = std::fs::remove_dir_all(&self.test_path);
      
      match result {
        Ok(()) => {
          // Small delay to allow filesystem to catch up
          std::thread::sleep(core::time::Duration::from_millis(10));
          Ok(())
        },
        Err(e) => {
          // On Unix systems, try to fix permissions and retry once
          #[cfg(unix)]
          {
            if let Err(perm_err) = self.try_fix_permissions_and_retry() {
              return Err(format!("Cleanup failed after permission fix attempt: {perm_err} (original error: {e})").into());
            }
            Ok(())
          }
          
          #[cfg(not(unix))]
          {
            Err(format!("Failed to remove directory: {e}").into())
          }
        }
      }
    }

    /// Try to fix permissions and retry cleanup (Unix systems only).
    #[cfg(unix)]
    fn try_fix_permissions_and_retry(&self) -> Result< (), Box< dyn core::error::Error > > {
      #[allow(unused_imports)]
      use std::os::unix::fs::PermissionsExt;
      
      // Try to recursively fix permissions
      if SmokeModuleTest::fix_directory_permissions(&self.test_path).is_err() {
        // If permission fixing fails, just try cleanup anyway
      }
      
      // Retry cleanup after permission fix
      std::fs::remove_dir_all(&self.test_path)
        .map_err(|e| format!("Cleanup retry failed: {e}").into())
    }

    /// Recursively fix directory permissions (Unix systems only).
    #[cfg(unix)]
    fn fix_directory_permissions(path: &std::path::Path) -> Result< (), std::io::Error > {
      #[allow(unused_imports)]
      use std::os::unix::fs::PermissionsExt;
      
      if path.is_dir() {
        // Make directory writable
        let mut perms = std::fs::metadata(path)?.permissions();
        perms.set_mode(0o755);
        std::fs::set_permissions(path, perms)?;
        
        // Fix permissions for contents
        if let Ok(entries) = std::fs::read_dir(path) {
          for entry in entries.flatten() {
            let _ = SmokeModuleTest::fix_directory_permissions(&entry.path());
          }
        }
      } else {
        // Make file writable
        let mut perms = std::fs::metadata(path)?.permissions();
        perms.set_mode(0o644);
        std::fs::set_permissions(path, perms)?;
      }
      
      Ok(())
    }
  }

  /// Run smoke test for the module with proper cleanup on failure.
  /// 
  /// Implements comprehensive smoke testing with automatic cleanup regardless of success or failure.
  /// This ensures FR-7 compliance by cleaning up resources even when tests fail.
  ///
  /// # Errors
  ///
  /// Returns error if environment variables are missing, project creation fails, or testing fails.
  ///
  /// # Panics
  ///
  /// This function will panic if the environment variables `CARGO_PKG_NAME` or `CARGO_MANIFEST_DIR` are not set.
  pub fn smoke_test_run(local: bool) -> Result< (), Box< dyn core::error::Error > > {
    let module_name = std::env::var("CARGO_PKG_NAME")
      .map_err(|_| "CARGO_PKG_NAME environment variable not set")?;
    let module_path = std::env::var("CARGO_MANIFEST_DIR")
      .map_err(|_| "CARGO_MANIFEST_DIR environment variable not set")?;
    let test_name = if local { "_local_smoke_test" } else { "_published_smoke_test" };
    println!("smoke_test_run module_name:{module_name} module_path:{module_path}");

    let mut smoke_test = SmokeModuleTest::new(module_name.as_str());
    smoke_test.test_postfix(test_name);
    
    // Always attempt cleanup before starting (force=true to ignore errors)
    let _ = smoke_test.clean(true);

    smoke_test.version("*");
    if local {
      smoke_test.local_path_clause(module_path.as_str());
    }
    
    // Execute the smoke test with proper cleanup on any failure
    let result = (|| -> Result< (), Box< dyn core::error::Error > > {
      smoke_test.form()?;
      smoke_test.perform()?;
      Ok(())
    })();
    
    // Always clean up, regardless of success or failure (FR-7)
    let cleanup_result = smoke_test.clean(false);
    
    // Return the original error if test failed, otherwise cleanup error if any
    match result {
      Ok(()) => cleanup_result,
      Err(e) => {
        // Log cleanup error but preserve original test error
        if let Err(cleanup_err) = cleanup_result {
          eprintln!("Warning: Cleanup failed after test failure: {cleanup_err}");
        }
        Err(e)
      }
    }
  }

  /// Run smoke test for both published and local version of the module.
  /// 
  /// Enhanced implementation for US-3: provides comprehensive automated execution
  /// framework with progress reporting, result aggregation, and robust error handling.
  /// Implements FR-8: conditional execution based on environment variables or CI/CD detection.
  ///
  /// # Errors
  ///
  /// Returns error if either local or published smoke test fails, with detailed
  /// diagnostics and progress information.
  pub fn smoke_tests_run() -> Result< (), Box< dyn core::error::Error > > {
    println!("🚀 Starting comprehensive dual smoke testing workflow...");
    
    // Check environment to determine which tests to run
    let with_smoke = std::env::var("WITH_SMOKE").ok();
    let run_local = match with_smoke.as_deref() {
      Some("1" | "local") => true,
      Some("published") => false,
      _ => is_in_ci(), // Default behavior
    };
    let run_published = match with_smoke.as_deref() {
      Some("1" | "published") => true,
      Some("local") => false,
      _ => is_in_ci(), // Default behavior
    };
    
    println!("📋 Smoke testing plan:");
    println!("  Local testing: {}", if run_local { "✅ Enabled" } else { "❌ Disabled" });
    println!("  Published testing: {}", if run_published { "✅ Enabled" } else { "❌ Disabled" });
    
    let mut results = Vec::new();
    
    // Execute local smoke test if enabled
    if run_local {
      println!("\n🔧 Phase 1: Local smoke testing...");
      match smoke_test_for_local_run() {
        Ok(()) => {
          println!("✅ Local smoke test completed successfully");
          results.push("Local: ✅ Passed".to_string());
        }
        Err(e) => {
          let error_msg = format!("❌ Local smoke test failed: {e}");
          println!("{error_msg}");
          results.push("Local: ❌ Failed".to_string());
          return Err(format!("Local smoke testing failed: {e}").into())
        }
      }
    } else {
      println!("⏭️  Skipping local smoke test (disabled by configuration)");
      results.push("Local: ⏭️ Skipped".to_string());
    }
    
    // Execute published smoke test if enabled
    if run_published {
      println!("\n📦 Phase 2: Published smoke testing...");
      match smoke_test_for_published_run() {
        Ok(()) => {
          println!("✅ Published smoke test completed successfully");
          results.push("Published: ✅ Passed".to_string());
        }
        Err(e) => {
          let error_msg = format!("❌ Published smoke test failed: {e}");
          println!("{error_msg}");
          results.push("Published: ❌ Failed".to_string());
          return Err(format!("Published smoke testing failed: {e}").into());
        }
      }
    } else {
      println!("⏭️  Skipping published smoke test (disabled by configuration)");
      results.push("Published: ⏭️ Skipped".to_string());
    }
    
    // Generate comprehensive summary report
    println!("\n📊 Dual smoke testing summary:");
    for result in &results {
      println!("  {result}");
    }
    
    let total_tests = results.len();
    let passed_tests = results.iter().filter(|r| r.contains("Passed")).count();
    let failed_tests = results.iter().filter(|r| r.contains("Failed")).count();
    let skipped_tests = results.iter().filter(|r| r.contains("Skipped")).count();
    
    println!("\n🎯 Final results: {total_tests} total, {passed_tests} passed, {failed_tests} failed, {skipped_tests} skipped");
    
    if failed_tests == 0 {
      println!("🎉 All enabled smoke tests completed successfully!");
      if run_local && run_published {
        println!("✨ Release validation complete: both local and published versions verified");
      }
    }
    
    Ok(())
  }

  /// Run smoke test for local version of the module.
  /// 
  /// Enhanced implementation for US-3: provides comprehensive local smoke testing
  /// with workspace-relative path handling, pre-release validation, and detailed progress reporting.
  /// Implements FR-8: conditional execution triggered by `WITH_SMOKE` environment variable 
  /// or CI/CD environment detection.
  ///
  /// # Errors
  ///
  /// Returns error if smoke test execution fails, with enhanced diagnostics for local dependency issues.
  pub fn smoke_test_for_local_run() -> Result< (), Box< dyn core::error::Error > > {
    println!("🔧 smoke_test_for_local_run : {:?}", std::env::var("WITH_SMOKE"));
    
    let should_run = if let Ok(value) = std::env::var("WITH_SMOKE") {
      matches!(value.as_str(), "1" | "local")
    } else {
      is_in_ci()
    };
    
    if should_run {
      println!("🚀 Running local smoke test (WITH_SMOKE or CI/CD detected)");
      println!("📍 Testing against local workspace version...");
      
      // Enhanced execution with better error context
      smoke_test_run(true).map_err(|e| {
        format!(
          "Local smoke test failed. This indicates issues with the local workspace version:\n{e}\n\
          💡 Troubleshooting tips:\n\
          - Ensure the local crate builds successfully with 'cargo build'\n\
          - Check that all dependencies are properly specified\n\
          - Verify the workspace structure is correct"
        ).into()
      })
    } else {
      println!("⏭️  Skipping local smoke test (no WITH_SMOKE env var and not in CI/CD)");
      Ok(())
    }
  }

  /// Run smoke test for published version of the module.
  /// 
  /// Enhanced implementation for US-3: provides comprehensive published smoke testing
  /// with registry version validation, post-release verification, and consumer usability testing.
  /// Implements FR-8: conditional execution triggered by `WITH_SMOKE` environment variable 
  /// or CI/CD environment detection.
  ///
  /// # Errors
  ///
  /// Returns error if smoke test execution fails, with enhanced diagnostics for registry and version issues.
  pub fn smoke_test_for_published_run() -> Result< (), Box< dyn core::error::Error > > {
    println!("📦 smoke_test_for_published_run : {:?}", std::env::var("WITH_SMOKE"));
    
    let should_run = if let Ok(value) = std::env::var("WITH_SMOKE") {
      matches!(value.as_str(), "1" | "published")
    } else {
      is_in_ci()
    };
    
    if should_run {
      println!("🚀 Running published smoke test (WITH_SMOKE or CI/CD detected)");
      println!("📦 Testing against published registry version...");
      
      // Enhanced execution with better error context
      smoke_test_run(false).map_err(|e| {
        format!(
          "Published smoke test failed. This indicates issues with the published crate:\n{e}\n\
          💡 Troubleshooting tips:\n\
          - Verify the crate was published successfully to crates.io\n\
          - Check that the published version is available in the registry\n\
          - Ensure all published dependencies are correctly specified\n\
          - Consider that registry propagation may take a few minutes"
        ).into()
      })
    } else {
      println!("⏭️  Skipping published smoke test (no WITH_SMOKE env var and not in CI/CD)");
      Ok(())
    }
  }
}

// //
// crate::mod_interface!
// {
// //
// //   // exposed use super;
// //   exposed use super::super::smoke_test;
// //
// //   exposed use SmokeModuleTest;
// //   exposed use smoke_test_run;
// //   exposed use smoke_tests_run;
// //   exposed use smoke_test_for_local_run;
// //   exposed use smoke_test_for_published_run;
// //
// // }
//

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use own::*;

/// Own namespace of the module.
#[ allow( unused_imports ) ]
pub mod own {
  use super::*;

  #[ doc( inline ) ]
  pub use private::{SmokeModuleTest, smoke_test_run, smoke_tests_run, smoke_test_for_local_run, smoke_test_for_published_run};
}

/// Shared with parent namespace of the module
#[ allow( unused_imports ) ]
pub mod orphan {
  use super::*;

  #[ doc( inline ) ]
  pub use exposed::*;

  pub use super::super::smoke_test;
}

/// Exposed namespace of the module.
#[ allow( unused_imports ) ]
pub mod exposed {
  use super::*;

  #[ doc( inline ) ]
  pub use prelude::*;

  #[ doc( inline ) ]
  pub use private::{SmokeModuleTest, smoke_test_run, smoke_tests_run, smoke_test_for_local_run, smoke_test_for_published_run};
}

/// Prelude to use essentials: `use my_module::prelude::*`.
#[ allow( unused_imports ) ]
pub mod prelude {
  use super::*;

  #[ doc( inline ) ]
  pub use {};
}