1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 13:32:54 +08:00

Merge pull request #14777 from peppy/osz-tests-use-temp-folder

Fix a couple of remaining temp file edge cases in tests
This commit is contained in:
Dan Balasescu 2021-09-17 18:57:46 +09:00 committed by GitHub
commit c87a48bd4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 7 deletions

View File

@ -424,14 +424,14 @@ namespace osu.Game.Tests.Beatmaps.IO
checkBeatmapCount(osu, 12); checkBeatmapCount(osu, 12);
checkSingleReferencedFileCount(osu, 18); checkSingleReferencedFileCount(osu, 18);
var breakTemp = TestResources.GetTestBeatmapForImport(); var brokenTempFilename = TestResources.GetTestBeatmapForImport();
MemoryStream brokenOsu = new MemoryStream(); MemoryStream brokenOsu = new MemoryStream();
MemoryStream brokenOsz = new MemoryStream(await File.ReadAllBytesAsync(breakTemp)); MemoryStream brokenOsz = new MemoryStream(await File.ReadAllBytesAsync(brokenTempFilename));
File.Delete(breakTemp); File.Delete(brokenTempFilename);
using (var outStream = File.Open(breakTemp, FileMode.CreateNew)) using (var outStream = File.Open(brokenTempFilename, FileMode.CreateNew))
using (var zip = ZipArchive.Open(brokenOsz)) using (var zip = ZipArchive.Open(brokenOsz))
{ {
zip.AddEntry("broken.osu", brokenOsu, false); zip.AddEntry("broken.osu", brokenOsu, false);
@ -441,7 +441,7 @@ namespace osu.Game.Tests.Beatmaps.IO
// this will trigger purging of the existing beatmap (online set id match) but should rollback due to broken osu. // this will trigger purging of the existing beatmap (online set id match) but should rollback due to broken osu.
try try
{ {
await manager.Import(new ImportTask(breakTemp)); await manager.Import(new ImportTask(brokenTempFilename));
} }
catch catch
{ {
@ -456,6 +456,8 @@ namespace osu.Game.Tests.Beatmaps.IO
checkSingleReferencedFileCount(osu, 18); checkSingleReferencedFileCount(osu, 18);
Assert.AreEqual(1, loggedExceptionCount); Assert.AreEqual(1, loggedExceptionCount);
File.Delete(brokenTempFilename);
} }
finally finally
{ {

View File

@ -1,9 +1,11 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.IO; using System.IO;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.IO.Stores; using osu.Framework.IO.Stores;
using osu.Framework.Testing;
namespace osu.Game.Tests.Resources namespace osu.Game.Tests.Resources
{ {
@ -11,6 +13,8 @@ namespace osu.Game.Tests.Resources
{ {
public const double QUICK_BEATMAP_LENGTH = 10000; public const double QUICK_BEATMAP_LENGTH = 10000;
private static readonly TemporaryNativeStorage temp_storage = new TemporaryNativeStorage("TestResources");
public static DllResourceStore GetStore() => new DllResourceStore(typeof(TestResources).Assembly); public static DllResourceStore GetStore() => new DllResourceStore(typeof(TestResources).Assembly);
public static Stream OpenResource(string name) => GetStore().GetStream($"Resources/{name}"); public static Stream OpenResource(string name) => GetStore().GetStream($"Resources/{name}");
@ -25,7 +29,7 @@ namespace osu.Game.Tests.Resources
/// <returns>A path to a copy of a beatmap archive (osz). Should be deleted after use.</returns> /// <returns>A path to a copy of a beatmap archive (osz). Should be deleted after use.</returns>
public static string GetQuickTestBeatmapForImport() public static string GetQuickTestBeatmapForImport()
{ {
var tempPath = Path.GetTempFileName() + ".osz"; var tempPath = getTempFilename();
using (var stream = OpenResource("Archives/241526 Soleily - Renatus_virtual_quick.osz")) using (var stream = OpenResource("Archives/241526 Soleily - Renatus_virtual_quick.osz"))
using (var newFile = File.Create(tempPath)) using (var newFile = File.Create(tempPath))
stream.CopyTo(newFile); stream.CopyTo(newFile);
@ -41,7 +45,7 @@ namespace osu.Game.Tests.Resources
/// <returns>A path to a copy of a beatmap archive (osz). Should be deleted after use.</returns> /// <returns>A path to a copy of a beatmap archive (osz). Should be deleted after use.</returns>
public static string GetTestBeatmapForImport(bool virtualTrack = false) public static string GetTestBeatmapForImport(bool virtualTrack = false)
{ {
var tempPath = Path.GetTempFileName() + ".osz"; var tempPath = getTempFilename();
using (var stream = GetTestBeatmapStream(virtualTrack)) using (var stream = GetTestBeatmapStream(virtualTrack))
using (var newFile = File.Create(tempPath)) using (var newFile = File.Create(tempPath))
@ -50,5 +54,7 @@ namespace osu.Game.Tests.Resources
Assert.IsTrue(File.Exists(tempPath)); Assert.IsTrue(File.Exists(tempPath));
return tempPath; return tempPath;
} }
private static string getTempFilename() => temp_storage.GetFullPath(Guid.NewGuid() + ".osz");
} }
} }