2020-05-04 16:01:05 +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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
2020-05-10 12:17:37 +08:00
|
|
|
using System.Linq;
|
2020-09-17 15:12:30 +08:00
|
|
|
using System.Runtime.CompilerServices;
|
2020-05-04 16:01:05 +08:00
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
2020-05-07 18:01:19 +08:00
|
|
|
using osu.Framework.Configuration;
|
2020-05-04 16:01:05 +08:00
|
|
|
using osu.Framework.Platform;
|
2021-08-18 14:32:59 +08:00
|
|
|
using osu.Framework.Testing;
|
2020-05-04 16:01:05 +08:00
|
|
|
using osu.Game.Configuration;
|
2020-05-07 18:01:19 +08:00
|
|
|
using osu.Game.IO;
|
2020-05-04 16:01:05 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Tests.NonVisual
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2020-09-18 17:05:33 +08:00
|
|
|
public class CustomDataDirectoryTest : ImportTest
|
2020-05-04 16:01:05 +08:00
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void TestDefaultDirectory()
|
|
|
|
{
|
2020-09-17 15:12:30 +08:00
|
|
|
using (var host = new CustomTestHeadlessGameHost())
|
2020-05-04 16:01:05 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-07-09 14:08:03 +08:00
|
|
|
string defaultStorageLocation = getDefaultLocationFor(nameof(TestDefaultDirectory));
|
|
|
|
|
2020-09-18 17:05:33 +08:00
|
|
|
var osu = LoadOsuIntoHost(host);
|
2020-05-04 16:01:05 +08:00
|
|
|
var storage = osu.Dependencies.Get<Storage>();
|
|
|
|
|
|
|
|
Assert.That(storage.GetFullPath("."), Is.EqualTo(defaultStorageLocation));
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestCustomDirectory()
|
|
|
|
{
|
2020-07-09 14:08:03 +08:00
|
|
|
string customPath = prepareCustomPath();
|
2020-05-04 16:01:05 +08:00
|
|
|
|
2020-09-17 15:12:30 +08:00
|
|
|
using (var host = new CustomTestHeadlessGameHost())
|
2020-07-09 14:08:03 +08:00
|
|
|
{
|
|
|
|
using (var storageConfig = new StorageConfigManager(host.InitialStorage))
|
2021-03-17 15:10:16 +08:00
|
|
|
storageConfig.SetValue(StorageConfig.FullPath, customPath);
|
2020-05-04 16:01:05 +08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-09-18 17:05:33 +08:00
|
|
|
var osu = LoadOsuIntoHost(host);
|
2020-05-04 16:01:05 +08:00
|
|
|
|
|
|
|
// switch to DI'd storage
|
2020-07-09 14:08:03 +08:00
|
|
|
var storage = osu.Dependencies.Get<Storage>();
|
2020-05-04 16:01:05 +08:00
|
|
|
|
2020-05-07 18:01:19 +08:00
|
|
|
Assert.That(storage.GetFullPath("."), Is.EqualTo(customPath));
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 12:17:37 +08:00
|
|
|
[Test]
|
|
|
|
public void TestSubDirectoryLookup()
|
|
|
|
{
|
2020-07-09 14:08:03 +08:00
|
|
|
string customPath = prepareCustomPath();
|
2020-05-10 12:17:37 +08:00
|
|
|
|
2020-09-17 15:12:30 +08:00
|
|
|
using (var host = new CustomTestHeadlessGameHost())
|
2020-07-09 14:08:03 +08:00
|
|
|
{
|
|
|
|
using (var storageConfig = new StorageConfigManager(host.InitialStorage))
|
2021-03-17 15:10:16 +08:00
|
|
|
storageConfig.SetValue(StorageConfig.FullPath, customPath);
|
2020-05-04 16:01:05 +08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-09-18 17:05:33 +08:00
|
|
|
var osu = LoadOsuIntoHost(host);
|
2020-05-04 16:01:05 +08:00
|
|
|
|
|
|
|
// switch to DI'd storage
|
2020-07-09 14:08:03 +08:00
|
|
|
var storage = osu.Dependencies.Get<Storage>();
|
2020-05-04 16:01:05 +08:00
|
|
|
|
2020-05-10 12:17:37 +08:00
|
|
|
string actualTestFile = Path.Combine(customPath, "rulesets", "test");
|
|
|
|
|
|
|
|
File.WriteAllText(actualTestFile, "test");
|
|
|
|
|
|
|
|
var rulesetStorage = storage.GetStorageForDirectory("rulesets");
|
2021-10-27 12:04:41 +08:00
|
|
|
string lookupPath = rulesetStorage.GetFiles(".").Single();
|
2020-05-10 12:17:37 +08:00
|
|
|
|
|
|
|
Assert.That(lookupPath, Is.EqualTo("test"));
|
2020-05-04 16:01:05 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 18:01:19 +08:00
|
|
|
[Test]
|
|
|
|
public void TestMigration()
|
|
|
|
{
|
2020-07-09 14:08:03 +08:00
|
|
|
string customPath = prepareCustomPath();
|
|
|
|
|
2020-09-17 15:12:30 +08:00
|
|
|
using (var host = new CustomTestHeadlessGameHost())
|
2020-05-07 18:01:19 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-07-09 14:08:03 +08:00
|
|
|
string defaultStorageLocation = getDefaultLocationFor(nameof(TestMigration));
|
|
|
|
|
2020-09-18 17:05:33 +08:00
|
|
|
var osu = LoadOsuIntoHost(host);
|
2020-05-07 18:01:19 +08:00
|
|
|
var storage = osu.Dependencies.Get<Storage>();
|
2020-06-25 05:01:56 +08:00
|
|
|
var osuStorage = storage as MigratableStorage;
|
2020-05-07 18:01:19 +08:00
|
|
|
|
2020-07-01 22:21:08 +08:00
|
|
|
// Store the current storage's path. We'll need to refer to this for assertions in the original directory after the migration completes.
|
|
|
|
string originalDirectory = storage.GetFullPath(".");
|
|
|
|
|
2020-05-07 18:01:19 +08:00
|
|
|
// ensure we perform a save
|
|
|
|
host.Dependencies.Get<FrameworkConfigManager>().Save();
|
|
|
|
|
|
|
|
// ensure we "use" cache
|
|
|
|
host.Storage.GetStorageForDirectory("cache");
|
|
|
|
|
2020-05-12 11:39:04 +08:00
|
|
|
// for testing nested files are not ignored (only top level)
|
|
|
|
host.Storage.GetStorageForDirectory("test-nested").GetStorageForDirectory("cache");
|
|
|
|
|
2020-05-07 18:01:19 +08:00
|
|
|
Assert.That(storage.GetFullPath("."), Is.EqualTo(defaultStorageLocation));
|
|
|
|
|
2020-05-11 20:37:07 +08:00
|
|
|
osu.Migrate(customPath);
|
2020-05-07 18:01:19 +08:00
|
|
|
|
|
|
|
Assert.That(storage.GetFullPath("."), Is.EqualTo(customPath));
|
|
|
|
|
2020-05-12 11:39:04 +08:00
|
|
|
// ensure cache was not moved
|
2020-07-01 22:21:08 +08:00
|
|
|
Assert.That(Directory.Exists(Path.Combine(originalDirectory, "cache")));
|
2020-05-12 11:39:04 +08:00
|
|
|
|
|
|
|
// ensure nested cache was moved
|
2020-07-01 22:21:08 +08:00
|
|
|
Assert.That(!Directory.Exists(Path.Combine(originalDirectory, "test-nested", "cache")));
|
2020-05-12 11:39:04 +08:00
|
|
|
Assert.That(storage.ExistsDirectory(Path.Combine("test-nested", "cache")));
|
|
|
|
|
2020-06-25 05:01:56 +08:00
|
|
|
Assert.That(osuStorage, Is.Not.Null);
|
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
foreach (string file in osuStorage.IgnoreFiles)
|
2020-06-25 05:01:56 +08:00
|
|
|
{
|
2021-04-26 14:37:19 +08:00
|
|
|
// avoid touching realm files which may be a pipe and break everything.
|
|
|
|
// this is also done locally inside OsuStorage via the IgnoreFiles list.
|
2021-01-21 21:57:55 +08:00
|
|
|
if (file.EndsWith(".ini", StringComparison.Ordinal))
|
|
|
|
Assert.That(File.Exists(Path.Combine(originalDirectory, file)));
|
2020-06-25 05:01:56 +08:00
|
|
|
Assert.That(storage.Exists(file), Is.False);
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
foreach (string dir in osuStorage.IgnoreDirectories)
|
2020-05-07 18:01:19 +08:00
|
|
|
{
|
2020-07-01 22:21:08 +08:00
|
|
|
Assert.That(Directory.Exists(Path.Combine(originalDirectory, dir)));
|
2020-06-25 05:01:56 +08:00
|
|
|
Assert.That(storage.ExistsDirectory(dir), Is.False);
|
2020-05-07 18:01:19 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 22:21:08 +08:00
|
|
|
Assert.That(new StreamReader(Path.Combine(originalDirectory, "storage.ini")).ReadToEnd().Contains($"FullPath = {customPath}"));
|
2020-05-04 16:01:05 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 19:13:37 +08:00
|
|
|
[Test]
|
|
|
|
public void TestMigrationBetweenTwoTargets()
|
|
|
|
{
|
2020-07-09 14:08:03 +08:00
|
|
|
string customPath = prepareCustomPath();
|
|
|
|
string customPath2 = prepareCustomPath("-2");
|
|
|
|
|
2020-09-17 15:12:30 +08:00
|
|
|
using (var host = new CustomTestHeadlessGameHost())
|
2020-05-09 19:13:37 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-18 17:05:33 +08:00
|
|
|
var osu = LoadOsuIntoHost(host);
|
2020-05-09 19:13:37 +08:00
|
|
|
|
|
|
|
const string database_filename = "client.db";
|
|
|
|
|
2020-05-11 20:37:07 +08:00
|
|
|
Assert.DoesNotThrow(() => osu.Migrate(customPath));
|
2020-05-09 19:13:37 +08:00
|
|
|
Assert.That(File.Exists(Path.Combine(customPath, database_filename)));
|
|
|
|
|
2020-05-11 20:37:07 +08:00
|
|
|
Assert.DoesNotThrow(() => osu.Migrate(customPath2));
|
2020-05-09 19:13:37 +08:00
|
|
|
Assert.That(File.Exists(Path.Combine(customPath2, database_filename)));
|
|
|
|
|
2021-07-09 11:21:24 +08:00
|
|
|
// some files may have been left behind for whatever reason, but that's not what we're testing here.
|
|
|
|
customPath = prepareCustomPath();
|
|
|
|
|
2020-05-11 20:37:07 +08:00
|
|
|
Assert.DoesNotThrow(() => osu.Migrate(customPath));
|
2020-05-09 19:13:37 +08:00
|
|
|
Assert.That(File.Exists(Path.Combine(customPath, database_filename)));
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestMigrationToSameTargetFails()
|
|
|
|
{
|
2020-07-09 14:08:03 +08:00
|
|
|
string customPath = prepareCustomPath();
|
|
|
|
|
2020-09-17 15:12:30 +08:00
|
|
|
using (var host = new CustomTestHeadlessGameHost())
|
2020-05-09 19:13:37 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-18 17:05:33 +08:00
|
|
|
var osu = LoadOsuIntoHost(host);
|
2020-05-09 19:13:37 +08:00
|
|
|
|
2020-05-11 20:37:07 +08:00
|
|
|
Assert.DoesNotThrow(() => osu.Migrate(customPath));
|
2020-05-14 19:18:57 +08:00
|
|
|
Assert.Throws<ArgumentException>(() => osu.Migrate(customPath));
|
2020-05-09 19:13:37 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 21:42:42 +08:00
|
|
|
[Test]
|
|
|
|
public void TestMigrationToNestedTargetFails()
|
|
|
|
{
|
2020-07-09 14:08:03 +08:00
|
|
|
string customPath = prepareCustomPath();
|
|
|
|
|
2020-09-17 15:12:30 +08:00
|
|
|
using (var host = new CustomTestHeadlessGameHost())
|
2020-05-14 21:42:42 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-18 17:05:33 +08:00
|
|
|
var osu = LoadOsuIntoHost(host);
|
2020-05-14 21:42:42 +08:00
|
|
|
|
|
|
|
Assert.DoesNotThrow(() => osu.Migrate(customPath));
|
|
|
|
|
|
|
|
string subFolder = Path.Combine(customPath, "sub");
|
|
|
|
|
2020-05-15 09:45:57 +08:00
|
|
|
if (Directory.Exists(subFolder))
|
|
|
|
Directory.Delete(subFolder, true);
|
|
|
|
|
2020-05-14 21:42:42 +08:00
|
|
|
Directory.CreateDirectory(subFolder);
|
|
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => osu.Migrate(subFolder));
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestMigrationToSeeminglyNestedTarget()
|
|
|
|
{
|
2020-07-09 14:08:03 +08:00
|
|
|
string customPath = prepareCustomPath();
|
|
|
|
|
2020-09-17 15:12:30 +08:00
|
|
|
using (var host = new CustomTestHeadlessGameHost())
|
2020-05-14 21:42:42 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-18 17:05:33 +08:00
|
|
|
var osu = LoadOsuIntoHost(host);
|
2020-05-14 21:42:42 +08:00
|
|
|
|
|
|
|
Assert.DoesNotThrow(() => osu.Migrate(customPath));
|
|
|
|
|
2020-05-15 09:45:57 +08:00
|
|
|
string seeminglySubFolder = customPath + "sub";
|
2020-05-14 21:42:42 +08:00
|
|
|
|
2020-05-15 09:45:57 +08:00
|
|
|
if (Directory.Exists(seeminglySubFolder))
|
|
|
|
Directory.Delete(seeminglySubFolder, true);
|
|
|
|
|
|
|
|
Directory.CreateDirectory(seeminglySubFolder);
|
2020-05-14 21:42:42 +08:00
|
|
|
|
2020-05-15 09:45:57 +08:00
|
|
|
osu.Migrate(seeminglySubFolder);
|
2020-05-14 21:42:42 +08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 14:08:03 +08:00
|
|
|
private static string getDefaultLocationFor(string testTypeName)
|
|
|
|
{
|
2021-08-18 14:32:59 +08:00
|
|
|
string path = Path.Combine(TestRunHeadlessGameHost.TemporaryTestDirectory, testTypeName);
|
2020-07-09 14:08:03 +08:00
|
|
|
|
|
|
|
if (Directory.Exists(path))
|
|
|
|
Directory.Delete(path, true);
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string prepareCustomPath(string suffix = "")
|
|
|
|
{
|
2021-08-18 14:32:59 +08:00
|
|
|
string path = Path.Combine(TestRunHeadlessGameHost.TemporaryTestDirectory, $"custom-path{suffix}");
|
2020-07-09 14:08:03 +08:00
|
|
|
|
|
|
|
if (Directory.Exists(path))
|
|
|
|
Directory.Delete(path, true);
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2020-09-17 15:12:30 +08:00
|
|
|
public class CustomTestHeadlessGameHost : CleanRunHeadlessGameHost
|
2020-07-09 14:08:03 +08:00
|
|
|
{
|
|
|
|
public Storage InitialStorage { get; }
|
|
|
|
|
2020-09-17 15:12:30 +08:00
|
|
|
public CustomTestHeadlessGameHost([CallerMemberName] string callingMethodName = @"")
|
|
|
|
: base(callingMethodName: callingMethodName)
|
2020-07-09 14:08:03 +08:00
|
|
|
{
|
2020-09-17 15:12:30 +08:00
|
|
|
string defaultStorageLocation = getDefaultLocationFor(callingMethodName);
|
2020-07-09 14:08:03 +08:00
|
|
|
|
|
|
|
InitialStorage = new DesktopStorage(defaultStorageLocation, this);
|
|
|
|
InitialStorage.DeleteDirectory(string.Empty);
|
|
|
|
}
|
2021-08-18 14:32:59 +08:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
2021-08-20 21:07:13 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// the storage may have changed from the initial location.
|
|
|
|
// this handles cleanup of the initial location.
|
|
|
|
InitialStorage.DeleteDirectory(string.Empty);
|
|
|
|
}
|
|
|
|
catch { }
|
2021-08-18 14:32:59 +08:00
|
|
|
}
|
2020-07-09 14:08:03 +08:00
|
|
|
}
|
2020-05-04 16:01:05 +08:00
|
|
|
}
|
|
|
|
}
|