Skip to content

Commit ab17999

Browse files
authored
Firmware archive related bug fixes (#307)
1 parent b5020e6 commit ab17999

File tree

6 files changed

+113
-38
lines changed

6 files changed

+113
-38
lines changed

nanoFirmwareFlasher.Library/ExitCodes.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,13 @@ public enum ExitCodes
339339
/// <summary>
340340
/// Error clearing cache location.
341341
/// </summary>
342-
[Display(Name = "Error occured when clearing the firmware cache location.")]
342+
[Display(Name = "Error occurred when clearing the firmware cache location.")]
343343
E9014 = 9014,
344+
345+
/// <summary>
346+
/// Can't find the target in the firmware archive.
347+
/// </summary>
348+
[Display(Name = "Can't find the target in the firmware archive.")]
349+
E9015 = 9015,
344350
}
345351
}

nanoFirmwareFlasher.Library/FirmwareArchiveManager.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,40 @@ public List<CloudSmithPackageDetail> GetTargetList(
6767
return targetPackages;
6868
}
6969

70+
/// <summary>
71+
/// Get the latest version present in the firmware archive for the specified target.
72+
/// </summary>
73+
/// <param name="preview">Option for preview version.</param>
74+
/// <param name="target">Target to find the latest version of.</param>
75+
/// <returns>The <see cref="CloudSmithPackageDetail"/> with details on latest firmware package for the target, or <see langword="null"/> if none is present.</returns>
76+
public CloudSmithPackageDetail GetLatestVersion(
77+
bool preview,
78+
string target)
79+
{
80+
CloudSmithPackageDetail result = null;
81+
Version latest = null;
82+
if (Directory.Exists(_archivePath) && !string.IsNullOrEmpty(target))
83+
{
84+
foreach (string filePath in Directory.EnumerateFiles(_archivePath, $"{target}-*{INFOFILE_EXTENSION}"))
85+
{
86+
PersistedPackageInformation packageInformation = JsonConvert.DeserializeObject<PersistedPackageInformation>(File.ReadAllText(filePath));
87+
if (packageInformation is not null && packageInformation.IsPreview == preview)
88+
{
89+
if (Version.TryParse(packageInformation.Version, out Version version))
90+
{
91+
if (latest is null || latest < version)
92+
{
93+
latest = version;
94+
result = packageInformation;
95+
}
96+
}
97+
}
98+
}
99+
}
100+
101+
return result;
102+
}
103+
70104
/// <summary>
71105
/// Download a firmware package from the repository and add it to the archive directory
72106
/// </summary>

nanoFirmwareFlasher.Library/FirmwarePackage.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,18 @@ void UpdateLocationPathAndFileName(bool initial)
344344

345345
}
346346

347+
if (archiveDirectoryPath is not null && Version is null)
348+
{
349+
// Find the latest version in the archive directory
350+
var archiveManager = new FirmwareArchiveManager(archiveDirectoryPath);
351+
Version = archiveManager.GetLatestVersion(_preview, _targetName)?.Version;
352+
if (Version is null)
353+
{
354+
// Package is considered to be not present, even if it does exist in the cache location
355+
return (ExitCodes.E9015, null);
356+
}
357+
}
358+
347359
// create the download folder
348360
try
349361
{
@@ -369,7 +381,7 @@ void UpdateLocationPathAndFileName(bool initial)
369381
if (!File.Exists(archiveFileName))
370382
{
371383
// Package is considered to be not present, even if it does exist in the cache location
372-
return (ExitCodes.E9007, null);
384+
return (ExitCodes.E9015, null);
373385
}
374386

375387
if (!skipDownload)

nanoFirmwareFlasher.Tests/FirmwarePackageTests.cs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ where d.StartsWith(targetName + "-")
194194

195195
[TestMethod]
196196
[TestCategory("CloudSmith")]
197-
public void FirmwarePackage_DebugFirmware_Download()
197+
public void FirmwarePackage_LegacyVirtualDevice_Download()
198198
{
199199
#region Setup
200200
using var output = new OutputWriterHelper();
@@ -236,32 +236,55 @@ public void FirmwarePackage_FromArchive()
236236
{
237237
Assert.Inconclusive("Cannot download the ESP32 package.");
238238
}
239-
string testTargetName = "NO_REAL_TARGET";
240-
string testVersion = "0.0.0.0";
241-
File.Copy(Path.Combine(cacheDirectory, package.Name, $"{package.Name}-{package.Version}.zip"), Path.Combine(archiveDirectory, $"{testTargetName}-{testVersion}.zip"));
239+
string testTarget1Name = "TARGET1_IN_ARCHIVE_ONLY";
240+
string testTarget2Name = "TARGET2_IN_ARCHIVE_ONLY";
241+
string testVersion = "1.23.4.5";
242+
string testOldVersion = "1.6.7.8";
243+
244+
// Copy the package to the archive directory, modified for TARGET1_IN_ARCHIVE_ONLY and TARGET2_IN_ARCHIVE_ONLY
245+
File.Copy(Path.Combine(cacheDirectory, package.Name, $"{package.Name}-{package.Version}.zip"), Path.Combine(archiveDirectory, $"{testTarget1Name}-{testVersion}.zip"));
246+
File.WriteAllText(Path.Combine(archiveDirectory, $"{testTarget1Name}-{testVersion}.zip.json"), $@"{{ ""Name"": ""{testTarget1Name}"", ""Version"": ""{testVersion}"", ""Platform"": ""esp32"" }}");
247+
File.Copy(Path.Combine(cacheDirectory, package.Name, $"{package.Name}-{package.Version}.zip"), Path.Combine(archiveDirectory, $"{testTarget2Name}-{testVersion}.zip"));
248+
File.WriteAllText(Path.Combine(archiveDirectory, $"{testTarget2Name}-{testVersion}.zip.json"), $@"{{ ""Name"": ""{testTarget2Name}"", ""Version"": ""{testVersion}"", ""Platform"": ""esp32"" }}");
249+
250+
// Create an invalid package for an earlier version of TARGET2_IN_ARCHIVE_ONLY - this should not be used
251+
File.WriteAllText(Path.Combine(archiveDirectory, $"{testTarget2Name}-{testOldVersion}.zip"), "");
252+
File.WriteAllText(Path.Combine(archiveDirectory, $"{testTarget2Name}-{testOldVersion}.zip.json"), $@"{{ ""Name"": ""{testTarget2Name}"", ""Version"": ""{testOldVersion}"", ""Platform"": ""esp32"" }}");
242253
#endregion
243254

244-
#region Get package that exist in the archive directory but not in the repository
255+
#region Get package that exist in the archive directory but not in the repository; include version
245256
output.Reset();
246-
var actual = new Esp32Firmware(testTargetName, testVersion, false, null);
257+
var actual = new Esp32Firmware(testTarget1Name, testVersion, false, null);
247258
exitCode = actual.DownloadAndExtractAsync(archiveDirectory).GetAwaiter().GetResult();
248259

249260
Assert.AreEqual(ExitCodes.OK, exitCode);
250261
output.AssertAreEqual("");
251-
Assert.IsTrue(Directory.Exists(Path.Combine(cacheDirectory, testTargetName)));
252-
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, testTargetName, $"{testTargetName}-{testVersion}.zip")));
253-
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, testTargetName, "nanoCLR.bin")));
262+
Assert.IsTrue(Directory.Exists(Path.Combine(cacheDirectory, testTarget1Name)));
263+
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, testTarget1Name, $"{testTarget1Name}-{testVersion}.zip")));
264+
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, testTarget1Name, "nanoCLR.bin")));
265+
#endregion
266+
267+
#region Get package that exist in the archive directory but not in the repository; do not include version
268+
output.Reset();
269+
actual = new Esp32Firmware(testTarget2Name, null, false, null);
270+
exitCode = actual.DownloadAndExtractAsync(archiveDirectory).GetAwaiter().GetResult();
271+
272+
Assert.AreEqual(ExitCodes.OK, exitCode);
273+
output.AssertAreEqual("");
274+
Assert.IsTrue(Directory.Exists(Path.Combine(cacheDirectory, testTarget2Name)));
275+
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, testTarget2Name, $"{testTarget2Name}-{testVersion}.zip")));
276+
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, testTarget2Name, "nanoCLR.bin")));
254277
#endregion
255278

256279
#region Get package that does not exist in the archive directory
257-
testTargetName = "MISSING_TARGET";
280+
testTarget1Name = "MISSING_TARGET";
258281

259-
actual = new Esp32Firmware(testTargetName, testVersion, false, null);
282+
actual = new Esp32Firmware(testTarget1Name, testVersion, false, null);
260283
exitCode = actual.DownloadAndExtractAsync(archiveDirectory).GetAwaiter().GetResult();
261284

262-
Assert.AreEqual(ExitCodes.E9007, exitCode);
285+
Assert.AreEqual(ExitCodes.E9015, exitCode);
263286
output.AssertAreEqual("");
264-
Assert.IsFalse(File.Exists(Path.Combine(cacheDirectory, testTargetName, $"{testTargetName}-{testVersion}.zip")));
287+
Assert.IsFalse(File.Exists(Path.Combine(cacheDirectory, testTarget1Name, $"{testTarget1Name}-{testVersion}.zip")));
265288
#endregion
266289
}
267290
}

nanoFirmwareFlasher.Tests/ToolFirmwareArchiveTests.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ public void FirmwareArchive_Platform_UpdateArchive_ListTargets()
3333
#endregion
3434

3535
#region List empty archive
36-
int actual = Program.Main(["--listtargets", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fromfwarchive", "--fwarchivepath", archiveDirectory])
36+
int actual = Program.Main(["--listtargets", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fromarchive", "--archivepath", archiveDirectory])
3737
.GetAwaiter().GetResult();
3838
Assert.AreEqual((int)ExitCodes.OK, actual);
3939
#endregion
4040

4141
#region Update archive
4242
output.Reset();
43-
actual = Program.Main(["--updatefwarchive", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fwarchivepath", archiveDirectory])
43+
actual = Program.Main(["--updatearchive", "--platform", $"{SupportedPlatform.ti_simplelink}", "--archivepath", archiveDirectory])
4444
.GetAwaiter().GetResult();
4545
Assert.AreEqual((int)ExitCodes.OK, actual);
4646
#endregion
4747

4848
#region List filled archive
4949
output.Reset();
50-
actual = Program.Main(["--listtargets", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fromfwarchive", "--fwarchivepath", archiveDirectory])
50+
actual = Program.Main(["--listtargets", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fromarchive", "--archivepath", archiveDirectory])
5151
.GetAwaiter().GetResult();
5252
Assert.AreEqual((int)ExitCodes.OK, actual);
5353

@@ -70,14 +70,14 @@ public void FirmwareArchive_Target_UpdateArchive_ListTargets()
7070

7171
#region Update archive
7272
output.Reset();
73-
int actual = Program.Main(["--updatefwarchive", "--target", $"{allPackages[0].Name}", "--fwarchivepath", archiveDirectory])
73+
int actual = Program.Main(["--updatearchive", "--target", $"{allPackages[0].Name}", "--archivepath", archiveDirectory])
7474
.GetAwaiter().GetResult();
7575
Assert.AreEqual((int)ExitCodes.OK, actual);
7676
#endregion
7777

7878
#region List filled archive
7979
output.Reset();
80-
actual = Program.Main(["--listtargets", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fromfwarchive", "--fwarchivepath", archiveDirectory])
80+
actual = Program.Main(["--listtargets", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fromarchive", "--archivepath", archiveDirectory])
8181
.GetAwaiter().GetResult();
8282
Assert.AreEqual((int)ExitCodes.OK, actual);
8383

@@ -96,11 +96,11 @@ public void FirmwareArchive_ListTargets_InvalidArguments()
9696
string testDirectory = TestDirectoryHelper.GetTestDirectory(TestContext);
9797
string archiveDirectory = Path.Combine(testDirectory, "archive");
9898

99-
int actual = Program.Main(["--listtargets", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fromfwarchive", "--verbosity", "diagnostic"])
99+
int actual = Program.Main(["--listtargets", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fromarchive", "--verbosity", "diagnostic"])
100100
.GetAwaiter().GetResult();
101101

102102
Assert.AreEqual((int)ExitCodes.E9000, actual);
103-
Assert.IsTrue(output.Output.Contains("--fwarchivepath is required when --fromfwarchive is specified."));
103+
Assert.IsTrue(output.Output.Contains("--archivepath is required when --fromarchive is specified."));
104104
}
105105

106106
[TestMethod]
@@ -110,25 +110,25 @@ public void FirmwareArchive_UpdateArchive_InvalidArguments()
110110
string testDirectory = TestDirectoryHelper.GetTestDirectory(TestContext);
111111
string archiveDirectory = Path.Combine(testDirectory, "archive");
112112

113-
int actual = Program.Main(["--updatefwarchive", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fromfwarchive", "--verbosity", "diagnostic"])
113+
int actual = Program.Main(["--updatearchive", "--platform", $"{SupportedPlatform.ti_simplelink}", "--fromarchive", "--verbosity", "diagnostic"])
114114
.GetAwaiter().GetResult();
115115

116116
Assert.AreEqual((int)ExitCodes.E9000, actual);
117-
Assert.IsTrue(output.Output.Contains("Incompatible option --fromfwarchive combined with --updatefwarchive."));
117+
Assert.IsTrue(output.Output.Contains("Incompatible option --fromarchive combined with --updatearchive."));
118118

119119
output.Reset();
120-
actual = Program.Main(["--updatefwarchive", "--platform", $"{SupportedPlatform.ti_simplelink}", "--verbosity", "diagnostic"])
120+
actual = Program.Main(["--updatearchive", "--platform", $"{SupportedPlatform.ti_simplelink}", "--verbosity", "diagnostic"])
121121
.GetAwaiter().GetResult();
122122

123123
Assert.AreEqual((int)ExitCodes.E9000, actual);
124-
Assert.IsTrue(output.Output.Contains("--fwarchivepath is required when --updatefwarchive is specified."));
124+
Assert.IsTrue(output.Output.Contains("--archivepath is required when --updatearchive is specified."));
125125

126126
output.Reset();
127-
actual = Program.Main(["--updatefwarchive", "--fwarchivepath", $"{SupportedPlatform.ti_simplelink}", "--verbosity", "diagnostic"])
127+
actual = Program.Main(["--updatearchive", "--archivepath", $"{SupportedPlatform.ti_simplelink}", "--verbosity", "diagnostic"])
128128
.GetAwaiter().GetResult();
129129

130130
Assert.AreEqual((int)ExitCodes.E9000, actual);
131-
Assert.IsTrue(output.Output.Contains("--platform or --target is required when --updatefwarchive is specified."));
131+
Assert.IsTrue(output.Output.Contains("--platform or --target is required when --updatearchive is specified."));
132132
}
133133

134134
[TestMethod]
@@ -138,18 +138,18 @@ public void FirmwareArchive_UpdateFirmware_InvalidArguments()
138138
string testDirectory = TestDirectoryHelper.GetTestDirectory(TestContext);
139139
string archiveDirectory = Path.Combine(testDirectory, "archive");
140140

141-
int actual = Program.Main(["--serialport", "COM3", "--target", "SOME_TARGET", "--fromfwarchive", "--verbosity", "diagnostic"])
141+
int actual = Program.Main(["--serialport", "COM3", "--target", "SOME_TARGET", "--fromarchive", "--verbosity", "diagnostic"])
142142
.GetAwaiter().GetResult();
143143

144144
Assert.AreEqual((int)ExitCodes.E9000, actual);
145-
Assert.IsTrue(output.Output.Contains("--fwarchivepath is required when --fromfwarchive is specified."));
145+
Assert.IsTrue(output.Output.Contains("--archivepath is required when --fromarchive is specified."));
146146

147147
output.Reset();
148-
actual = Program.Main(["--serialport", "COM3", "--target", "SOME_TARGET", "--fwarchivepath", archiveDirectory, "--verbosity", "diagnostic"])
148+
actual = Program.Main(["--serialport", "COM3", "--target", "SOME_TARGET", "--archivepath", archiveDirectory, "--verbosity", "diagnostic"])
149149
.GetAwaiter().GetResult();
150150

151151
Assert.AreEqual((int)ExitCodes.E9000, actual);
152-
Assert.IsTrue(output.Output.Contains("--fromfwarchive is required when --fwarchivepath is specified."));
152+
Assert.IsTrue(output.Output.Contains("--fromarchive is required when --archivepath is specified."));
153153
}
154154
}
155155
}

nanoFirmwareFlasher.Tool/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
289289
if (string.IsNullOrEmpty(o.FwArchivePath))
290290
{
291291
_exitCode = ExitCodes.E9000;
292-
_extraMessage = "--fwarchivepath is required when --fromfwarchive is specified.";
292+
_extraMessage = "--archivepath is required when --fromarchive is specified.";
293293
return;
294294
}
295295

@@ -558,20 +558,20 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
558558
if (o.FromFwArchive)
559559
{
560560
_exitCode = ExitCodes.E9000;
561-
_extraMessage = "Incompatible option --fromfwarchive combined with --updatefwarchive.";
561+
_extraMessage = "Incompatible option --fromarchive combined with --updatearchive.";
562562
return;
563563
}
564564
if (string.IsNullOrEmpty(o.FwArchivePath))
565565
{
566566
_exitCode = ExitCodes.E9000;
567-
_extraMessage = $"--fwarchivepath is required when --updatefwarchive is specified.";
567+
_extraMessage = $"--archivepath is required when --updatearchive is specified.";
568568
return;
569569
}
570570

571571
if (o.Platform is null && string.IsNullOrEmpty(o.TargetName))
572572
{
573573
_exitCode = ExitCodes.E9000;
574-
_extraMessage = $"--platform or --target is required when --updatefwarchive is specified.";
574+
_extraMessage = $"--platform or --target is required when --updatearchive is specified.";
575575
return;
576576
}
577577

@@ -591,14 +591,14 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
591591
if (o.FromFwArchive)
592592
{
593593
_exitCode = ExitCodes.E9000;
594-
_extraMessage = $"--fwarchivepath is required when --fromfwarchive is specified.";
594+
_extraMessage = $"--archivepath is required when --fromarchive is specified.";
595595
return;
596596
}
597597
}
598598
else if (!o.FromFwArchive)
599599
{
600600
_exitCode = ExitCodes.E9000;
601-
_extraMessage = $"--fromfwarchive is required when --fwarchivepath is specified.";
601+
_extraMessage = $"--fromarchive is required when --archivepath is specified.";
602602
return;
603603
}
604604
#endregion

0 commit comments

Comments
 (0)