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

//!
//! Smoke test checking health of a module.
//!

// #![ allow( dead_code ) ]

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

/// Internal namespace.
pub( crate ) mod private
{

  /// 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,
  }

  impl< 'a > SmokeModuleTest< 'a >
  {
    /// Constructor of a context for smoke testing.
    pub fn new( dependency_name : &'a str ) -> SmokeModuleTest< 'a >
    {
      let test_postfix = "_smoke_test";
      let smoke_test_path = format!( "{}{}", dependency_name, test_postfix );
      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,
      }
    }

    /// 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 >
    {
      self.test_postfix = test_postfix;
      let smoke_test_path = format!( "{}{}", self.dependency_name, test_postfix );
      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
    }

    /// Prepare files at temp dir for smoke testing.
    pub fn form( &mut self ) -> Result< (), &'static str >
    {
      std::fs::create_dir( &self.test_path ).unwrap();

      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}" );

      let output = std::process::Command::new( "cargo" )
      .current_dir( &test_path )
      .args([ "new", "--bin", &test_name ])
      .output()
      .expect( "Failed to execute command" )
      ;
      println!( "{}", std::str::from_utf8( &output.stderr ).expect( "Invalid UTF-8" ) );

      test_path.push( test_name );

      /* setup config */
      #[ cfg( target_os = "windows" ) ]
      let local_path_clause = if self.local_path_clause == "" { "".to_string() } else { format!( ", path = \"{}\"", self.local_path_clause.escape_default() ) };
      #[ cfg( not( target_os = "windows" ) ) ]
      let local_path_clause = if self.local_path_clause == "" { "".to_string() } else { format!( ", path = \"{}\"", self.local_path_clause ) };
      let dependencies_section = format!( "{} = {{ version = \"{}\" {} }}", self.dependency_name, self.version, &local_path_clause );
      let config_data = format!
      (
        "[package]
        edition = \"2021\"
        name = \"{}_smoke_test\"
        version = \"0.0.1\"

        [dependencies]
        {}",
        &self.dependency_name,
        &dependencies_section
      );
      let mut config_path = test_path.clone();
      config_path.push( "Cargo.toml" );
      println!( "\n{}\n", config_data );
      std::fs::write( config_path, config_data ).unwrap();

      /* write code */
      test_path.push( "src" );
      test_path.push( "main.rs" );
      if self.code == ""
      {
        self.code = format!( "use ::{}::*;", self.dependency_name );
      }
      let code = format!
      (
        "#[ allow( unused_imports ) ]
        fn main()
        {{
          {}
        }}",
        self.code,
      );
      println!( "\n{}\n", code );
      std::fs::write( &test_path, code ).unwrap();

      Ok( () )
    }

    /// Do smoke testing.
    pub fn perform( &self ) -> Result<(), &'static str>
    {
      let mut test_path = self.test_path.clone();
      let test_name = format!( "{}{}", self.dependency_name, self.test_postfix );
      test_path.push( test_name );

      let output = std::process::Command::new( "cargo" )
      .current_dir( test_path.clone() )
      .args([ "test" ])
      .output()
      .unwrap()
      ;
      println!( "status : {}", output.status );
      println!( "{}", std::str::from_utf8( &output.stdout ).expect( "Invalid UTF-8" ) );
      println!( "{}", std::str::from_utf8( &output.stderr ).expect( "Invalid UTF-8" ) );
      assert!( output.status.success(), "Smoke test failed" );

      let output = std::process::Command::new( "cargo" )
      .current_dir( test_path )
      .args([ "run", "--release" ])
      .output()
      .unwrap()
      ;
      println!( "status : {}", output.status );
      println!( "{}", std::str::from_utf8( &output.stdout ).expect( "Invalid UTF-8" ) );
      println!( "{}", std::str::from_utf8( &output.stderr ).expect( "Invalid UTF-8" ) );
      assert!( output.status.success(), "Smoke test failed" );

      Ok( () )
    }

    /// Cleaning temp directory after testing.
    pub fn clean( &self, force : bool ) -> Result<(), &'static str>
    {
      let result = std::fs::remove_dir_all( &self.test_path );
      if force
      {
        result.unwrap_or_default();
      }
      else
      {
        let msg = format!( "Cannot remove temporary directory {}. Please, remove it manually", &self.test_path.display() );
        result.expect( &msg );
      }
      Ok( () )
    }

  }

  //
  //   index!
  //   {
  //
  //     new,
  //     version,
  //     local_path_clause,
  //     code,
  //     form,
  //     perform,
  //     clean,
  //
  //   }
  //
  //

  /// Run smoke test for the module.

  pub fn smoke_test_run( local : bool )
  {
    let module_name = std::env::var( "CARGO_PKG_NAME" ).unwrap();
    let module_path = std::env::var( "CARGO_MANIFEST_DIR" ).unwrap();
    let test_name = match local
    {
      false => "_published_smoke_test",
      true => "_local_smoke_test",
    };
    println!( "smoke_test_run module_name:{module_name} module_path:{module_path}" );

    // let mut code_path = std::path::PathBuf::from( module_path.clone() );
    // code_path.push( "rust" );
    // code_path.push( "test" );
    // code_path.push( if module_name.starts_with( "w" ) { &module_name[ 1.. ] } else { module_name.as_str() } );
    // code_path.push( "_asset" );
    // code_path.push( "smoke.rs" );

    let mut t = SmokeModuleTest::new( module_name.as_str() );
    t.test_postfix( test_name );
    t.clean( true ).unwrap();

    // let data;
    // if code_path.exists()
    // {
    //   data = std::fs::read_to_string( code_path ).unwrap();
      // t.code( data );
    // }

    t.version( "*" );
    if local
    {
      t.local_path_clause( module_path.as_str() );
    }

    t.form().unwrap();
    t.perform().unwrap();
    t.clean( false ).unwrap();
  }

  /// Run smoke test for both published and local version of the module.

  pub fn smoke_tests_run()
  {
    smoke_test_for_local_run();
    smoke_test_for_published_run();
  }

  /// Run smoke test for local version of the module.

  pub fn smoke_test_for_local_run()
  {
    println!( "smoke_test_for_local_run : {:?}", std::env::var( "WITH_SMOKE" ) );
    let run = if let Ok( value ) = std::env::var( "WITH_SMOKE" )
    {
      match value.as_str()
      {
        "0" => false,
        "1" => true,
        "false" => false,
        "local" => true,
        "published" => false,
        _ => false,
      }
    }
    else
    {
      true
    };
    if run
    {
      smoke_test_run( true );
    }
  }

  /// Run smoke test for published version of the module.

  pub fn smoke_test_for_published_run()
  {
    let run = if let Ok( value ) = std::env::var( "WITH_SMOKE" )
    {
      match value.as_str()
      {
        "0" => false,
        "1" => true,
        "false" => false,
        "local" => false,
        "published" => true,
        _ => false,
      }
    }
    else
    {
      true
    };
    if run
    {
      smoke_test_run( false );
    }
  }

}


//

crate::mod_interface!
{

  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;

}