Skip to content

Commit

Permalink
Remove ConfigureAwait() calls from test code
Browse files Browse the repository at this point in the history
Which requires ignoring the warning we enforce elsewhere. This resolves
Warning xUnit1030, "Do not call ConfigureAwait in test method."
  • Loading branch information
andyleejordan committed Sep 27, 2023
1 parent 6c4511c commit 55f7172
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 279 deletions.
5 changes: 0 additions & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ dotnet_diagnostic.VSTHRD114.severity = error
# VSTHRD200: Use "Async" suffix for awaitable methods
dotnet_diagnostic.VSTHRD200.severity = silent

# xUnit2013: Do not use equality check to check for collection size
dotnet_diagnostic.xUnit2013.severity = error
# xUnit1004: Test methods should not be skipped
dotnet_diagnostic.xUnit1004.severity = suggestion

# IDE0001: Simplify name
dotnet_diagnostic.IDE0001.severity = error
# IDE0002: Simplify member access
Expand Down
15 changes: 15 additions & 0 deletions test/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# EditorConfig is awesome: http://EditorConfig.org

# not the top-most EditorConfig file
root = false

[*.{cs}]
# CA2007: Do not directly await a Task
dotnet_diagnostic.CA2007.severity = none

# xUnit1004: Test methods should not be skipped
dotnet_diagnostic.xUnit1004.severity = suggestion
# xUnit1030: Do not call ConfigureAwait in test method
dotnet_diagnostic.xUnit1030.severity = error
# xUnit2013: Do not use equality check to check for collection size
dotnet_diagnostic.xUnit2013.severity = error
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public static async Task LaunchScript(this DebugAdapterClient debugAdapterClient
Script = script,
Cwd = "",
CreateTemporaryIntegratedConsole = false
}).ConfigureAwait(true);
});

if (launchResponse is null)
{
throw new Exception("Launch response was null.");
}

// This will check to see if we received the Initialized event from the server.
await started.Task.ConfigureAwait(true);
await started.Task;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task InitializeAsync()
{
LoggerFactory factory = new();
_psesProcess = new PsesStdioProcess(factory, true);
await _psesProcess.Start().ConfigureAwait(true);
await _psesProcess.Start();

TaskCompletionSource<bool> initialized = new();

Expand Down Expand Up @@ -91,9 +91,9 @@ public async Task InitializeAsync()
// This tells us that we are ready to send messages to PSES... but are not stuck waiting for
// Initialized.
#pragma warning disable CS4014
PsesDebugAdapterClient.Initialize(CancellationToken.None).ConfigureAwait(true);
PsesDebugAdapterClient.Initialize(CancellationToken.None);
#pragma warning restore CS4014
await initialized.Task.ConfigureAwait(true);
await initialized.Task;
}

public async Task DisposeAsync()
Expand All @@ -102,8 +102,8 @@ await PsesDebugAdapterClient.RequestDisconnect(new DisconnectArguments
{
Restart = false,
TerminateDebuggee = true
}).ConfigureAwait(true);
await _psesProcess.Stop().ConfigureAwait(true);
});
await _psesProcess.Stop();
}

public void Dispose()
Expand Down Expand Up @@ -164,10 +164,10 @@ private static async Task<string[]> GetLog()
{
for (int i = 0; !File.Exists(s_testOutputPath) && i < 60; i++)
{
await Task.Delay(1000).ConfigureAwait(true);
await Task.Delay(1000);
}
// Sleep one more time after the file exists so whatever is writing can finish.
await Task.Delay(1000).ConfigureAwait(true);
await Task.Delay(1000);
return File.ReadLines(s_testOutputPath).ToArray();
}

Expand All @@ -186,10 +186,10 @@ public void CanInitializeWithCorrectServerSettings()
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);
await PsesDebugAdapterClient.LaunchScript(filePath, Started);
ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments());
Assert.NotNull(configDoneResponse);
Assert.Collection(await GetLog().ConfigureAwait(true),
Assert.Collection(await GetLog(),
(i) => Assert.StartsWith(". '", i));
}

Expand All @@ -198,11 +198,11 @@ public async Task CanLaunchScriptWithNoBreakpointsAsync()
{
string filePath = NewTestFile(GenerateScriptFromLoggingStatements("works"));

await PsesDebugAdapterClient.LaunchScript(filePath, Started).ConfigureAwait(true);
await PsesDebugAdapterClient.LaunchScript(filePath, Started);

ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments()).ConfigureAwait(true);
ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments());
Assert.NotNull(configDoneResponse);
Assert.Collection(await GetLog().ConfigureAwait(true),
Assert.Collection(await GetLog(),
(i) => Assert.Equal("works", i));
}

Expand All @@ -218,32 +218,32 @@ public async Task CanSetBreakpointsAsync()
"after breakpoint"
));

await PsesDebugAdapterClient.LaunchScript(filePath, Started).ConfigureAwait(true);
await PsesDebugAdapterClient.LaunchScript(filePath, Started);

// {"command":"setBreakpoints","arguments":{"source":{"name":"dfsdfg.ps1","path":"/Users/tyleonha/Code/PowerShell/Misc/foo/dfsdfg.ps1"},"lines":[2],"breakpoints":[{"line":2}],"sourceModified":false},"type":"request","seq":3}
SetBreakpointsResponse setBreakpointsResponse = await PsesDebugAdapterClient.SetBreakpoints(new SetBreakpointsArguments
{
Source = new Source { Name = Path.GetFileName(filePath), Path = filePath },
Breakpoints = new SourceBreakpoint[] { new SourceBreakpoint { Line = 2 } },
SourceModified = false,
}).ConfigureAwait(true);
});

Breakpoint breakpoint = setBreakpointsResponse.Breakpoints.First();
Assert.True(breakpoint.Verified);
Assert.Equal(filePath, breakpoint.Source.Path, ignoreCase: s_isWindows);
Assert.Equal(2, breakpoint.Line);

ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments()).ConfigureAwait(true);
ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments());
Assert.NotNull(configDoneResponse);
Assert.Collection(await GetLog().ConfigureAwait(true),
Assert.Collection(await GetLog(),
(i) => Assert.Equal("before breakpoint", i));
File.Delete(s_testOutputPath);

ContinueResponse continueResponse = await PsesDebugAdapterClient.RequestContinue(
new ContinueArguments { ThreadId = 1 }).ConfigureAwait(true);
new ContinueArguments { ThreadId = 1 });

Assert.NotNull(continueResponse);
Assert.Collection(await GetLog().ConfigureAwait(true),
Assert.Collection(await GetLog(),
(i) => Assert.Equal("at breakpoint", i),
(i) => Assert.Equal("after breakpoint", i));
}
Expand Down Expand Up @@ -274,24 +274,24 @@ public async Task CanStepPastSystemWindowsForms()
"Write-Host $form"
}));

await PsesDebugAdapterClient.LaunchScript(filePath, Started).ConfigureAwait(true);
await PsesDebugAdapterClient.LaunchScript(filePath, Started);

SetFunctionBreakpointsResponse setBreakpointsResponse = await PsesDebugAdapterClient.SetFunctionBreakpoints(
new SetFunctionBreakpointsArguments
{
Breakpoints = new FunctionBreakpoint[]
{ new FunctionBreakpoint { Name = "Write-Host", } }
}).ConfigureAwait(true);
});

Breakpoint breakpoint = setBreakpointsResponse.Breakpoints.First();
Assert.True(breakpoint.Verified);

ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments()).ConfigureAwait(true);
ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments());
Assert.NotNull(configDoneResponse);
await Task.Delay(5000).ConfigureAwait(true);
await Task.Delay(5000);

VariablesResponse variablesResponse = await PsesDebugAdapterClient.RequestVariables(
new VariablesArguments { VariablesReference = 1 }).ConfigureAwait(true);
new VariablesArguments { VariablesReference = 1 });

Variable form = variablesResponse.Variables.FirstOrDefault(v => v.Name == "$form");
Assert.NotNull(form);
Expand All @@ -312,15 +312,15 @@ public async Task CanLaunchScriptWithCommentedLastLineAsync()
// PsesLaunchRequestArguments.Script, which is then assigned to
// DebugStateService.ScriptToLaunch in that handler, and finally used by the
// ConfigurationDoneHandler in LaunchScriptAsync.
await PsesDebugAdapterClient.LaunchScript(script, Started).ConfigureAwait(true);
await PsesDebugAdapterClient.LaunchScript(script, Started);

ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments()).ConfigureAwait(true);
ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments());
Assert.NotNull(configDoneResponse);
// We can check that the script was invoked as expected, which is to dot-source a script
// block with the contents surrounded by newlines. While we can't check that the last
// line was a curly brace by itself, we did check that the contents ended with a
// comment, so if this output exists then the bug did not recur.
Assert.Collection(await GetLog().ConfigureAwait(true),
Assert.Collection(await GetLog(),
(i) => Assert.Equal(". {", i),
(i) => Assert.Equal("", i));
}
Expand All @@ -342,9 +342,9 @@ public async Task CanRunPesterTestFile()
using CancellationTokenSource cts = new(5000);
while (!File.Exists(pesterLog) && !cts.Token.IsCancellationRequested)
{
await Task.Delay(1000).ConfigureAwait(true);
await Task.Delay(1000);
}
await Task.Delay(15000).ConfigureAwait(true);
await Task.Delay(15000);
_output.WriteLine(File.ReadAllText(pesterLog));
*/

Expand All @@ -360,9 +360,9 @@ public async Task CanRunPesterTestFile()
}
}", isPester: true);

await PsesDebugAdapterClient.LaunchScript($"Invoke-Pester -Script '{pesterTest}'", Started).ConfigureAwait(true);
await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments()).ConfigureAwait(true);
Assert.Collection(await GetLog().ConfigureAwait(true), (i) => Assert.Equal("pester", i));
await PsesDebugAdapterClient.LaunchScript($"Invoke-Pester -Script '{pesterTest}'", Started);
await PsesDebugAdapterClient.RequestConfigurationDone(new ConfigurationDoneArguments());
Assert.Collection(await GetLog(), (i) => Assert.Equal("pester", i));
}
}
}
8 changes: 4 additions & 4 deletions test/PowerShellEditorServices.Test.E2E/LSPTestsFixures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task InitializeAsync()
{
LoggerFactory factory = new();
_psesProcess = new PsesStdioProcess(factory, IsDebugAdapterTests);
await _psesProcess.Start().ConfigureAwait(false);
await _psesProcess.Start();

DirectoryInfo testDir =
Directory.CreateDirectory(Path.Combine(s_binDir, Path.GetRandomFileName()));
Expand Down Expand Up @@ -79,7 +79,7 @@ public async Task InitializeAsync()
}
});

await PsesLanguageClient.Initialize(CancellationToken.None).ConfigureAwait(false);
await PsesLanguageClient.Initialize(CancellationToken.None);

// Make sure Script Analysis is enabled because we'll need it in the tests.
// This also makes sure the configuration is set to default values.
Expand All @@ -97,8 +97,8 @@ public async Task InitializeAsync()

public async Task DisposeAsync()
{
await PsesLanguageClient.Shutdown().ConfigureAwait(false);
await _psesProcess.Stop().ConfigureAwait(false);
await PsesLanguageClient.Shutdown();
await _psesProcess.Stop();
PsesLanguageClient?.Dispose();
}
}
Expand Down
Loading

0 comments on commit 55f7172

Please sign in to comment.