Skip to content

Commit d791312

Browse files
author
davidkline-ms
committed
fix comparison logic, added edit mode test cases
1 parent 8965d35 commit d791312

File tree

3 files changed

+216
-27
lines changed

3 files changed

+216
-27
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using Microsoft.MixedReality.Toolkit.Utilities.Editor;
5+
using NUnit.Framework;
6+
using System;
7+
using System.Collections;
8+
using System.Collections.Generic;
9+
using UnityEngine;
10+
11+
namespace Microsoft.MixedReality.Toolkit.Tests.EditMode.Core.Utilities.Editor
12+
{
13+
/// <summary>
14+
/// Tests that verify the correct behavior of the PackageManifestUpdater.
15+
/// </summary>
16+
public class PackageManifestUpdaterTests
17+
{
18+
/// <summary>
19+
/// Verifies parsing of a properly formatted value returns the correct results.
20+
/// </summary>
21+
[Test]
22+
public void TryGetValidVersion()
23+
{
24+
Debug.Log("Try to get version from properly formatted version with no prerelease.");
25+
26+
Version v;
27+
float f;
28+
29+
bool success = PackageManifestUpdater.TryGetVersionComponents("17.27.43", out v, out f);
30+
Assert.IsTrue(success);
31+
Assert.AreEqual(v, new Version(17, 27, 43));
32+
Assert.AreEqual(f, 0f);
33+
34+
Debug.Log("Try to get version from properly formatted version with prerelease.");
35+
36+
success = PackageManifestUpdater.TryGetVersionComponents("0.9.1-20200131.12", out v, out f);
37+
Assert.IsTrue(success);
38+
Assert.AreEqual(v, new Version(0, 9, 1));
39+
Assert.AreEqual(f, float.Parse("20200131.12"));
40+
}
41+
42+
/// <summary>
43+
/// Verifies parsing of an improperly formatted string returns the correct failure results.
44+
/// </summary>
45+
[Test]
46+
public void TryGetInvalidVersion()
47+
{
48+
Debug.Log("Try to get version from improperly formatted version.");
49+
Version v;
50+
float f;
51+
52+
bool success = PackageManifestUpdater.TryGetVersionComponents("x.2.3", out v, out f);
53+
Assert.IsFalse(success);
54+
Assert.IsNull(v);
55+
Assert.AreEqual(f, float.NaN);
56+
57+
Debug.Log("Try to get version from improperly formatted prerelease.");
58+
59+
v = new Version(5, 6, 7);
60+
f = 17f;
61+
success = PackageManifestUpdater.TryGetVersionComponents("1.2.3-v20200417.19", out v, out f);
62+
Assert.IsFalse(success);
63+
Assert.IsNull(v);
64+
Assert.AreEqual(f, float.NaN);
65+
66+
Debug.Log("Try to get version from an empty string.");
67+
68+
v = new Version(5, 6, 7);
69+
f = 17f;
70+
success = PackageManifestUpdater.TryGetVersionComponents("", out v, out f);
71+
Assert.IsFalse(success);
72+
Assert.IsNull(v);
73+
Assert.AreEqual(f, float.NaN);
74+
}
75+
76+
/// <summary>
77+
/// Verifies that an outdated MSBuild for Unity version string will return the correct result.
78+
/// </summary>
79+
[Test]
80+
public void OutdatedMSBuild()
81+
{
82+
Debug.Log("Upgrade to new verion / prerelease");
83+
string minVersion = "1.5.22-20200919.28";
84+
string currentVersion = "0.7.15";
85+
86+
bool isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
87+
Assert.IsFalse(isAppropriate);
88+
89+
Debug.Log("Upgrade to new prerelease");
90+
minVersion = "9.4.2-20200622.4";
91+
currentVersion = "9.4.2-20200530.9";
92+
isAppropriate = true;
93+
94+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
95+
Assert.IsFalse(isAppropriate);
96+
97+
Debug.Log("Upgrade to new version");
98+
minVersion = "1.0.0";
99+
currentVersion = "0.7.15";
100+
isAppropriate = true;
101+
102+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
103+
Assert.IsFalse(isAppropriate);
104+
105+
Debug.Log("Upgrade prerelease to final");
106+
minVersion = "0.9.19";
107+
currentVersion = "0.9.19-20200101.55";
108+
isAppropriate = true;
109+
110+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
111+
Assert.IsFalse(isAppropriate);
112+
}
113+
114+
/// <summary>
115+
/// Verifies that an exactly matching MSBuild for Unity version returns the correct result.
116+
/// </summary>
117+
[Test]
118+
public void MatchingMSBuild()
119+
{
120+
Debug.Log("Compare version");
121+
string minVersion = "28.32.44";
122+
string currentVersion = "28.32.44";
123+
124+
bool isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
125+
Assert.IsTrue(isAppropriate);
126+
127+
Debug.Log("Compare version / prerelease");
128+
minVersion = "1.5.22-20200919.28";
129+
currentVersion = "1.5.22-20200919.28";
130+
isAppropriate = false;
131+
132+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
133+
Assert.IsTrue(isAppropriate);
134+
}
135+
136+
/// <summary>
137+
/// Verifies that a more recent MSBuild for Unity version returns the correct result.
138+
/// </summary>
139+
[Test]
140+
public void NewerMSBuild()
141+
{
142+
Debug.Log("Newer version");
143+
string minVersion = "28.32.44";
144+
string currentVersion = "28.32.45";
145+
146+
bool isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
147+
Assert.IsTrue(isAppropriate);
148+
149+
Debug.Log("Newer prerelease");
150+
minVersion = "1.5.22-20200919.28";
151+
currentVersion = "1.5.22-20200919.29";
152+
isAppropriate = false;
153+
154+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
155+
Assert.IsTrue(isAppropriate);
156+
157+
Debug.Log("Newer version / prerelease");
158+
minVersion = "1.5.22-20200919.28";
159+
currentVersion = "1.6.30-20201031.6";
160+
isAppropriate = false;
161+
162+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
163+
Assert.IsTrue(isAppropriate);
164+
}
165+
}
166+
}

Assets/MixedRealityToolkit.Tests/EditModeTests/Core/Utilities/PackageManifestUpdaterTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/MixedRealityToolkit/Utilities/Editor/PackageManifest/PackageManifestUpdater.cs

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using UnityEngine.Networking;
1111
using Version = System.Version;
1212

13+
[assembly: InternalsVisibleTo("Microsoft.MixedReality.Toolkit.Tests.EditModeTests")]
1314
namespace Microsoft.MixedReality.Toolkit.Utilities.Editor
1415
{
1516
/// <summary>
@@ -45,14 +46,11 @@ private static string GetPackageManifestFilePath()
4546
return manifestPath;
4647
}
4748

48-
private static bool TryGetVersionComponents(
49+
internal static bool TryGetVersionComponents(
4950
string packageVersion,
5051
out Version version,
5152
out float prerelease)
5253
{
53-
version = null;
54-
prerelease = float.NaN;
55-
5654
char[] trimChars = new char[] { ' ', '\"', ',' };
5755

5856
// Note: The version is in the following format Major.Minor.Revision[-Date.Build]
@@ -62,46 +60,48 @@ private static bool TryGetVersionComponents(
6260

6361
// Parse the version component.
6462
string versionString = versionComponents[0].Trim(trimChars);
65-
if (!Version.TryParse(versionString, out version))
63+
if (Version.TryParse(versionString, out version))
6664
{
67-
// Reset the version out param.
68-
version = null;
69-
return false;
70-
}
71-
72-
if (versionComponents.Length == 2)
73-
{
74-
// Parse the float component
75-
string prereleaseString = versionComponents[1].Trim(trimChars);
76-
if (!float.TryParse(prereleaseString, out prerelease))
65+
if (versionComponents.Length == 2)
7766
{
78-
// Reset both out params.
79-
version = null;
80-
prerelease = float.NaN;
81-
return false;
67+
// Parse the float component
68+
string prereleaseString = versionComponents[1].Trim(trimChars);
69+
if (float.TryParse(prereleaseString, out prerelease))
70+
{
71+
return true;
72+
}
73+
}
74+
else
75+
{
76+
prerelease = 0f;
77+
return true;
8278
}
8379
}
8480

85-
return true;
81+
version = null;
82+
prerelease = float.NaN;
83+
return false;
8684
}
8785

8886
/// <summary>
8987
/// Reports whether or not the appropriate version of MSBuild for Unity is specified
9088
/// in the Unity Package Manager manifest.
9189
/// </summary>
90+
/// <param name="minPackageVersion">The minimum version of the package, as listed in the manifest.</param>
9291
/// <param name="packageVersion">The version of the package, as listed in the manifest.</param>
9392
/// <returns>
9493
/// True if an appropriate verson of MS Build for Unity is configured in the manifest, otherwise false.
9594
/// </returns>
96-
private static bool IsAppropriateMBuildVersion(string packageVersion)
95+
internal static bool IsAppropriateMBuildVersion(string minPackageVersion, string packageVersion)
9796
{
9897
// Get the version of the package.
9998
// Note: The version is in the following format Major.Minor.Revision[-Date.Build]
10099

101100
Version minVersion;
102101
float minPrerelease;
103102

104-
if (!TryGetVersionComponents(MSBuildPackageVersion, out minVersion, out minPrerelease))
103+
// Get the min version
104+
if (!TryGetVersionComponents(minPackageVersion, out minVersion, out minPrerelease))
105105
{
106106
return false;
107107
}
@@ -114,11 +114,23 @@ private static bool IsAppropriateMBuildVersion(string packageVersion)
114114
return false;
115115
}
116116

117-
// Compare the version and prerelease values
118-
bool isAppropriateVersion = currentVersion >= minVersion;
119-
bool isAppropriatePrerelease = currentPrerelease >= minPrerelease;
117+
// Evaluate the results.
118+
// * (currentVersion > minVersion) return true;
119+
// * (currentVersion == minVersion && currentPrerelease == minPrerelease) return true;
120+
// * (currentVersion == minVersion && minPrerelease != 0 && currentPrerelease >= minPrerelease) return true;
121+
// * all other combinatons return false;
122+
if (currentVersion > minVersion)
123+
{ return true; }
124+
else if (currentVersion == minVersion)
125+
{
126+
// The current and minumum versions are the same, check the prerelease portion
127+
if (currentPrerelease == minPrerelease)
128+
{ return true; }
129+
else if ((minPrerelease != 0f) && (currentPrerelease >= minPrerelease))
130+
{ return true; };
131+
}
120132

121-
return (isAppropriateVersion && isAppropriatePrerelease);
133+
return false;
122134
}
123135

124136
/// <summary>
@@ -156,7 +168,7 @@ internal static bool IsMSBuildForUnityEnabled()
156168
// Split the line into packageName : packageVersion
157169
string[] lineComponents = line.Split(new char[] { ':' }, 2);
158170

159-
return IsAppropriateMBuildVersion(lineComponents[1]);
171+
return IsAppropriateMBuildVersion(MSBuildPackageVersion, lineComponents[1]);
160172
}
161173
}
162174
}

0 commit comments

Comments
 (0)