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