1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 19:42:55 +08:00

Fix incorrect relative paths when using GetStorageForDirectory

This commit is contained in:
Dean Herbert 2020-05-10 13:17:37 +09:00
parent 56f17fa64f
commit 384862d48b
2 changed files with 53 additions and 4 deletions

View File

@ -3,6 +3,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
@ -36,6 +37,8 @@ namespace osu.Game.Tests.NonVisual
} }
} }
private string customPath => Path.Combine(Environment.CurrentDirectory, "custom-path");
[Test] [Test]
public void TestCustomDirectory() public void TestCustomDirectory()
{ {
@ -49,7 +52,7 @@ namespace osu.Game.Tests.NonVisual
storage.DeleteDirectory(string.Empty); storage.DeleteDirectory(string.Empty);
using (var storageConfig = new StorageConfigManager(storage)) using (var storageConfig = new StorageConfigManager(storage))
storageConfig.Set(StorageConfig.FullPath, Path.Combine(Environment.CurrentDirectory, "custom-path")); storageConfig.Set(StorageConfig.FullPath, customPath);
try try
{ {
@ -58,7 +61,45 @@ namespace osu.Game.Tests.NonVisual
// switch to DI'd storage // switch to DI'd storage
storage = osu.Dependencies.Get<Storage>(); storage = osu.Dependencies.Get<Storage>();
Assert.That(storage.GetFullPath("."), Is.EqualTo(Path.Combine(Environment.CurrentDirectory, "custom-path"))); Assert.That(storage.GetFullPath("."), Is.EqualTo(customPath));
}
finally
{
host.Exit();
}
}
}
[Test]
public void TestSubDirectoryLookup()
{
using (var host = new HeadlessGameHost(nameof(TestSubDirectoryLookup)))
{
string headlessPrefix = Path.Combine("headless", nameof(TestSubDirectoryLookup));
// need access before the game has constructed its own storage yet.
Storage storage = new DesktopStorage(headlessPrefix, host);
// manual cleaning so we can prepare a config file.
storage.DeleteDirectory(string.Empty);
using (var storageConfig = new StorageConfigManager(storage))
storageConfig.Set(StorageConfig.FullPath, customPath);
try
{
var osu = loadOsu(host);
// switch to DI'd storage
storage = osu.Dependencies.Get<Storage>();
string actualTestFile = Path.Combine(customPath, "rulesets", "test");
File.WriteAllText(actualTestFile, "test");
var rulesetStorage = storage.GetStorageForDirectory("rulesets");
var lookupPath = rulesetStorage.GetFiles(".").Single();
Assert.That(lookupPath, Is.EqualTo("test"));
} }
finally finally
{ {

View File

@ -48,10 +48,18 @@ namespace osu.Game.IO
UnderlyingStorage.Delete(MutatePath(path)); UnderlyingStorage.Delete(MutatePath(path));
public override IEnumerable<string> GetDirectories(string path) => public override IEnumerable<string> GetDirectories(string path) =>
UnderlyingStorage.GetDirectories(MutatePath(path)); ToLocalRelative(UnderlyingStorage.GetDirectories(MutatePath(path)));
public IEnumerable<string> ToLocalRelative(IEnumerable<string> paths)
{
string localRoot = GetFullPath(string.Empty);
foreach (var path in paths)
yield return Path.GetRelativePath(localRoot, UnderlyingStorage.GetFullPath(path));
}
public override IEnumerable<string> GetFiles(string path, string pattern = "*") => public override IEnumerable<string> GetFiles(string path, string pattern = "*") =>
UnderlyingStorage.GetFiles(MutatePath(path), pattern); ToLocalRelative(UnderlyingStorage.GetFiles(MutatePath(path), pattern));
public override Stream GetStream(string path, FileAccess access = FileAccess.Read, FileMode mode = FileMode.OpenOrCreate) => public override Stream GetStream(string path, FileAccess access = FileAccess.Read, FileMode mode = FileMode.OpenOrCreate) =>
UnderlyingStorage.GetStream(MutatePath(path), access, mode); UnderlyingStorage.GetStream(MutatePath(path), access, mode);