Skip to content

Commit 97de52b

Browse files
Merge pull request #1858 from PowerShell/andschwa/dotsource-test
Add regression test for debugging script with dot-source operator
2 parents 7252de6 + 0759d9d commit 97de52b

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

test/PowerShellEditorServices.Test.E2E/DebugAdapterProtocolMessageTests.cs

+33-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace PowerShellEditorServices.Test.E2E
2020
{
2121
[Trait("Category", "DAP")]
22-
public class DebugAdapterProtocolMessageTests : IAsyncLifetime
22+
public class DebugAdapterProtocolMessageTests : IAsyncLifetime, IDisposable
2323
{
2424
private const string TestOutputFileName = "__dapTestOutputFile.txt";
2525
private static readonly bool s_isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
@@ -104,7 +104,13 @@ await PsesDebugAdapterClient.RequestDisconnect(new DisconnectArguments
104104
TerminateDebuggee = true
105105
}).ConfigureAwait(true);
106106
await _psesProcess.Stop().ConfigureAwait(true);
107+
}
108+
109+
public void Dispose()
110+
{
111+
GC.SuppressFinalize(this);
107112
PsesDebugAdapterClient?.Dispose();
113+
_psesProcess?.Dispose();
108114
}
109115

110116
private static string NewTestFile(string script, bool isPester = false)
@@ -130,11 +136,23 @@ private string GenerateScriptFromLoggingStatements(params string[] logStatements
130136
}
131137

132138
// Have script create file first with `>` (but don't rely on overwriting).
133-
StringBuilder builder = new StringBuilder().Append('\'').Append(logStatements[0]).Append("' > '").Append(s_testOutputPath).AppendLine("'");
139+
// NOTE: We uses double quotes so that we can use PowerShell variables.
140+
StringBuilder builder = new StringBuilder()
141+
.Append("Write-Output \"")
142+
.Append(logStatements[0])
143+
.Append("\" > '")
144+
.Append(s_testOutputPath)
145+
.AppendLine("'");
146+
134147
for (int i = 1; i < logStatements.Length; i++)
135148
{
136149
// Then append to that script with `>>`.
137-
builder.Append('\'').Append(logStatements[i]).Append("' >> '").Append(s_testOutputPath).AppendLine("'");
150+
builder
151+
.Append("Write-Output \"")
152+
.Append(logStatements[i])
153+
.Append("\" >> '")
154+
.Append(s_testOutputPath)
155+
.AppendLine("'");
138156
}
139157

140158
_output.WriteLine("Script is:");
@@ -144,7 +162,7 @@ private string GenerateScriptFromLoggingStatements(params string[] logStatements
144162

145163
private static async Task<string[]> GetLog()
146164
{
147-
while (!File.Exists(s_testOutputPath))
165+
for (int i = 0; !File.Exists(s_testOutputPath) && i < 10; i++)
148166
{
149167
await Task.Delay(1000).ConfigureAwait(true);
150168
}
@@ -164,6 +182,17 @@ public void CanInitializeWithCorrectServerSettings()
164182
Assert.True(PsesDebugAdapterClient.ServerSettings.SupportsSetVariable);
165183
}
166184

185+
[Fact]
186+
public async Task UsesDotSourceOperatorAndQuotesAsync()
187+
{
188+
string filePath = NewTestFile(GenerateScriptFromLoggingStatements("$($MyInvocation.Line)"));
189+
await PsesDebugAdapterClient.LaunchScript(filePath, Started).ConfigureAwait(true);
190+
ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments()).ConfigureAwait(true);
191+
Assert.NotNull(configDoneResponse);
192+
Assert.Collection(await GetLog().ConfigureAwait(true),
193+
(i) => Assert.StartsWith(". \"", i));
194+
}
195+
167196
[Fact]
168197
public async Task CanLaunchScriptWithNoBreakpointsAsync()
169198
{

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ private Task ExecutePowerShellCommand(string command, params string[] args)
103103

104104
private void AssertDebuggerPaused()
105105
{
106-
DebuggerStoppedEventArgs eventArgs = debuggerStoppedQueue.Take(CancellationToken.None);
106+
using CancellationTokenSource cts = new(10000);
107+
DebuggerStoppedEventArgs eventArgs = debuggerStoppedQueue.Take(cts.Token);
107108
Assert.Empty(eventArgs.OriginalEvent.Breakpoints);
108109
}
109110

@@ -112,7 +113,8 @@ private void AssertDebuggerStopped(
112113
int lineNumber = -1,
113114
CommandBreakpointDetails commandBreakpointDetails = default)
114115
{
115-
DebuggerStoppedEventArgs eventArgs = debuggerStoppedQueue.Take(CancellationToken.None);
116+
using CancellationTokenSource cts = new(10000);
117+
DebuggerStoppedEventArgs eventArgs = debuggerStoppedQueue.Take(cts.Token);
116118

117119
Assert.True(psesHost.DebugContext.IsStopped);
118120

@@ -587,10 +589,9 @@ await debugService.SetLineBreakpointsAsync(
587589
Assert.Equal("$false", falseVar.ValueString);
588590
}
589591

590-
[SkippableFact]
592+
[Fact]
591593
public async Task DebuggerSetsVariablesNoConversion()
592594
{
593-
Skip.If(VersionUtils.IsLinux, "Test hangs on Linux for some reason");
594595
await debugService.SetLineBreakpointsAsync(
595596
variableScriptFile,
596597
new[] { BreakpointDetails.Create(variableScriptFile.FilePath, 14) }).ConfigureAwait(true);

0 commit comments

Comments
 (0)