22// SPDX-License-Identifier: MIT
33
44using System . Management . Automation . Runspaces ;
5- using System . Runtime . InteropServices ;
65using Microsoft . PowerShell ;
7- using Unindent ;
86
9- namespace PSql . Tests ;
10-
11- using static FormattableString ;
7+ namespace PSql ;
128
139internal static class ScriptExecutor
1410{
15- private static readonly InitialSessionState
16- InitialState = CreateInitialSessionState ( ) ;
17-
18- private static readonly string
19- ScriptPreamble = Invariant ( $@ "
20- Set-Location ""{ TestPath . EscapeForDoubleQuoteString ( ) } ""
21- " ) . Unindent ( ) ;
11+ private const string
12+ ModuleFileName = "PSql.psd1" ,
13+ TestingVariableName = "PSQL_TESTING" ;
2214
2315 private static string
2416 TestPath => TestContext . CurrentContext . TestDirectory ;
2517
18+ private static readonly InitialSessionState
19+ InitialState = CreateInitialSessionState ( ) ;
20+
21+ private static readonly PSInvocationSettings
22+ Settings = new ( ) { ErrorActionPreference = ActionPreference . Stop } ;
23+
2624 private static InitialSessionState CreateInitialSessionState ( )
2725 {
2826 var state = InitialSessionState . CreateDefault ( ) ;
2927
30- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
28+ if ( OperatingSystem . IsWindows ( ) )
3129 state . ExecutionPolicy = ExecutionPolicy . RemoteSigned ;
3230
33- state . Variables . Add ( new SessionStateVariableEntry (
34- "ErrorActionPreference" , "Stop" , description : null
35- ) ) ;
36-
37- state . ImportPSModule (
38- Path . Combine ( TestPath , "PSql.psd1" )
31+ state . EnvironmentVariables . Add (
32+ new SessionStateVariableEntry ( TestingVariableName , "1" , null )
3933 ) ;
4034
35+ state . ImportPSModule ( Path . Combine ( TestPath , ModuleFileName ) ) ;
36+
4137 return state ;
4238 }
4339
4440 internal static ( IReadOnlyList < PSObject ? > , Exception ? ) Execute ( string script )
4541 {
46- if ( script is null )
47- throw new ArgumentNullException ( nameof ( script ) ) ;
48-
49- script = ScriptPreamble + script . Unindent ( ) ;
42+ ArgumentNullException . ThrowIfNull ( script ) ;
5043
5144 var output = new List < PSObject ? > ( ) ;
5245 var exception = null as Exception ;
@@ -55,26 +48,53 @@ internal static (IReadOnlyList<PSObject?>, Exception?) Execute(string script)
5548
5649 Redirect ( shell . Streams , output ) ;
5750
51+ shell
52+ . AddCommand ( "Set-Location" ) . AddParameter ( "LiteralPath" , TestPath )
53+ . AddScript ( script ) ;
54+
5855 try
5956 {
60- shell . AddScript ( script ) . Invoke ( input : null , output ) ;
57+ shell . Invoke ( input : null , output , Settings ) ;
6158 }
6259 catch ( Exception e )
6360 {
6461 exception = e ;
6562 }
6663
64+ exception ??= shell . Streams . Error . FirstOrDefault ( ) ? . Exception ;
65+
6766 return ( output , exception ) ;
6867 }
6968
70- internal static string EscapeForDoubleQuoteString ( this string s )
71- => s . Replace ( "\" " , "`\" " )
72- . Replace ( "`" , "``" ) ;
73-
7469 private static void Redirect ( PSDataStreams streams , List < PSObject ? > output )
7570 {
76- streams . Warning . DataAdding += ( _ , data ) => StoreWarning ( data , output ) ;
77- streams . Error . DataAdding += ( _ , data ) => StoreError ( data , output ) ;
71+ streams . Debug . DataAdding += ( _ , data ) => StoreDebug ( data , output ) ;
72+ streams . Verbose . DataAdding += ( _ , data ) => StoreVerbose ( data , output ) ;
73+ streams . Information . DataAdding += ( _ , data ) => StoreInformation ( data , output ) ;
74+ streams . Warning . DataAdding += ( _ , data ) => StoreWarning ( data , output ) ;
75+ streams . Error . DataAdding += ( _ , data ) => StoreError ( data , output ) ;
76+ streams . Progress . DataAdding += ( _ , data ) => StoreProgress ( data , output ) ;
77+ }
78+
79+ private static void StoreDebug ( DataAddingEventArgs data , List < PSObject ? > output )
80+ {
81+ var written = ( DebugRecord ) data . ItemAdded ;
82+ var message = new PSDebug ( written . Message ) ;
83+ output . Add ( new PSObject ( message ) ) ;
84+ }
85+
86+ private static void StoreVerbose ( DataAddingEventArgs data , List < PSObject ? > output )
87+ {
88+ var written = ( VerboseRecord ) data . ItemAdded ;
89+ var message = new PSVerbose ( written . Message ) ;
90+ output . Add ( new PSObject ( message ) ) ;
91+ }
92+
93+ private static void StoreInformation ( DataAddingEventArgs data , List < PSObject ? > output )
94+ {
95+ var written = ( InformationRecord ) data . ItemAdded ;
96+ var message = new PSInformation ( written . ToString ( ) ) ;
97+ output . Add ( new PSObject ( message ) ) ;
7898 }
7999
80100 private static void StoreWarning ( DataAddingEventArgs data , List < PSObject ? > output )
@@ -90,7 +110,18 @@ private static void StoreError(DataAddingEventArgs data, List<PSObject?> output)
90110 var message = new PSError ( written . Exception . Message ) ;
91111 output . Add ( new PSObject ( message ) ) ;
92112 }
113+
114+ private static void StoreProgress ( DataAddingEventArgs data , List < PSObject ? > output )
115+ {
116+ var written = ( ProgressRecord ) data . ItemAdded ;
117+ var message = new PSProgress ( written . ToString ( ) ) ;
118+ output . Add ( new PSObject ( message ) ) ;
119+ }
93120}
94121
95- internal record PSWarning ( string Message ) ;
96- internal record PSError ( string Message ) ;
122+ internal readonly record struct PSDebug ( string Message ) ;
123+ internal readonly record struct PSVerbose ( string Message ) ;
124+ internal readonly record struct PSInformation ( string Message ) ;
125+ internal readonly record struct PSWarning ( string Message ) ;
126+ internal readonly record struct PSError ( string Message ) ;
127+ internal readonly record struct PSProgress ( string Message ) ;
0 commit comments