Fix XSLT tests to deterministically pick newer xsltc.exe (dotnet/corefx#41919)
authorDan Moseley <danmose@microsoft.com>
Mon, 21 Oct 2019 13:50:01 +0000 (06:50 -0700)
committerStephen Toub <stoub@microsoft.com>
Mon, 21 Oct 2019 13:50:01 +0000 (09:50 -0400)
* Fix XSLT tests

* Typo

* Typo

* Nits

Commit migrated from https://github.com/dotnet/corefx/commit/340db06faadb1256c02d3eab8e36b6f3c2972bb0

src/libraries/System.Private.Xml/tests/Xslt/XsltCompiler/XsltCommon.cs

index 76e8982..8624a7c 100644 (file)
@@ -10,6 +10,7 @@ using System.Diagnostics;
 using System.Globalization;
 using System.IO;
 using System.Linq;
+using System.Text.RegularExpressions;
 using System.Xml;
 using System.Xml.XmlDiff;
 
@@ -353,12 +354,31 @@ namespace XmlCoreTest.Common
             if (files.Count == 0)
                 throw new FileNotFoundException(fileName);
 
-            // Crudely prefer newer versions, eg 4.6.2 over 4.6.1,
-            // but it currently is not important
-            files.Sort(StringComparer.OrdinalIgnoreCase);
+            // Prefer newer versions, for stability
+            files.Sort((left, right) =>
+            {
+                int comparison = Comparer<float>.Default.Compare(GetVersionFromSDKPath(left), GetVersionFromSDKPath(right));
+
+                if (comparison == 0)
+                    comparison = string.Compare(left, right, StringComparison.OrdinalIgnoreCase);
+
+                return comparison;
+            });
 
             return files[files.Count - 1];
         }
+
+        // Pull the version out of a path like "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\xsltc.exe"
+        private static float GetVersionFromSDKPath(string s)
+        {
+            Match match = Regex.Match(s, @"\\v(\d+\.\d+)\w?\\", RegexOptions.IgnoreCase);
+
+            float val = 0;
+            if (match.Success)
+                float.TryParse(match.Groups[1].Value, out val);
+
+            return val;
+        }
     }
 
     /*************************************************