Skip to content

Commit 8619e9a

Browse files
committed
Refactor.
1 parent 7369781 commit 8619e9a

2 files changed

Lines changed: 77 additions & 46 deletions

File tree

PSql.Tests/Tests.Integration/InvokeSqlCommandTests.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ public void ProjectDecimal_ToClrDecimal_TooLow()
279279
""
280280
");
281281

282-
exception.ShouldBeOfType<CmdletInvocationException>()
283-
.InnerException.ShouldBeOfType<OverflowException>();
282+
exception.ShouldBeOfType<OverflowException>();
284283

285-
objects.ShouldBeEmpty();
284+
objects.ShouldHaveSingleItem().ShouldNotBeNull()
285+
.BaseObject.ShouldBeOfType<PSError>();
286286
}
287287

288288
[Test]
@@ -294,10 +294,10 @@ public void ProjectDecimal_ToClrDecimal_TooHigh()
294294
""
295295
");
296296

297-
exception.ShouldBeOfType<CmdletInvocationException>()
298-
.InnerException.ShouldBeOfType<OverflowException>();
297+
exception.ShouldBeOfType<OverflowException>();
299298

300-
objects.ShouldBeEmpty();
299+
objects.ShouldHaveSingleItem().ShouldNotBeNull()
300+
.BaseObject.ShouldBeOfType<PSError>();
301301
}
302302

303303
[Test]
@@ -986,11 +986,11 @@ public void ProjectHierarchyId_UseClrTypes()
986986
""
987987
");
988988

989-
exception .ShouldBeOfType<CmdletInvocationException>();
990-
exception.InnerException .ShouldNotBeNull();
991-
exception.InnerException.GetType().Name.ShouldBe("InvalidUdtException");
989+
exception.ShouldNotBeNull();
990+
exception.GetType().Name.ShouldBe("InvalidUdtException");
992991

993-
objects.ShouldBeEmpty();
992+
objects.ShouldHaveSingleItem().ShouldNotBeNull()
993+
.BaseObject.ShouldBeOfType<PSError>();
994994
}
995995

996996
[Test]
@@ -1011,11 +1011,11 @@ public void ProjectHierarchyId_UseSqlTypes()
10111011
""
10121012
");
10131013

1014-
exception .ShouldBeOfType<CmdletInvocationException>();
1015-
exception.InnerException .ShouldNotBeNull();
1016-
exception.InnerException.GetType().Name.ShouldBe("InvalidUdtException");
1014+
exception.ShouldNotBeNull();
1015+
exception.GetType().Name.ShouldBe("InvalidUdtException");
10171016

1018-
objects.ShouldBeEmpty();
1017+
objects.ShouldHaveSingleItem().ShouldNotBeNull()
1018+
.BaseObject.ShouldBeOfType<PSError>();
10191019
}
10201020

10211021
private static SqlString Greenlandic(string s)

PSql.Tests/Tests.Support/ScriptExecutor.cs

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,44 @@
22
// SPDX-License-Identifier: MIT
33

44
using System.Management.Automation.Runspaces;
5-
using System.Runtime.InteropServices;
65
using Microsoft.PowerShell;
7-
using Unindent;
86

9-
namespace PSql.Tests;
10-
11-
using static FormattableString;
7+
namespace PSql;
128

139
internal 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

Comments
 (0)