Skip to content

Commit f90fe39

Browse files
authored
CLR helper now updates local CLR instance (#203)
***NO_CI***
1 parent 860df6f commit f90fe39

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

source/TestAdapter/Executor.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrame
8080
try
8181
{
8282
InitializeLogger(runContext, frameworkHandle);
83-
83+
8484
foreach (var source in sources)
8585
{
8686
var testsCases = TestDiscoverer.ComposeTestCases(source);
@@ -198,8 +198,8 @@ private async Task<List<TestResult>> RunTestOnHardwareAsync(List<TestCase> tests
198198
var serialDebugClient = PortBase.CreateInstanceForSerial(true, 2000);
199199

200200
retryConnection:
201-
202-
if(string.IsNullOrEmpty(_settings.RealHardwarePort))
201+
202+
if (string.IsNullOrEmpty(_settings.RealHardwarePort))
203203
{
204204
_logger.LogMessage($"Waiting for device enumeration to complete.", Settings.LoggingLevel.Verbose);
205205
}
@@ -241,7 +241,7 @@ private async Task<List<TestResult>> RunTestOnHardwareAsync(List<TestCase> tests
241241
device = serialDebugClient.NanoFrameworkDevices.FirstOrDefault(m => m.SerialNumber == _settings.RealHardwarePort);
242242

243243
// sanity check
244-
if(device is null)
244+
if (device is null)
245245
{
246246
// no device, done here
247247
_logger.LogMessage($"No device available at {_settings.RealHardwarePort}.", Settings.LoggingLevel.Verbose);
@@ -595,6 +595,14 @@ private async Task<List<TestResult>> RunTestOnEmulatorAsync(
595595
return results;
596596
}
597597

598+
// update nanoCLR instance, if not running a local one
599+
if (string.IsNullOrEmpty(_settings.PathToLocalCLRInstance))
600+
{
601+
NanoCLRHelper.UpdateNanoCLRInstance(
602+
_settings.CLRVersion,
603+
_logger);
604+
}
605+
598606
_logger.LogMessage(
599607
"Processing assemblies to load into test runner...",
600608
Settings.LoggingLevel.Verbose);
@@ -621,7 +629,7 @@ private async Task<List<TestResult>> RunTestOnEmulatorAsync(
621629
}
622630

623631
// if requested, set diagnostic output
624-
if(_settings.Logging > Settings.LoggingLevel.None)
632+
if (_settings.Logging > Settings.LoggingLevel.None)
625633
{
626634
arguments.Append(" -v diag");
627635
}
@@ -640,7 +648,7 @@ private async Task<List<TestResult>> RunTestOnEmulatorAsync(
640648
{
641649
var cliResult = await cmd.ExecuteBufferedAsync(cts.Token);
642650
var exitCode = cliResult.ExitCode;
643-
651+
644652
// read standard output
645653
var output = cliResult.StandardOutput;
646654

@@ -684,7 +692,7 @@ private async Task<List<TestResult>> RunTestOnEmulatorAsync(
684692

685693
results.First().Outcome = TestOutcome.Failed;
686694
results.First().ErrorMessage = $"nanoCLR execution ended with exit code: {exitCode}. Check log for details.";
687-
695+
688696
return results;
689697
}
690698
}
@@ -793,7 +801,7 @@ private void ParseTestResults(string rawOutput, List<TestResult> results)
793801
var allTestToSkip = results.Where(m => m.TestCase.FullyQualifiedName.Contains(testCasesToSkipName));
794802
foreach (var testToSkip in allTestToSkip)
795803
{
796-
if(testToSkip.TestCase.FullyQualifiedName == resultDataSet[1])
804+
if (testToSkip.TestCase.FullyQualifiedName == resultDataSet[1])
797805
{
798806
continue;
799807
}

source/TestAdapter/NanoCLRHelper.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,56 @@ public static bool InstallNanoClr(LogMessenger logger)
7575
// report outcome
7676
return NanoClrIsInstalled;
7777
}
78+
79+
public static void UpdateNanoCLRInstance(
80+
string clrVersion,
81+
LogMessenger logger)
82+
{
83+
logger.LogMessage(
84+
"Upate nanoCLR instance",
85+
Settings.LoggingLevel.Verbose);
86+
87+
var arguments = "instance --update";
88+
89+
if (!string.IsNullOrEmpty(clrVersion))
90+
{
91+
arguments += $" --clrversion {clrVersion}";
92+
}
93+
94+
var cmd = Cli.Wrap("nanoclr")
95+
.WithArguments(arguments)
96+
.WithValidation(CommandResultValidation.None);
97+
98+
// setup cancellation token with a timeout of 1 minute
99+
using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)))
100+
{
101+
var cliResult = cmd.ExecuteBufferedAsync(cts.Token).Task.Result;
102+
103+
if (cliResult.ExitCode == 0)
104+
{
105+
// this will be either (on update):
106+
// Updated to v1.8.1.102
107+
// or (on same version):
108+
// Already at v1.8.1.102
109+
var regexResult = Regex.Match(cliResult.StandardOutput, @"((?>version )(?'version'\d+\.\d+\.\d+))");
110+
111+
if (regexResult.Success)
112+
{
113+
logger.LogMessage($"nanoCLR instance updated to v{regexResult.Groups["version"].Value}",
114+
Settings.LoggingLevel.Verbose);
115+
}
116+
else
117+
{
118+
logger.LogPanicMessage($"*** Failed to update nanoCLR instance. {cliResult.StandardOutput}.");
119+
}
120+
}
121+
else
122+
{
123+
logger.LogMessage(
124+
$"Failed to update nanoCLR instance. Exit code {cliResult.ExitCode}.",
125+
Settings.LoggingLevel.Detailed);
126+
}
127+
}
128+
}
78129
}
79130
}

source/TestAdapter/Settings.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public class Settings
2929
/// </summary>
3030
public string PathToLocalCLRInstance { get; set; } = string.Empty;
3131

32+
/// <summary>
33+
/// Version of nanoCLR instance to use when running Unit Tests.
34+
/// </summary>
35+
public string CLRVersion { get; set; } = string.Empty;
36+
3237
/// <summary>
3338
/// Level of logging for test execution.
3439
/// </summary>
@@ -65,6 +70,12 @@ public static Settings Extract(XmlNode node)
6570
settings.Logging = logging;
6671
}
6772
}
73+
74+
var clrversion = node.SelectSingleNode(nameof(CLRVersion))?.FirstChild;
75+
if (clrversion != null && clrversion.NodeType == XmlNodeType.Text)
76+
{
77+
settings.CLRVersion = clrversion.Value;
78+
}
6879
}
6980

7081
return settings;

source/runsettings/nano.runsettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<nanoFrameworkAdapter>
1212
<Logging>None</Logging>
1313
<IsRealHardware>False</IsRealHardware>
14+
<CLRVersion></CLRVersion><!--Specify the nanoCLR version to use. If not specified, the latest available will be used. -->
1415
</nanoFrameworkAdapter>
1516
</RunSettings>

0 commit comments

Comments
 (0)