From 18c97e70ee3c06a4a0d54e630219e63ce61d8666 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Sat, 2 Nov 2024 04:30:28 +0900 Subject: [PATCH 1/5] Fix using obsolete property refs #92 --- Runtime/Autopilot.cs | 4 ++-- Runtime/Settings/AutopilotSettings.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Runtime/Autopilot.cs b/Runtime/Autopilot.cs index 70e60a0..027bf08 100644 --- a/Runtime/Autopilot.cs +++ b/Runtime/Autopilot.cs @@ -134,9 +134,9 @@ public async UniTask TerminateAsync(ExitCode exitCode, string message = null, st _isTerminating = true; - if (reporting && _state.IsRunning && _settings.reporter != null) + if (reporting && _state.IsRunning && _settings.Reporter != null) { - await _settings.reporter.PostReportAsync(message, stackTrace, exitCode, token); + await _settings.Reporter.PostReportAsync(message, stackTrace, exitCode, token); } if (_state.settings != null && !string.IsNullOrEmpty(_state.settings.junitReportPath)) diff --git a/Runtime/Settings/AutopilotSettings.cs b/Runtime/Settings/AutopilotSettings.cs index 5f5c52c..34ed78e 100644 --- a/Runtime/Settings/AutopilotSettings.cs +++ b/Runtime/Settings/AutopilotSettings.cs @@ -132,7 +132,7 @@ public class AutopilotSettings : ScriptableObject /// /// Logger used for this autopilot settings. /// - [Obsolete("Use `loggerAssets` field or `LoggerAsset` property instead")] + [Obsolete("Use `LoggerAsset` property instead")] public AbstractLoggerAsset loggerAsset; /// @@ -163,7 +163,7 @@ public CompositeLoggerAsset LoggerAsset /// /// Reporter that called when some errors occurred /// - [Obsolete("Use `reporters` field or `Reporter` property instead")] + [Obsolete("Use `Reporter` property instead")] public AbstractReporter reporter; /// From 40f774a5f8b3dbe8e83ed17fb50c1e77be29b8e1 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 5 Nov 2024 00:46:31 +0900 Subject: [PATCH 2/5] Add SaveConvertedObject method --- Runtime/Settings/AutopilotSettings.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Runtime/Settings/AutopilotSettings.cs b/Runtime/Settings/AutopilotSettings.cs index 34ed78e..edef953 100644 --- a/Runtime/Settings/AutopilotSettings.cs +++ b/Runtime/Settings/AutopilotSettings.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using DeNA.Anjin.Agents; using DeNA.Anjin.Attributes; @@ -10,6 +11,10 @@ using DeNA.Anjin.Reporters; using UnityEngine; using UnityEngine.Assertions; +using Object = UnityEngine.Object; +#if UNITY_EDITOR +using UnityEditor; +#endif namespace DeNA.Anjin.Settings { @@ -314,5 +319,19 @@ internal void ConvertSlackReporterFromObsoleteSlackSettings(ILogger logger) convertedReporter.addHereInSlackMessage = this.addHereInSlackMessage; this.reporters.Add(convertedReporter); } + + private void SaveConvertedObject(Object obj) + { +#if UNITY_EDITOR + var settingsPath = AssetDatabase.GetAssetPath(this); + if (string.IsNullOrEmpty(settingsPath)) + { + return; + } + + var dir = Path.GetDirectoryName(settingsPath) ?? "Assets"; + AssetDatabase.CreateAsset(obj, Path.Combine(dir, $"New {obj.GetType().Name}.asset")); +#endif + } } } From 9835d6c88a7b4800b805b1fe5ba9aa1ea927a864 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 5 Nov 2024 04:48:06 +0900 Subject: [PATCH 3/5] Fix Loggers and Reporters convert, set to field directly --- Runtime/Settings/AutopilotSettings.cs | 6 ++---- Tests/Runtime/Settings/AutopilotSettingsTest.cs | 10 ++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/Runtime/Settings/AutopilotSettings.cs b/Runtime/Settings/AutopilotSettings.cs index edef953..adb3bf1 100644 --- a/Runtime/Settings/AutopilotSettings.cs +++ b/Runtime/Settings/AutopilotSettings.cs @@ -279,8 +279,7 @@ internal void ConvertLoggersFromObsoleteLogger() Please delete the reference using Debug Mode in the Inspector window. And add to the list Loggers. This time, temporarily converting."); - this.LoggerAsset.loggerAssets = new List { this.loggerAsset }; - // not change field directly. + this.loggerAssets.Add(this.loggerAsset); } [Obsolete("Remove this method when bump major version")] @@ -295,8 +294,7 @@ internal void ConvertReportersFromObsoleteReporter(ILogger logger) Please delete the reference using Debug Mode in the Inspector window. And add to the list Reporters. This time, temporarily converting."); - this.Reporter.reporters = new List { this.reporter }; - // not change field directly. + this.reporters.Add(this.reporter); } [Obsolete("Remove this method when bump major version")] diff --git a/Tests/Runtime/Settings/AutopilotSettingsTest.cs b/Tests/Runtime/Settings/AutopilotSettingsTest.cs index 26ad16b..d26e64c 100644 --- a/Tests/Runtime/Settings/AutopilotSettingsTest.cs +++ b/Tests/Runtime/Settings/AutopilotSettingsTest.cs @@ -98,9 +98,8 @@ public void ConvertLoggersFromObsoleteLogger_HasLogger_IncludeToLoggers() settings.loggerAsset = legacyLogger; // already exists settings.ConvertLoggersFromObsoleteLogger(); - Assert.That(settings.loggerAssets.Count, Is.EqualTo(0)); // Not added directly to the field - Assert.That(settings.LoggerAsset.loggerAssets.Count, Is.EqualTo(1)); - Assert.That(settings.LoggerAsset.loggerAssets, Has.Member(legacyLogger)); + Assert.That(settings.loggerAssets.Count, Is.EqualTo(1)); + Assert.That(settings.loggerAssets, Has.Member(legacyLogger)); Assert.That(legacyLogger.Logs, Has.Member((LogType.Warning, @"Single Logger setting in AutopilotSettings has been obsolete. @@ -139,9 +138,8 @@ public void ConvertReportersFromObsoleteReporter_HasReporter_IncludeToReporters( var spyLogger = ScriptableObject.CreateInstance(); settings.ConvertReportersFromObsoleteReporter(spyLogger.Logger); - Assert.That(settings.reporters.Count, Is.EqualTo(0)); // Not added directly to the field - Assert.That(settings.Reporter.reporters.Count, Is.EqualTo(1)); - Assert.That(settings.Reporter.reporters, Has.Member(legacyReporter)); + Assert.That(settings.reporters.Count, Is.EqualTo(1)); + Assert.That(settings.reporters, Has.Member(legacyReporter)); Assert.That(spyLogger.Logs, Has.Member((LogType.Warning, @"Single Reporter setting in AutopilotSettings has been obsolete. From 6a800984d5da7d6b37f5fd9f758c8ff288d0330d Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 5 Nov 2024 05:01:56 +0900 Subject: [PATCH 4/5] Fix SlackReporter convert condition and create file Same as a JUnitXmlReporter; see #98 --- Runtime/Settings/AutopilotSettings.cs | 11 ++++++++--- Tests/Runtime/Settings/AutopilotSettingsTest.cs | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Runtime/Settings/AutopilotSettings.cs b/Runtime/Settings/AutopilotSettings.cs index adb3bf1..7117581 100644 --- a/Runtime/Settings/AutopilotSettings.cs +++ b/Runtime/Settings/AutopilotSettings.cs @@ -301,20 +301,25 @@ internal void ConvertReportersFromObsoleteReporter(ILogger logger) internal void ConvertSlackReporterFromObsoleteSlackSettings(ILogger logger) { if (string.IsNullOrEmpty(this.slackToken) || string.IsNullOrEmpty(this.slackChannels) || - this.reporters.Any()) + this.reporters.Any(x => x.GetType() == typeof(SlackReporter))) { return; } - logger.Log(LogType.Warning, @"Slack settings in AutopilotSettings has been obsolete. + const string AutoConvertingMessage = @"Slack settings in AutopilotSettings has been obsolete. Please delete the value using Debug Mode in the Inspector window. And create a SlackReporter asset file. -This time, temporarily generate and use SlackReporter instance."); +This time, temporarily generate and use SlackReporter instance."; + logger.Log(LogType.Warning, AutoConvertingMessage); var convertedReporter = CreateInstance(); convertedReporter.slackToken = this.slackToken; convertedReporter.slackChannels = this.slackChannels; convertedReporter.mentionSubTeamIDs = this.mentionSubTeamIDs; convertedReporter.addHereInSlackMessage = this.addHereInSlackMessage; +#if UNITY_EDITOR + convertedReporter.description = AutoConvertingMessage; + SaveConvertedObject(convertedReporter); +#endif this.reporters.Add(convertedReporter); } diff --git a/Tests/Runtime/Settings/AutopilotSettingsTest.cs b/Tests/Runtime/Settings/AutopilotSettingsTest.cs index d26e64c..9f3d547 100644 --- a/Tests/Runtime/Settings/AutopilotSettingsTest.cs +++ b/Tests/Runtime/Settings/AutopilotSettingsTest.cs @@ -6,7 +6,6 @@ using DeNA.Anjin.TestDoubles; using NUnit.Framework; using UnityEngine; -using UnityEngine.TestTools; namespace DeNA.Anjin.Settings { @@ -220,14 +219,15 @@ public void ConvertSlackReporterFromObsoleteSlackSettings_HasNotSlackChannels_No [Test] public void ConvertSlackReporterFromObsoleteSlackSettings_ExistReporter_NotGenerateSlackReporter() { + var existReporter = ScriptableObject.CreateInstance(); var settings = ScriptableObject.CreateInstance(); - settings.reporters.Add(ScriptableObject.CreateInstance()); // already exists + settings.reporters.Add(existReporter); // already exists settings.slackToken = "token"; settings.slackChannels = "channels"; settings.ConvertSlackReporterFromObsoleteSlackSettings(Debug.unityLogger); Assert.That(settings.reporters.Count, Is.EqualTo(1)); - Assert.That(settings.reporters, Has.No.InstanceOf()); + Assert.That(settings.reporters, Does.Contain(existReporter)); } } } From 09dfbefd7e2aba3ded01a5f0ee0bfb0da2e0efe1 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 5 Nov 2024 07:31:53 +0900 Subject: [PATCH 5/5] Add set dirty flag after convert obsolete fields --- Runtime/Settings/AutopilotSettings.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Runtime/Settings/AutopilotSettings.cs b/Runtime/Settings/AutopilotSettings.cs index 7117581..b87c81f 100644 --- a/Runtime/Settings/AutopilotSettings.cs +++ b/Runtime/Settings/AutopilotSettings.cs @@ -264,6 +264,9 @@ private void CreateDefaultLoggerIfNeeded() // not change field directly. this.LoggerAsset.Logger.Log("Create default logger."); +#if UNITY_EDITOR + EditorUtility.SetDirty(this); +#endif } } @@ -295,6 +298,9 @@ internal void ConvertReportersFromObsoleteReporter(ILogger logger) This time, temporarily converting."); this.reporters.Add(this.reporter); +#if UNITY_EDITOR + EditorUtility.SetDirty(this); +#endif } [Obsolete("Remove this method when bump major version")] @@ -321,6 +327,9 @@ internal void ConvertSlackReporterFromObsoleteSlackSettings(ILogger logger) SaveConvertedObject(convertedReporter); #endif this.reporters.Add(convertedReporter); +#if UNITY_EDITOR + EditorUtility.SetDirty(this); +#endif } private void SaveConvertedObject(Object obj)