Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Test/DurableTask.AzureStorage.Tests/AssemblyFileVersionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace DurableTask.AzureStorage.Tests
{
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class AssemblyFileVersionTests
{
[TestMethod]
public void GetAssemblyFileVersion_UsesFileVersionInfoWhenLocationIsKnown()
{
Assembly assembly = typeof(AssemblyFileVersionTests).Assembly;
string expected = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;

Assert.AreEqual(expected, DurableTask.AzureStorage.Utils.GetAssemblyFileVersion(assembly));
}

#if !NETFRAMEWORK
[TestMethod]
public void GetAssemblyFileVersion_FallsBackToAttributeWhenLocationIsEmpty()
{
// A dynamic assembly has an empty Location, like an assembly inside a single-file bundle.
var name = new AssemblyName("AssemblyFileVersionTestAssembly");
AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
ConstructorInfo ctor = typeof(AssemblyFileVersionAttribute).GetConstructor(new[] { typeof(string) });
assembly.SetCustomAttribute(new CustomAttributeBuilder(ctor, new object[] { "1.2.3.4" }));

Assert.AreEqual(string.Empty, assembly.Location);
Assert.AreEqual("1.2.3.4", DurableTask.AzureStorage.Utils.GetAssemblyFileVersion(assembly));
}
#endif
}
}
48 changes: 48 additions & 0 deletions Test/DurableTask.Core.Tests/AssemblyFileVersionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace DurableTask.Core.Tests
{
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class AssemblyFileVersionTests
{
[TestMethod]
public void GetAssemblyFileVersion_UsesFileVersionInfoWhenLocationIsKnown()
{
Assembly assembly = typeof(AssemblyFileVersionTests).Assembly;
string expected = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;

Assert.AreEqual(expected, DurableTask.Core.Common.Utils.GetAssemblyFileVersion(assembly));
}

#if !NETFRAMEWORK
[TestMethod]
public void GetAssemblyFileVersion_FallsBackToAttributeWhenLocationIsEmpty()
{
// A dynamic assembly has an empty Location, like an assembly inside a single-file bundle.
var name = new AssemblyName("AssemblyFileVersionTestAssembly");
AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
ConstructorInfo ctor = typeof(AssemblyFileVersionAttribute).GetConstructor(new[] { typeof(string) });
assembly.SetCustomAttribute(new CustomAttributeBuilder(ctor, new object[] { "1.2.3.4" }));

Assert.AreEqual(string.Empty, assembly.Location);
Assert.AreEqual("1.2.3.4", DurableTask.Core.Common.Utils.GetAssemblyFileVersion(assembly));
}
#endif
}
}
19 changes: 18 additions & 1 deletion src/DurableTask.AzureStorage/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace DurableTask.AzureStorage
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -29,7 +30,23 @@ static class Utils
{
public static readonly Task CompletedTask = Task.FromResult(0);

public static readonly string ExtensionVersion = FileVersionInfo.GetVersionInfo(typeof(AzureStorageOrchestrationService).Assembly.Location).FileVersion;
public static readonly string ExtensionVersion = GetAssemblyFileVersion(typeof(AzureStorageOrchestrationService).Assembly);

/// <summary>
/// Gets the file version of an assembly. Assemblies in a single-file bundle have an empty
/// <see cref="Assembly.Location"/>, so the version is read from the assembly's
/// <see cref="AssemblyFileVersionAttribute"/> instead.
/// </summary>
internal static string GetAssemblyFileVersion(Assembly assembly)
{
string location = assembly.Location;
if (!string.IsNullOrEmpty(location))
{
return FileVersionInfo.GetVersionInfo(location).FileVersion;
}

return assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version ?? string.Empty;
}

// DurableTask.Core has a public static variable that contains the app name
public static readonly string AppName = DurableTask.Core.Common.Utils.AppName;
Expand Down
18 changes: 17 additions & 1 deletion src/DurableTask.Core/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,23 @@ public static class Utils
/// <summary>
/// Gets the version of the DurableTask.Core nuget package, which by convension is the same as the assembly file version.
/// </summary>
internal static readonly string PackageVersion = FileVersionInfo.GetVersionInfo(typeof(TaskOrchestration).Assembly.Location).FileVersion;
internal static readonly string PackageVersion = GetAssemblyFileVersion(typeof(TaskOrchestration).Assembly);

/// <summary>
/// Gets the file version of an assembly. Assemblies in a single-file bundle have an empty
/// <see cref="Assembly.Location"/>, so the version is read from the assembly's
/// <see cref="AssemblyFileVersionAttribute"/> instead.
/// </summary>
internal static string GetAssemblyFileVersion(Assembly assembly)
{
string location = assembly.Location;
if (!string.IsNullOrEmpty(location))
{
return FileVersionInfo.GetVersionInfo(location).FileVersion;
}

return assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version ?? string.Empty;
}

private static readonly JsonSerializerSettings ObjectJsonSettings = new JsonSerializerSettings
{
Expand Down