Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Detect CSharp, FSharp and Visual Basic projects #1234

Merged
merged 2 commits into from
Aug 15, 2024
Merged
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
2 changes: 1 addition & 1 deletion .cake-scripts/version.cake
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#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

Expand Down
19 changes: 10 additions & 9 deletions src/Testcontainers/Builders/CommonDirectoryPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ public static CommonDirectoryPath GetSolutionDirectory([CallerFilePath, NotNull]
}

/// <summary>
/// Resolves the first CSharp project file upwards the directory tree.
/// Resolves the first CSharp, FSharp or Visual Basic project file upwards the directory tree.
/// </summary>
/// <remarks>
/// Start node is the caller file path directory. End node is the root directory.
/// </remarks>
/// <param name="filePath">The caller file path.</param>
/// <returns>The first CSharp project file upwards the directory tree.</returns>
/// <exception cref="DirectoryNotFoundException">Thrown when the CSharp project file was not found upwards the directory tree.</exception>
/// <returns>The first CSharp, FSharp or Visual Basic project file upwards the directory tree.</returns>
/// <exception cref="DirectoryNotFoundException">Thrown when no CSharp, FSharp or Visual Basic project file was found upwards the directory tree.</exception>
[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"));
}

/// <summary>
Expand All @@ -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);
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Testcontainers/Images/DockerImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@ public bool MatchVersion(Predicate<string> predicate)
public bool MatchVersion(Predicate<Version> 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));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using System.IO;
using DotNet.Testcontainers.Commons;
using DotNet.Testcontainers.Builders;
using Xunit;

public sealed class CommonDirectoryPathTest
{
public static TheoryData<CommonDirectoryPath> 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<CommonDirectoryPath>();
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;
}

Expand All @@ -28,7 +33,7 @@ public void CommonDirectoryPathExists(CommonDirectoryPath commonDirectoryPath)
public void CommonDirectoryPathNotExists()
{
var callerFilePath = Path.GetPathRoot(Directory.GetCurrentDirectory());
Assert.Throws<DirectoryNotFoundException>(() => CommonDirectoryPath.GetGitDirectory(callerFilePath));
Assert.Throws<DirectoryNotFoundException>(() => CommonDirectoryPath.GetGitDirectory(callerFilePath!));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading