| | 1 | | // Copyright (c) 2020-2022 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.IO; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using GDX.Developer.Reports; |
| | 10 | | using GDX.Developer.Reports.BuildVerification; |
| | 11 | | using UnityEngine; |
| | 12 | | using UnityEngine.SceneManagement; |
| | 13 | |
|
| | 14 | | namespace GDX.Developer.ConsoleCommands |
| | 15 | | { |
| | 16 | | #if UNITY_2022_2_OR_NEWER |
| | 17 | |
|
| | 18 | | public class BuildVerificationTestConsoleCommand : ConsoleCommandBase |
| | 19 | | { |
| | 20 | | TestScene[] m_Scenes; |
| | 21 | | int m_ScenesCount; |
| | 22 | |
|
| | 23 | | State m_State; |
| | 24 | | Task m_TestRunner; |
| | 25 | |
|
| | 26 | | /// <inheritdoc /> |
| | 27 | | public override bool Evaluate(float deltaTime) |
| 0 | 28 | | { |
| 0 | 29 | | switch (m_State) |
| | 30 | | { |
| | 31 | | case State.Init: |
| 0 | 32 | | BuildVerificationReport.Reset(); |
| 0 | 33 | | m_State = State.Run; |
| 0 | 34 | | return false; |
| | 35 | | case State.Run: |
| 0 | 36 | | m_TestRunner = TestRunner.Execute(m_Scenes); |
| 0 | 37 | | m_State = State.Wait; |
| 0 | 38 | | return false; |
| | 39 | |
|
| | 40 | | case State.Wait: |
| | 41 | |
|
| 0 | 42 | | if (m_TestRunner == null) |
| 0 | 43 | | { |
| 0 | 44 | | Debug.LogError("The test runner was unable to run."); |
| 0 | 45 | | return true; |
| | 46 | | } |
| | 47 | |
|
| | 48 | | // Test runner thinks everything is done |
| 0 | 49 | | if (m_TestRunner.IsCompleted) |
| 0 | 50 | | { |
| 0 | 51 | | m_State = State.Output; |
| 0 | 52 | | return false; |
| | 53 | | } |
| | 54 | |
|
| | 55 | |
|
| 0 | 56 | | return false; |
| | 57 | |
|
| | 58 | | case State.Output: |
| | 59 | |
|
| 0 | 60 | | string outputPath = Path.GetFullPath(Path.Combine(Platform.GetOutputFolder(), |
| | 61 | | $"BVT-{DateTime.Now.ToString(Localization.FilenameTimestampFormat)}.xml")); |
| | 62 | |
|
| 0 | 63 | | string result = BuildVerificationReport.OutputReport(outputPath); |
| | 64 | |
|
| 0 | 65 | | Debug.Log(File.Exists(outputPath) |
| | 66 | | ? $"Build checks ({result}) written to {outputPath}." |
| | 67 | | : $"Unable to write file to {outputPath}."); |
| 0 | 68 | | return true; |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | return true; |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | /// <inheritdoc /> |
| | 75 | | public override string GetKeyword() |
| 7 | 76 | | { |
| 7 | 77 | | return "bvt"; |
| 7 | 78 | | } |
| | 79 | |
|
| | 80 | | /// <inheritdoc /> |
| | 81 | | public override string GetHelpUsage() |
| 0 | 82 | | { |
| 0 | 83 | | return "bvt <SceneName, ...>"; |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | /// <inheritdoc /> |
| | 87 | | public override string GetHelpMessage() |
| 0 | 88 | | { |
| 0 | 89 | | return "Loads a specific scene and attempts to run any automated tests associated with that process."; |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <inheritdoc /> |
| | 93 | | public override ConsoleCommandBase GetInstance(string context) |
| 0 | 94 | | { |
| 0 | 95 | | BuildVerificationTestConsoleCommand command = |
| | 96 | | new BuildVerificationTestConsoleCommand { m_State = State.Init }; |
| | 97 | |
|
| 0 | 98 | | string[] scenes = context.Split(','); |
| 0 | 99 | | int sceneCount = scenes.Length; |
| 0 | 100 | | List<TestScene> validScenes = new List<TestScene>(sceneCount); |
| 0 | 101 | | for (int i = 0; i < sceneCount; i++) |
| 0 | 102 | | { |
| 0 | 103 | | Scene s = SceneManager.GetSceneByName(scenes[i]); |
| 0 | 104 | | if (s.IsValid()) |
| 0 | 105 | | { |
| 0 | 106 | | validScenes.Add(new TestScene(s.buildIndex)); |
| 0 | 107 | | } |
| 0 | 108 | | } |
| | 109 | |
|
| 0 | 110 | | command.m_Scenes = validScenes.ToArray(); |
| 0 | 111 | | command.m_ScenesCount = command.m_Scenes.Length; |
| | 112 | |
|
| 0 | 113 | | if (command.m_ScenesCount > 0) |
| 0 | 114 | | { |
| 0 | 115 | | return command; |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | Debug.LogWarning($"No valid scenes were able to be extracted from '{context}'."); |
| 0 | 119 | | return null; |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | enum State |
| | 123 | | { |
| | 124 | | Init, |
| | 125 | | Run, |
| | 126 | | Wait, |
| | 127 | | Output |
| | 128 | | } |
| | 129 | | } |
| | 130 | | #endif // UNITY_2022_2_OR_NEWER |
| | 131 | | } |