Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regression test for debugging script with dot-source operator #1858

Merged
merged 5 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace PowerShellEditorServices.Test.E2E
{
[Trait("Category", "DAP")]
public class DebugAdapterProtocolMessageTests : IAsyncLifetime
public class DebugAdapterProtocolMessageTests : IAsyncLifetime, IDisposable
{
private const string TestOutputFileName = "__dapTestOutputFile.txt";
private static readonly bool s_isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
Expand Down Expand Up @@ -104,7 +104,13 @@ await PsesDebugAdapterClient.RequestDisconnect(new DisconnectArguments
TerminateDebuggee = true
}).ConfigureAwait(true);
await _psesProcess.Stop().ConfigureAwait(true);
}

public void Dispose()
{
GC.SuppressFinalize(this);
PsesDebugAdapterClient?.Dispose();
_psesProcess?.Dispose();
}

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

// Have script create file first with `>` (but don't rely on overwriting).
StringBuilder builder = new StringBuilder().Append('\'').Append(logStatements[0]).Append("' > '").Append(s_testOutputPath).AppendLine("'");
// NOTE: We uses double quotes so that we can use PowerShell variables.
StringBuilder builder = new StringBuilder()
.Append("Write-Output \"")
.Append(logStatements[0])
.Append("\" > '")
.Append(s_testOutputPath)
.AppendLine("'");

for (int i = 1; i < logStatements.Length; i++)
{
// Then append to that script with `>>`.
builder.Append('\'').Append(logStatements[i]).Append("' >> '").Append(s_testOutputPath).AppendLine("'");
builder
.Append("Write-Output \"")
.Append(logStatements[i])
.Append("\" >> '")
.Append(s_testOutputPath)
.AppendLine("'");
}

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

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

[Fact]
public async Task UsesDotSourceOperatorAndQuotesAsync()
{
string filePath = NewTestFile(GenerateScriptFromLoggingStatements("$($MyInvocation.Line)"));
await PsesDebugAdapterClient.LaunchScript(filePath, Started).ConfigureAwait(true);
ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments()).ConfigureAwait(true);
Assert.NotNull(configDoneResponse);
Assert.Collection(await GetLog().ConfigureAwait(true),
(i) => Assert.StartsWith(". \"", i));
}

[Fact]
public async Task CanLaunchScriptWithNoBreakpointsAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ private Task ExecutePowerShellCommand(string command, params string[] args)

private void AssertDebuggerPaused()
{
DebuggerStoppedEventArgs eventArgs = debuggerStoppedQueue.Take(CancellationToken.None);
using CancellationTokenSource cts = new(10000);
DebuggerStoppedEventArgs eventArgs = debuggerStoppedQueue.Take(cts.Token);
Assert.Empty(eventArgs.OriginalEvent.Breakpoints);
}

Expand All @@ -112,7 +113,8 @@ private void AssertDebuggerStopped(
int lineNumber = -1,
CommandBreakpointDetails commandBreakpointDetails = default)
{
DebuggerStoppedEventArgs eventArgs = debuggerStoppedQueue.Take(CancellationToken.None);
using CancellationTokenSource cts = new(10000);
DebuggerStoppedEventArgs eventArgs = debuggerStoppedQueue.Take(cts.Token);

Assert.True(psesHost.DebugContext.IsStopped);

Expand Down Expand Up @@ -587,10 +589,9 @@ await debugService.SetLineBreakpointsAsync(
Assert.Equal("$false", falseVar.ValueString);
}

[SkippableFact]
[Fact]
public async Task DebuggerSetsVariablesNoConversion()
{
Skip.If(VersionUtils.IsLinux, "Test hangs on Linux for some reason");
await debugService.SetLineBreakpointsAsync(
variableScriptFile,
new[] { BreakpointDetails.Create(variableScriptFile.FilePath, 14) }).ConfigureAwait(true);
Expand Down