From 3e179d06f4302eddbddc3217302f37d4c5f34724 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Tue, 19 Feb 2019 05:40:40 -0800 Subject: [PATCH 1/3] Use product version in RuntimeInformation.FrameworkDescription --- .../RuntimeInformation/RuntimeInformation.cs | 12 +++-- .../tests/DescriptionNameTests.cs | 44 +++++++------------ 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs b/src/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs index 64858792750e..c3d2f9512777 100644 --- a/src/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs +++ b/src/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs @@ -23,9 +23,15 @@ public static string FrameworkDescription { if (s_frameworkDescription == null) { - AssemblyFileVersionAttribute attr = (AssemblyFileVersionAttribute)(typeof(object).GetTypeInfo().Assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))); - Debug.Assert(attr != null); - s_frameworkDescription = $"{FrameworkName} {attr.Version}"; + string versionString = (string)AppContext.GetData("FX_PRODUCT_VERSION"); + + if (versionString == null) + { + // Use AssemblyInformationalVersionAttribute as fallback if the exact product version is not specified by the host + versionString = typeof(object).Assembly.GetCustomAttribute()?.InformationalVersion; + } + + s_frameworkDescription = $"{FrameworkName} {versionString}"; } return s_frameworkDescription; diff --git a/src/System.Runtime.InteropServices.RuntimeInformation/tests/DescriptionNameTests.cs b/src/System.Runtime.InteropServices.RuntimeInformation/tests/DescriptionNameTests.cs index 59f49dd91f3d..16fa42d08394 100644 --- a/src/System.Runtime.InteropServices.RuntimeInformation/tests/DescriptionNameTests.cs +++ b/src/System.Runtime.InteropServices.RuntimeInformation/tests/DescriptionNameTests.cs @@ -44,31 +44,17 @@ public void DumpRuntimeInformationToConsole() [Fact] [SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp)] - public void VerifyRuntimeDebugNameOnNetCoreApp() + public void VerifyRuntimeNameOnNetCoreApp() { - AssemblyFileVersionAttribute attr = (AssemblyFileVersionAttribute)(typeof(object).GetTypeInfo().Assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))); - string expected = string.Format(".NET Core {0}", attr.Version); - Assert.Equal(expected, RuntimeInformation.FrameworkDescription); + Assert.True(RuntimeInformation.FrameworkDescription.StartsWith(".NET Core")); Assert.Same(RuntimeInformation.FrameworkDescription, RuntimeInformation.FrameworkDescription); } [Fact] - [SkipOnTargetFramework(~TargetFrameworkMonikers.NetFramework)] - public void VerifyRuntimeDebugNameOnNetFramework() + [SkipOnTargetFramework(~TargetFrameworkMonikers.UapAot)] + public void VerifyRuntimeNameOnNetNative() { - AssemblyFileVersionAttribute attr = (AssemblyFileVersionAttribute)(typeof(object).GetTypeInfo().Assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))); - string expected = string.Format(".NET Framework {0}", attr.Version); - Assert.Equal(expected, RuntimeInformation.FrameworkDescription); - Assert.Same(RuntimeInformation.FrameworkDescription, RuntimeInformation.FrameworkDescription); - } - - [Fact] - [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework | TargetFrameworkMonikers.Netcoreapp)] - public void VerifyRuntimeDebugNameOnNetCoreUwp() - { - AssemblyFileVersionAttribute attr = (AssemblyFileVersionAttribute)(typeof(object).GetTypeInfo().Assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))); - string expected = string.Format(PlatformDetection.IsNetNative ? ".NET Native {0}" : ".NET Core {0}", attr.Version); - Assert.Equal(expected, RuntimeInformation.FrameworkDescription); + Assert.True(RuntimeInformation.FrameworkDescription.StartsWith(".NET Native")); Assert.Same(RuntimeInformation.FrameworkDescription, RuntimeInformation.FrameworkDescription); } @@ -87,32 +73,32 @@ public void VerifyWindowsDescriptionDoesNotContainTrailingWhitespace() Assert.False(RuntimeInformation.OSDescription.EndsWith(" ")); } - [Fact, PlatformSpecific(TestPlatforms.Windows)] // Checks Windows debug name in RuntimeInformation - public void VerifyWindowsDebugName() + [Fact, PlatformSpecific(TestPlatforms.Windows)] // Checks Windows name in RuntimeInformation + public void VerifyWindowsName() { Assert.Contains("windows", RuntimeInformation.OSDescription, StringComparison.OrdinalIgnoreCase); } - [Fact, PlatformSpecific(TestPlatforms.Linux)] // Checks Linux debug name in RuntimeInformation - public void VerifyLinuxDebugName() + [Fact, PlatformSpecific(TestPlatforms.Linux)] // Checks Linux name in RuntimeInformation + public void VerifyLinuxName() { Assert.Contains("linux", RuntimeInformation.OSDescription, StringComparison.OrdinalIgnoreCase); } - [Fact, PlatformSpecific(TestPlatforms.NetBSD)] // Checks NetBSD debug name in RuntimeInformation - public void VerifyNetBSDDebugName() + [Fact, PlatformSpecific(TestPlatforms.NetBSD)] // Checks NetBSD name in RuntimeInformation + public void VerifyNetBSDName() { Assert.Contains("netbsd", RuntimeInformation.OSDescription, StringComparison.OrdinalIgnoreCase); } - [Fact, PlatformSpecific(TestPlatforms.FreeBSD)] // Checks FreeBSD debug name in RuntimeInformation - public void VerifyFreeBSDDebugName() + [Fact, PlatformSpecific(TestPlatforms.FreeBSD)] // Checks FreeBSD name in RuntimeInformation + public void VerifyFreeBSDName() { Assert.Contains("FreeBSD", RuntimeInformation.OSDescription, StringComparison.OrdinalIgnoreCase); } - [Fact, PlatformSpecific(TestPlatforms.OSX)] // Checks OSX debug name in RuntimeInformation - public void VerifyOSXDebugName() + [Fact, PlatformSpecific(TestPlatforms.OSX)] // Checks OSX name in RuntimeInformation + public void VerifyOSXName() { Assert.Contains("darwin", RuntimeInformation.OSDescription, StringComparison.OrdinalIgnoreCase); } From 18da9cb74d4a4f170c10217eb52f39b7fee01fe9 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Fri, 22 Feb 2019 12:02:15 -0800 Subject: [PATCH 2/3] Include FrameworkDescription in the Assert --- .../tests/DescriptionNameTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Runtime.InteropServices.RuntimeInformation/tests/DescriptionNameTests.cs b/src/System.Runtime.InteropServices.RuntimeInformation/tests/DescriptionNameTests.cs index 16fa42d08394..821c1123c6bf 100644 --- a/src/System.Runtime.InteropServices.RuntimeInformation/tests/DescriptionNameTests.cs +++ b/src/System.Runtime.InteropServices.RuntimeInformation/tests/DescriptionNameTests.cs @@ -46,7 +46,7 @@ public void DumpRuntimeInformationToConsole() [SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp)] public void VerifyRuntimeNameOnNetCoreApp() { - Assert.True(RuntimeInformation.FrameworkDescription.StartsWith(".NET Core")); + Assert.True(RuntimeInformation.FrameworkDescription.StartsWith(".NET Core"), RuntimeInformation.FrameworkDescription); Assert.Same(RuntimeInformation.FrameworkDescription, RuntimeInformation.FrameworkDescription); } @@ -54,7 +54,7 @@ public void VerifyRuntimeNameOnNetCoreApp() [SkipOnTargetFramework(~TargetFrameworkMonikers.UapAot)] public void VerifyRuntimeNameOnNetNative() { - Assert.True(RuntimeInformation.FrameworkDescription.StartsWith(".NET Native")); + Assert.True(RuntimeInformation.FrameworkDescription.StartsWith(".NET Native"), RuntimeInformation.FrameworkDescription); Assert.Same(RuntimeInformation.FrameworkDescription, RuntimeInformation.FrameworkDescription); } From 57b249e0cecc5cad02ec74318cf8b01dec87ef5d Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Fri, 22 Feb 2019 17:57:40 -0800 Subject: [PATCH 3/3] Strip the git hash in the fallback --- .../InteropServices/RuntimeInformation/RuntimeInformation.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs b/src/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs index c3d2f9512777..1d180e54ea3a 100644 --- a/src/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs +++ b/src/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs @@ -29,6 +29,11 @@ public static string FrameworkDescription { // Use AssemblyInformationalVersionAttribute as fallback if the exact product version is not specified by the host versionString = typeof(object).Assembly.GetCustomAttribute()?.InformationalVersion; + + // Strip the git hash if there is one + int plusIndex = versionString.IndexOf('+'); + if (plusIndex != -1) + versionString = versionString.Substring(0, plusIndex); } s_frameworkDescription = $"{FrameworkName} {versionString}";