Skip to content

Commit

Permalink
ParseAssemblyInformationalVersion tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Jul 6, 2023
1 parent 62ed5d6 commit 84b3f02
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/OpenTelemetry/Sdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,7 @@ static Sdk()
SelfDiagnostics.EnsureInitialized();

var assemblyInformationalVersion = typeof(Sdk).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
if (string.IsNullOrWhiteSpace(assemblyInformationalVersion))
{
assemblyInformationalVersion = "1.0.0";
}

/*
* InformationalVersion will be in the following format:
* {majorVersion}.{minorVersion}.{patchVersion}.{pre-release label}.{pre-release version}.{gitHeight}+{Git SHA of current commit}
* Ex: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4
* The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit
*/

var indexOfPlusSign = assemblyInformationalVersion!.IndexOf('+');
InformationalVersion = indexOfPlusSign > 0
? assemblyInformationalVersion.Substring(0, indexOfPlusSign)
: assemblyInformationalVersion;
InformationalVersion = ParseAssemblyInformationalVersion(assemblyInformationalVersion);
}

/// <summary>
Expand Down Expand Up @@ -119,5 +104,25 @@ public static TracerProviderBuilder CreateTracerProviderBuilder()
{
return new TracerProviderBuilderBase();
}

internal static string ParseAssemblyInformationalVersion(string? informationalVersion)
{
if (string.IsNullOrWhiteSpace(informationalVersion))
{
informationalVersion = "1.0.0";
}

/*
* InformationalVersion will be in the following format:
* {majorVersion}.{minorVersion}.{patchVersion}.{pre-release label}.{pre-release version}.{gitHeight}+{Git SHA of current commit}
* Ex: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4
* The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit
*/

var indexOfPlusSign = informationalVersion!.IndexOf('+');
return indexOfPlusSign > 0
? informationalVersion.Substring(0, indexOfPlusSign)
: informationalVersion;
}
}
}
41 changes: 41 additions & 0 deletions test/OpenTelemetry.Tests/SdkTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// <copyright file="SdkTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// 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.
// </copyright>

#nullable enable

using Xunit;

namespace OpenTelemetry.Tests;

public class SdkTests
{
[Theory]
[InlineData(null, "1.0.0")]
[InlineData("1.5.0", "1.5.0")]
[InlineData("1.0.0.0", "1.0.0.0")]
[InlineData("1.0-beta.1", "1.0-beta.1")]
[InlineData("1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4", "1.5.0-alpha.1.40")]
[InlineData("1.5.0-rc.1+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4", "1.5.0-rc.1")]
[InlineData("8.0", "8.0")]
[InlineData("8", "8")]
[InlineData("8.0.1.18-alpha1", "8.0.1.18-alpha1")]
public void ParseAssemblyInformationalVersionTests(string? informationalVersion, string expectedVersion)
{
var actualVersion = Sdk.ParseAssemblyInformationalVersion(informationalVersion);

Assert.Equal(expectedVersion, actualVersion);
}
}

0 comments on commit 84b3f02

Please sign in to comment.