Welcome to mirror list, hosted at ThFree Co, Russian Federation.

harness.cs « harness « ios « sdks - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cd838be4ed2f418c4e384913781399c461280636 (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
using System;
using System.IO;
using System.Json;
using System.Threading;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using Mono.Options;

public class Harness
{
	public const string SIM_NAME = "xamarin.ios-sdk.sim";

	static void Usage () {
		Console.WriteLine ("Usage: mono harness.exe <options> <app dir>");
		Console.WriteLine ("Where options are:");
		Console.WriteLine ("\t--run-sim");
		Console.WriteLine ("\t--app=<app bundle id>");
		Console.WriteLine ("\t--logfile=<log file name>");
	}

	public static int Main (string[] args) {
		new Harness ().Run (args);
		return 0;
	}

	string bundle_id;
	string bundle_dir;
	string logfile_name;
	string[] new_args;

	public void Run (string[] args) {
		string action = "";
		bundle_id = "";
		bundle_dir = "";
		logfile_name = "";

		var p = new OptionSet () {
		  { "start-sim", s => action = "start-sim" },
		  { "run-sim", s => action = "run-sim" },
		  { "bundle-id=", s => bundle_id = s },
		  { "bundle-dir=", s => bundle_dir = s },
		  { "logfile=", s => logfile_name = s },
		};
		new_args = p.Parse (args).ToArray ();

		if (action == "start-sim") {
			StartSim ();
		} else if (action == "run-sim") {
			if (bundle_id == "" || bundle_dir == "") {
				Console.WriteLine ("The --bundle-id and --bundle-dir arguments are mandatory.");
				Environment.Exit (1);
			}
			RunSim ();
		} else {
			Usage ();
			Environment.Exit (1);
		}
	}

	void StartSim () {
		// Check whenever our simulator instance exists
		string state_line = "";
		{
			var args = "simctl list devices";
			Console.WriteLine ("Running: " + "xcrun " + args);
			var start_info = new ProcessStartInfo ("xcrun", args);
			start_info.RedirectStandardOutput = true;
			start_info.UseShellExecute = false;
			var process = Process.Start (start_info);
			var stream = process.StandardOutput;
			string line = "";
			while (true) {
				line = stream.ReadLine ();
				if (line == null)
					break;
				if (line.Contains (SIM_NAME)) {
					state_line = line;
					break;
				}
			}
			process.WaitForExit ();
			if (process.ExitCode != 0)
				Environment.Exit (1);
		}

		bool need_start = false;
		if (state_line == "") {
			// Get the runtime type
			var args = "simctl list -j runtimes";
			Console.WriteLine ("Running: " + "xcrun " + args);
			var start_info = new ProcessStartInfo ("xcrun", args);
			start_info.RedirectStandardOutput = true;
			start_info.UseShellExecute = false;
			var process = Process.Start (start_info);
			var stream = process.StandardOutput;
			JsonObject value = JsonValue.Parse (stream.ReadToEnd ()) as JsonObject;
			string runtime = value ["runtimes"][0]["identifier"];

			// Create the simulator
			args = "simctl create " + SIM_NAME + " 'iPhone 7' " + runtime;
			Console.WriteLine ("Running: " + "xcrun " + args);
			process = Process.Start ("xcrun", args);
			process.WaitForExit ();
			if (process.ExitCode != 0)
				Environment.Exit (1);
			need_start = true;
		} else if (state_line.Contains ("(Shutdown)")) {
			need_start = true;
		}

		if (need_start) {
			var args = "simctl boot " + SIM_NAME;
			Console.WriteLine ("Running: " + "xcrun " + args);
			var process = Process.Start ("xcrun", args);
			process.WaitForExit ();
			if (process.ExitCode != 0)
				Environment.Exit (1);
		}
	}

	void RunSim () {
		Console.WriteLine ("App: " + bundle_id);

		StartSim ();

		// Install the app
		// We do this all the time since its cheap
		string exe = "xcrun";
		string args = "simctl install " + SIM_NAME + " " + bundle_dir;
		Console.WriteLine ("Running: " + exe + " " + args);
		var process = Process.Start (exe, args);
		process.WaitForExit ();
		if (process.ExitCode != 0)
			Environment.Exit (1);

		//
		// Test results are returned using an socket connection.
		//
		var host = Dns.GetHostEntry (Dns.GetHostName ());
		var server = new TcpListener (System.Net.IPAddress.Loopback, 0);
		server.Start ();
		int port = ((IPEndPoint)server.LocalEndpoint).Port;

		string app_args = "";
		foreach (var a in new_args)
			app_args += a + " ";
		if (!app_args.Contains ("CONNSTR"))
			throw new Exception ();
		app_args = app_args.Replace ("CONNSTR", $"tcp:localhost:{port}");

		// Terminate previous app
		exe = "xcrun";
		args = "simctl terminate " + SIM_NAME + " " + bundle_id;
		Console.WriteLine ("Running: " + exe + " " + args);
		process = Process.Start (exe, args);
		process.WaitForExit ();
		if (process.ExitCode != 0)
			Environment.Exit (1);

		// Launch new app
		exe = "xcrun";
		args = "simctl launch " + SIM_NAME + " " + bundle_id + " " + app_args;
		Console.WriteLine ("Running: " + exe + " " + args);
		process = Process.Start (exe, args);
		process.WaitForExit ();
		if (process.ExitCode != 0)
			Environment.Exit (1);

		//
		// Read test results from the tcp connection
		//
		TextWriter w = new StreamWriter (logfile_name);
		string result_line = null;
		var client = server.AcceptTcpClient ();
		var stream = client.GetStream ();
		var reader = new StreamReader (stream);
		while (true) {
			var line = reader.ReadLine ();
			if (line == null)
				break;
			Console.WriteLine (line);
			w.WriteLine (line);
			if (line.Contains ("Tests run:"))
				result_line = line;
			// Printed by the runtime
			if (line.Contains ("Exit code:"))
				break;
		}

		if (result_line != null && result_line.Contains ("Errors: 0"))
			Environment.Exit (0);
		else
			Environment.Exit (1);
	}
}