Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 9d4ff11

Browse files
StanleyGoldmanshana
authored andcommitted
Adding a null/empty check for last skipped version (#726)
* Empty/null check when parsing versions, so we don't throw * Add version tests for null/empty
1 parent cf53058 commit 9d4ff11

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/GitHub.Api/Primitives/TheVersion.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitHub.Logging;
12
using System;
23
using System.Text.RegularExpressions;
34

@@ -7,6 +8,7 @@ public struct TheVersion : IComparable<TheVersion>
78
{
89
private const string versionRegex = @"(?<major>\d+)(\.?(?<minor>[^.]+))?(\.?(?<patch>[^.]+))?(\.?(?<build>.+))?";
910
private const int PART_COUNT = 4;
11+
public static TheVersion Default { get; } = default(TheVersion).Initialize(null);
1012

1113
[NotSerialized] private int major;
1214
[NotSerialized] public int Major { get { Initialize(Version); return major; } }
@@ -36,16 +38,13 @@ public struct TheVersion : IComparable<TheVersion>
3638

3739
public static TheVersion Parse(string version)
3840
{
39-
Guard.ArgumentNotNull(version, "version");
40-
TheVersion ret = default(TheVersion);
41-
ret.Initialize(version);
42-
return ret;
41+
return default(TheVersion).Initialize(version);
4342
}
4443

45-
private void Initialize(string version)
44+
private TheVersion Initialize(string version)
4645
{
4746
if (initialized)
48-
return;
47+
return this;
4948

5049
this.Version = version;
5150

@@ -58,6 +57,9 @@ private void Initialize(string version)
5857
special = null;
5958
parts = 0;
6059

60+
if (String.IsNullOrEmpty(version))
61+
return this;
62+
6163
intParts = new int[PART_COUNT];
6264
stringParts = new string[PART_COUNT];
6365

@@ -66,7 +68,10 @@ private void Initialize(string version)
6668

6769
var match = regex.Match(version);
6870
if (!match.Success)
69-
throw new ArgumentException("Invalid version: " + version, "version");
71+
{
72+
LogHelper.Error(new ArgumentException("Invalid version: " + version, "version"));
73+
return this;
74+
}
7075

7176
major = int.Parse(match.Groups["major"].Value);
7277
intParts[0] = major;
@@ -125,6 +130,7 @@ private void Initialize(string version)
125130
isBeta = special.IndexOf("beta") >= 0;
126131
}
127132
initialized = true;
133+
return this;
128134
}
129135

130136
public override string ToString()

src/UnityExtension/Assets/Editor/UnityTests/VersionTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,15 @@ public void DetectingUnstableVersionsWorks()
186186
version = TheVersion.Parse("1.2.3.4whatever");
187187
Assert.IsTrue(version.IsUnstable);
188188
}
189+
190+
[Test]
191+
public void ParsingInvalidVersionStringsWorks()
192+
{
193+
var ret = TheVersion.Parse(null);
194+
Assert.AreEqual(TheVersion.Default, ret);
195+
ret = TheVersion.Parse("");
196+
Assert.AreEqual(TheVersion.Default, ret);
197+
ret = TheVersion.Parse("bla");
198+
Assert.AreEqual(TheVersion.Default, ret);
199+
}
189200
}

0 commit comments

Comments
 (0)