forked from cake-contrib/Cake.AppleSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unittest.cake
185 lines (163 loc) · 5.46 KB
/
unittest.cake
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
using System.Threading;
//////////////////////////////////////////////////////////////////////
// ADDINS
//////////////////////////////////////////////////////////////////////
#addin "Cake.FileHelpers"
#addin "Cake.AppleSimulator.SushiHangover"
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
if (string.IsNullOrWhiteSpace(target))
{
target = "Default";
}
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
// Build configuration
//(Context.Environment.Platform.Family == PlatformFamily.OSX); // Still broken
var isRunningOnMacOS = IsRunningOnUnix();
var isRunningOnUnix = IsRunningOnUnix();
var isRunningOnWindows = IsRunningOnWindows();
var solution = "./src/Cake.AppleSimulator.sln";
var buildConfiguration = "Release";
var buildTarget = "Build";
// Macros
Action Abort = () => { throw new Exception("A non-recoverable fatal error occurred."); };
Action TestFailuresAbort = () => { throw new Exception("Testing revealed failed unit tests"); };
Action NonMacOSAbort = () => { throw new Exception("Running on platforms other macOS is not supported."); };
Action<string, string, string> buildThisApp = (p,c,t) =>
{
Information(string.Format($"{t}|{c}|{p}"));
if (isRunningOnMacOS)
{
var settings = new XBuildSettings()
.WithProperty("SolutionDir", new string[] { @"./" })
.WithProperty("OutputPath", new string[] { @"../../artifacts/" })
.SetConfiguration(c)
.SetVerbosity(Verbosity.Quiet)
.WithTarget(t);
XBuild(p, settings);
};
};
Action<string, string> unitTestApp = (bundleId, appPath) =>
{
Information("Shutdown");
ShutdownAllAppleSimulators();
var setting = new SimCtlSettings() { ToolPath = FindXCodeTool("simctl") };
var simulators = ListAppleSimulators(setting);
var device = simulators.First(x => x.Name == "xUnit Runner" & x.Runtime == "iOS 10.1");
Information(string.Format($"Name={device.Name}, UDID={device.UDID}, Runtime={device.Runtime}, Availability={device.Availability}"));
Information("LaunchAppleSimulator");
LaunchAppleSimulator(device.UDID);
Thread.Sleep(60 * 1000);
Information("UninstalliOSApplication");
UninstalliOSApplication(
device.UDID,
bundleId,
setting);
Thread.Sleep(5 * 1000);
Information("InstalliOSApplication");
InstalliOSApplication(
device.UDID,
appPath,
setting);
// Delay to allow simctl install to finish, otherwise you can receive the following error:
// The request was denied by service delegate (SBMainWorkspace) for reason:
// Busy ("Application "cake.applesimulator.test-xunit" is installing or uninstalling, and cannot be launched").
Thread.Sleep(5 * 1000);
Information("TestiOSApplication");
var testResults = TestiOSApplication(
device.UDID,
bundleId,
setting);
Information("Test Results:");
Information(string.Format($"Tests Run:{testResults.Run} Passed:{testResults.Passed} Failed:{testResults.Failed} Skipped:{testResults.Skipped} Inconclusive:{testResults.Inconclusive}"));
Information("UninstalliOSApplication");
UninstalliOSApplication(
device.UDID,
bundleId,
setting);
Information("Shutdown");
ShutdownAllAppleSimulators();
if (testResults.Run > 0 && testResults.Failed > 0)
{
Information(string.Format($"Tests Run:{testResults.Run} Passed:{testResults.Passed} Failed:{testResults.Failed} Skipped:{testResults.Skipped} Inconclusive:{testResults.Inconclusive}"));
TestFailuresAbort();
}
};
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup((context) =>
{
// Executed BEFORE the first task.
});
Teardown((context) =>
{
// Executed AFTER the last task.
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("RunningOnMacOS")
.Does (() =>
{
Information(string.Format($"{Context.Environment.Platform.Family}")); // Still reports Linux... :-(
if (!isRunningOnMacOS)
{
NonMacOSAbort();
}
else
{
Information("Running on MacOS");
}
});
Task("RestorePackages")
.IsDependentOn("RunningOnMacOS")
.Does (() =>
{
NuGetRestore(solution);
});
Task("UnitTestingFailExample")
.IsDependentOn("RunningOnMacOS")
.IsDependentOn("RestorePackages")
.Does (() =>
{
buildThisApp(
"./src/Test.NUnit/Test.NUnit.csproj",
"UnitTesting",
"Clean"
);
buildThisApp(
"./src/Test.NUnit/Test.NUnit.csproj",
"UnitTesting",
"Build"
);
unitTestApp(
"cake.applesimulator.test-nunit",
"./artifacts/Test.NUnit.app"
);
});
Task("UnitTestingPassExample")
.IsDependentOn("RunningOnMacOS")
.IsDependentOn("RestorePackages")
.Does (() =>
{
buildThisApp(
"./src/Test.XUnit/Test.XUnit.csproj",
"UnitTesting",
"Clean"
);
buildThisApp(
"./src/Test.XUnit/Test.XUnit.csproj",
"UnitTesting",
"Build"
);
unitTestApp(
"cake.applesimulator.test-xunit",
"./artifacts/Test.XUnit.app"
);
});
RunTarget(target);