Make integration tests fail when logging console errors (#4747)

* Make tests fail when logging console errors

* Add log convenience property
This commit is contained in:
Leon Friedrich
2023-12-22 21:53:23 -05:00
committed by GitHub
parent bb5cb10d57
commit 9446ab76f9
6 changed files with 62 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ using Robust.Shared.Network.Messages;
namespace Robust.Client.Console;
internal sealed partial class ClientConsoleHost
internal partial class ClientConsoleHost
{
private readonly Dictionary<int, PendingCompletion> _completionsPending = new();
private int _completionSeq;

View File

@@ -47,7 +47,8 @@ namespace Robust.Client.Console
}
/// <inheritdoc cref="IClientConsoleHost" />
internal sealed partial class ClientConsoleHost : ConsoleHost, IClientConsoleHost, IConsoleHostInternal, IPostInjectInit
[Virtual]
internal partial class ClientConsoleHost : ConsoleHost, IClientConsoleHost, IConsoleHostInternal, IPostInjectInit
{
[Dependency] private readonly IClientConGroupController _conGroup = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;

View File

@@ -15,7 +15,8 @@ using Robust.Shared.Utility;
namespace Robust.Server.Console
{
/// <inheritdoc cref="IServerConsoleHost" />
internal sealed class ServerConsoleHost : ConsoleHost, IServerConsoleHost, IConsoleHostInternal
[Virtual]
internal class ServerConsoleHost : ConsoleHost, IServerConsoleHost, IConsoleHostInternal
{
[Dependency] private readonly IConGroupController _groupController = default!;
[Dependency] private readonly IPlayerManager _players = default!;
@@ -127,11 +128,17 @@ namespace Robust.Server.Console
// toolshed time
_toolshed.InvokeCommand(shell, command, null, out var res, out var ctx);
bool anyErrors = false;
foreach (var err in ctx.GetErrors())
{
anyErrors = true;
ctx.WriteLine(err.Describe());
}
// why does ctx not have any write-error support?
if (anyErrors)
shell.WriteError($"Failed to execute toolshed command");
shell.WriteLine(FormattedMessage.FromMarkupPermissive(_toolshed.PrettyPrintType(res, out var more, moreUsed: true)));
ctx.WriteVar("more", more);
}

View File

@@ -12,6 +12,7 @@ using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Robust.Client;
using Robust.Client.Console;
using Robust.Client.GameStates;
using Robust.Client.Player;
using Robust.Client.Timing;
@@ -271,6 +272,7 @@ namespace Robust.UnitTesting
public IGameTiming Timing { get; private set; } = default!;
public IMapManager MapMan { get; private set; } = default!;
public IConsoleHost ConsoleHost { get; private set; } = default!;
public ISawmill Log { get; private set; } = default!;
protected virtual void ResolveIoC(IDependencyCollection deps)
{
@@ -281,6 +283,7 @@ namespace Robust.UnitTesting
Timing = deps.Resolve<IGameTiming>();
MapMan = deps.Resolve<IMapManager>();
ConsoleHost = deps.Resolve<IConsoleHost>();
Log = deps.Resolve<ILogManager>().GetSawmill("test");
}
public T System<T>() where T : IEntitySystem
@@ -668,6 +671,9 @@ namespace Robust.UnitTesting
deps.Register<TestingModLoader, TestingModLoader>(true);
deps.RegisterInstance<IStatusHost>(new Mock<IStatusHost>().Object, true);
deps.Register<IRobustMappedStringSerializer, IntegrationMappedStringSerializer>(true);
deps.Register<IServerConsoleHost, TestingServerConsoleHost>(true);
deps.Register<IConsoleHost, TestingServerConsoleHost>(true);
deps.Register<IConsoleHostInternal, TestingServerConsoleHost>(true);
Options?.InitIoC?.Invoke();
deps.BuildGraph();
//ServerProgram.SetupLogging();
@@ -836,6 +842,9 @@ namespace Robust.UnitTesting
deps.Register<IModLoaderInternal, TestingModLoader>(true);
deps.Register<TestingModLoader, TestingModLoader>(true);
deps.Register<IRobustMappedStringSerializer, IntegrationMappedStringSerializer>(true);
deps.Register<IClientConsoleHost, TestingClientConsoleHost>(true);
deps.Register<IConsoleHost, TestingClientConsoleHost>(true);
deps.Register<IConsoleHostInternal, TestingClientConsoleHost>(true);
Options?.InitIoC?.Invoke();
deps.BuildGraph();

View File

@@ -27,6 +27,24 @@ namespace Robust.UnitTesting.Shared
Assert.That(client, Is.Not.Null);
}
[Test]
public async Task ConsoleErrorsFailTest()
{
var server = StartServer();
var client = StartClient();
await Task.WhenAll(client.WaitIdleAsync(), server.WaitIdleAsync());
// test missing commands
await client.WaitPost(() => Assert.Throws<AssertionException>(() => client.ConsoleHost.ExecuteCommand("aaaaaaaa")));
// test invalid commands / missing arguments
await client.WaitPost(() => Assert.Throws<AssertionException>(() => client.ConsoleHost.ExecuteCommand("cvar")));
// and repeat for the server
await server.WaitPost(() => Assert.Throws<AssertionException>(() => server.ConsoleHost.ExecuteCommand("aaaaaaaa")));
await server.WaitPost(() => Assert.Throws<AssertionException>(() => server.ConsoleHost.ExecuteCommand("cvar")));
}
[Test]
public async Task ServerClientPairConnectCorrectlyTest()
{

View File

@@ -0,0 +1,24 @@
using NUnit.Framework;
using Robust.Client.Console;
using Robust.Server.Console;
using Robust.Shared.Player;
namespace Robust.UnitTesting;
internal sealed class TestingServerConsoleHost : ServerConsoleHost
{
public override void WriteError(ICommonSession? session, string text)
{
base.WriteError(session, text);
Assert.Fail($"Console command encountered an error: {text}");
}
}
internal sealed class TestingClientConsoleHost : ClientConsoleHost
{
public override void WriteError(ICommonSession? session, string text)
{
base.WriteError(session, text);
Assert.Fail($"Console command encountered an error: {text}");
}
}