A game about forced loneliness, made by TACStudios
1using System;
2using System.IO;
3using UnityEditor.TestRunner.CommandLineParser;
4using UnityEditor.TestTools.TestRunner.Api;
5
6namespace UnityEditor.TestTools.TestRunner.CommandLineTest
7{
8 internal class SettingsBuilder : ISettingsBuilder
9 {
10 private ITestSettingsDeserializer m_TestSettingsDeserializer;
11 private Action<string> m_LogAction;
12 private Action<string> m_LogWarningAction;
13 internal Func<string, bool> fileExistsCheck = File.Exists;
14 private Func<bool> m_ScriptCompilationFailedCheck;
15 internal Func<string, string[]> readAllLines = filePath => File.ReadAllText(filePath).Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
16 public SettingsBuilder(ITestSettingsDeserializer testSettingsDeserializer, Action<string> logAction, Action<string> logWarningAction, Func<bool> scriptCompilationFailedCheck)
17 {
18 m_LogAction = logAction;
19 m_LogWarningAction = logWarningAction;
20 m_ScriptCompilationFailedCheck = scriptCompilationFailedCheck;
21 m_TestSettingsDeserializer = testSettingsDeserializer;
22 }
23
24 public Api.ExecutionSettings BuildApiExecutionSettings(string[] commandLineArgs)
25 {
26 var quit = false;
27 string testPlatform = TestMode.EditMode.ToString();
28 string[] testFilters = null;
29 string[] testCategories = null;
30 string testSettingsFilePath = null;
31 int? playerHeartbeatTimeout = null;
32 bool runSynchronously = false;
33 string[] testAssemblyNames = null;
34 string buildPlayerPath = string.Empty;
35 string orderedTestListFilePath = null;
36 int retry = 0;
37 int repeat = 0;
38 int randomOrderSeed = 0;
39
40
41 var optionSet = new CommandLineOptionSet(
42 new CommandLineOption("quit", () => { quit = true; }),
43 new CommandLineOption("testPlatform", platform => { testPlatform = platform; }),
44 new CommandLineOption("editorTestsFilter", filters => { testFilters = filters; }),
45 new CommandLineOption("testFilter", filters => { testFilters = filters; }),
46 new CommandLineOption("editorTestsCategories", catagories => { testCategories = catagories; }),
47 new CommandLineOption("testCategory", catagories => { testCategories = catagories; }),
48 new CommandLineOption("testSettingsFile", settingsFilePath => { testSettingsFilePath = settingsFilePath; }),
49 new CommandLineOption("playerHeartbeatTimeout", timeout => { playerHeartbeatTimeout = int.Parse(timeout); }),
50 new CommandLineOption("runSynchronously", () => { runSynchronously = true; }),
51 new CommandLineOption("assemblyNames", assemblyNames => { testAssemblyNames = assemblyNames; }),
52 new CommandLineOption("buildPlayerPath", buildPath => { buildPlayerPath = buildPath; }),
53 new CommandLineOption("orderedTestListFile", filePath => { orderedTestListFilePath = filePath; }),
54 new CommandLineOption("randomOrderSeed", seed => { randomOrderSeed = int.Parse(seed);}),
55 new CommandLineOption("retry", n => { retry = int.Parse(n); }),
56 new CommandLineOption("repeat", n => { repeat = int.Parse(n); })
57 );
58 optionSet.Parse(commandLineArgs);
59
60 DisplayQuitWarningIfQuitIsGiven(quit);
61
62 CheckForScriptCompilationErrors();
63
64 var testSettings = GetTestSettings(testSettingsFilePath);
65 var filter = new Filter
66 {
67 testMode = testPlatform.ToLower() == "editmode" ? TestMode.EditMode : TestMode.PlayMode,
68 groupNames = testFilters,
69 categoryNames = testCategories,
70 assemblyNames = testAssemblyNames
71 };
72
73 var settings = new Api.ExecutionSettings
74 {
75 filters = new []{ filter },
76 overloadTestRunSettings = new RunSettings(testSettings),
77 ignoreTests = testSettings?.ignoreTests,
78 featureFlags = testSettings?.featureFlags,
79 targetPlatform = GetBuildTarget(testPlatform),
80 runSynchronously = runSynchronously,
81 playerSavePath = buildPlayerPath,
82 orderedTestNames = GetOrderedTestList(orderedTestListFilePath),
83 repeatCount = repeat,
84 retryCount = retry,
85 randomOrderSeed = randomOrderSeed
86 };
87
88 if (playerHeartbeatTimeout != null)
89 {
90 settings.playerHeartbeatTimeout = playerHeartbeatTimeout.Value;
91 }
92
93 return settings;
94 }
95
96 public ExecutionSettings BuildExecutionSettings(string[] commandLineArgs)
97 {
98 string resultFilePath = null;
99 string deviceLogsDirectory = null;
100
101 var optionSet = new CommandLineOptionSet(
102 new CommandLineOption("editorTestsResultFile", filePath => { resultFilePath = filePath; }),
103 new CommandLineOption("testResults", filePath => { resultFilePath = filePath; }),
104 new CommandLineOption("deviceLogs", dirPath => { deviceLogsDirectory = dirPath; })
105 );
106 optionSet.Parse(commandLineArgs);
107
108 return new ExecutionSettings
109 {
110 TestResultsFile = resultFilePath,
111 DeviceLogsDirectory = deviceLogsDirectory
112 };
113 }
114
115 private void DisplayQuitWarningIfQuitIsGiven(bool quitIsGiven)
116 {
117 if (quitIsGiven)
118 {
119 m_LogWarningAction("Running tests from command line arguments will not work when \"quit\" is specified.");
120 }
121 }
122
123 private void CheckForScriptCompilationErrors()
124 {
125 if (m_ScriptCompilationFailedCheck())
126 {
127 throw new SetupException(SetupException.ExceptionType.ScriptCompilationFailed);
128 }
129 }
130
131 private ITestSettings GetTestSettings(string testSettingsFilePath)
132 {
133 ITestSettings testSettings = null;
134 if (!string.IsNullOrEmpty(testSettingsFilePath))
135 {
136 if (!fileExistsCheck(testSettingsFilePath))
137 {
138 throw new SetupException(SetupException.ExceptionType.TestSettingsFileNotFound, testSettingsFilePath);
139 }
140
141 testSettings = m_TestSettingsDeserializer.GetSettingsFromJsonFile(testSettingsFilePath);
142 }
143 return testSettings;
144 }
145
146 private string[] GetOrderedTestList(string orderedTestListFilePath)
147 {
148 if (!string.IsNullOrEmpty(orderedTestListFilePath))
149 {
150 if (!fileExistsCheck(orderedTestListFilePath))
151 {
152 throw new SetupException(SetupException.ExceptionType.OrderedTestListFileNotFound, orderedTestListFilePath);
153 }
154
155 return readAllLines(orderedTestListFilePath);
156 }
157 return null;
158 }
159
160 private static BuildTarget? GetBuildTarget(string testPlatform)
161 {
162 var testPlatformLower = testPlatform.ToLower();
163 if (testPlatformLower == "editmode" || testPlatformLower == "playmode" || testPlatformLower == "editor" ||
164 string.IsNullOrEmpty(testPlatformLower))
165 {
166 return null;
167 }
168
169 try
170 {
171 return (BuildTarget)Enum.Parse(typeof(BuildTarget), testPlatform, true);
172 }
173 catch (ArgumentException)
174 {
175 throw new SetupException(SetupException.ExceptionType.PlatformNotFound, testPlatform);
176 }
177 }
178 }
179}