Skip to content

Commit

Permalink
chore: Remove Sonar findings
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Mar 19, 2024
1 parent 9b83bde commit 14f9ad9
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Testcontainers/Builders/BuildConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class BuildConfiguration
/// <returns>Changed configuration object. If there is no change, the previous configuration object.</returns>
public static T Combine<T>(T oldValue, T newValue)
{
return newValue == null ? oldValue : newValue;
return Equals(default(T), newValue) ? oldValue : newValue;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers/Guard.Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ArgumentInfo(TType value, string name)
/// <returns>True if the argument has a value; otherwise, false.</returns>
public bool HasValue()
{
return Value != null;
return !Equals(default(TType), Value);
}
}
}
Expand Down
11 changes: 3 additions & 8 deletions src/Testcontainers/Images/DockerImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public sealed class DockerImage : IImage

private static readonly char[] TrimChars = { ' ', ':', '/' };

private static readonly char[] HostnameIdentifierChars = { '.', ':' };

private readonly string _hubImageNamePrefix;

private readonly Lazy<string> _lazyFullName;
Expand Down Expand Up @@ -86,14 +88,7 @@ public DockerImage(
.First()
.Split('/')[0];
if (firstSegmentOfRepository.IndexOfAny(new[] { '.', ':' }) >= 0)
{
return firstSegmentOfRepository;
}
else
{
return null;
}
return firstSegmentOfRepository.IndexOfAny(HostnameIdentifierChars) >= 0 ? firstSegmentOfRepository : null;
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers/Images/DockerfileArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public DockerfileArchive(DirectoryInfo dockerfileDirectory, FileInfo dockerfile,
throw new ArgumentException($"Directory '{dockerfileDirectory.FullName}' does not exist.");
}

if (!dockerfileDirectory.GetFiles(dockerfile.ToString(), SearchOption.TopDirectoryOnly).Any())
if (dockerfileDirectory.GetFiles(dockerfile.ToString(), SearchOption.TopDirectoryOnly).Length == 0)
{
throw new ArgumentException($"{dockerfile} does not exist in '{dockerfileDirectory.FullName}'.");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Testcontainers.Commons/CommonImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace DotNet.Testcontainers.Commons;
[PublicAPI]
public static class CommonImages
{
public static readonly IImage Ryuk = new DockerImage("testcontainers/ryuk:0.5.1");
public static readonly IImage Ryuk = new DockerImage("testcontainers/ryuk:0.6.0");

public static readonly IImage Alpine = new DockerImage("alpine:3.17");

Expand Down
10 changes: 7 additions & 3 deletions tests/Testcontainers.Databases.Tests/DatabasesContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public void ShouldNotImplementIDatabaseContainer(Type type)
Assert.False(type.IsAssignableTo(typeof(IDatabaseContainer)), $"The type '{type.Name}' does implement the database interface.");
}

public static IEnumerable<object[]> GetContainerImplementations(bool expectDataProvider)
public static TheoryData<Type> GetContainerImplementations(bool expectDataProvider)
{
var theoryData = new TheoryData<Type>();

var testAssemblies = Directory.GetFiles(".", "Testcontainers.*.Tests.dll", SearchOption.TopDirectoryOnly)
.Select(Path.GetFullPath)
.Select(Assembly.LoadFrom)
Expand All @@ -41,14 +43,16 @@ public static IEnumerable<object[]> GetContainerImplementations(bool expectDataP

if (expectDataProvider && hasDataProvider)
{
yield return new object[] { containerType };
theoryData.Add(containerType);
}

if (!expectDataProvider && !hasDataProvider)
{
yield return new object[] { containerType };
theoryData.Add(containerType);
}
}
}

return theoryData;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using System;
using System.Collections.Generic;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
Expand All @@ -14,20 +13,20 @@ public static class DockerImageNameSubstitutionTest
[Collection(nameof(DockerImageNameSubstitutionTest))]
public sealed class HubImageNamePrefixIsSet : IDisposable
{
public static IEnumerable<object[]> Substitutions { get; }
= new[]
public static TheoryData<string, string, string> Substitutions { get; }
= new TheoryData<string, string, string>
{
new[] { "my.proxy.com", "bar", "my.proxy.com/bar:latest" },
new[] { "my.proxy.com", "bar:latest", "my.proxy.com/bar:latest" },
new[] { "my.proxy.com", "bar:1.0.0", "my.proxy.com/bar:1.0.0" },
new[] { "my.proxy.com/my-path", "bar:1.0.0", "my.proxy.com/my-path/bar:1.0.0" },
new[] { "my.proxy.com:443", "bar:1.0.0", "my.proxy.com:443/bar:1.0.0" },
new[] { "my.proxy.com", "foo/bar:1.0.0", "my.proxy.com/foo/bar:1.0.0" },
new[] { "my.proxy.com/my-path", "foo/bar:1.0.0", "my.proxy.com/my-path/foo/bar:1.0.0" },
new[] { "my.proxy.com:443", "foo/bar:1.0.0", "my.proxy.com:443/foo/bar:1.0.0" },
new[] { "my.proxy.com:443/my-path", "foo/bar:1.0.0", "my.proxy.com:443/my-path/foo/bar:1.0.0" },
new[] { "my.proxy.com", "myregistry.azurecr.io/foo/bar:1.0.0", "myregistry.azurecr.io/foo/bar:1.0.0" },
new[] { "my.proxy.com", "myregistry.azurecr.io:443/foo/bar:1.0.0", "myregistry.azurecr.io:443/foo/bar:1.0.0" },
{ "my.proxy.com", "bar", "my.proxy.com/bar:latest" },
{ "my.proxy.com", "bar:latest", "my.proxy.com/bar:latest" },
{ "my.proxy.com", "bar:1.0.0", "my.proxy.com/bar:1.0.0" },
{ "my.proxy.com/my-path", "bar:1.0.0", "my.proxy.com/my-path/bar:1.0.0" },
{ "my.proxy.com:443", "bar:1.0.0", "my.proxy.com:443/bar:1.0.0" },
{ "my.proxy.com", "foo/bar:1.0.0", "my.proxy.com/foo/bar:1.0.0" },
{ "my.proxy.com/my-path", "foo/bar:1.0.0", "my.proxy.com/my-path/foo/bar:1.0.0" },
{ "my.proxy.com:443", "foo/bar:1.0.0", "my.proxy.com:443/foo/bar:1.0.0" },
{ "my.proxy.com:443/my-path", "foo/bar:1.0.0", "my.proxy.com:443/my-path/foo/bar:1.0.0" },
{ "my.proxy.com", "myregistry.azurecr.io/foo/bar:1.0.0", "myregistry.azurecr.io/foo/bar:1.0.0" },
{ "my.proxy.com", "myregistry.azurecr.io:443/foo/bar:1.0.0", "myregistry.azurecr.io:443/foo/bar:1.0.0" },
};

[Theory]
Expand Down

0 comments on commit 14f9ad9

Please sign in to comment.