Skip to content

Commit 0804ac4

Browse files
committed
Bump to 1.2.1, add VersionAsync, add unit tests
1 parent 7d29486 commit 0804ac4

5 files changed

Lines changed: 139 additions & 23 deletions

File tree

GithubUpdateCheck/GithubUpdateCheck.cs

Lines changed: 94 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,38 @@ public InvalidVersionException(string message, Exception innerException) : base(
8585
};
8686

8787

88+
/// <summary>
89+
/// The exception is thrown if the remote repo does not have releases
90+
/// </summary>
91+
public class NoVersionException: InvalidVersionException
92+
{
93+
/// <summary>
94+
/// Initializes a new instance of the <see cref="NoVersionException"/> class
95+
/// </summary>
96+
[ExcludeFromCodeCoverage]
97+
public NoVersionException()
98+
{
99+
}
100+
101+
/// <summary>
102+
/// Initializes a new instance of the <see cref="NoVersionException"/> class with a specified error message
103+
/// </summary>
104+
public NoVersionException(String message) : base(message)
105+
{
106+
}
107+
108+
/// <summary>
109+
/// Initializes a new instance of the <see cref="NoVersionException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception
110+
/// </summary>
111+
/// <param name="message"></param>
112+
/// <param name="innerException"></param>
113+
[ExcludeFromCodeCoverage]
114+
public NoVersionException(string message, Exception innerException) : base(message, innerException)
115+
{
116+
}
117+
}
118+
119+
88120
/// <summary>
89121
/// Checks if your github repo is on a newer version or not
90122
/// </summary>
@@ -271,6 +303,31 @@ private static bool IsValidInputBoolean(string version)
271303
}
272304

273305

306+
/// <summary>
307+
/// strip the given url, to only contain the github version (not normalized)
308+
/// </summary>
309+
/// <param name="url"></param>
310+
/// <returns>striped repo version string</returns>
311+
private string ExtractVersionFromUrl(string url)
312+
{
313+
// extract the tag from the url and validate/normalize input
314+
//no releases yet
315+
if (!url.Contains("/tag/"))
316+
throw new NoVersionException(url + " the github version number does not contain \"tag\" in its path. Usually occurs when no release exists. [Remote error]");
317+
318+
//get everything after last /tag/
319+
int indexOfTag = CultureInfo.InvariantCulture.CompareInfo.LastIndexOf(url, "/tag/");
320+
url = url.Substring(indexOfTag + "/tag/".Length);
321+
322+
if (!IsValidInput(url))
323+
{
324+
throw new InvalidVersionException(url + " the github version number does not follow the specified version pattern [Remote error]");
325+
}
326+
327+
return url;
328+
}
329+
330+
274331
/// <summary>
275332
/// Removes leading v. and v from the string
276333
/// </summary>
@@ -289,16 +346,43 @@ private string NormalizeVersionString(string version)
289346
return NormalizeVersionStringBoolean(version);
290347
}
291348
}
349+
292350
/// <summary>
293-
/// Returns the latest version on github
351+
/// Returns the latest version on github.
294352
/// </summary>
353+
/// <exception cref="InvalidVersionException">Is thrown if the supplied version or the remote version does not match the allowed version pattern</exception>
354+
/// <exception cref="NoVersionException">Is thrown if the remote repo does not have any releases</exception>
355+
/// <returns>normalized string</returns>
356+
public async Task<string> VersionAsync()
357+
{
358+
string resolved = await GetResponseUrlAsync(githubUrl + Username + "/" + Repository + latestVersionString).ConfigureAwait(false);
359+
return extractVersion(resolved);
360+
}
361+
362+
/// <summary>
363+
/// Returns the latest version on github.
364+
/// </summary>
365+
/// <exception cref="InvalidVersionException">Is thrown if the supplied version or the remote version does not match the allowed version pattern</exception>
366+
/// <exception cref="NoVersionException">Is thrown if the remote repo does not have any releases</exception>
295367
/// <returns>normalized string</returns>
296368
public string Version()
297369
{
298370
string resolved = GetResponseUrl(githubUrl + Username + "/" + Repository + latestVersionString);
299-
string version = NormalizeVersionString(resolved);
371+
return extractVersion(resolved);
372+
}
373+
374+
/// <summary>
375+
/// extract the normalized version from the given url, may throw <see cref="NoVersionException"/>
376+
/// </summary>
377+
/// <param name="url"></param>
378+
/// <returns></returns>
379+
private string extractVersion(string url)
380+
{
381+
string extract = ExtractVersionFromUrl(url); // strip the url part
382+
string version = NormalizeVersionString(extract);
300383
return version;
301384
}
385+
302386
/// <summary>
303387
/// Removes leading v. and v from the string
304388
/// </summary>
@@ -387,24 +471,16 @@ public bool IsUpdateAvailable(string CurrentVersion, VersionChange VersionChange
387471
/// <returns>true if a newer version is available</returns>
388472
private bool CompareVersions(string current, string github, VersionChange changeLevel)
389473
{
390-
// extract the tag from the url and validate/normalize input
391-
//no releases yet
392-
if (!github.Contains("/tag/"))
393-
return false;
394-
395-
396-
//get everything after last /tag/
397-
int indexOfTag = CultureInfo.InvariantCulture.CompareInfo.LastIndexOf(github, "/tag/");
398-
github = github.Substring(indexOfTag + "/tag/".Length);
399-
400-
if (!IsValidInput(github))
474+
try
401475
{
402-
throw new InvalidVersionException(github + " the github version number does not follow the specified version pattern [Remote error]");
476+
github = extractVersion(github);
403477
}
404-
405-
github = NormalizeVersionString(github);
406-
407-
478+
catch (NoVersionException)
479+
{
480+
// assume this is equivalent to no update
481+
return false;
482+
}
483+
408484

409485
// choose CompareType specific compare method
410486
switch (cmpType)

GithubUpdateCheck/GithubUpdateCheck.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
* Supports async requests
1313
* Incremental versioning 1.0.0.0, 1.0.0 and v.1.0.0 (and combinations) are supported (v. can be any non-number)
1414
* Boolean versioning (assume update as soon local and remote are not equal)</Description>
15-
<Copyright>(c) - Christian Mayer 2020</Copyright>
15+
<Copyright>(c) - Christian Mayer 2022</Copyright>
1616
<PackageLicenseFile></PackageLicenseFile>
1717
<PackageProjectUrl>https://github.com/Mayerch1/GithubUpdateCheck</PackageProjectUrl>
1818
<RepositoryUrl>https://github.com/Mayerch1/GithubUpdateCheck</RepositoryUrl>
1919
<PackageTags>Github, Version, VersionCheck, UpdateCheck, Update</PackageTags>
20-
<PackageReleaseNotes>project is now distributed under MIT. (Change from GPLv3 to MIT)</PackageReleaseNotes>
21-
<AssemblyVersion>1.2.0.0</AssemblyVersion>
22-
<FileVersion>1.2.0.0</FileVersion>
23-
<Version>1.2.0</Version>
20+
<PackageReleaseNotes>Added methods to get the current repo version</PackageReleaseNotes>
21+
<AssemblyVersion>1.2.1.0</AssemblyVersion>
22+
<FileVersion>1.2.1.0</FileVersion>
23+
<Version>1.2.1</Version>
2424
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2525
</PropertyGroup>
2626

GithubUpdateCheckTest/GithubUpdateCheckTest.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
<TargetFramework>net5.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
7+
8+
<AssemblyVersion>1.2.1.0</AssemblyVersion>
9+
10+
<FileVersion>1.2.1.0</FileVersion>
11+
12+
<Version>1.2.1</Version>
713
</PropertyGroup>
814

915
<ItemGroup>

GithubUpdateCheckTest/UnitTestGeneral.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,32 @@ public async Task TestInvalidUserAsync()
203203
GithubUpdateCheck obj = new GithubUpdateCheck("5fe54fd3-2fd8-48ff-8f63-1b8575348b5f", "GithubUpdateCheck");
204204
Assert.IsFalse(await obj.IsUpdateAvailableAsync("1.0.0.0"));
205205
}
206+
207+
[TestMethod]
208+
public void TestGetVersionIncremental()
209+
{
210+
GithubUpdateCheck getV = new GithubUpdateCheck("Mayerch1", "GithubUpdateCheckUnitTest", CompareType.Incremental);
211+
var version = getV.Version();
212+
213+
Assert.IsTrue(version.Equals("2.4.1.5"));
214+
}
215+
216+
[TestMethod]
217+
public void TestGetVersionBoolean()
218+
{
219+
GithubUpdateCheck getV = new GithubUpdateCheck("Mayerch1", "GithubUpdateCheckUnitTest", CompareType.Boolean);
220+
var version = getV.Version();
221+
222+
Assert.IsTrue(version.Equals("v.2.4.1.5"));
223+
}
224+
225+
[TestMethod]
226+
public void TestGetVersionNoVersion()
227+
{
228+
// repo doesn't have releases
229+
GithubUpdateCheck getV = new GithubUpdateCheck("Mayerch1", "BunnyVisual", CompareType.Incremental);
230+
Assert.ThrowsException<Mayerch1.GithubUpdateCheck.NoVersionException>(() => getV.Version());
231+
}
232+
206233
}
207234
}

GithubUpdateCheckTest/UnitTestIncremental.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ public void TestExceedRemoteDepth()
226226
Assert.IsFalse(obj.IsUpdateAvailable("2.4.1.5.10", (VersionChange)5));
227227
}
228228

229+
[TestMethod]
230+
public void TestGetVersionInvalidVersion()
231+
{
232+
// repo doesn't have releases
233+
GithubUpdateCheck getV = new GithubUpdateCheck("Mayerch1", "GithubUpdateCheckUnitTest2", CompareType.Incremental);
234+
Assert.ThrowsException<Mayerch1.GithubUpdateCheck.InvalidVersionException>(() => getV.Version());
235+
}
229236

230237
}
231238
}

0 commit comments

Comments
 (0)