Files
sbox-public/engine/Tools/SboxBuild/Steps/Test.cs
Lorenz Junglas 91f8fcf183 Speed up / parallelize tests (#3587)
- Added Sandbox.Test.Unit project (contains independent tests that can run in parallel) 
- Modify some slow/stress tests (e.g. instead of doing a million iterations settle for 10k).

Tests run almost twice as fast now.
2025-12-10 14:23:00 +01:00

85 lines
2.2 KiB
C#

using System.Text;
using static Facepunch.Constants;
namespace Facepunch.Steps;
internal class Test( string name ) : Step( name )
{
protected override ExitCode RunInternal()
{
try
{
string rootDir = Directory.GetCurrentDirectory();
string engineDir = Path.Combine( rootDir, "engine" );
string gameDir = Path.Combine( rootDir, "game" );
var managedTestArgs = "test --logger \"console;verbosity=normal;consoleLoggerParameters=ErrorsOnly\" -c Release --property:OutputPath=bin/test";
//if ( Utility.IsCi() )
//{
// Use cusotm loger for problem matching
// TODO fix me, add GitHubActions logger to our projects?
// managedTestArgs += " --logger GitHubActions";
//}
// Track output for failed tests:
List<string> failedTests = new List<string>();
StringBuilder currentFailedTestInfo = new();
var isCollectingFailedTestInfo = false;
bool managedTestSuccess = Utility.RunProcess(
"dotnet",
managedTestArgs,
engineDir,
new Dictionary<string, string> { { "FACEPUNCH_ENGINE", gameDir } },
// A bit hacky but we collect failed tests to get a nicer summary in the end
onDataReceived: ( sender, e ) =>
{
if ( e.Data != null )
{
Log.Info( e.Data );
if ( isCollectingFailedTestInfo && e.Data.TrimStart().StartsWith( "Passed" ) )
{
failedTests.Add( currentFailedTestInfo.ToString().Trim( '\n' ) );
currentFailedTestInfo = currentFailedTestInfo.Clear();
isCollectingFailedTestInfo = false;
}
if ( e.Data.TrimStart().StartsWith( "Failed " ) )
{
isCollectingFailedTestInfo = true;
}
if ( isCollectingFailedTestInfo )
{
currentFailedTestInfo.AppendLine( e.Data );
}
}
}
);
if ( !managedTestSuccess )
{
Log.Info( "" );
Log.Info( "Failed Tests Summary:" );
Log.Info( "" );
// Log failed tests
foreach ( var failedTest in failedTests )
{
Log.Info( failedTest );
Log.Info( "" );
}
Log.Error( "Managed tests failed!" );
return ExitCode.Failure;
}
Log.Info( "All tests completed successfully!" );
return ExitCode.Success;
}
catch ( Exception ex )
{
Log.Error( $"Test operations failed with error: {ex}" );
return ExitCode.Failure;
}
}
}