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

Fixing async issues in OrchardCore.Tests (Lombiq Technologies: OCORE-80) #11195

Merged
merged 3 commits into from
Feb 16, 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 @@ -145,7 +145,15 @@ private static object[] GetInvokeParametersForMethod(MethodInfo methodInfo, ILis
}
}

if (methodHasParams && (methodParameters.Length - args.Count == 1)) invokeParameters.Add(new string[] { });
var lastParameterIsParams = methodParameters
.LastOrDefault()
?.GetCustomAttributes(typeof(ParamArrayAttribute), false)
?.Length > 0;

if (methodHasParams && (methodParameters.Length - args.Count == 1) && !lastParameterIsParams)
{
invokeParameters.Add(new string[] { });
}
Comment on lines +153 to +156
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not sure why this logic was there. However, adding a second string[] argument to a method invocation that has a params parameter (in addition to an already added string[] argument on line 131) seems like a no-go in any case.


return invokeParameters.ToArray();
}
Expand Down
60 changes: 30 additions & 30 deletions test/OrchardCore.Tests/Commands/CommandHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,28 @@ private CommandContext CreateCommandContext(string commandName, IDictionary<stri
}

[Fact]
public void TestFooCommand()
public async Task TestFooCommand()
{
var commandContext = CreateCommandContext("Foo");
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Equal("Command Foo Executed", commandContext.Output.ToString());
}

[Fact]
public void TestNotExistingCommand()
public async Task TestNotExistingCommand()
{
Assert.Throws<InvalidOperationException>(() =>
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
var commandContext = CreateCommandContext("NoSuchCommand");
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
});
}

[Fact]
public void TestCommandWithCustomAlias()
public async Task TestCommandWithCustomAlias()
{
var commandContext = CreateCommandContext("Bar");
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Equal("Hello World!", commandContext.Output.ToString());
}

Expand All @@ -88,34 +88,34 @@ public void TestEmptyHelpText()
}

[Fact]
public void TestCaseInsensitiveForCommand()
public async Task TestCaseInsensitiveForCommand()
{
var commandContext = CreateCommandContext("BAZ", new Dictionary<string, string> { { "VERBOSE", "true" } });
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Equal("Command Baz Called : This was a test", commandContext.Output.ToString());
}

[Fact]
public void TestBooleanSwitchForCommand()
public async Task TestBooleanSwitchForCommand()
{
var commandContext = CreateCommandContext("Baz", new Dictionary<string, string> { { "Verbose", "true" } });
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Equal("Command Baz Called : This was a test", commandContext.Output.ToString());
}

[Fact]
public void TestIntSwitchForCommand()
public async Task TestIntSwitchForCommand()
{
var commandContext = CreateCommandContext("Baz", new Dictionary<string, string> { { "Level", "2" } });
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Equal("Command Baz Called : Entering Level 2", commandContext.Output.ToString());
}

[Fact]
public void TestStringSwitchForCommand()
public async Task TestStringSwitchForCommand()
{
var commandContext = CreateCommandContext("Baz", new Dictionary<string, string> { { "User", "OrchardUser" } });
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Equal("Command Baz Called : current user is OrchardUser", commandContext.Output.ToString());
}

Expand All @@ -124,14 +124,14 @@ public async Task TestSwitchForCommandWithoutSupportForIt()
{
var switches = new Dictionary<string, string> { { "User", "OrchardUser" } };
var commandContext = CreateCommandContext("Foo", switches);
await Assert.ThrowsAsync<InvalidOperationException>(() => _handler.ExecuteAsync(commandContext));
await Assert.ThrowsAsync<InvalidOperationException>(async () => await _handler.ExecuteAsync(commandContext));
}

[Fact]
public void TestCommandThatDoesNotReturnAValue()
public async Task TestCommandThatDoesNotReturnAValue()
{
var commandContext = CreateCommandContext("Log");
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Empty(commandContext.Output.ToString());
}

Expand All @@ -140,62 +140,62 @@ public async Task TestNotExistingSwitch()
{
var switches = new Dictionary<string, string> { { "ThisSwitchDoesNotExist", "Insignificant" } };
var commandContext = CreateCommandContext("Foo", switches);
await Assert.ThrowsAsync<InvalidOperationException>(() => _handler.ExecuteAsync(commandContext));
await Assert.ThrowsAsync<InvalidOperationException>(async () => await _handler.ExecuteAsync(commandContext));
}

[Fact]
public void TestCommandArgumentsArePassedCorrectly()
public async Task TestCommandArgumentsArePassedCorrectly()
{
var commandContext = CreateCommandContext("Concat", new Dictionary<string, string>(), new[] { "left to ", "right" });
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Equal("left to right", commandContext.Output.ToString());
}

[Fact]
public void TestCommandArgumentsArePassedCorrectlyWithAParamsParameters()
public async Task TestCommandArgumentsArePassedCorrectlyWithAParamsParameters()
{
var commandContext = CreateCommandContext("ConcatParams", new Dictionary<string, string>(), new[] { "left to ", "right" });
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Equal("left to right", commandContext.Output.ToString());
}

[Fact]
public void TestCommandArgumentsArePassedCorrectlyWithAParamsParameterAndNoArguments()
public async Task TestCommandArgumentsArePassedCorrectlyWithAParamsParameterAndNoArguments()
{
var commandContext = CreateCommandContext("ConcatParams", new Dictionary<string, string>());
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Empty(commandContext.Output.ToString());
}

[Fact]
public void TestCommandArgumentsArePassedCorrectlyWithNormalParametersAndAParamsParameters()
public async Task TestCommandArgumentsArePassedCorrectlyWithNormalParametersAndAParamsParameters()
{
var commandContext = CreateCommandContext("ConcatAllParams",
new Dictionary<string, string>(),
new[] { "left-", "center-", "right" });
_handler.ExecuteAsync(commandContext);
await _handler.ExecuteAsync(commandContext);
Assert.Equal("left-center-right", commandContext.Output.ToString());
}

[Fact]
public async Task TestCommandParamsMismatchWithoutParamsNotEnoughArguments()
{
var commandContext = CreateCommandContext("Concat", new Dictionary<string, string>(), new[] { "left to " });
await Assert.ThrowsAsync<InvalidOperationException>(() => _handler.ExecuteAsync(commandContext));
await Assert.ThrowsAsync<InvalidOperationException>(async () => await _handler.ExecuteAsync(commandContext));
}

[Fact]
public async Task TestCommandParamsMismatchWithoutParamsTooManyArguments()
{
var commandContext = CreateCommandContext("Foo", new Dictionary<string, string>(), new[] { "left to " });
await Assert.ThrowsAsync<InvalidOperationException>(() => _handler.ExecuteAsync(commandContext));
await Assert.ThrowsAsync<InvalidOperationException>(async () => await _handler.ExecuteAsync(commandContext));
}

[Fact]
public async Task TestCommandParamsMismatchWithParamsButNotEnoughArguments()
{
var commandContext = CreateCommandContext("ConcatAllParams", new Dictionary<string, string>());
await Assert.ThrowsAsync<InvalidOperationException>(() => _handler.ExecuteAsync(commandContext));
await Assert.ThrowsAsync<InvalidOperationException>(async () => await _handler.ExecuteAsync(commandContext));
}
}

Expand Down
9 changes: 5 additions & 4 deletions test/OrchardCore.Tests/Commands/CommandManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using OrchardCore.Environment.Commands;
Expand All @@ -24,18 +25,18 @@ public CommandManagerTests()
}

[Fact]
public void ManagerCanRunACommand()
public async Task ManagerCanRunACommand()
{
var context = new CommandParameters { Arguments = new string[] { "FooBar" }, Output = new StringWriter() };
_manager.ExecuteAsync(context);
await _manager.ExecuteAsync(context);
Assert.Equal("success!", context.Output.ToString());
}

[Fact]
public void ManagerCanRunACompositeCommand()
public async Task ManagerCanRunACompositeCommand()
{
var context = new CommandParameters { Arguments = ("Foo Bar Bleah").Split(' '), Output = new StringWriter() };
_manager.ExecuteAsync(context);
await _manager.ExecuteAsync(context);
Assert.Equal("Bleah", context.Output.ToString());
}

Expand Down
5 changes: 3 additions & 2 deletions test/OrchardCore.Tests/Localization/CultureScopeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
using System.Threading.Tasks;
using OrchardCore.Localization;
using Xunit;

Expand Down Expand Up @@ -57,14 +58,14 @@ public void CultureScopeRetrievesTheOrginalCulturesAfterScopeEnded()
}

[Fact]
public void CultureScopeRetrievesTheOrginalCulturesIfExceptionOccurs()
public async Task CultureScopeRetrievesTheOrginalCulturesIfExceptionOccurs()
{
// Arrange
var culture = CultureInfo.CurrentCulture;
var uiCulture = CultureInfo.CurrentUICulture;

// Act & Assert
Assert.ThrowsAsync<Exception>(() =>
await Assert.ThrowsAsync<Exception>(() =>
{
using (var cultureScope = CultureScope.Create("FR"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ public async Task DisposesMediaCreatingStreams()
finally
{
// This disposes the final outputStream.
outputStream?.Dispose();
if (outputStream != null)
{
await outputStream.DisposeAsync();
}
}
}
finally
{
inputStream?.Dispose();
if (inputStream != null)
{
await inputStream.DisposeAsync();
}
}

foreach (var stream in streams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContex
public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
var text = await _scriptEvaluator.EvaluateAsync(Text, workflowContext);
_output.WriteLine(text);
await _output.WriteLineAsync(text);
return Outcomes("Done");
}
}
Expand Down