Skip to content

Conversation

cimnine
Copy link
Contributor

@cimnine cimnine commented Jul 30, 2025

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.

WithResourceMapping(FileInfo, DirectoryInfo, UnixFileModes fileMode = Unix.FileMode644);
WithResourceMapping(FileInfo, FileInfo, UnixFileModes fileMode = Unix.FileMode644);
WithResourceMapping(DirectoryInfo, DirectoryInfo, UnixFileModes fileMode = Unix.FileMode644);
WithResourceMapping(byte[], FileInfo, UnixFileModes fileMode = Unix.FileMode644);
WithResourceMapping(Uri, FileInfo, UnixFileModes fileMode = Unix.FileMode644);
WithResourceMapping(Uri, DirectoryInfo, UnixFileModes fileMode = Unix.FileMode644);

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

Copy link

netlify bot commented Jul 30, 2025

Deploy Preview for testcontainers-dotnet ready!

Name Link
🔨 Latest commit 016630f
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-dotnet/deploys/688db5b0647fa40008870858
😎 Deploy Preview https://deploy-preview-1497--testcontainers-dotnet.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Collaborator

@HofmeisterAn HofmeisterAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙏.

@cimnine cimnine force-pushed the WithResourceMapping branch from 8e1a8dc to 0dbb5a1 Compare July 31, 2025 15:23
@cimnine cimnine force-pushed the WithResourceMapping branch from 0dbb5a1 to 3399e09 Compare July 31, 2025 15:25
@cimnine
Copy link
Contributor Author

cimnine commented Jul 31, 2025

Could you please remove/revert the unnecessary changes

Sure, sorry for that. I also rebased while at it.

@HofmeisterAn HofmeisterAn added the enhancement New feature or request label Aug 2, 2025
Copy link
Collaborator

@HofmeisterAn HofmeisterAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

if (source.IsFile)
{
return WithResourceMapping(new FileResourceMapping(source.AbsolutePath, target, fileMode));
return WithResourceMapping(new FileResourceMapping(source.AbsolutePath, target.FullName, fileMode));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return WithResourceMapping(new FileResourceMapping(source.AbsolutePath, target.FullName, fileMode));
return WithResourceMapping(new FileInfo(source.AbsolutePath), target, fileMode);

}
else

return WithResourceMapping(new UriResourceMapping(source, target.FullName, fileMode));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will copy the file to the wrong place.

using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

public sealed class WithResourceMappingTest(AlpineBuilderFixture image) : IClassFixture<AlpineBuilderFixture>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of the methods are tested here (this supports HTTP too):

@cimnine
Copy link
Contributor Author

cimnine commented Aug 31, 2025

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).

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 FilePath.of("/blabla/file") and DirPath.of("/jiiihaaa/dir"). And/or an extension method for String like "/path/to/file".AsFile() and "/path/to/dir".AsDir() (comparable to the approach Flurl takes).

@HofmeisterAn
Copy link
Collaborator

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?

@HofmeisterAn HofmeisterAn force-pushed the develop branch 3 times, most recently from 4900ecd to 8fa5f1b Compare October 3, 2025 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Copy a file WithResourceMapping leads to an (empty) directory in the container
2 participants