Class ScratchAssetFixture
One scratch-asset fixture for EditMode tests that create real assets under a throwaway folder and must
leave nothing behind: a folder to create assets in, a GUID ledger for the Addressables entries those
assets may pick up, and a delete pass that removes both. Replaces the ~60-line "scratch settings asset"
fixture ten test suites used to hand-roll on their own — UnitySingletonSettingsLoaderTests,
UnityCollectionSettingsLoaderTests, WiredOrDiscoveredSettingsTests,
SystemAdoptionRunnerTests, ProjectWiringPinResolutionTests,
AdoptedReferenceRemapperTests, AdoptedReferenceEndToEndTests,
SerenityAdoptedReferenceValidatorTests, FoundationReferenceIndexTests and
SerenityPackageContentRelocatorTests.
A test owns one instance per [SetUp]/[TearDown] pair, constructed with every scratch
folder the test will use (so a crashed previous run's leftovers are still reachable before this test
creates anything), and disposes it in [TearDown]:
private ScratchAssetFixture _fixture;
[SetUp]
public void SetUp()
{
_fixture = new ScratchAssetFixture(PackageScratchFolder, ProjectScratchFolder);
_fixture.DeleteScratchFolders();
}
[TearDown]
public void TearDown() => _fixture.Dispose();
UnityAddressablesLabeler.RetagAssets is internal to the Global editor assembly and only
Tests.Editor carries an InternalsVisibleTo grant for it, not this fixture's own
Tests assembly — so retagging a created asset as Addressable stays one call at the test's own
call site, right after CreateSettingsAsset<TSettings>(string, string, string, string) or SaveAsset(Object, string, string, string) returns
its path.
Inheritance
ScratchAssetFixture
Assembly: Tests.dll
Syntax
public sealed class ScratchAssetFixture : IDisposable
Constructors
ScratchAssetFixture(params string[])
Declaration
public ScratchAssetFixture(params string[] scratchFolders)
Parameters
| Type |
Name |
Description |
| string[] |
scratchFolders |
Every folder this test will create assets under. Known upfront so a
fresh instance can clear a previous run's leftovers before this test creates anything.
|
Fields
DefaultAssetFileExtension
Declaration
public const string DefaultAssetFileExtension = ".asset"
Field Value
Properties
ScratchFolders
Declaration
public IReadOnlyCollection<string> ScratchFolders { get; }
Property Value
TrackedGuids
Declaration
public IReadOnlyList<string> TrackedGuids { get; }
Property Value
Methods
CreateSettingsAsset<TSettings>(string, string, string, string)
Creates a real TSettings asset with the given Id/Guid under
folderPath, saves and refreshes the AssetDatabase, and tracks its GUID. Does not
register the asset as Addressable — call UnityAddressablesLabeler.RetagAssets at the
call site afterward when the test needs the fixture registered.
Declaration
public string CreateSettingsAsset<TSettings>(string folderPath, string fileName, string id, string guidValue) where TSettings : ScriptableObject, IFoundationSettings
Parameters
Returns
Type Parameters
| Name |
Description |
| TSettings |
|
DeleteScratchFolders()
Deletes every scratch folder this fixture has touched, wrapped in
Serenity.Global.Infrastructure.Editor.Validation.References.SerenitySafeDeleteScope.Suppressed so a legitimate scratch-asset delete does not trip
the safe-delete guard. Retries up to Serenity.Tests.Shared.Fakes.ScratchAssetFixture.DeleteFolderMaxAttempts times with an
UnityEditor.AssetDatabase.Refresh between attempts: this is the one place that absorbs the
temp-folder .meta write-lock flake (observed as Defender still holding a freshly written
.meta file for a moment after UnityEditor.AssetDatabase.DeleteAsset(System.String) returns). If a folder
still survives every attempt, this throws an IOException naming it instead of leaving
it behind for the next test's [SetUp] to also fail to clear — a leaked scratch folder must
fail the test that leaked it.
Declaration
public void DeleteScratchFolders()
Dispose()
Declaration
EnsureFolder(string)
Creates the folder (and every missing ancestor) if it does not already exist, and tracks
it for DeleteScratchFolders().
Declaration
public void EnsureFolder(string folderPath)
Parameters
| Type |
Name |
Description |
| string |
folderPath |
|
SaveAsset(Object, string, string, string)
Creates folderPath, writes asset under it as
fileName + assetFileExtension, and tracks the resulting GUID. The caller is expected to have
already set every field the created asset needs — this only places it on disk.
Declaration
public string SaveAsset(Object asset, string folderPath, string fileName, string assetFileExtension = ".asset")
Parameters
| Type |
Name |
Description |
| Object |
asset |
|
| string |
folderPath |
|
| string |
fileName |
|
| string |
assetFileExtension |
|
Returns
TrackGuid(string)
Declaration
public void TrackGuid(string assetPath)
Parameters
| Type |
Name |
Description |
| string |
assetPath |
|
TrackGuids(IEnumerable<string>)
Declaration
public void TrackGuids(IEnumerable<string> assetPaths)
Parameters
Implements