Skip to content

Commit

Permalink
LibraryVersionInfo
Browse files Browse the repository at this point in the history
- Add LibraryVersionInfo class and simple test case.
  • Loading branch information
jthelin committed May 23, 2016
1 parent 1e12dc6 commit e5bd104
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
79 changes: 79 additions & 0 deletions ServerHost/LibraryVersionInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Jorgen Thelin. All rights reserved.
// Licensed with Apache 2.0 https://github.com/jthelin/ServerHost/blob/master/LICENSE

using System.Diagnostics;
using System.Reflection;

namespace Server.Host
{
/// <summary>
/// Version info for ServerHost library.
/// </summary>
/// <remarks>
/// Based on the <c>Orleans.Runtime.RuntimeVersion</c> class.
/// https://github.com/dotnet/orleans/blob/master/src/Orleans/Runtime/RuntimeVersion.cs
/// </remarks>
public static class LibraryVersionInfo
{
private static readonly TypeInfo libraryTypeInfo = typeof(ServerHost).GetTypeInfo();

/// <summary>
/// The full version string of the library.
/// eg: '2012.5.9.51607 Build:12345 Timestamp: 20120509-185359'
/// </summary>
public static string Current
{
get
{
Assembly me = libraryTypeInfo.Assembly;
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(me.Location);
// progVersionInfo.IsDebug; does not work
bool isDebug = IsAssemblyDebugBuild(me);
string productVersion = versionInfo.ProductVersion + (isDebug ? " (Debug)." : " (Release).");
return string.IsNullOrEmpty(productVersion) ? ApiVersion : productVersion;
}
}

/// <summary>
/// The ApiVersion of the library.
/// eg: '1.0.0.0'
/// </summary>
public static string ApiVersion
{
get
{
Assembly me = libraryTypeInfo.Assembly;
AssemblyName libraryInfo = me.GetName();
return libraryInfo.Version.ToString();
}
}

/// <summary>
/// The FileVersion of the library.
/// eg: '2012.5.9.51607'
/// </summary>
public static string FileVersion
{
get
{
Assembly me = libraryTypeInfo.Assembly;
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(me.Location);
string fileVersion = versionInfo.FileVersion;
return string.IsNullOrEmpty(fileVersion) ? ApiVersion : fileVersion;
}
}

private static bool IsAssemblyDebugBuild(Assembly assembly)
{
foreach (object attribute in assembly.GetCustomAttributes(false))
{
DebuggableAttribute debuggableAttribute = attribute as DebuggableAttribute;
if (debuggableAttribute != null)
{
return debuggableAttribute.IsJITTrackingEnabled;
}
}
return false;
}
}
}
1 change: 1 addition & 0 deletions ServerHost/ServerHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServerHost.cs" />
<Compile Include="LibraryVersionInfo.cs" />
<Compile Include="ServerHostHandle.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions Tests/ServerHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ public void LoadServerInNewAppDomain()
serverHostHandle.Server.Should().BeOfType<TestServer.Server>("Server instance type.");
}

[Fact]
[Trait("Category", "BVT")]
public void ServerHost_Version()
{
string versionString = LibraryVersionInfo.FileVersion;
output.WriteLine("ServerHost library version = {0}", versionString);

versionString.Should().NotBeNullOrEmpty("Version value should be returned");

versionString.Should().Contain(".", "Version format = Major.Minor");
versionString.Should().NotStartWith("1.0.0.0", "Version should be specific.");
}

#region Test Initialization / Cleanup methods

// TestInitialize
Expand All @@ -42,6 +55,10 @@ public ServerHostTests(ITestOutputHelper output, ServerHostTestFixture fixture)
output.WriteLine("{0} Fixture = {1}", className, fixture);

output.WriteLine("{0} Current directory = {1}", className, Environment.CurrentDirectory);

output.WriteLine("ServerHost library API version = {0}", LibraryVersionInfo.ApiVersion);
output.WriteLine("ServerHost library file version = {0}", LibraryVersionInfo.FileVersion);
output.WriteLine("ServerHost library full version info string = {0}", LibraryVersionInfo.Current);
}

// TestCleanup
Expand Down

0 comments on commit e5bd104

Please sign in to comment.