Skip to content

Commit 2e32476

Browse files
authored
fix: Compute correct relative Dockerfile file path (#1558)
1 parent 1cfac50 commit 2e32476

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<PackageId>$(AssemblyName)</PackageId>
5-
<Version>4.8.0</Version>
5+
<Version>4.8.1</Version>
66
<AssemblyVersion>$(Version)</AssemblyVersion>
77
<FileVersion>$(Version)</FileVersion>
88
<Product>Testcontainers</Product>

src/Testcontainers/Images/DockerfileArchive.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,15 @@ await AddAsync(absoluteFilePath, relativeFilePath, tarOutputStream)
213213
.ConfigureAwait(false);
214214
}
215215

216-
await AddAsync(_dockerfile.FullName, _dockerfile.Name, tarOutputStream)
216+
var dockerfileDirectoryLength = _dockerfileDirectory.FullName
217+
.TrimEnd(Path.DirectorySeparatorChar).Length + 1;
218+
219+
var dockerfileRelativeFilePath = _dockerfile.FullName
220+
.Substring(dockerfileDirectoryLength );
221+
222+
var dockerfileNormalizedRelativeFilePath = Unix.Instance.NormalizePath(dockerfileRelativeFilePath);
223+
224+
await AddAsync(_dockerfile.FullName, dockerfileNormalizedRelativeFilePath, tarOutputStream)
217225
.ConfigureAwait(false);
218226
}
219227
}

tests/Testcontainers.Tests/Unit/Images/ImageFromDockerfileTest.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,40 @@ public async Task DockerfileArchiveTar()
7878
Assert.Equal(expected, actual);
7979
}
8080

81+
[Fact]
82+
public async Task IgnoredDockerfileIsCopiedToTarball()
83+
{
84+
// Given
85+
86+
// This test ensures Dockerfiles in subdirectories are copied to the right paths
87+
// in the tarball, instead of ending up at the root directory:
88+
// https://github.com/testcontainers/testcontainers-dotnet/issues/1557.
89+
IImage image = new DockerImage("localhost/testcontainers", Guid.NewGuid().ToString("D"), string.Empty);
90+
91+
var actual = new SortedSet<string>();
92+
93+
var buildArguments = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>());
94+
95+
var dockerfileArchive = new DockerfileArchive(null, "Assets/", "target/Dockerfile", image, buildArguments, NullLogger.Instance);
96+
97+
var dockerfileArchiveFilePath = await dockerfileArchive.Tar(TestContext.Current.CancellationToken)
98+
.ConfigureAwait(true);
99+
100+
// When
101+
using (var tarOut = new FileStream(dockerfileArchiveFilePath, FileMode.Open, FileAccess.Read))
102+
{
103+
using (var tarIn = TarArchive.CreateInputTarArchive(tarOut, Encoding.Default))
104+
{
105+
tarIn.ProgressMessageEvent += (_, entry, _) => actual.Add(entry.Name);
106+
tarIn.ListContents();
107+
}
108+
}
109+
110+
// Then
111+
Assert.Contains("target/Dockerfile", actual);
112+
Assert.DoesNotContain("Dockerfile", actual);
113+
}
114+
81115
[Fact]
82116
public async Task ThrowsDockerfileDoesNotExist()
83117
{

0 commit comments

Comments
 (0)