feat: Add typed WithResourceMapping(...) overloads#1497
feat: Add typed WithResourceMapping(...) overloads#1497HofmeisterAn merged 11 commits intotestcontainers:developfrom
Conversation
✅ Deploy Preview for testcontainers-dotnet ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
HofmeisterAn
left a comment
There was a problem hiding this comment.
Thanks for taking care of this! Could you please remove/revert the unnecessary changes (like extra whitespace, line breaks, etc.)? That would make the review much easier 🙏.
8e1a8dc to
0dbb5a1
Compare
0dbb5a1 to
3399e09
Compare
Sure, sorry for that. I also rebased while at it. |
HofmeisterAn
left a comment
There was a problem hiding this comment.
Thanks for the PR 🙏.
After taking a closer look at the PR, I remembered why we didn't introduce this earlier.
The issue with FileSystemInfo implementations (DirectoryInfo, FileInfo) is that they obviously refer to the underlying OS.
On Windows, some of the tests fail because files can't be copied into the container properly.
For example, using new FileInfo("/test.txt") ends up referring to C:\test.txt, which isn't what we want (as target).
We need an abstraction that works with the target container OS as well, unfortunately.
tests/Testcontainers.Tests/Unit/Configurations/WithResourceMappingTest.cs
Outdated
Show resolved
Hide resolved
Pity. What about providing our own abstraction for File- vs. Directory-Paths? Basically a Record containing a single String. I imagine two classes with each a static helper like |
|
Sounds like a good idea 👍. We probably just need something like this (and the equivalent for directory): public readonly record struct FilePath
{
private FilePath(string value)
{
Value = Unix.Instance.NormalizePath(value); // This method already exists in Testcontainers.
}
public string Value { get; }
public static FilePath Of(string path) => new FilePath(path);
}WDYT? |
4900ecd to
8fa5f1b
Compare
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughAdds strongly-typed path wrappers (FilePath, DirectoryPath) and path helpers, expands WithResourceMapping with many new overloads (FilePath/DirectoryPath/FileInfo/DirectoryInfo/Uri/byte[] permutations), updates tests and docs to use them, and standardizes ThrowIf lambdas to return exception instances. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
src/Testcontainers/Builders/IContainerBuilder`2.cs (1)
438-458: Minor documentation inconsistency in remarks.The remarks on lines 448-449 reference
WithResourceMapping(Uri, FilePath, ...)as the method for copying a file to a specific target file path. However, for file-to-file mappings,WithResourceMapping(FileInfo, FileInfo, ...)orWithResourceMapping(FilePath, FilePath, ...)would be more appropriate references since this overload already accepts aDirectoryPathtarget.📝 Suggested documentation fix
/// If you prefer to copy a file to a specific target file path instead of a - /// directory, use: <see cref="WithResourceMapping(Uri, FilePath, uint, uint, UnixFileModes)" />. + /// directory, use: <see cref="WithResourceMapping(FilePath, FilePath, uint, uint, UnixFileModes)" />.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Testcontainers/Builders/IContainerBuilder``2.cs around lines 438 - 458, The XML remarks wrongly suggest the file-to-file overload as WithResourceMapping(Uri, FilePath, ...) while this overload accepts a DirectoryPath target; update the remark to reference the correct file-to-file overloads such as WithResourceMapping(FilePath, FilePath, ...) and/or WithResourceMapping(FileInfo, FileInfo, ...) (or add both) so callers understand which methods to use when copying to a specific target file path; keep the existing DirectoryPath explanation for the current WithResourceMapping(Uri, DirectoryPath, ...) signature.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/Testcontainers/Builders/ContainerBuilder``3.cs:
- Around line 272-276: The WithResourceMapping(FileInfo source, DirectoryInfo
target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
overload in class ContainerBuilder`3 is using target.ToString() when creating
the DirectoryPath; change it to use target.FullName so the path is the actual
filesystem path (i.e., call DirectoryPath.Of(target.FullName)) to match the
FileInfo usage and ensure correct path resolution.
- Around line 309-313: In WithResourceMapping(Uri source, DirectoryInfo target,
uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644) in
ContainerBuilder`3, replace target.ToString() with target.FullName when creating
the DirectoryPath (i.e., call DirectoryPath.Of(target.FullName)) to ensure the
absolute filesystem path is used instead of the DirectoryInfo's ToString()
representation.
- Around line 254-258: Multiple overloads in ContainerBuilder`3 are using
target.ToString() (e.g., in WithResourceMapping and other mapping methods) which
can return a non-absolute path; replace those calls with target.FullName so
DirectoryPath.Of(...) and FilePath.Of(...) receive absolute paths. Update the
occurrences cited: change DirectoryPath.Of(target.ToString()) to
DirectoryPath.Of(target.FullName) and FilePath.Of(target.ToString()) to
FilePath.Of(target.FullName) in the WithResourceMapping(DirectoryInfo, ...), the
other DirectoryInfo overload(s) and the FileInfo overload(s) so all path
resolution uses FullName.
---
Nitpick comments:
In `@src/Testcontainers/Builders/IContainerBuilder``2.cs:
- Around line 438-458: The XML remarks wrongly suggest the file-to-file overload
as WithResourceMapping(Uri, FilePath, ...) while this overload accepts a
DirectoryPath target; update the remark to reference the correct file-to-file
overloads such as WithResourceMapping(FilePath, FilePath, ...) and/or
WithResourceMapping(FileInfo, FileInfo, ...) (or add both) so callers understand
which methods to use when copying to a specific target file path; keep the
existing DirectoryPath explanation for the current WithResourceMapping(Uri,
DirectoryPath, ...) signature.
ℹ️ Review info
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
src/Testcontainers/Builders/ContainerBuilder3.cs`src/Testcontainers/Builders/DirectoryPath.cssrc/Testcontainers/Builders/FilePath.cssrc/Testcontainers/Builders/IContainerBuilder2.cs`src/Testcontainers/Builders/PathExtensions.cstests/Testcontainers.Platform.Linux.Tests/TarOutputMemoryStreamTest.cstests/Testcontainers.Tests/Fixtures/Configurations/AlpineBuilderFixture.cs
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/Testcontainers/Builders/IContainerBuilder`2.cs (1)
332-367: Minor documentation inconsistency in XML comments.The summary for lines 332-333 says "Copies a test host directory or file" but the method signature (
FileInfo source/FilePath source) clearly indicates it's for file sources only. Consider updating to "Copies a test host file to the given directory".📝 Suggested documentation fix
/// <summary> - /// Copies a test host directory or file to the given directory in the container before it starts. + /// Copies a test host file to the given directory in the container before it starts. /// </summary>🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Testcontainers/Builders/IContainerBuilder``2.cs around lines 332 - 367, Update the XML <summary> for the WithResourceMapping overloads to accurately reflect that the source is a file (not "directory or file"): change the summary text for WithResourceMapping(FileInfo, string, ...), WithResourceMapping(FileInfo, DirectoryInfo, ...), and WithResourceMapping(FilePath, DirectoryPath, ...) from "Copies a test host directory or file to the given directory in the container before it starts." to something like "Copies a test host file to the given directory in the container before it starts." and ensure the parameter descriptions remain consistent with the file-source signatures.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/Testcontainers/Builders/ContainerBuilder``3.cs:
- Around line 278-282: The FilePath->DirectoryPath overload
(WithResourceMapping(FilePath source, DirectoryPath target, ...)) currently
passes the target directory as the containerPath, causing incorrect mappings;
change it to extract the filename from source (e.g., via
Path.GetFileName(source.Value) or source.FileName) and combine that filename
with the target directory to produce a full container file path before
constructing FileResourceMapping, mirroring the behavior in the
Uri->DirectoryPath overload (WithResourceMapping(Uri, DirectoryPath, ...)); also
ensure the FileInfo->DirectoryInfo overload that delegates to the FilePath
overload continues to work correctly after this change.
---
Nitpick comments:
In `@src/Testcontainers/Builders/IContainerBuilder``2.cs:
- Around line 332-367: Update the XML <summary> for the WithResourceMapping
overloads to accurately reflect that the source is a file (not "directory or
file"): change the summary text for WithResourceMapping(FileInfo, string, ...),
WithResourceMapping(FileInfo, DirectoryInfo, ...), and
WithResourceMapping(FilePath, DirectoryPath, ...) from "Copies a test host
directory or file to the given directory in the container before it starts." to
something like "Copies a test host file to the given directory in the container
before it starts." and ensure the parameter descriptions remain consistent with
the file-source signatures.
ℹ️ Review info
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/Testcontainers/Builders/ContainerBuilder3.cs`src/Testcontainers/Builders/IContainerBuilder2.cs`
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
docs/api/create_docker_container.md (1)
63-63: Add an explicit deprecation/migration note for string-based overloads.This section introduces typed paths well, but a short note here that
WithResourceMapping(string, string)is deprecated would reduce ambiguity during migration.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@docs/api/create_docker_container.md` at line 63, Add a short explicit deprecation/migration note after the paragraph introducing FilePath and DirectoryPath that states the old string-based overload WithResourceMapping(string, string) is deprecated; instruct consumers to switch to the strongly-typed overloads WithResourceMapping(FilePath, DirectoryPath) (or the corresponding FilePath/FilePath variants) and briefly mention any behavioral or validation differences to be aware of during migration (e.g., path normalization/validation).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@docs/api/create_docker_container.md`:
- Line 82: The example uses Encoding.Default which is platform-dependent; update
the .WithResourceMapping call to use Encoding.UTF8 for the byte array so the
docs/tests produce consistent output across environments (i.e., replace
Encoding.Default.GetBytes("{}") with Encoding.UTF8.GetBytes("{}") in the
.WithResourceMapping(FilePath.Of("/app/appsettings.json")) example).
---
Nitpick comments:
In `@docs/api/create_docker_container.md`:
- Line 63: Add a short explicit deprecation/migration note after the paragraph
introducing FilePath and DirectoryPath that states the old string-based overload
WithResourceMapping(string, string) is deprecated; instruct consumers to switch
to the strongly-typed overloads WithResourceMapping(FilePath, DirectoryPath) (or
the corresponding FilePath/FilePath variants) and briefly mention any behavioral
or validation differences to be aware of during migration (e.g., path
normalization/validation).
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@docs/api/create_docker_container.md`:
- Around line 115-121: The doc example title mismatches the snippet: the heading
says "Copying a file with specific UID and GID" but the code uses
DirectoryPath.Of(...) and ContainerBuilder.WithResourceMapping to copy a
directory; either update the heading to "Copying a directory with specific UID
and GID" or change the snippet to use FilePath.Of(...) (and adjust the
destination to a file path) so the example and title match; reference
ContainerBuilder, WithResourceMapping, DirectoryPath.Of and FilePath.Of when
making the edit.
- Around line 128-133: The heading "Copying a script with executable
permissions" doesn't match the snippet which uses DirectoryPath.Of and maps a
directory via ContainerBuilder.WithResourceMapping; update the example title to
"Copying a directory with executable permissions" to match the code (references:
ContainerBuilder, WithResourceMapping, DirectoryPath.Of, Unix.FileMode755) so
the doc accurately describes the shown directory mapping.
In `@src/Testcontainers/Builders/IContainerBuilder``2.cs:
- Around line 448-552: The XML docs for the typed Uri overloads are
contradictory: update the comments for WithResourceMapping(Uri, DirectoryInfo),
WithResourceMapping(Uri, DirectoryPath) to clearly state these overloads always
copy the source into the specified target directory (filename is derived from
the source and directory structure is not preserved) and remove the ambiguous
"if scheme is file/http/https" wording that implies different behaviors;
likewise update WithResourceMapping(Uri, FileInfo) and WithResourceMapping(Uri,
FilePath) to state they always copy to the specified target file path and accept
http/https/file sources, ensuring summary/remarks match the concrete intent
encoded by the method signatures and adjust or remove misleading examples in
those overloads.
ℹ️ Review info
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
docs/api/create_docker_container.mdsrc/Testcontainers/Builders/IContainerBuilder2.cs`tests/Testcontainers.Platform.Linux.Tests/TarOutputMemoryStreamTest.cs
What does this PR do?
Overhaul of
WithResourceMapping().The methods with string arguments are deprecated and replaced with the following methods. These signatures are explicit with regard to the target path being a file or directory path.
Why is it important?
With the previous API, it wasn't consistent whether the target string path is a directory or a file.
Related issues
Summary by CodeRabbit
New Features
Deprecated
Refactor
Tests
Documentation