Repository navigation
fix: Interrupted PlayMode tests no longer leave domain reload disabled #1254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
433ca1d
Restore interrupted domain reload settings
hatayama 3a616a6
Handle abandoned domain reload scopes before new runs
hatayama ae80366
Delete invalid domain reload recovery markers
hatayama 6a58b17
Reject unrecovered domain reload recovery markers
hatayama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| using NUnit.Framework; | ||
| using UnityEditor; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools; | ||
|
|
||
| using io.github.hatayama.UnityCliLoop.FirstPartyTools; | ||
|
|
||
| namespace io.github.hatayama.UnityCliLoop.Tests.Editor | ||
| { | ||
| /// <summary> | ||
| /// Test fixture that verifies Enter Play Mode settings recovery for DomainReloadDisableScope. | ||
| /// </summary> | ||
| public sealed class DomainReloadDisableScopeTests | ||
| { | ||
| private static readonly string MarkerFilePath = DomainReloadDisableScopeRecovery.MarkerFilePathForTests; | ||
| private static readonly string TempFilePath = DomainReloadDisableScopeRecovery.TempFilePathForTests; | ||
|
|
||
| private bool _originalEnabled; | ||
| private EnterPlayModeOptions _originalOptions; | ||
| private bool _markerFileExisted; | ||
| private string _markerFileContent; | ||
| private bool _tempFileExisted; | ||
| private string _tempFileContent; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| _originalEnabled = EditorSettings.enterPlayModeOptionsEnabled; | ||
| _originalOptions = EditorSettings.enterPlayModeOptions; | ||
| _markerFileExisted = File.Exists(MarkerFilePath); | ||
| _markerFileContent = _markerFileExisted ? File.ReadAllText(MarkerFilePath) : string.Empty; | ||
| _tempFileExisted = File.Exists(TempFilePath); | ||
| _tempFileContent = _tempFileExisted ? File.ReadAllText(TempFilePath) : string.Empty; | ||
|
|
||
| DomainReloadDisableScope.ResetActiveScopeCountForTests(); | ||
| DomainReloadDisableScopeRecovery.ClearPendingRestoreForTests(); | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown() | ||
| { | ||
| EditorSettings.enterPlayModeOptionsEnabled = _originalEnabled; | ||
| EditorSettings.enterPlayModeOptions = _originalOptions; | ||
| DomainReloadDisableScope.ResetActiveScopeCountForTests(); | ||
| DomainReloadDisableScopeRecovery.ClearPendingRestoreForTests(); | ||
| RestoreFile(MarkerFilePath, _markerFileExisted, _markerFileContent); | ||
| RestoreFile(TempFilePath, _tempFileExisted, _tempFileContent); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Dispose_RestoresOriginalSettingsAndDeletesMarkerFile() | ||
| { | ||
| // Verifies that a completed scope restores settings and removes its recovery marker. | ||
| SetEnterPlayModeSettings(false, EnterPlayModeOptions.None); | ||
|
|
||
| using (DomainReloadDisableScope scope = new DomainReloadDisableScope()) | ||
| { | ||
| Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.True); | ||
| Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.DisableDomainReload)); | ||
| Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.True); | ||
| } | ||
|
|
||
| Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.False); | ||
| Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.None)); | ||
| Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.False); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Dispose_KeepsDomainReloadDisabled_UntilLastNestedScopeDisposes() | ||
| { | ||
| // Verifies that nested scopes restore settings only after the final scope exits. | ||
| SetEnterPlayModeSettings(false, EnterPlayModeOptions.None); | ||
|
|
||
| DomainReloadDisableScope outerScope = new DomainReloadDisableScope(); | ||
| DomainReloadDisableScope innerScope = new DomainReloadDisableScope(); | ||
|
|
||
| innerScope.Dispose(); | ||
|
|
||
| Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.True); | ||
| Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.DisableDomainReload)); | ||
| Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.True); | ||
|
|
||
| outerScope.Dispose(); | ||
|
|
||
| Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.False); | ||
| Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.None)); | ||
| Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.False); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RestoreIfPending_RestoresOriginalSettings_WhenScopeWasAbandoned() | ||
| { | ||
| // Verifies that a marker left by an abandoned scope can restore the original settings. | ||
| SetEnterPlayModeSettings(false, EnterPlayModeOptions.None); | ||
|
|
||
| DomainReloadDisableScope scope = new DomainReloadDisableScope(); | ||
| Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.True); | ||
| Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.DisableDomainReload)); | ||
|
|
||
| DomainReloadDisableScopeRecovery.RestoreIfPending(); | ||
|
|
||
| Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.False); | ||
| Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.None)); | ||
| Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.False); | ||
| System.GC.KeepAlive(scope); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Constructor_RestoresStaleMarker_BeforeSavingNewRunMarker() | ||
| { | ||
| // Verifies that stale recovery state is consumed before the next run records its baseline. | ||
| SetEnterPlayModeSettings(false, EnterPlayModeOptions.None); | ||
|
|
||
| DomainReloadDisableScope abandonedScope = new DomainReloadDisableScope(); | ||
| Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.True); | ||
| DomainReloadDisableScope.RecoverAbandonedScopeBeforeNewRun(); | ||
|
|
||
| DomainReloadDisableScope nextScope = new DomainReloadDisableScope(); | ||
| DomainReloadDisableScopeRecoveryData markerData = DomainReloadDisableScopeRecovery.ReadMarkerDataForTests(); | ||
|
|
||
| Assert.That(markerData.originalOptionsEnabled, Is.False); | ||
| Assert.That(markerData.originalOptions, Is.EqualTo((int)EnterPlayModeOptions.None)); | ||
|
|
||
| nextScope.Dispose(); | ||
|
|
||
| Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.False); | ||
| Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.None)); | ||
| Assert.That(DomainReloadDisableScopeRecovery.HasPendingRestoreForTests(), Is.False); | ||
| System.GC.KeepAlive(abandonedScope); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RestoreIfPending_LeavesNoMarkerFile_AfterSuccessfulRestore() | ||
| { | ||
| // Verifies that successful recovery deletes the marker instead of saving a cleared state. | ||
| SetEnterPlayModeSettings(false, EnterPlayModeOptions.None); | ||
| DomainReloadDisableScope scope = new DomainReloadDisableScope(); | ||
|
|
||
| DomainReloadDisableScopeRecovery.RestoreIfPending(); | ||
|
|
||
| Assert.That(File.Exists(MarkerFilePath), Is.False); | ||
| Assert.That(File.Exists(TempFilePath), Is.False); | ||
| System.GC.KeepAlive(scope); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RestoreIfPending_RejectsUnsupportedEnterPlayModeOptionBits() | ||
| { | ||
| // Verifies that corrupted markers cannot write unsupported option bits into EditorSettings. | ||
| SetEnterPlayModeSettings(true, EnterPlayModeOptions.DisableDomainReload); | ||
| string json = "{ \"originalOptionsEnabled\": false, \"originalOptions\": 1024 }"; | ||
| File.WriteAllText(MarkerFilePath, json); | ||
|
|
||
| InvalidOperationException exception = | ||
| Assert.Throws<InvalidOperationException>(() => DomainReloadDisableScopeRecovery.RestoreIfPending()); | ||
|
|
||
| Assert.That(exception.Message, Does.Contain("supported Enter Play Mode option bits")); | ||
| Assert.That(EditorSettings.enterPlayModeOptionsEnabled, Is.True); | ||
| Assert.That(EditorSettings.enterPlayModeOptions, Is.EqualTo(EnterPlayModeOptions.DisableDomainReload)); | ||
| Assert.That(File.Exists(MarkerFilePath), Is.False); | ||
| } | ||
|
|
||
| [Test] | ||
| public void SaveCurrentSettings_ThrowsAndKeepsExistingMarker_WhenMarkerAlreadyExists() | ||
| { | ||
| // Verifies that saving refuses to replace an unrecovered marker. | ||
| string directoryPath = Path.GetDirectoryName(MarkerFilePath); | ||
| if (!string.IsNullOrEmpty(directoryPath)) | ||
| { | ||
| Directory.CreateDirectory(directoryPath); | ||
| } | ||
|
|
||
| string existingJson = "{ \"originalOptionsEnabled\": true, \"originalOptions\": 1 }"; | ||
| File.WriteAllText(MarkerFilePath, existingJson); | ||
|
|
||
| LogAssert.Expect(LogType.Assert, "recovery marker must be restored before saving a new marker"); | ||
| InvalidOperationException exception = | ||
| Assert.Throws<InvalidOperationException>(() => DomainReloadDisableScopeRecovery.SaveCurrentSettings()); | ||
|
|
||
| Assert.That(exception.Message, Does.Contain("must be restored before saving")); | ||
| Assert.That(File.ReadAllText(MarkerFilePath), Is.EqualTo(existingJson)); | ||
| Assert.That(File.Exists(TempFilePath), Is.False); | ||
| } | ||
|
|
||
| private static void SetEnterPlayModeSettings(bool enabled, EnterPlayModeOptions options) | ||
| { | ||
| EditorSettings.enterPlayModeOptionsEnabled = enabled; | ||
| EditorSettings.enterPlayModeOptions = options; | ||
| } | ||
|
|
||
| private static void RestoreFile(string filePath, bool fileExisted, string fileContent) | ||
| { | ||
| if (!fileExisted) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| string directoryPath = Path.GetDirectoryName(filePath); | ||
| if (!string.IsNullOrEmpty(directoryPath)) | ||
| { | ||
| Directory.CreateDirectory(directoryPath); | ||
| } | ||
|
|
||
| File.WriteAllText(filePath, fileContent); | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
Packages/src/Editor/FirstPartyTools/Common/EditorUtility/AssemblyInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| [assembly: InternalsVisibleTo("UnityCLILoop.FirstPartyTools.Editor")] | ||
| [assembly: InternalsVisibleTo("UnityCLILoop.FirstPartyTools.RunTests.TestFramework.Editor")] | ||
| [assembly: InternalsVisibleTo("UnityCLILoop.Tests.Editor")] | ||
11 changes: 11 additions & 0 deletions
11
Packages/src/Editor/FirstPartyTools/Common/EditorUtility/AssemblyInfo.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.