Repository navigation
fix(tests): keep temporary Play Mode options out of EditorSettings.asset #1481
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
Open
pukibopu
wants to merge
2
commits into
CoplayDev:beta
Choose a base branch
from
pukibopu:fix/persist-restored-play-mode-options
base: beta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−0
Open
Changes from all commits
Commits
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
110 changes: 110 additions & 0 deletions
110
...cts/UnityMCPTests/Assets/Tests/EditMode/Services/PlayModeOptionsGuardSettingsFileTests.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,110 @@ | ||
| using System.IO; | ||
| using MCPForUnity.Editor.Services; | ||
| using NUnit.Framework; | ||
| using UnityEditor; | ||
|
|
||
| namespace MCPForUnityTests.Editor.Services | ||
| { | ||
| [TestFixture] | ||
| public class PlayModeOptionsGuardSettingsFileTests | ||
| { | ||
| private const string SettingsPath = "ProjectSettings/EditorSettings.asset"; | ||
| private const string GuardPrefix = "MCPForUnity.PlayModeOptions."; | ||
| private const string MarkerPath = "Library/MCPPlayModeOptionsBackup.txt"; | ||
|
|
||
| private bool _originalEnabled; | ||
| private EnterPlayModeOptions _originalOptions; | ||
| private byte[] _settingsFile; | ||
| private bool _guardPending; | ||
| private bool _guardEnabled; | ||
| private int _guardOptions; | ||
| private byte[] _marker; | ||
|
|
||
| private static string Settings(int enabled, int options, string newline = "\n") => | ||
| "%YAML 1.1" + newline + | ||
| "--- !u!159 &1" + newline + | ||
| "EditorSettings:" + newline + | ||
| " m_EnterPlayModeOptionsEnabled: " + enabled + newline + | ||
| " m_EnterPlayModeOptions: " + options + newline + | ||
| " m_GameObjectNamingDigits: 1" + newline; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| _originalEnabled = EditorSettings.enterPlayModeOptionsEnabled; | ||
| _originalOptions = EditorSettings.enterPlayModeOptions; | ||
| _settingsFile = File.Exists(SettingsPath) ? File.ReadAllBytes(SettingsPath) : null; | ||
| _guardPending = SessionState.GetBool(GuardPrefix + "PendingRestore", false); | ||
| _guardEnabled = SessionState.GetBool(GuardPrefix + "OriginalEnabled", false); | ||
| _guardOptions = SessionState.GetInt(GuardPrefix + "OriginalOptions", 0); | ||
| _marker = File.Exists(MarkerPath) ? File.ReadAllBytes(MarkerPath) : null; | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown() | ||
| { | ||
| EditorSettings.enterPlayModeOptions = _originalOptions; | ||
| EditorSettings.enterPlayModeOptionsEnabled = _originalEnabled; | ||
| if (_settingsFile != null) File.WriteAllBytes(SettingsPath, _settingsFile); | ||
| SessionState.SetBool(GuardPrefix + "PendingRestore", _guardPending); | ||
| SessionState.SetBool(GuardPrefix + "OriginalEnabled", _guardEnabled); | ||
| SessionState.SetInt(GuardPrefix + "OriginalOptions", _guardOptions); | ||
| if (_marker == null) | ||
| { | ||
| if (File.Exists(MarkerPath)) File.Delete(MarkerPath); | ||
| } | ||
| else File.WriteAllBytes(MarkerPath, _marker); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RevertTemporaryValues_RestoresBothTemporaryValues() | ||
| { | ||
| string reverted = PlayModeOptionsGuard.RevertTemporaryValues(Settings(1, 1), false, EnterPlayModeOptions.None); | ||
|
|
||
| Assert.AreEqual(Settings(0, 0), reverted); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RevertTemporaryValues_WhenOnlyDomainReloadWasAdded_RestoresOptionsOnly() | ||
| { | ||
| string reverted = PlayModeOptionsGuard.RevertTemporaryValues( | ||
| Settings(1, 3), true, EnterPlayModeOptions.DisableSceneReload); | ||
|
|
||
| Assert.AreEqual(Settings(1, 2), reverted); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RevertTemporaryValues_LeavesValuesItDidNotSet() | ||
| { | ||
| string settings = Settings(0, 2); | ||
|
|
||
| Assert.AreEqual(settings, PlayModeOptionsGuard.RevertTemporaryValues(settings, false, EnterPlayModeOptions.None)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RevertTemporaryValues_KeepsLineEndings() | ||
| { | ||
| string reverted = PlayModeOptionsGuard.RevertTemporaryValues(Settings(1, 1, "\r\n"), false, EnterPlayModeOptions.None); | ||
|
|
||
| Assert.AreEqual(Settings(0, 0, "\r\n"), reverted); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Restore_AfterAssetsWereSavedWithTemporaryValues_WritesOriginalsToDisk() | ||
| { | ||
| PlayModeOptionsGuard.Save(false, EnterPlayModeOptions.None); | ||
| EditorSettings.enterPlayModeOptionsEnabled = true; | ||
| EditorSettings.enterPlayModeOptions = EnterPlayModeOptions.DisableDomainReload; | ||
| AssetDatabase.SaveAssets(); | ||
| StringAssert.Contains("m_EnterPlayModeOptionsEnabled: 1", File.ReadAllText(SettingsPath)); | ||
|
|
||
| PlayModeOptionsGuard.Restore(); | ||
|
|
||
| string onDisk = File.ReadAllText(SettingsPath); | ||
| StringAssert.Contains("m_EnterPlayModeOptionsEnabled: 0", onDisk); | ||
| StringAssert.Contains("m_EnterPlayModeOptions: 0", onDisk); | ||
| Assert.IsFalse(EditorSettings.enterPlayModeOptionsEnabled); | ||
| Assert.AreEqual(EnterPlayModeOptions.None, EditorSettings.enterPlayModeOptions); | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...nityMCPTests/Assets/Tests/EditMode/Services/PlayModeOptionsGuardSettingsFileTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.