2019-01-24 16:43:03 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2021-12-24 19:15:10 +08:00
using System ;
2020-09-08 10:31:42 +08:00
using System.Runtime.CompilerServices ;
2022-01-24 18:10:14 +08:00
using osu.Framework ;
2021-08-18 14:32:59 +08:00
using osu.Framework.Testing ;
2018-04-13 17:19:50 +08:00
2018-02-08 16:07:18 +08:00
namespace osu.Game.Tests
{
/// <summary>
/// A headless host which cleans up before running (removing any remnants from a previous execution).
/// </summary>
2021-08-18 14:32:59 +08:00
public class CleanRunHeadlessGameHost : TestRunHeadlessGameHost
2018-02-08 16:07:18 +08:00
{
2022-07-04 15:45:21 +08:00
private readonly bool bypassCleanupOnSetup ;
2020-09-08 10:31:42 +08:00
/// <summary>
/// Create a new instance.
/// </summary>
/// <param name="bindIPC">Whether to bind IPC channels.</param>
/// <param name="realtime">Whether the host should be forced to run in realtime, rather than accelerated test time.</param>
2022-07-04 15:45:21 +08:00
/// <param name="bypassCleanupOnSetup">Whether to bypass directory cleanup on <see cref="SetupForRun"/>.</param>
/// <param name="bypassCleanupOnDispose">Whether to bypass directory cleanup on host disposal. Should be used only if a subsequent test relies on the files still existing.</param>
2020-09-08 10:31:42 +08:00
/// <param name="callingMethodName">The name of the calling method, used for test file isolation and clean-up.</param>
2022-07-04 15:45:21 +08:00
public CleanRunHeadlessGameHost ( bool bindIPC = false , bool realtime = true , bool bypassCleanupOnSetup = false , bool bypassCleanupOnDispose = false ,
[CallerMemberName] string callingMethodName = @"" )
2022-01-24 18:10:14 +08:00
: base ( $"{callingMethodName}-{Guid.NewGuid()}" , new HostOptions
{
BindIPC = bindIPC ,
2022-07-04 15:45:21 +08:00
} , bypassCleanup : bypassCleanupOnDispose , realtime : realtime )
2018-02-08 16:07:18 +08:00
{
2022-07-04 15:45:21 +08:00
this . bypassCleanupOnSetup = bypassCleanupOnSetup ;
2019-03-28 00:29:06 +08:00
}
protected override void SetupForRun ( )
{
2022-07-04 15:45:21 +08:00
if ( ! bypassCleanupOnSetup )
2021-12-24 22:55:08 +08:00
{
2022-07-04 15:45:21 +08:00
try
{
Storage . DeleteDirectory ( string . Empty ) ;
}
catch
{
// May fail if a logging target has already been set via OsuStorage.ChangeTargetStorage.
}
2021-12-24 22:55:08 +08:00
}
2021-07-09 11:15:28 +08:00
// base call needs to be run *after* storage is emptied, as it updates the (static) logger's storage and may start writing
// log entries from another source if a unit test host is shared over multiple tests, causing a file access denied exception.
base . SetupForRun ( ) ;
2018-02-08 16:07:18 +08:00
}
}
}