summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.UITest.TestCloud/Uploader.cs
blob: b0cf299c058fa5285de4e48810674e972efe32f9 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using Mono.Options;
using Xamarin.Forms.Core.UITests;
using Xamarin.Forms.Loader;

namespace Xamarin.Forms.UITest.TestCloud
{
	// TODO: Provide way to construct url for results easy access

	static class Uploader
	{
		static LoaderActions loaderActions;

		static int Main(string[] args)
		{
			loaderActions = new LoaderActions();

			var categories = new List<string>();
			string series = null;
			var platform = DeviceSet.Platform.None;
			DeviceSet deviceSet = null;
			var validate = false;
			string outputFile = null;
			var account = "";
			var user = "";

			OptionSet optionSet = null;
			optionSet = new OptionSet
			{
				{
					"p|platform=", "specify the test platform, iOS or Android",
					s => platform = (DeviceSet.Platform)Enum.Parse(typeof (DeviceSet.Platform), s)
				},
				{ "d|deviceset=", "the device set to use for the test run", s => deviceSet = StringToDeviceSet(s) },
				{ "c|category=", "add a category to the test run", s => categories.Add(s) },
				{ "s|series=", "specify the series when uploaded to Test Cloud", s => series = s },
				{ "l|list", "list categories available in test suite", ListCategories },
				{ "sets", "list available device sets", ListDeviceSets },
				{ "i|interactive", "start uploader in interactive mode", InteractiveMode },
				{ "h|help", "show this message and exit", s => ShowHelp(optionSet) },
				{ "v|validate", "validate all tests or a specified category", s => validate = true },
				{ "o|output=", "output destination for NUnit XML", s => outputFile = s },
				{ "a|account=", "Test Cloud key", s => account = s },
				{ "u|user=", "Test Cloud user", s => user = s }
			};

			List<string> extra;
			try
			{
				extra = optionSet.Parse(args);
			}
			catch (OptionException ex)
			{
				Console.Write("Uploader:");
				Console.WriteLine(ex.Message);
				Console.WriteLine("Try --help for more informaiton");
			}

			if (args.Length == 0)
				ShowHelp(optionSet);

			if (validate)
			{
				var category = categories.FirstOrDefault();
				return loaderActions.ValidateCategory(category) ? 0 : 1;
			}

			if (platform == DeviceSet.Platform.None)
			{
				Console.WriteLine("Platform must be specified");
				return 1;
			}

			if (deviceSet != null && !deviceSet.DeviceSetPlatform.Contains(platform))
			{
				Console.WriteLine("DeviceSet platform does not match specified platform");
				return 1;
			}

			if (deviceSet == null)
			{
				if (platform == DeviceSet.Platform.Android)
					deviceSet = DeviceSets.AndroidFastParallel;
				else
					deviceSet = DeviceSets.IOsFastParallel;
			}

			var execString = BuildExecutionString(platform, deviceSet, categories, series, account, user, outputFile);

			Console.WriteLine(execString);

			var processStatus = TestCloudUtils.UploadApp(execString);

			Console.WriteLine("test-cloud.exe status: " + processStatus);
			return processStatus;
		}

		static string BuildExecutionString(DeviceSet.Platform platform, DeviceSet deviceSet, IEnumerable<string> categories,
			string series, string account, string user, string outputFile = null)
		{
			var stringBuilder = new StringBuilder();
			stringBuilder.Append(ConsolePath);
			stringBuilder.Append(" submit ");

			switch (platform)
			{
				case DeviceSet.Platform.Android:
					stringBuilder.Append(ApkPath);
					break;
				case DeviceSet.Platform.IOs:
					stringBuilder.Append(IpaPath);
					break;
				case DeviceSet.Platform.IOsClassic:
					stringBuilder.Append(IpaClassicPath);
					break;
			}

			stringBuilder.Append(" ");
			stringBuilder.Append(account);
			stringBuilder.Append(" --user ");
			stringBuilder.Append(user);
			stringBuilder.Append(" --devices ");
			stringBuilder.Append(deviceSet.Id);

			foreach (var category in categories)
			{
				stringBuilder.Append(" --include ");
				stringBuilder.Append(category);
			}

			if (!string.IsNullOrEmpty(series))
			{
				stringBuilder.Append(" --series ");
				stringBuilder.Append(series);
			}

			stringBuilder.Append(" --locale \"en_US\"");

			switch (platform)
			{
				case DeviceSet.Platform.Android:
					stringBuilder.Append(" --app-name \"AndroidControlGallery\"");
					break;
				case DeviceSet.Platform.IOs:
				case DeviceSet.Platform.IOsClassic:
					stringBuilder.Append(" --app-name \"XamControl\"");
					break;
			}

			stringBuilder.Append(" --assembly-dir ");

			if (platform == DeviceSet.Platform.Android)
				stringBuilder.Append(AndroidTestingDirectory);
			else
				stringBuilder.Append(iOSTestingDirectory);

			stringBuilder.Append(" --fixture-chunk");

			if (!string.IsNullOrEmpty(outputFile))
				stringBuilder.Append($" --nunit-xml {outputFile}");

			return stringBuilder.ToString();
		}

		static void InteractiveMode(string s)
		{
			Console.WriteLine("Interactive testcloud uploader. Type --help for help");
			Console.WriteLine(
				"Usage: >>> -d <deviceset> -c <category> -a <key> -u <user> [-c <category> -c <category> -c <category>]");

			while (true)
			{
				Console.Write(">>> ");
				var command = Console.ReadLine();
				var commandList = command.Split(' ');

				var platform = DeviceSet.Platform.None;
				var deviceSet = "";
				var categories = new List<string>();
				var series = "";
				var account = "";
				var user = "";

				OptionSet options = null;
				options = new OptionSet
				{
					{ "q|quit", "quit", Exit },
					{ "h|help", "show this message and exit", str => ShowInteractiveHelp(options) },
					{ "c|category=", "specify the category to run in Test cloud", str => categories.Add(str) },
					{ "d|deviceset=", "specify the device set to upload", str => deviceSet = str },
					{ "lc|listcategories", "Lists categories in uitests", ListCategories },
					{ "ld|listdevicesets", "Lists defined devices sets", ListDeviceSets },
					{ "a|account=", "Test Cloud key", str => account = str },
					{ "u|user=", "Test Cloud user", str => user = str }
				};

				List<string> extra;
				try
				{
					extra = options.Parse(commandList);
				}
				catch (OptionException ex)
				{
					Console.Write("Uploader:");
					Console.WriteLine(ex.Message);
					Console.WriteLine("Try --help for more informaiton");
				}

				if (command.Length == 0)
					ShowHelp(options);

				// by default take the first category as the series name
				if (categories.Count >= 1)
					series = categories.First();

				if (commandList.Length >= 4)
				{
					var validQuery = true;

					if (!IsValidDeviceSet(deviceSet))
					{
						Console.WriteLine("Invalid DeviceSet: {0}", deviceSet);
						validQuery = false;
					}

					if (!CategoriesValid(categories))
					{
						Console.Write("Invalid Category(s):");
						foreach (var c in categories)
							Console.Write(" {0} ", c);
						Console.Write("\n");
						validQuery = false;
					}

					if (validQuery)
					{
						var devSet = StringToDeviceSet(deviceSet);
						var execString = BuildExecutionString(devSet.DeviceSetPlatform.First(), devSet, categories, series, account, user);
						Console.WriteLine(execString);
						TestCloudUtils.UploadApp(execString);
					}
				}
			}
		}

		static void ShowInteractiveHelp(OptionSet options)
		{
			Console.WriteLine("Usage: [OPTIONS]");
			Console.WriteLine();
			Console.WriteLine("Options:");
			options.WriteOptionDescriptions(Console.Out);
		}

		static void Exit(string s)
		{
			Environment.Exit(0);
		}

		static void ShowHelp(OptionSet options)
		{
			Console.WriteLine("Usage: Uploader [OPTIONS]");
			Console.WriteLine();
			Console.WriteLine("Options:");
			options.WriteOptionDescriptions(Console.Out);

			Environment.Exit(0);
		}

		static DeviceSet StringToDeviceSet(string s)
		{
			try
			{
				var device = (DeviceSet)typeof (DeviceSets).GetField(s).GetValue(null);
				return device;
			}
			catch (Exception ex)
			{
				Console.WriteLine("DeviceSet not found");
				return null;
			}
		}

		static void ListDeviceSets(string s)
		{
			var deviceSetsType = typeof (DeviceSets);

			var fields = deviceSetsType.GetFields();

			foreach (var field in fields)
				Console.WriteLine(field.Name);
		}

		static void ListCategories(string s)
		{
			loaderActions.ListCategories();
		}

		static bool CategoriesValid(List<string> categories)
		{
			var areValid = true;

			if (categories.Count < 1)
				return false;

			foreach (var category in categories)
			{
				if (!loaderActions.ValidateCategory(category))
					return false;
			}

			return areValid;
		}

		static bool IsValidDeviceSet(string deviceSet)
		{
			var deviceSetsType = typeof (DeviceSets);
			var isValid = deviceSetsType.GetFields().Any(ds => ds.Name == deviceSet);
			return isValid;
		}

		public static string ConsolePath
		{
			get
			{
				string[] consolePathElements = { "..", "..", "..", "packages", "Xamarin.UITest.1.3.3", "tools", "test-cloud.exe" };
				return Path.Combine(consolePathElements);
			}
		}

		public static string IpaPath
		{
			get
			{
				string[] ipaPathElements =
				{
					"..",
					"..",
					"..",
					"Xamarin.Forms.ControlGallery.iOS",
					"bin",
					"iPhone",
					"Debug",
					"XamarinFormsControlGalleryiOS-1.0.ipa"
				};
				return Path.Combine(ipaPathElements);
			}
		}

		public static string IpaClassicPath
		{
			get
			{
				string[] ipaPathElements =
				{
					"..",
					"..",
					"..",
					"Xamarin.Forms.ControlGallery.iOS",
					"classic_bin",
					"iPhone",
					"Debug",
					"XamarinFormsControlGalleryiOS-1.0.ipa"
				};
				return Path.Combine(ipaPathElements);
			}
		}

		public static string ApkPath
		{
			get
			{
				string[] apkPathElements =
				{
					"..",
					"..",
					"..",
					"Xamarin.Forms.ControlGallery.Android",
					"bin",
					"Debug",
					"AndroidControlGallery.AndroidControlGallery-Signed.apk"
				};
				return Path.Combine(apkPathElements);
			}
		}

		public static string iOSTestingDirectory
		{
			get
			{
				string[] testDiriOSPathElements = { "..", "..", "..", "Xamarin.Forms.Core.iOS.UITests", "bin", "Debug" };
				return Path.Combine(testDiriOSPathElements);
			}
		}

		public static string AndroidTestingDirectory
		{
			get
			{
				string[] testDirAndroidPathElements = { "..", "..", "..", "Xamarin.Forms.Core.Android.UITests", "bin", "Debug" };
				return Path.Combine(testDirAndroidPathElements);
			}
		}
	}
}