Skip to content

Commit 21d694b

Browse files
committed
refactor: Share version property
1 parent e2bc001 commit 21d694b

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

src/Testcontainers/Clients/DefaultLabels.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace DotNet.Testcontainers.Clients
22
{
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
5-
using System.Reflection;
65
using DotNet.Testcontainers.Containers;
76

87
internal sealed class DefaultLabels : ReadOnlyDictionary<string, string>
@@ -16,7 +15,7 @@ private DefaultLabels()
1615
{
1716
{ TestcontainersClient.TestcontainersLabel, bool.TrueString.ToLowerInvariant() },
1817
{ TestcontainersClient.TestcontainersLangLabel, "dotnet" },
19-
{ TestcontainersClient.TestcontainersVersionLabel, typeof(DefaultLabels).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion },
18+
{ TestcontainersClient.TestcontainersVersionLabel, TestcontainersClient.Version },
2019
{ TestcontainersClient.TestcontainersSessionIdLabel, ResourceReaper.DefaultSessionId.ToString("D") },
2120
{ ResourceReaper.ResourceReaperSessionLabel, ResourceReaper.DefaultSessionId.ToString("D") },
2221
})

src/Testcontainers/Clients/TestcontainersClient.cs

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace DotNet.Testcontainers.Clients
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Reflection;
78
using System.Text;
89
using System.Text.RegularExpressions;
910
using System.Threading;
@@ -28,6 +29,8 @@ internal sealed class TestcontainersClient : ITestcontainersClient
2829

2930
public const string TestcontainersSessionIdLabel = TestcontainersLabel + ".session-id";
3031

32+
public static readonly string Version = typeof(TestcontainersClient).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
33+
3134
private static readonly string OSRootDirectory = Path.GetPathRoot(Directory.GetCurrentDirectory());
3235

3336
private static readonly Regex FromLinePattern = new Regex("FROM (?<arg>--[^\\s]+\\s)*(?<image>[^\\s]+).*", RegexOptions.None, TimeSpan.FromSeconds(1));

src/Testcontainers/Configurations/AuthConfigs/DockerEndpointAuthenticationConfiguration.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
using System.Reflection;
2-
using DotNet.Testcontainers.Clients;
3-
41
namespace DotNet.Testcontainers.Configurations
52
{
63
using System;
74
using System.Collections.Generic;
85
using System.Collections.ObjectModel;
96
using Docker.DotNet;
7+
using DotNet.Testcontainers.Clients;
108
using JetBrains.Annotations;
119

1210
/// <inheritdoc cref="IDockerEndpointAuthenticationConfiguration" />
@@ -33,12 +31,12 @@ public DockerEndpointAuthenticationConfiguration(Uri endpoint, Credentials crede
3331
/// <inheritdoc />
3432
public DockerClientConfiguration GetDockerClientConfiguration(Guid sessionId = default)
3533
{
36-
var testcontainersVersion = typeof(DefaultLabels).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
3734
var defaultHttpRequestHeaders = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>
3835
{
36+
{ "User-Agent", "tc-dotnet/" + TestcontainersClient.Version },
3937
{ "x-tc-sid", sessionId.ToString("D") },
40-
{ "User-Agent", "tc-dotnet/" + testcontainersVersion}
4138
});
39+
4240
return new DockerClientConfiguration(Endpoint, Credentials, defaultHttpRequestHeaders: defaultHttpRequestHeaders);
4341
}
4442
}

0 commit comments

Comments
 (0)