Skip to content

Commit 517876e

Browse files
author
David Kline
authored
Merge pull request #7230 from davidkline-ms/pkgUpdateTests
Improve package manifest update logic, add test cases
2 parents 4ee0b4f + 823101f commit 517876e

File tree

3 files changed

+262
-34
lines changed

3 files changed

+262
-34
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 UnityEngine;
8+
9+
namespace Microsoft.MixedReality.Toolkit.Tests.EditMode.Core.Utilities.Editor
10+
{
11+
/// <summary>
12+
/// Tests that verify the correct behavior of the PackageManifestUpdater.
13+
/// </summary>
14+
public class PackageManifestUpdaterTests
15+
{
16+
/// <summary>
17+
/// Verifies parsing of a properly formatted value returns the correct results.
18+
/// </summary>
19+
[Test]
20+
public void TryGetValidVersion()
21+
{
22+
Version version;
23+
float prerelease;
24+
25+
bool success = PackageManifestUpdater.TryGetVersionComponents("17.27.43", out version, out prerelease);
26+
Assert.IsTrue(success);
27+
Assert.AreEqual(version, new Version(17, 27, 43));
28+
Assert.AreEqual(prerelease, 0f);
29+
30+
success = PackageManifestUpdater.TryGetVersionComponents("0.9.1-20200131.12", out version, out prerelease);
31+
Assert.IsTrue(success);
32+
Assert.AreEqual(version, new Version(0, 9, 1));
33+
Assert.AreEqual(prerelease, float.Parse("20200131.12"));
34+
}
35+
36+
/// <summary>
37+
/// Verifies parsing of an improperly formatted string returns the correct failure results.
38+
/// </summary>
39+
[Test]
40+
public void TryGetInvalidVersion()
41+
{
42+
Version version;
43+
float prerelease;
44+
45+
bool success = PackageManifestUpdater.TryGetVersionComponents("x.2.3", out version, out prerelease);
46+
Assert.IsFalse(success);
47+
Assert.IsNull(version);
48+
Assert.AreEqual(prerelease, float.NaN);
49+
50+
// Setting arbitrary values to ensure the function modifies them appropriately.
51+
version = new Version(5, 6, 7);
52+
prerelease = 17f;
53+
success = PackageManifestUpdater.TryGetVersionComponents("1.2.3-v20200417.19", out version, out prerelease);
54+
Assert.IsFalse(success);
55+
Assert.IsNull(version);
56+
Assert.AreEqual(prerelease, float.NaN);
57+
58+
// Setting arbitrary values to ensure the function modifies them appropriately.
59+
version = new Version(5, 6, 7);
60+
prerelease = 17f;
61+
success = PackageManifestUpdater.TryGetVersionComponents("", out version, out prerelease);
62+
Assert.IsFalse(success);
63+
Assert.IsNull(version);
64+
Assert.AreEqual(prerelease, float.NaN);
65+
}
66+
67+
/// <summary>
68+
/// Verifies that an outdated MSBuild for Unity version string will return the correct result.
69+
/// </summary>
70+
[Test]
71+
public void OutdatedMSBuild()
72+
{
73+
string minVersion = "1.5.22-20200919.28";
74+
string currentVersion = "0.7.15";
75+
76+
bool isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
77+
Assert.IsFalse(isAppropriate);
78+
79+
minVersion = "9.4.2-20200622.4";
80+
currentVersion = "9.4.2-20200530.9";
81+
82+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
83+
Assert.IsFalse(isAppropriate);
84+
85+
minVersion = "1.0.0";
86+
currentVersion = "0.7.15";
87+
88+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
89+
Assert.IsFalse(isAppropriate);
90+
91+
minVersion = "0.9.19";
92+
currentVersion = "0.9.19-20200101.55";
93+
94+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
95+
Assert.IsFalse(isAppropriate);
96+
}
97+
98+
/// <summary>
99+
/// Verifies that an exactly matching MSBuild for Unity version returns the correct result.
100+
/// </summary>
101+
[Test]
102+
public void MatchingMSBuild()
103+
{
104+
string minVersion = "28.32.44";
105+
string currentVersion = "28.32.44";
106+
107+
bool isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
108+
Assert.IsTrue(isAppropriate);
109+
110+
minVersion = "1.5.22-20200919.28";
111+
currentVersion = "1.5.22-20200919.28";
112+
113+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
114+
Assert.IsTrue(isAppropriate);
115+
}
116+
117+
/// <summary>
118+
/// Verifies that a more recent MSBuild for Unity version returns the correct result.
119+
/// </summary>
120+
[Test]
121+
public void NewerMSBuild()
122+
{
123+
string minVersion = "28.32.44";
124+
string currentVersion = "28.32.45";
125+
126+
bool isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
127+
Assert.IsTrue(isAppropriate);
128+
129+
minVersion = "1.5.22-20200919.28";
130+
currentVersion = "1.5.22-20200919.29";
131+
132+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
133+
Assert.IsTrue(isAppropriate);
134+
135+
minVersion = "1.5.22-20200919.28";
136+
currentVersion = "1.6.30-20201031.6";
137+
138+
isAppropriate = PackageManifestUpdater.IsAppropriateMBuildVersion(minVersion, currentVersion);
139+
Assert.IsTrue(isAppropriate);
140+
}
141+
}
142+
}

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: 109 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Runtime.CompilerServices;
78
using UnityEditor.PackageManager;
89
using UnityEngine;
10+
using UnityEngine.Networking;
11+
using Version = System.Version;
912

13+
[assembly: InternalsVisibleTo("Microsoft.MixedReality.Toolkit.Tests.EditModeTests")]
1014
namespace Microsoft.MixedReality.Toolkit.Utilities.Editor
1115
{
1216
/// <summary>
@@ -19,7 +23,7 @@ internal static class PackageManifestUpdater
1923
private static string[] MSBuildRegistryScopes = new string[] { "com.microsoft" };
2024

2125
internal const string MSBuildPackageName = "com.microsoft.msbuildforunity";
22-
internal const string MSBuildPackageVersion = "0.9.1";
26+
internal const string MSBuildPackageVersion = "0.9.1-20200131.12";
2327

2428
/// <summary>
2529
/// Finds and returns the fully qualified path to the Unity Package Manager manifest
@@ -42,10 +46,102 @@ private static string GetPackageManifestFilePath()
4246
return manifestPath;
4347
}
4448

49+
internal static bool TryGetVersionComponents(
50+
string packageVersion,
51+
out Version version,
52+
out float prerelease)
53+
{
54+
char[] trimChars = new char[] { ' ', '\"', ',' };
55+
56+
// Note: The version is in the following format Major.Minor.Revision[-Date.Build]
57+
58+
// Attempt to split the version string into version and float components
59+
string[] versionComponents = packageVersion.Split(new char[] { '-' }, 2);
60+
61+
// Parse the version component.
62+
string versionString = versionComponents[0].Trim(trimChars);
63+
if (Version.TryParse(versionString, out version))
64+
{
65+
if (versionComponents.Length == 2)
66+
{
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;
78+
}
79+
}
80+
81+
version = null;
82+
prerelease = float.NaN;
83+
return false;
84+
}
85+
4586
/// <summary>
4687
/// Reports whether or not the appropriate version of MSBuild for Unity is specified
4788
/// in the Unity Package Manager manifest.
4889
/// </summary>
90+
/// <param name="minPackageVersion">The minimum version of the package, as listed in the manifest.</param>
91+
/// <param name="packageVersion">The version of the package, as listed in the manifest.</param>
92+
/// <returns>
93+
/// True if an appropriate verson of MS Build for Unity is configured in the manifest, otherwise false.
94+
/// </returns>
95+
internal static bool IsAppropriateMBuildVersion(string minPackageVersion, string packageVersion)
96+
{
97+
// Get the version of the package.
98+
// Note: The version is in the following format Major.Minor.Revision[-Date.Build]
99+
100+
Version minVersion;
101+
float minPrerelease;
102+
103+
// Get the min version
104+
if (!TryGetVersionComponents(minPackageVersion, out minVersion, out minPrerelease))
105+
{
106+
return false;
107+
}
108+
109+
// Get the current version from the manifest
110+
Version currentVersion;
111+
float currentPrerelease;
112+
if (!TryGetVersionComponents(packageVersion, out currentVersion, out currentPrerelease))
113+
{
114+
return false;
115+
}
116+
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+
{
124+
return true;
125+
}
126+
else if (currentVersion == minVersion)
127+
{
128+
// The current and minumum versions are the same, check the prerelease portion
129+
if (currentPrerelease == minPrerelease)
130+
{
131+
return true;
132+
}
133+
else if ((minPrerelease != 0f) && (currentPrerelease >= minPrerelease))
134+
{
135+
return true;
136+
};
137+
}
138+
139+
return false;
140+
}
141+
142+
/// <summary>
143+
/// Reports whether or not the MSBuild for Unity is properly enabled in the Unity Package Manager manifest.
144+
/// </summary>
49145
/// <returns>
50146
/// True if an appropriate verson of MS Build for Unity is configured in the manifest, otherwise false.
51147
/// </returns>
@@ -65,9 +161,6 @@ internal static bool IsMSBuildForUnityEnabled()
65161
}
66162

67163
// Read the package manifest a line at a time.
68-
bool msBuildFound = false;
69-
bool isAppropriateVersion = false;
70-
Version minVersion = Version.Parse(MSBuildPackageVersion);
71164
using (FileStream manifestStream = new FileStream(manifestPath, FileMode.Open, FileAccess.Read))
72165
{
73166
using (StreamReader reader = new StreamReader(manifestStream))
@@ -78,38 +171,16 @@ internal static bool IsMSBuildForUnityEnabled()
78171
string line = reader.ReadLine();
79172
if (line.Contains(MSBuildPackageName))
80173
{
81-
msBuildFound = true;
82-
83-
// Next, check the version.
84-
string[] splitLine = line.Split(new char[] { ':' });
85-
if (splitLine.Length == 2)
86-
{
87-
// Ensure correct formatting of the version string, before we attempt to parse it.
88-
string versionString = splitLine[1].Trim(new char[] { ' ', '\"', ',' });
89-
bool replaceOnEquals = false;
90-
if (versionString.Contains("-"))
91-
{
92-
// The string references a preview version. Truncate at the '-'.
93-
versionString = versionString.Substring(0, versionString.IndexOf('-'));
94-
95-
// We want to update preview versions to the final.
96-
replaceOnEquals = true;
97-
}
98-
99-
Version version;
100-
if (Version.TryParse(versionString, out version))
101-
{
102-
isAppropriateVersion = replaceOnEquals ? (version > minVersion) : (version >= minVersion);
103-
}
104-
}
105-
106-
break;
174+
// Split the line into packageName : packageVersion
175+
string[] lineComponents = line.Split(new char[] { ':' }, 2);
176+
177+
return IsAppropriateMBuildVersion(MSBuildPackageVersion, lineComponents[1]);
107178
}
108179
}
109180
}
110181
}
111182

112-
return (msBuildFound && isAppropriateVersion);
183+
return false;
113184
}
114185

115186
/// <summary>
@@ -189,10 +260,14 @@ internal static void EnsureMSBuildForUnity()
189260
int scopedRegistriesEndIndex = -1;
190261
int packageLine = -1;
191262

263+
// Presume that we need to add the MSBuild for Unity package. If this value is false,
264+
// we will check to see if the currently configured version meets or exceeds the
265+
// minimum requirements.
266+
bool needToAddPackage = true;
267+
192268
// Attempt to find the MSBuild for Unity package entry in the dependencies collection
193269
// This loop also identifies the dependecies collection line and the start / end of a
194270
// pre-existing scoped registries collections
195-
bool addPackage = true;
196271
for (int i = 0; i < manifestFileLines.Count; i++)
197272
{
198273
if (manifestFileLines[i].Contains("\"scopedRegistries\":"))
@@ -210,12 +285,12 @@ internal static void EnsureMSBuildForUnity()
210285
if (manifestFileLines[i].Contains(MSBuildPackageName))
211286
{
212287
packageLine = i;
213-
addPackage = false;
288+
needToAddPackage = false;
214289
}
215290
}
216291

217292
// If no package was found add it to the dependencies collection.
218-
if (addPackage)
293+
if (needToAddPackage)
219294
{
220295
// Add the package to the collection (pad the entry with four spaces)
221296
manifestFileLines.Insert(dependenciesStartIndex + 1, $" \"{MSBuildPackageName}\": \"{MSBuildPackageVersion}\",");

0 commit comments

Comments
 (0)