forked from killswitch1111/powerguivsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerShellTestExecutor.cs
More file actions
288 lines (239 loc) · 11.2 KB
/
Copy pathPowerShellTestExecutor.cs
File metadata and controls
288 lines (239 loc) · 11.2 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using Microsoft.PowerShell;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using PowerShellTools.TestAdapter.Properties;
using System.Collections.ObjectModel;
namespace PowerShellTools.TestAdapter
{
[ExtensionUri(ExecutorUriString)]
public class PowerShellTestExecutor : ITestExecutor
{
public void RunTests(IEnumerable<string> sources, IRunContext runContext,
IFrameworkHandle frameworkHandle)
{
SetupExecutionPolicy();
IEnumerable<TestCase> tests = PowerShellTestDiscoverer.GetTests(sources, null);
RunTests(tests, runContext, frameworkHandle);
}
private static void SetupExecutionPolicy()
{
SetExecutionPolicy(ExecutionPolicy.RemoteSigned, ExecutionPolicyScope.Process);
}
private static void SetExecutionPolicy(ExecutionPolicy policy, ExecutionPolicyScope scope)
{
ExecutionPolicy currentPolicy = ExecutionPolicy.Undefined;
using (var ps = PowerShell.Create())
{
ps.AddCommand("Get-ExecutionPolicy");
foreach (var result in ps.Invoke())
{
currentPolicy = ((ExecutionPolicy)result.BaseObject);
break;
}
if ((policy <= currentPolicy || currentPolicy == ExecutionPolicy.Bypass) && currentPolicy != ExecutionPolicy.Undefined) //Bypass is the absolute least restrictive, but as added in PS 2.0, and thus has a value of '4' instead of a value that corresponds to it's relative restrictiveness
return;
ps.Commands.Clear();
ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", policy).AddParameter("Scope", scope).AddParameter("Force");
ps.Invoke();
}
}
public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
{
_mCancelled = false;
SetupExecutionPolicy();
foreach (var test in tests)
{
if (_mCancelled) break;
var testResult = new TestResult(test);
testResult.Outcome = TestOutcome.Failed;
testResult.ErrorMessage = Resources.UnexpectedError;
PowerShellTestResult testResultData = null;
var testOutput = new StringBuilder();
try
{
var testAdapter = new TestAdapterHost();
testAdapter.HostUi.OutputString = s => testOutput.Append(s);
var runpsace = RunspaceFactory.CreateRunspace(testAdapter);
runpsace.Open();
using (var ps = PowerShell.Create())
{
ps.Runspace = runpsace;
testResultData = RunTest(ps, test, runContext);
}
}
catch (Exception ex)
{
testResult.Outcome = TestOutcome.Failed;
testResult.ErrorMessage = ex.Message;
testResult.ErrorStackTrace = ex.StackTrace;
}
if (testResultData != null)
{
testResult.Outcome = testResultData.Outcome;
testResult.ErrorMessage = testResultData.ErrorMessage;
testResult.ErrorStackTrace = testResultData.ErrorStacktrace;
}
if (testOutput.Length > 0)
{
frameworkHandle.SendMessage(TestMessageLevel.Informational, testOutput.ToString());
}
frameworkHandle.RecordResult(testResult);
}
}
public void Cancel()
{
_mCancelled = true;
}
public const string ExecutorUriString = "executor://PowerShellTestExecutor/v1";
public static readonly Uri ExecutorUri = new Uri(ExecutorUriString);
private bool _mCancelled;
public PowerShellTestResult RunTest(PowerShell powerShell, TestCase testCase, IRunContext runContext)
{
var module = FindModule("Pester", runContext);
powerShell.AddCommand("Import-Module").AddParameter("Name", module);
powerShell.Invoke();
powerShell.Commands.Clear();
if (powerShell.HadErrors)
{
var errorRecord = powerShell.Streams.Error.FirstOrDefault();
var errorMessage = errorRecord == null ? string.Empty : errorRecord.ToString();
return new PowerShellTestResult(TestOutcome.Failed, Resources.FailedToLoadPesterModule + errorMessage, string.Empty);
}
var fi = new FileInfo(testCase.CodeFilePath);
var tempFile = Path.GetTempFileName();
var describeName = testCase.FullyQualifiedName;
powerShell.AddCommand("Invoke-Pester")
.AddParameter("Path", fi.Directory.FullName)
.AddParameter("TestName", describeName)
.AddParameter("PassThru");
var pesterResults = powerShell.Invoke();
powerShell.Commands.Clear();
// The test results are not necessary stored in the first PSObject.
var results = GetTestResults(pesterResults);
TestOutcome testOutcome = TestOutcome.NotFound;
var error = new StringBuilder();
var stackTrace = new StringBuilder();
foreach (PSObject result in results)
{
var describe = result.Properties["Describe"].Value as string;
var errorMessage = string.Format("Error in {0}", fi.FullName);
// Pester returns either the "describe" or an errorMessage when there was an error/exception while running the script
if (describeName.Equals(describe, StringComparison.OrdinalIgnoreCase) || errorMessage.Equals(describe, StringComparison.OrdinalIgnoreCase))
{
var currentOutcome = GetOutcome(result.Properties["Result"].Value as string);
if (currentOutcome == TestOutcome.Failed)
{
testOutcome = TestOutcome.Failed;
}
else if (testOutcome == TestOutcome.Passed && currentOutcome != TestOutcome.Passed)
{
testOutcome = currentOutcome;
}
else if (testOutcome == TestOutcome.NotFound)
{
testOutcome = currentOutcome;
}
var context = result.Properties["Context"].Value as string;
var name = result.Properties["Name"].Value as string;
var stackTraceString = result.Properties["StackTrace"].Value as string;
var errorString = result.Properties["FailureMessage"].Value as string;
if (!string.IsNullOrEmpty(stackTraceString))
{
stackTrace.AppendLine(string.Format("{0} it {1}\r\n{2}", context, name, stackTraceString));
}
if (!string.IsNullOrEmpty(errorString))
{
error.AppendLine(string.Format("{0} it {1}\r\n{2}", context, name, errorString));
}
}
}
return new PowerShellTestResult(testOutcome, error.ToString(), stackTrace.ToString());
}
private TestOutcome GetOutcome(string testResult)
{
if (string.IsNullOrEmpty(testResult))
{
return TestOutcome.NotFound;
}
if (testResult.Equals("passed", StringComparison.OrdinalIgnoreCase))
{
return TestOutcome.Passed;
}
if (testResult.Equals("skipped", StringComparison.OrdinalIgnoreCase))
{
return TestOutcome.Skipped;
}
if (testResult.Equals("pending", StringComparison.OrdinalIgnoreCase))
{
return TestOutcome.Skipped;
}
return TestOutcome.Failed;
}
protected string FindModule(string moduleName, IRunContext runContext)
{
var pesterPath = GetModulePath(moduleName, runContext.TestRunDirectory);
if (string.IsNullOrEmpty(pesterPath))
{
pesterPath = GetModulePath(moduleName, runContext.SolutionDirectory);
}
if (string.IsNullOrEmpty(pesterPath))
{
pesterPath = moduleName;
}
return pesterPath;
}
/// <summary>
/// Gets test results from the <see cref="PSObject"/> collection.
/// </summary>
/// <param name="psObjects">
/// The <see cref="PSObject"/> collection as returned from the <c>Invoke-Pester</c> command
/// </param>
/// <returns>
/// The test results as <see cref="Array"/>
/// </returns>
private static Array GetTestResults(Collection<PSObject> psObjects)
{
var resultObject = psObjects.Where(o => o.Properties["TestResult"] != null).FirstOrDefault();
return resultObject.Properties["TestResult"].Value as Array;
}
private static string GetModulePath(string moduleName, string root)
{
if (root == null)
return null;
// Default packages path for nuget.
var packagesRoot = Path.Combine(root, "packages");
// TODO: Scour for custom nuget packages paths.
if (Directory.Exists(packagesRoot))
{
var packagePath = Directory.GetDirectories(packagesRoot, moduleName + "*", SearchOption.TopDirectoryOnly).FirstOrDefault();
if (null != packagePath)
{
var psd1 = Path.Combine(packagePath, string.Format(@"tools\{0}.psd1", moduleName));
if (File.Exists(psd1))
{
return psd1;
}
var psm1 = Path.Combine(packagePath, string.Format(@"tools\{0}.psm1", moduleName));
if (File.Exists(psm1))
{
return psm1;
}
var dll = Path.Combine(packagePath, string.Format(@"tools\{0}.dll", moduleName));
if (File.Exists(dll))
{
return dll;
}
}
}
return null;
}
}
}