forked from testcontainers/testcontainers-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testcontainers#569): Support relative base directories other tha…
…n the working directory with WithDockerfileDirectory
- Loading branch information
1 parent
0eaea8b
commit fcbd4df
Showing
5 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using JetBrains.Annotations; | ||
|
||
/// <summary> | ||
/// Resolves common directory paths. | ||
/// </summary> | ||
[PublicAPI] | ||
public readonly struct CommonDirectoryPath | ||
{ | ||
private static readonly string WorkingDirectoryPath = Directory.GetCurrentDirectory(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CommonDirectoryPath" /> struct. | ||
/// </summary> | ||
/// <param name="directoryPath">The directory path.</param> | ||
[PublicAPI] | ||
public CommonDirectoryPath(string directoryPath) | ||
{ | ||
this.DirectoryPath = directoryPath; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the directory path. | ||
/// </summary> | ||
[PublicAPI] | ||
public string DirectoryPath { get; } | ||
|
||
/// <summary> | ||
/// Resolves the first bin directory upwards the directory tree. | ||
/// </summary> | ||
/// <returns>The first bin directory upwards the directory tree.</returns> | ||
/// <exception cref="DirectoryNotFoundException">Thrown when the bin directory was not found upwards the directory tree.</exception> | ||
[PublicAPI] | ||
public static CommonDirectoryPath GetBinDirectory() | ||
{ | ||
var indexOfBinDirectory = WorkingDirectoryPath.LastIndexOf("bin", StringComparison.OrdinalIgnoreCase); | ||
|
||
if (indexOfBinDirectory > -1) | ||
{ | ||
return new CommonDirectoryPath(WorkingDirectoryPath.Substring(0, indexOfBinDirectory)); | ||
} | ||
|
||
const string message = "Cannot find 'bin' and resolve the base directory in the directory tree."; | ||
throw new DirectoryNotFoundException(message); | ||
} | ||
|
||
/// <summary> | ||
/// Resolves the first Git directory 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 Git directory upwards the directory tree.</returns> | ||
/// <exception cref="DirectoryNotFoundException">Thrown when the Git directory was not found upwards the directory tree.</exception> | ||
[PublicAPI] | ||
public static CommonDirectoryPath GetGitDirectory([CallerFilePath, NotNull] string filePath = "") | ||
{ | ||
return new CommonDirectoryPath(GetDirectoryPath(Path.GetDirectoryName(filePath), ".git")); | ||
} | ||
|
||
/// <summary> | ||
/// Resolves the first Visual Studio solution 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 Visual Studio solution file upwards the directory tree.</returns> | ||
/// <exception cref="DirectoryNotFoundException">Thrown when the Visual Studio solution file was not found upwards the directory tree.</exception> | ||
[PublicAPI] | ||
public static CommonDirectoryPath GetSolutionDirectory([CallerFilePath, NotNull] string filePath = "") | ||
{ | ||
return new CommonDirectoryPath(GetDirectoryPath(Path.GetDirectoryName(filePath), "*.sln")); | ||
} | ||
|
||
/// <summary> | ||
/// Resolves the first CSharp 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> | ||
[PublicAPI] | ||
public static CommonDirectoryPath GetProjectDirectory([CallerFilePath, NotNull] string filePath = "") | ||
{ | ||
return new CommonDirectoryPath(GetDirectoryPath(Path.GetDirectoryName(filePath), "*.csproj")); | ||
} | ||
|
||
/// <summary> | ||
/// Resolves the caller file path directory. | ||
/// </summary> | ||
/// <param name="filePath">The caller file path.</param> | ||
/// <returns>The caller file path directory.</returns> | ||
[PublicAPI] | ||
public static CommonDirectoryPath GetCallerFileDirectory([CallerFilePath, NotNull] string filePath = "") | ||
{ | ||
return new CommonDirectoryPath(Path.GetDirectoryName(filePath)); | ||
} | ||
|
||
private static string GetDirectoryPath(string path, string searchPattern) | ||
{ | ||
return GetDirectoryPath(Directory.Exists(path) ? new DirectoryInfo(path) : null, searchPattern); | ||
} | ||
|
||
private static string GetDirectoryPath(DirectoryInfo path, string searchPattern) | ||
{ | ||
if (path != null) | ||
{ | ||
return path.EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly).Any() ? path.FullName : GetDirectoryPath(path.Parent, searchPattern); | ||
} | ||
|
||
var message = $"Cannot find '{searchPattern}' and resolve the base directory in the directory tree."; | ||
throw new DirectoryNotFoundException(message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/Testcontainers.Tests/Unit/Builders/CommonDirectoryPathTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace DotNet.Testcontainers.Tests.Unit | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using DotNet.Testcontainers.Builders; | ||
using Xunit; | ||
|
||
public sealed class CommonDirectoryPathTest | ||
{ | ||
public static IEnumerable<object[]> CommonDirectoryPaths { get; } | ||
= new[] | ||
{ | ||
new[] { (object)CommonDirectoryPath.GetBinDirectory() }, | ||
new[] { (object)CommonDirectoryPath.GetGitDirectory() }, | ||
new[] { (object)CommonDirectoryPath.GetProjectDirectory() }, | ||
new[] { (object)CommonDirectoryPath.GetSolutionDirectory() }, | ||
new[] { (object)CommonDirectoryPath.GetCallerFileDirectory() }, | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(CommonDirectoryPaths))] | ||
public void CommonDirectoryPathExists(CommonDirectoryPath commonDirectoryPath) | ||
{ | ||
Assert.True(Directory.Exists(commonDirectoryPath.DirectoryPath)); | ||
} | ||
|
||
[Fact] | ||
public void CommonDirectoryPathNotExists() | ||
{ | ||
var callerFilePath = Path.GetPathRoot(Directory.GetCurrentDirectory()); | ||
Assert.Throws<DirectoryNotFoundException>(() => CommonDirectoryPath.GetGitDirectory(callerFilePath)); | ||
} | ||
} | ||
} |