-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathGlobal.cs
643 lines (536 loc) · 24.8 KB
/
Global.cs
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
using Meadow.Contract;
using Meadow.Core.AccountDerivation;
using Meadow.Core.Utils;
using Meadow.CoverageReport;
using Meadow.CoverageReport.Debugging;
using Meadow.DebugAdapterServer;
using Meadow.DebugAdapterServer.DebuggerTransport;
using Meadow.JsonRpc;
using Meadow.JsonRpc.Client;
using Meadow.JsonRpc.Types;
using Meadow.JsonRpc.Types.Debugging;
using Meadow.TestNode;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
namespace Meadow.UnitTestTemplate
{
public static class Global
{
public static readonly AsyncObjectPool<TestServices> TestServicesPool;
public static readonly ConcurrentBag<(CompoundCoverageMap Coverage, SolcBytecodeInfo Contract)[]> CoverageMaps;
public static readonly ConcurrentBag<UnitTestResult> UnitTestResults;
private static string _callerFilePath;
const string REPORT_DIR = "Report";
/// <summary>
/// Initially populated by app.config (if set).
/// Defaults to 5 000 000 if not set.
/// </summary>
[DefaultValue(5_000_000), DisplayName("DefaultGasLimit")]
[System.ComponentModel.Description("TODO")]
public static long? DefaultGasLimit { get; set; }
/// <summary>
/// Initially populated by app.config (if set).
/// Defaults to 100 000 000 000 if not set.
/// </summary>
[DefaultValue(100_000_000_000), DisplayName("DefaultGasPrice")]
[System.ComponentModel.Description("TODO")]
public static long? DefaultGasPrice { get; set; }
/// <summary>
/// Initially populated by app.config (if set).
/// Defaults to 100 if not set;
/// </summary>
[DefaultValue(100), DisplayName("AccountCount")]
[System.ComponentModel.Description("TODO")]
public static int? AccountCount { get; set; }
/// <summary>
/// Initially populated by app.config (if set).
/// Defaults to 2000 if not set.
/// </summary>
[DefaultValue(2000), DisplayName("AccountBalance")]
[System.ComponentModel.Description("TODO")]
public static long? AccountBalance { get; set; }
/// <summary>
/// Initially populated by app.config (if set).
/// Defaults to a randomly generated mnemonic if not set.
/// </summary>
[DefaultValue(null), DisplayName("AccountMnemonic")]
[System.ComponentModel.Description("TODO")]
public static string AccountMnemonic { get; set; }
/// <summary>
/// Initially populated by app.config (if set). Describes the endpoint of an external node
/// to connect to and tests against.
/// </summary>
[DefaultValue(null), DisplayName("ExternalNodeHost")]
[System.ComponentModel.Description("Describes the endpoint of an external node")]
internal static Uri ExternalNodeHost { get; set; }
/// <summary>
/// Initially populated by app.config (if set). Indicates if the external node should be used in place
/// of tests intended for the built-in node. Enabling this option disables the built-in node, and parallel
/// tests will be run singularly only against the external node.
/// </summary>
[DefaultValue(false), DisplayName("OnlyUseExternalNode")]
[System.ComponentModel.Description("TODO")]
public static bool? OnlyUseExternalNode { get; set; }
[DefaultValue(null), DisplayName("SolcVersion")]
[System.ComponentModel.Description("Currently only used in MSBuild script")]
static string SolcVersion { get; set; }
[DefaultValue(0), DisplayName("SolcOptimizer")]
[System.ComponentModel.Description("Currently only used in MSBuild script")]
static int? SolcOptimizer { get; set; }
/// <summary>
/// Identifies information for tests to be run on an external node.
/// </summary>
public static TestServices ExternalNodeTestServices { get; private set; }
//static readonly Dictionary<string, string[]> AppConfigValues = new Dictionary<string, string[]>();
static (string Key, string Value)[] appConfigValues;
static Action _debuggerCleanup;
static readonly CancellationTokenSource _debuggerCancellationToken;
static Global()
{
if (SolidityDebugger.DebugStopOnEntry && !Debugger.IsAttached)
{
Debugger.Launch();
}
if (!SolidityDebugger.IsSolidityDebuggerAttached && SolidityDebugger.HasSolidityDebugAttachRequest)
{
_debuggerCancellationToken = new CancellationTokenSource();
var namedPipeDebuggerTransport = new NamedPipeServerDebuggerTransport();
var debuggerDisposal = SolidityDebugger.AttachSolidityDebugger(namedPipeDebuggerTransport, _debuggerCancellationToken);
_debuggerCleanup = () =>
{
_debuggerCleanup = null;
debuggerDisposal.Dispose();
};
}
// Hook onto events for after tests have ran so we can call
// cleanup which does report generation.
// Its unclear which of these may or may not work in any given
// execution context and operating system, so hook to both.
// HOWEVER, neither seem to be called while running
// tests through test panels or `dotnet test`
AppDomain.CurrentDomain.ProcessExit += (s, e) => OnProcessExit();
AssemblyLoadContext.Default.Unloading += (s) => OnProcessExit();
// Initialize our test variables.
TestServicesPool = new AsyncObjectPool<TestServices>(CreateTestServicesInstance);
CoverageMaps = new ConcurrentBag<(CompoundCoverageMap Coverage, SolcBytecodeInfo Contract)[]>();
UnitTestResults = new ConcurrentBag<UnitTestResult>();
var thisAsm = Assembly.GetExecutingAssembly().GetName().Name;
var unitTestAssembly = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => !a.IsDynamic && !a.GlobalAssemblyCache)
.Where(a => a.Location.EndsWith(".dll", StringComparison.Ordinal))
.Where(a => File.Exists(a.Location + ".config"))
.Where(a => a.GetReferencedAssemblies().Any(r => r.Name == thisAsm))
.FirstOrDefault();
ParseAppConfigSettings(unitTestAssembly);
//var dtest = Environment.GetEnvironmentVariable("MEADOW_SOLIDITY_DEBUG_SESSION");
//var debugSessionID = Environment.GetEnvironmentVariable("DEBUG_SESSION_ID");
//File.AppendAllLines("/Users/matthewlittle/Desktop/log.txt", new[] { "ENV TEST: " + debugSessionID });
}
/// <summary>
/// Launches MSTest runner. Connects to debugger if detected via env params.
/// </summary>
public static void Launch()
{
var cancelToken = _debuggerCancellationToken?.Token ?? CancellationToken.None;
MSTestRunner.RunAllTests(Assembly.GetExecutingAssembly(), cancelToken);
Console.WriteLine("Tests completed");
}
static void OnProcessExit()
{
// Perform report generation on progrma exit.
// (The cleanup method handles itself when being called multiple times).
GenerateCoverageReport().GetAwaiter().GetResult();
_debuggerCleanup?.Invoke();
}
static void ParseAppConfigSettings(Assembly callingAssembly)
{
if (callingAssembly != null)
{
var assemblyConfig = ConfigurationManager.OpenExeConfiguration(callingAssembly.Location);
appConfigValues = assemblyConfig.AppSettings
.Settings
.Cast<KeyValueConfigurationElement>()
.Select(s => (s.Key, s.Value))
.ToArray();
}
else
{
appConfigValues = Array.Empty<(string Key, string Value)>();
}
var configValueList = appConfigValues.ToList();
SetupConfigValue(
configValueList,
() => OnlyUseExternalNode,
str => bool.Parse(str));
SetupConfigValue(
configValueList,
() => ExternalNodeHost,
str => ServerEndpointParser.Parse(str));
SetupConfigValue(
configValueList,
() => DefaultGasLimit,
str => long.Parse(str, NumberStyles.AllowThousands | NumberStyles.Integer, CultureInfo.InvariantCulture));
SetupConfigValue(
configValueList,
() => DefaultGasPrice,
str => long.Parse(str, NumberStyles.AllowThousands | NumberStyles.Integer, CultureInfo.InvariantCulture));
SetupConfigValue(
configValueList,
() => AccountCount,
str => int.Parse(str, NumberStyles.AllowThousands | NumberStyles.Integer, CultureInfo.InvariantCulture));
SetupConfigValue(
configValueList,
() => AccountBalance,
str => int.Parse(str, NumberStyles.AllowThousands, CultureInfo.InvariantCulture));
SetupConfigValue(
configValueList,
() => AccountMnemonic,
str => str);
SetupConfigValue(
configValueList,
() => SolcVersion,
str => str);
SetupConfigValue(
configValueList,
() => SolcOptimizer,
str => int.Parse(str, NumberStyles.AllowThousands | NumberStyles.Integer, CultureInfo.InvariantCulture));
var unknownConfigOptions = new List<Exception>();
foreach (var opts in configValueList)
{
unknownConfigOptions.Add(new Exception($"Unknown setting in app.config '{opts.Key}={opts.Value}'"));
}
if (unknownConfigOptions.Count == 1)
{
throw unknownConfigOptions[0];
}
else if (unknownConfigOptions.Count > 1)
{
throw new AggregateException("Multiple unknown settings in app.config", unknownConfigOptions.ToArray());
}
if (ExternalNodeHost != null)
{
CreateExternalNodeClient();
}
}
static void SetupConfigValue<TVal>(
List<(string Key, string Value)> configEntries,
Expression<Func<TVal?>> exp,
Func<string, TVal> configParser) where TVal : struct
{
var displayName = AttributeHelper.GetAttribute<DisplayNameAttribute>(exp).DisplayName;
SetupConfigValue(
configEntries,
displayName,
configParser,
() => AttributeHelper.GetDefault(exp),
v => ExpressionUtil.GetSetter(exp)(v));
}
static void SetupConfigValue<TVal>(
List<(string Key, string Value)> configEntries,
Expression<Func<TVal>> exp,
Func<string, TVal> configParser)
{
var displayName = AttributeHelper.GetAttribute<DisplayNameAttribute>(exp).DisplayName;
SetupConfigValue(
configEntries,
displayName,
configParser,
() => AttributeHelper.GetDefault(exp),
v => ExpressionUtil.GetSetter(exp)(v));
}
static void SetupConfigValue<TVal>(
List<(string Key, string Value)> configEntries,
string displayName,
Func<string, TVal> configParser,
Func<TVal> getDefaultValue,
Action<TVal> setValue)
{
(string Key, string Value) configEntry = default;
for (var i = 0; i < configEntries.Count; i++)
{
if (configEntries[i].Key.Equals(displayName, StringComparison.OrdinalIgnoreCase))
{
configEntry = configEntries[i];
configEntries.RemoveAt(i);
break;
}
}
TVal resultValue;
if (!string.IsNullOrWhiteSpace(configEntry.Value))
{
try
{
resultValue = configParser(configEntry.Value);
}
catch (Exception ex)
{
throw new Exception($"Could not parse app.config '{displayName}' value '{configEntry.Value}'", ex);
}
}
else
{
resultValue = getDefaultValue();
}
setValue(resultValue);
}
static bool TryGetAppConfigValue(string key, out string value)
{
var match = appConfigValues.FirstOrDefault(c => c.Key.Equals(key, StringComparison.OrdinalIgnoreCase));
if (match.Value != null)
{
if (!string.IsNullOrWhiteSpace(match.Value))
{
value = match.Value;
return true;
}
}
value = null;
return false;
}
public static async Task<TestServices> CreateTestServicesInstance()
{
// Setup account derivation / keys.
var mnemonic = AccountMnemonic ?? AttributeHelper.GetDefault(() => AccountMnemonic);
var accountDerivation = string.IsNullOrWhiteSpace(mnemonic)
? HDAccountDerivation.Create()
: new HDAccountDerivation(mnemonic);
// Create our local test node.
var accountConfig = new AccountConfiguration
{
AccountGenerationCount = AccountCount ?? AttributeHelper.GetDefault(() => AccountCount),
DefaultAccountEtherBalance = AccountBalance ?? AttributeHelper.GetDefault(() => AccountBalance),
AccountDerivationMethod = accountDerivation
};
var testNodeServer = new TestNodeServer(accountConfig: accountConfig);
// Start our local test node.
await testNodeServer.RpcServer.StartAsync();
// Create an RPC client for our local test node.
var serverPort = testNodeServer.RpcServer.ServerPort;
var localServerUri = new Uri($"http://{IPAddress.Loopback}:{serverPort}");
var jsonRpcClient = JsonRpcClient.Create(
localServerUri,
defaultGasLimit: DefaultGasLimit ?? AttributeHelper.GetDefault(() => DefaultGasLimit),
defaultGasPrice: DefaultGasPrice ?? AttributeHelper.GetDefault(() => DefaultGasPrice));
jsonRpcClient.ErrorFormatter = GetExecutionTraceException;
// Cache our accounts for our test node.
var accounts = await jsonRpcClient.Accounts();
// Enable coverage and tracing on the main node.
await jsonRpcClient.SetCoverageEnabled(true);
await jsonRpcClient.SetTracingEnabled(true);
// Return new test services with the main client information we created.
return new TestServices(jsonRpcClient, testNodeServer, accounts);
}
private static void CreateExternalNodeClient()
{
// If we have an external test node configured, but it's not instantiated yet, we do that here.
if (ExternalNodeHost != null && ExternalNodeTestServices == null)
{
// Create an RPC client for our external test node.
var externalServerUri = ExternalNodeHost;
var externalNodeClient = JsonRpcClient.Create(
externalServerUri,
defaultGasLimit: DefaultGasLimit ?? AttributeHelper.GetDefault(() => DefaultGasLimit),
defaultGasPrice: DefaultGasPrice ?? AttributeHelper.GetDefault(() => DefaultGasPrice));
// Cache our accounts for our external test node.
var externalAccounts = externalNodeClient.Accounts().Result;
// Initialize our external node test services
ExternalNodeTestServices = new TestServices(externalNodeClient, null, externalAccounts);
}
}
static async Task<Exception> GetExecutionTraceException(IJsonRpcClient rpcClient, JsonRpcError error)
{
var executionTrace = await rpcClient.GetExecutionTrace();
var traceAnalysis = new ExecutionTraceAnalysis(executionTrace);
// Build our aggregate exception
var aggregateException = traceAnalysis.GetAggregateException(error.ToException());
if (aggregateException == null)
{
throw new Exception("RPC error occurred with tracing enabled but no exceptions could be found in the trace data. Please report this issue.", error.ToException());
}
return aggregateException;
}
[Obsolete("This no longer needs to be called.")]
public static Task Init(TestContext testContext)
{
return Task.CompletedTask;
}
static readonly ConcurrentBag<string> _reportBlacklist = new ConcurrentBag<string>();
/// <summary>
/// Specify solidity files or directories to be ignored from the test report.
/// </summary>
public static void HideSolidityFromReport(params string[] directoryOrSolidityFile)
{
foreach (var item in directoryOrSolidityFile)
{
_reportBlacklist.Add(item);
}
}
static readonly object _cleanupSyncRoot = new object();
static bool _didCleanup = false;
[Obsolete("This function has been replaced with 'GenerateCoverageReport()'")]
public static Task Cleanup() => GenerateCoverageReport();
public static async Task GenerateCoverageReport()
{
lock (_cleanupSyncRoot)
{
if (_didCleanup)
{
return;
}
else
{
_didCleanup = true;
}
}
var reportGeneratorExceptions = new List<Exception>();
try
{
var callerFilePath = Directory.GetCurrentDirectory();
var normalizedPath = callerFilePath.Replace('\\', '/');
const string OUTPUT_DIR_DEBUG = "bin/Debug/netcoreapp2.1";
const string OUTPUT_DIR_RELEASE = "bin/Release/netcoreapp2.1";
if (normalizedPath.EndsWith(OUTPUT_DIR_DEBUG, StringComparison.OrdinalIgnoreCase))
{
callerFilePath = callerFilePath.Substring(0, callerFilePath.Length - OUTPUT_DIR_DEBUG.Length);
}
else if (normalizedPath.EndsWith(OUTPUT_DIR_RELEASE, StringComparison.OrdinalIgnoreCase))
{
callerFilePath = callerFilePath.Substring(0, callerFilePath.Length - OUTPUT_DIR_RELEASE.Length);
}
_callerFilePath = callerFilePath;
await CleanupInternal(reportGeneratorExceptions);
}
catch (Exception ex)
{
reportGeneratorExceptions.Add(ex);
}
if (reportGeneratorExceptions.Count > 0)
{
var ex = reportGeneratorExceptions[0];
if (reportGeneratorExceptions.Count > 1)
{
ex = new AggregateException(reportGeneratorExceptions);
}
File.WriteAllText($"post-test-exception-{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss", CultureInfo.InvariantCulture)}.txt", "Post-test exception: " + ex.ToString());
throw ex;
}
}
static string GetProjectDirectory()
{
var projDir = Path.GetDirectoryName(_callerFilePath);
string path;
if (Directory.EnumerateFiles(projDir, "*.csproj", SearchOption.TopDirectoryOnly).Any())
{
path = Path.GetFullPath(Path.Combine(projDir, REPORT_DIR));
}
else
{
path = REPORT_DIR;
}
MiscUtil.ResetDirectory(path);
return path;
}
static async Task CleanupInternal(List<Exception> catchExceptions)
{
var reportOutputDirectory = GetProjectDirectory();
// Find the generated class field that contains the solc source data
var sourcesData = GeneratedSolcData.Default.GetSolcData();
var solidityCompilerVersion = GeneratedSolcData.Default.SolidityCompilerVersion;
var flattenedCoverageMaps = CoverageMaps.SelectMany(c => c).ToArray();
var unitTestResults = UnitTestResults.GroupBy(t => t.Namespace).OrderBy(t => t.Key).ToArray();
// Determine any solidity files or directories that should not be included in the report
var ignoreSolidityFiles = new List<string>();
var ignoreSolidityDirectories = new List<string>();
foreach (var ignorePath in _reportBlacklist)
{
// Normalize path separator, and set lowercase
var normalizedIgnorePath = ignorePath.Replace('\\', '/').Trim('/').ToLowerInvariant();
if (normalizedIgnorePath.EndsWith(".sol", StringComparison.OrdinalIgnoreCase))
{
ignoreSolidityFiles.Add(normalizedIgnorePath);
}
else
{
ignoreSolidityDirectories.Add(normalizedIgnorePath + "/");
}
}
var solcSourceInfos = new List<SolcSourceInfo>();
var ignoredSourceFiles = new List<string>();
foreach (var sourceInfo in sourcesData.SolcSourceInfo)
{
var lowercaseSourceFile = sourceInfo.FileName.ToLowerInvariant();
// File is explicitly ignored
if (ignoreSolidityFiles.Contains(lowercaseSourceFile))
{
ignoredSourceFiles.Add(sourceInfo.FileName);
continue;
}
// Source file directory is ignored
if (ignoreSolidityDirectories.Any(d => lowercaseSourceFile.StartsWith(d, StringComparison.Ordinal)))
{
ignoredSourceFiles.Add(sourceInfo.FileName);
continue;
}
solcSourceInfos.Add(sourceInfo);
}
// Generate report from coverage data
ReportGenerator.CreateReport(
solidityCompilerVersion,
flattenedCoverageMaps,
solcSourceInfos.ToArray(),
sourcesData.SolcBytecodeInfo,
unitTestResults,
ignoredSourceFiles.ToArray(),
reportOutputDirectory,
catchExceptions);
var reportFilePath = Path.GetFullPath(Path.Combine(reportOutputDirectory, ReportGenerator.REPORT_INDEX_FILE));
var reportFileUri = MiscUtil.GetFileUrl(reportFilePath);
var reportGeneratedMessage = $"Coverage report generated at:{Environment.NewLine}{reportFileUri}";
Console.WriteLine(reportGeneratedMessage);
System.Diagnostics.Debug.WriteLine(reportGeneratedMessage);
var shouldOpenReport = Environment.GetEnvironmentVariable("OPEN_REPORT") == "TRUE";
if (shouldOpenReport)
{
MiscUtil.OpenBrowser(reportFilePath);
}
// Dispose of all test servers.
// A shutdown can take a couple seconds so start all the shutdown
// tasks at once, then do the waiting in the Dispose afterwards.
var testServices = await TestServicesPool.GetItemsAsync();
foreach (var testService in testServices)
{
#pragma warning disable CS4014
testService.TestNodeServer.RpcServer.StopAsync();
#pragma warning restore CS4014
}
foreach (var testService in testServices)
{
testService.TestNodeServer.Dispose();
}
var shouldPause = Environment.GetEnvironmentVariable("PAUSE_ON_COMPLETE") == "TRUE";
if (shouldPause)
{
Console.ReadLine();
//Process.GetCurrentProcess().WaitForExit();
}
}
}
}