@@ -272,57 +272,81 @@ public void SetupModules(params string[] modules)
272
272
this . modules . AddRange ( modules ) ;
273
273
}
274
274
275
+
275
276
public virtual Collection < PSObject > RunPowerShellTest ( params string [ ] scripts )
276
277
{
277
278
using ( var powershell = System . Management . Automation . PowerShell . Create ( RunspaceMode . NewRunspace ) )
278
279
{
279
- SetupPowerShellModules ( powershell ) ;
280
+ return ExecuteShellTest ( powershell , null , scripts ) ;
281
+ }
282
+ }
283
+ public virtual Collection < PSObject > RunPowerShellTest ( IEnumerable < string > setupScripts , IEnumerable < string > scripts )
284
+ {
285
+ using ( var powershell = System . Management . Automation . PowerShell . Create ( RunspaceMode . NewRunspace ) )
286
+ {
287
+ return ExecuteShellTest ( powershell , setupScripts , scripts ) ;
288
+ }
289
+ }
280
290
281
- Collection < PSObject > output = null ;
282
- for ( int i = 0 ; i < scripts . Length ; ++ i )
283
- {
284
- Console . WriteLine ( scripts [ i ] ) ;
285
- powershell . AddScript ( scripts [ i ] ) ;
286
- }
287
- try
288
- {
289
- powershell . Runspace . Events . Subscribers . Clear ( ) ;
290
- powershell . Streams . Error . Clear ( ) ;
291
- output = powershell . Invoke ( ) ;
291
+ private Collection < PSObject > ExecuteShellTest (
292
+ System . Management . Automation . PowerShell powershell ,
293
+ IEnumerable < string > setupScripts ,
294
+ IEnumerable < string > scripts )
295
+ {
296
+ SetupPowerShellModules ( powershell , null ) ;
292
297
293
- if ( powershell . Streams . Error . Count > 0 )
294
- {
295
- var sb = new StringBuilder ( ) ;
298
+ Collection < PSObject > output = null ;
296
299
297
- sb . AppendLine ( "Test failed due to a non-empty error stream, check the error stream in the test log for more details." ) ;
298
- sb . AppendLine ( string . Format ( "{0} total Errors" , powershell . Streams . Error . Count ) ) ;
299
- foreach ( var error in powershell . Streams . Error )
300
- {
301
- sb . AppendLine ( error . Exception . ToString ( ) ) ;
302
- }
300
+ foreach ( var script in scripts )
301
+ {
302
+ Console . WriteLine ( script ) ;
303
+ powershell . AddScript ( script ) ;
304
+ }
305
+ try
306
+ {
307
+ powershell . Runspace . Events . Subscribers . Clear ( ) ;
308
+ powershell . Streams . Error . Clear ( ) ;
309
+ output = powershell . Invoke ( ) ;
303
310
304
- throw new RuntimeException ( sb . ToString ( ) ) ;
311
+ if ( powershell . Streams . Error . Count > 0 )
312
+ {
313
+ var sb = new StringBuilder ( ) ;
314
+
315
+ sb . AppendLine ( "Test failed due to a non-empty error stream, check the error stream in the test log for more details." ) ;
316
+ sb . AppendLine ( string . Format ( "{0} total Errors" , powershell . Streams . Error . Count ) ) ;
317
+ foreach ( var error in powershell . Streams . Error )
318
+ {
319
+ sb . AppendLine ( error . Exception . ToString ( ) ) ;
305
320
}
306
321
307
- return output ;
308
- }
309
- catch ( Exception psException )
310
- {
311
- powershell . LogPowerShellException ( psException ) ;
312
- throw ;
313
- }
314
- finally
315
- {
316
- powershell . LogPowerShellResults ( output ) ;
317
- powershell . Streams . Error . Clear ( ) ;
322
+ throw new RuntimeException ( sb . ToString ( ) ) ;
318
323
}
324
+
325
+ return output ;
326
+ }
327
+ catch ( Exception psException )
328
+ {
329
+ powershell . LogPowerShellException ( psException ) ;
330
+ throw ;
331
+ }
332
+ finally
333
+ {
334
+ powershell . LogPowerShellResults ( output ) ;
335
+ powershell . Streams . Error . Clear ( ) ;
319
336
}
320
337
}
321
338
322
- private void SetupPowerShellModules ( System . Management . Automation . PowerShell powershell )
339
+ private void SetupPowerShellModules ( System . Management . Automation . PowerShell powershell , IEnumerable < string > setupScripts )
323
340
{
324
341
powershell . AddScript ( "$error.clear()" ) ;
325
342
powershell . AddScript ( string . Format ( "cd \" {0}\" " , AppDomain . CurrentDomain . BaseDirectory ) ) ;
343
+ if ( setupScripts != null )
344
+ {
345
+ foreach ( var script in setupScripts )
346
+ {
347
+ powershell . AddScript ( script ) ;
348
+ }
349
+ }
326
350
327
351
foreach ( string moduleName in modules )
328
352
{
0 commit comments