Skip to content

Commit 6336d35

Browse files
committed
Merge pull request #40 from watson-developer-cloud/feature-unittest_projectdefinition-saveconfig
* Added Project name definition on Unit test
2 parents 033a3de + a40b563 commit 6336d35

File tree

4 files changed

+105
-35
lines changed

4 files changed

+105
-35
lines changed

Config.json.enc

0 Bytes
Binary file not shown.

Scripts/UnitTests/UnitTest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@ public void Test(bool condition)
4242
TestFailed = true;
4343
}
4444
}
45+
46+
public virtual string ProjectToTest()
47+
{
48+
return null;
49+
}
4550
}
46-
}
51+
}

Scripts/UnitTests/UnitTestManager.cs

Lines changed: 80 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class UnitTestManager : MonoBehaviour
6363
/// </summary>
6464
public OnTestsComplete OnTestCompleteCallback { get; set; }
6565

66+
public static string ProjectToTest = null;
67+
6668
/// <summary>
6769
/// Queue test by Type to run.
6870
/// </summary>
@@ -109,47 +111,57 @@ private IEnumerator RunTestsCR()
109111
m_ActiveTest = Activator.CreateInstance(testType) as UnitTest;
110112
if (m_ActiveTest != null)
111113
{
112-
Log.Status("UnitTestManager", "STARTING UnitTest {0} ...", testType.Name);
113-
114-
// wait for the test to complete..
115-
bool bTestException = true;
116-
DateTime startTime = DateTime.Now;
117-
try
114+
if ( (string.IsNullOrEmpty(m_ActiveTest.ProjectToTest()) && string.IsNullOrEmpty(ProjectToTest)) || (m_ActiveTest.ProjectToTest() == ProjectToTest) || ( !string.IsNullOrEmpty(m_ActiveTest.ProjectToTest()) && !string.IsNullOrEmpty(ProjectToTest) && ProjectToTest.ToLower().Contains(m_ActiveTest.ProjectToTest().ToLower())))
118115
{
119-
IEnumerator e = m_ActiveTest.RunTest();
120-
while (e.MoveNext())
116+
Log.Status("UnitTestManager", "STARTING UnitTest {0} ...", testType.Name);
117+
118+
// wait for the test to complete..
119+
bool bTestException = true;
120+
DateTime startTime = DateTime.Now;
121+
try
121122
{
122-
if (m_ActiveTest.TestFailed)
123-
break;
123+
IEnumerator e = m_ActiveTest.RunTest();
124+
while (e.MoveNext())
125+
{
126+
if (m_ActiveTest.TestFailed)
127+
break;
128+
129+
yield return null;
130+
if ((DateTime.Now - startTime).TotalSeconds > TEST_TIMEOUT)
131+
{
132+
Log.Error("UnitTestManager", "UnitTest {0} has timed out.", testType.Name);
133+
m_ActiveTest.TestFailed = true;
134+
break;
135+
}
136+
}
124137

125-
yield return null;
126-
if ((DateTime.Now - startTime).TotalSeconds > TEST_TIMEOUT)
138+
bTestException = false;
139+
if (m_ActiveTest.TestFailed)
127140
{
128-
Log.Error("UnitTestManager", "UnitTest {0} has timed out.", testType.Name);
129-
m_ActiveTest.TestFailed = true;
130-
break;
141+
Log.Error("UnitTestManager", "... UnitTest {0} FAILED.", testType.Name);
142+
TestsFailed += 1;
143+
}
144+
else
145+
{
146+
Log.Status("UnitTestManager", "... UnitTest {0} COMPLETED.", testType.Name);
147+
TestsComplete += 1;
131148
}
132149
}
133-
134-
bTestException = false;
135-
if (m_ActiveTest.TestFailed)
150+
finally
136151
{
137-
Log.Error("UnitTestManager", "... UnitTest {0} FAILED.", testType.Name);
138-
TestsFailed += 1;
139152
}
140-
else
153+
154+
if (bTestException)
141155
{
142-
Log.Status("UnitTestManager", "... UnitTest {0} COMPLETED.", testType.Name);
143-
TestsComplete += 1;
156+
Log.Error("UnitTestManager", "... UnitTest {0} threw exception.", testType.Name);
157+
TestsFailed += 1;
144158
}
145159
}
146-
finally { }
147-
148-
if (bTestException)
160+
else
149161
{
150-
Log.Error("UnitTestManager", "... UnitTest {0} threw exception.", testType.Name);
151-
TestsFailed += 1;
162+
//do nothing - because the test we have is not in the project we are testing
152163
}
164+
153165
}
154166
else
155167
{
@@ -162,7 +174,15 @@ private IEnumerator RunTestsCR()
162174
if (OnTestCompleteCallback != null)
163175
OnTestCompleteCallback();
164176

165-
Log.Status("UnitTestManager", "Tests Completed: {0}, Tests Failed: {1}", TestsComplete, TestsFailed);
177+
if (TestsComplete == 0 && TestsFailed == 0)
178+
{
179+
Log.Status("UnitTestManager", "Nothing to Test");
180+
}
181+
else
182+
{
183+
Log.Status("UnitTestManager", "Tests Completed: {0}, Tests Failed: {1}", TestsComplete, TestsFailed);
184+
}
185+
166186
#if UNITY_EDITOR
167187
if (QuitOnTestsComplete)
168188
EditorApplication.Exit(TestsFailed > 0 ? 1 : 0);
@@ -181,22 +201,24 @@ private void OnGUI()
181201

182202
if (m_TestsAvailable != null)
183203
{
184-
GUILayout.BeginArea(new Rect(Screen.width * 0.3f, Screen.height * 0.15f, Screen.width * 0.4f, Screen.height * 0.85f));
204+
GUILayout.BeginArea(new Rect(Screen.width * 0.3f, Screen.height * 0.15f, Screen.width * 0.4f, Screen.height * 0.85f));
185205
foreach (var t in m_TestsAvailable)
186206
{
187207
string sButtonLabel = "Run " + t.Name;
188-
if (GUILayout.Button(sButtonLabel, GUILayout.MinWidth(Screen.width * 0.4f), GUILayout.MinHeight(Screen.height * 0.04f)))
208+
if (GUILayout.Button(sButtonLabel,GUILayout.MinWidth(Screen.width * 0.4f), GUILayout.MinHeight(Screen.height * 0.04f)))
189209
{
210+
IBM.Watson.DeveloperCloud.Editor.UnitTestManager.ProjectToTest = Config.Instance.GetVariableValue("PACKAGE_PREFIX");
190211
QueueTest(t, true);
191212
}
192213
}
193-
GUILayout.EndArea();
214+
GUILayout.EndArea();
194215
}
195216
}
196217
#endregion
197218
}
198219
}
199220

221+
200222
/// <summary>
201223
/// This static class is for menu items and invoking a function from the command line, since it doesn't like namespaces.
202224
/// </summary>
@@ -210,7 +232,30 @@ static public void All()
210232
#if UNITY_EDITOR
211233
Runnable.EnableRunnableInEditor();
212234
#endif
235+
string ProjectToTest = "";
236+
string[] args = Environment.GetCommandLineArgs();
237+
for (int i = 0; i < args.Length; ++i)
238+
{
239+
if (args[i] == "-packageOptions" && (i + 1) < args.Length)
240+
{
241+
string[] options = args[i + 1].Split(',');
242+
foreach (string option in options)
243+
{
244+
if (string.IsNullOrEmpty(option))
245+
continue;
246+
247+
string[] kv = option.Split('=');
248+
if (kv[0] == "ProjectName")
249+
{
250+
ProjectToTest = kv.Length > 1 ? kv[1] : "";
251+
Log.Status("RunUnitTest", "AutoLunchOptions ProjectToTest:{0}", ProjectToTest);
252+
break;
253+
}
254+
}
255+
}
256+
}
213257

258+
IBM.Watson.DeveloperCloud.Editor.UnitTestManager.ProjectToTest = ProjectToTest;
214259
IBM.Watson.DeveloperCloud.Editor.UnitTestManager instance = IBM.Watson.DeveloperCloud.Editor.UnitTestManager.Instance;
215260
instance.QuitOnTestsComplete = true;
216261
instance.OnTestCompleteCallback = OnTestsComplete;
@@ -221,18 +266,19 @@ static public void All()
221266
/// <summary>
222267
/// Menu item handler for running all unit tests.
223268
/// </summary>
224-
[MenuItem("Watson/Run All UnitTests", false, 50)]
269+
[MenuItem("Watson/Run All UnitTests",false, 50)]
225270
static public void AllNoQuit()
226271
{
227272
Runnable.EnableRunnableInEditor();
228273

274+
IBM.Watson.DeveloperCloud.Editor.UnitTestManager.ProjectToTest = Config.Instance.GetVariableValue("PACKAGE_PREFIX");
229275
IBM.Watson.DeveloperCloud.Editor.UnitTestManager instance = IBM.Watson.DeveloperCloud.Editor.UnitTestManager.Instance;
230276
instance.OnTestCompleteCallback = OnTestsComplete;
231277
instance.QueueTests(Utility.FindAllDerivedTypes(typeof(UnitTest)), true);
232278
}
233279
#endif
234280

235281
static void OnTestsComplete()
236-
{ }
282+
{}
237283
}
238284

Scripts/Utilities/Config.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,25 @@ public string SaveConfig(bool pretty = true)
253253
return fsJsonPrinter.CompressedJson(data);
254254
}
255255

256+
/// <summary>
257+
/// Save this config to fileSystem
258+
/// </summary>
259+
public bool SaveConfigToFileSystem()
260+
{
261+
bool success = true;
262+
try
263+
{
264+
File.WriteAllText(Application.streamingAssetsPath + Constants.Path.CONFIG_FILE, SaveConfig(true));
265+
}
266+
catch (Exception ex)
267+
{
268+
success = false;
269+
Log.Error("Config", "Exception on SaveConfigToFileSystem. {0}", ex.Message);
270+
}
271+
272+
return success;
273+
}
274+
256275
/// <summary>
257276
/// Finds a variable name and returns the Variable object
258277
/// </summary>

0 commit comments

Comments
 (0)