From 34b4696658e04fd21c64ecddcf8f96f43da582c4 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Wed, 14 Aug 2024 20:19:37 +0200 Subject: [PATCH 1/2] feat: Find CSharp, FSharp or Visual Basic projects --- build.cake | 6 +++--- .../Builders/CommonDirectoryPath.cs | 19 ++++++++++--------- src/Testcontainers/Images/DockerImage.cs | 7 ++++++- .../Unit/Builders/CommonDirectoryPathTest.cs | 7 ++++++- ...ockerEndpointAuthenticationProviderTest.cs | 6 +++--- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/build.cake b/build.cake index 742aa36f0..19090eb2d 100644 --- a/build.cake +++ b/build.cake @@ -1,8 +1,8 @@ -#tool nuget:?package=dotnet-sonarscanner&version=5.15.0 +#tool nuget:?package=dotnet-sonarscanner&version=7.1.1 -#addin nuget:?package=Cake.Sonar&version=1.1.32 +#addin nuget:?package=Cake.Sonar&version=1.1.33 -#addin nuget:?package=Cake.Git&version=3.0.0 +#addin nuget:?package=Cake.Git&version=4.0.0 #load ".cake-scripts/parameters.cake" diff --git a/src/Testcontainers/Builders/CommonDirectoryPath.cs b/src/Testcontainers/Builders/CommonDirectoryPath.cs index 5aead1617..c7dd2c04f 100644 --- a/src/Testcontainers/Builders/CommonDirectoryPath.cs +++ b/src/Testcontainers/Builders/CommonDirectoryPath.cs @@ -80,18 +80,18 @@ public static CommonDirectoryPath GetSolutionDirectory([CallerFilePath, NotNull] } /// - /// Resolves the first CSharp project file upwards the directory tree. + /// Resolves the first CSharp, FSharp or Visual Basic project file upwards the directory tree. /// /// /// Start node is the caller file path directory. End node is the root directory. /// /// The caller file path. - /// The first CSharp project file upwards the directory tree. - /// Thrown when the CSharp project file was not found upwards the directory tree. + /// The first CSharp, FSharp or Visual Basic project file upwards the directory tree. + /// Thrown when no CSharp, FSharp or Visual Basic project file was found upwards the directory tree. [PublicAPI] public static CommonDirectoryPath GetProjectDirectory([CallerFilePath, NotNull] string filePath = "") { - return new CommonDirectoryPath(GetDirectoryPath(Path.GetDirectoryName(filePath), "*.csproj")); + return new CommonDirectoryPath(GetDirectoryPath(Path.GetDirectoryName(filePath), "*.csproj", "*.fsproj", "*.vbproj")); } /// @@ -105,19 +105,20 @@ public static CommonDirectoryPath GetCallerFileDirectory([CallerFilePath, NotNul return new CommonDirectoryPath(Path.GetDirectoryName(filePath)); } - private static string GetDirectoryPath(string path, string searchPattern) + private static string GetDirectoryPath(string path, params string[] searchPatterns) { - return GetDirectoryPath(Directory.Exists(path) ? new DirectoryInfo(path) : null, searchPattern); + return GetDirectoryPath(Directory.Exists(path) ? new DirectoryInfo(path) : null, searchPatterns); } - private static string GetDirectoryPath(DirectoryInfo path, string searchPattern) + private static string GetDirectoryPath(DirectoryInfo path, params string[] searchPatterns) { if (path != null) { - return path.EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly).Any() ? path.FullName : GetDirectoryPath(path.Parent, searchPattern); + var paths = searchPatterns.SelectMany(searchPattern => path.EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly)).Any(); + return paths ? path.FullName : GetDirectoryPath(path.Parent, searchPatterns); } - var message = $"Cannot find '{searchPattern}' and resolve the base directory in the directory tree."; + var message = $"Cannot find '{string.Join(", ", searchPatterns)}' and resolve the base directory in the directory tree."; throw new DirectoryNotFoundException(message); } } diff --git a/src/Testcontainers/Images/DockerImage.cs b/src/Testcontainers/Images/DockerImage.cs index 1014d54e9..ef7341c44 100644 --- a/src/Testcontainers/Images/DockerImage.cs +++ b/src/Testcontainers/Images/DockerImage.cs @@ -127,13 +127,18 @@ public bool MatchVersion(Predicate predicate) public bool MatchVersion(Predicate predicate) { var versionMatch = Regex.Match(Tag, @"^(\d+)(\.\d+)?(\.\d+)?", RegexOptions.None, TimeSpan.FromSeconds(1)); + if (!versionMatch.Success) + { return false; + } if (Version.TryParse(versionMatch.Value, out var version)) + { return predicate(version); + } - // If the regex matches and Version.TryParse fails then it means it's a major version only (i.e. without any . in the version) + // If the Regex matches and Version.TryParse(string?, out Version?) fails then it means it is a major version only (i.e. without any dot separator) return predicate(new Version(int.Parse(versionMatch.Groups[1].Value, NumberStyles.None), 0)); } diff --git a/tests/Testcontainers.Tests/Unit/Builders/CommonDirectoryPathTest.cs b/tests/Testcontainers.Tests/Unit/Builders/CommonDirectoryPathTest.cs index fb8ce7193..14e4e5ea7 100644 --- a/tests/Testcontainers.Tests/Unit/Builders/CommonDirectoryPathTest.cs +++ b/tests/Testcontainers.Tests/Unit/Builders/CommonDirectoryPathTest.cs @@ -1,6 +1,7 @@ namespace DotNet.Testcontainers.Tests.Unit { using System.IO; + using DotNet.Testcontainers.Commons; using DotNet.Testcontainers.Builders; using Xunit; @@ -8,12 +9,16 @@ public sealed class CommonDirectoryPathTest { public static TheoryData CommonDirectoryPaths() { + using var fsprojFileStream = File.Create(Path.Combine(TestSession.TempDirectoryPath, "Testcontainers.fsproj")); + using var vbprojFileStream = File.Create(Path.Combine(TestSession.TempDirectoryPath, "Testcontainers.vbproj")); var theoryData = new TheoryData(); theoryData.Add(CommonDirectoryPath.GetBinDirectory()); theoryData.Add(CommonDirectoryPath.GetGitDirectory()); theoryData.Add(CommonDirectoryPath.GetProjectDirectory()); theoryData.Add(CommonDirectoryPath.GetSolutionDirectory()); theoryData.Add(CommonDirectoryPath.GetCallerFileDirectory()); + theoryData.Add(CommonDirectoryPath.GetProjectDirectory(fsprojFileStream.Name)); + theoryData.Add(CommonDirectoryPath.GetProjectDirectory(vbprojFileStream.Name)); return theoryData; } @@ -28,7 +33,7 @@ public void CommonDirectoryPathExists(CommonDirectoryPath commonDirectoryPath) public void CommonDirectoryPathNotExists() { var callerFilePath = Path.GetPathRoot(Directory.GetCurrentDirectory()); - Assert.Throws(() => CommonDirectoryPath.GetGitDirectory(callerFilePath)); + Assert.Throws(() => CommonDirectoryPath.GetGitDirectory(callerFilePath!)); } } } diff --git a/tests/Testcontainers.Tests/Unit/Configurations/DockerEndpointAuthenticationProviderTest.cs b/tests/Testcontainers.Tests/Unit/Configurations/DockerEndpointAuthenticationProviderTest.cs index 391a163d6..2ba73314f 100644 --- a/tests/Testcontainers.Tests/Unit/Configurations/DockerEndpointAuthenticationProviderTest.cs +++ b/tests/Testcontainers.Tests/Unit/Configurations/DockerEndpointAuthenticationProviderTest.cs @@ -23,9 +23,9 @@ public sealed class DockerEndpointAuthenticationProviderTest static DockerEndpointAuthenticationProviderTest() { _ = Directory.CreateDirectory(CertificatesDirectoryPath); - _ = File.Create(Path.Combine(CertificatesDirectoryPath, "ca.pem")); - _ = File.Create(Path.Combine(CertificatesDirectoryPath, "cert.pem")); - _ = File.Create(Path.Combine(CertificatesDirectoryPath, "key.pem")); + using var fileStream1 = File.Create(Path.Combine(CertificatesDirectoryPath, "ca.pem")); + using var fileStream2 = File.Create(Path.Combine(CertificatesDirectoryPath, "cert.pem")); + using var fileStream3 = File.Create(Path.Combine(CertificatesDirectoryPath, "key.pem")); } [Theory] From db737667c556b2b8d996f4935e29654afd05bcf6 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Wed, 14 Aug 2024 20:42:14 +0200 Subject: [PATCH 2/2] fix: Do not update Cake --- .cake-scripts/version.cake | 2 +- build.cake | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.cake-scripts/version.cake b/.cake-scripts/version.cake index aaad7af64..a8ab7b043 100644 --- a/.cake-scripts/version.cake +++ b/.cake-scripts/version.cake @@ -1,4 +1,4 @@ -#addin nuget:?package=Cake.Git&version=2.0.0 +#addin nuget:?package=Cake.Git&version=3.0.0 internal sealed class BuildInformation { diff --git a/build.cake b/build.cake index 19090eb2d..7ce01ac0c 100644 --- a/build.cake +++ b/build.cake @@ -1,8 +1,8 @@ #tool nuget:?package=dotnet-sonarscanner&version=7.1.1 -#addin nuget:?package=Cake.Sonar&version=1.1.33 +#addin nuget:?package=Cake.Sonar&version=1.1.32 -#addin nuget:?package=Cake.Git&version=4.0.0 +#addin nuget:?package=Cake.Git&version=3.0.0 #load ".cake-scripts/parameters.cake"