Skip to content

Commit ac9625b

Browse files
authored
chore: Rename UnixFileMode to UnixFileModes (#925)
1 parent f7f3c49 commit ac9625b

13 files changed

+108
-60
lines changed

build.cake

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#tool nuget:?package=dotnet-sonarscanner&version=5.11.0
1+
#tool nuget:?package=dotnet-sonarscanner&version=5.13.0
22

33
#addin nuget:?package=Cake.Sonar&version=1.1.31
44

src/Testcontainers/Clients/ITestcontainersClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ internal interface ITestcontainersClient
122122
/// <param name="fileMode">The POSIX file mode permission.</param>
123123
/// <param name="ct">Cancellation token.</param>
124124
/// <returns>A task that completes when the directory has been copied.</returns>
125-
Task CopyAsync(string id, DirectoryInfo source, string target, UnixFileMode fileMode, CancellationToken ct = default);
125+
Task CopyAsync(string id, DirectoryInfo source, string target, UnixFileModes fileMode, CancellationToken ct = default);
126126

127127
/// <summary>
128128
/// Copies a test host file to the container.
@@ -133,7 +133,7 @@ internal interface ITestcontainersClient
133133
/// <param name="fileMode">The POSIX file mode permission.</param>
134134
/// <param name="ct">Cancellation token.</param>
135135
/// <returns>A task that completes when the file has been copied.</returns>
136-
Task CopyAsync(string id, FileInfo source, string target, UnixFileMode fileMode, CancellationToken ct = default);
136+
Task CopyAsync(string id, FileInfo source, string target, UnixFileModes fileMode, CancellationToken ct = default);
137137

138138
/// <summary>
139139
/// Copies a file to the container.

src/Testcontainers/Clients/TestcontainersClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ await Container.ExtractArchiveToContainerAsync(id, "/", tarOutputMemStream, ct)
179179
}
180180

181181
/// <inheritdoc />
182-
public async Task CopyAsync(string id, DirectoryInfo source, string target, UnixFileMode fileMode, CancellationToken ct = default)
182+
public async Task CopyAsync(string id, DirectoryInfo source, string target, UnixFileModes fileMode, CancellationToken ct = default)
183183
{
184184
using (var tarOutputMemStream = new TarOutputMemoryStream(target))
185185
{
@@ -195,7 +195,7 @@ await Container.ExtractArchiveToContainerAsync(id, "/", tarOutputMemStream, ct)
195195
}
196196

197197
/// <inheritdoc />
198-
public async Task CopyAsync(string id, FileInfo source, string target, UnixFileMode fileMode, CancellationToken ct = default)
198+
public async Task CopyAsync(string id, FileInfo source, string target, UnixFileModes fileMode, CancellationToken ct = default)
199199
{
200200
using (var tarOutputMemStream = new TarOutputMemoryStream(target))
201201
{

src/Testcontainers/Configurations/Unix.cs

+34-34
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,56 @@ public sealed class Unix : IOperatingSystem
1313
/// <summary>
1414
/// Represents the Unix file mode 644, which grants read and write permissions to the user and read permissions to the group and others.
1515
/// </summary>
16-
public const UnixFileMode FileMode644 =
17-
UnixFileMode.UserRead |
18-
UnixFileMode.UserWrite |
19-
UnixFileMode.GroupRead |
20-
UnixFileMode.OtherRead;
16+
public const UnixFileModes FileMode644 =
17+
UnixFileModes.UserRead |
18+
UnixFileModes.UserWrite |
19+
UnixFileModes.GroupRead |
20+
UnixFileModes.OtherRead;
2121

2222
/// <summary>
2323
/// Represents the Unix file mode 666, which grants read and write permissions to the user, group, and others.
2424
/// </summary>
25-
public const UnixFileMode FileMode666 =
26-
UnixFileMode.UserRead |
27-
UnixFileMode.UserWrite |
28-
UnixFileMode.GroupRead |
29-
UnixFileMode.GroupWrite |
30-
UnixFileMode.OtherRead |
31-
UnixFileMode.OtherWrite;
25+
public const UnixFileModes FileMode666 =
26+
UnixFileModes.UserRead |
27+
UnixFileModes.UserWrite |
28+
UnixFileModes.GroupRead |
29+
UnixFileModes.GroupWrite |
30+
UnixFileModes.OtherRead |
31+
UnixFileModes.OtherWrite;
3232

3333
/// <summary>
3434
/// Represents the Unix file mode 700, which grants read, write, and execute permissions to the user, and no permissions to the group and others.
3535
/// </summary>
36-
public const UnixFileMode FileMode700 =
37-
UnixFileMode.UserRead |
38-
UnixFileMode.UserWrite |
39-
UnixFileMode.UserExecute;
36+
public const UnixFileModes FileMode700 =
37+
UnixFileModes.UserRead |
38+
UnixFileModes.UserWrite |
39+
UnixFileModes.UserExecute;
4040

4141
/// <summary>
4242
/// Represents the Unix file mode 755, which grants read, write, and execute permissions to the user, and read and execute permissions to the group and others.
4343
/// </summary>
44-
public const UnixFileMode FileMode755 =
45-
UnixFileMode.UserRead |
46-
UnixFileMode.UserWrite |
47-
UnixFileMode.UserExecute |
48-
UnixFileMode.GroupRead |
49-
UnixFileMode.GroupExecute |
50-
UnixFileMode.OtherRead |
51-
UnixFileMode.OtherExecute;
44+
public const UnixFileModes FileMode755 =
45+
UnixFileModes.UserRead |
46+
UnixFileModes.UserWrite |
47+
UnixFileModes.UserExecute |
48+
UnixFileModes.GroupRead |
49+
UnixFileModes.GroupExecute |
50+
UnixFileModes.OtherRead |
51+
UnixFileModes.OtherExecute;
5252

5353
/// <summary>
5454
/// Represents the Unix file mode 777, which grants read, write, and execute permissions to the user, group, and others.
5555
/// </summary>
56-
public const UnixFileMode FileMode777 =
57-
UnixFileMode.UserRead |
58-
UnixFileMode.UserWrite |
59-
UnixFileMode.UserExecute |
60-
UnixFileMode.GroupRead |
61-
UnixFileMode.GroupWrite |
62-
UnixFileMode.GroupExecute |
63-
UnixFileMode.OtherRead |
64-
UnixFileMode.OtherWrite |
65-
UnixFileMode.OtherExecute;
56+
public const UnixFileModes FileMode777 =
57+
UnixFileModes.UserRead |
58+
UnixFileModes.UserWrite |
59+
UnixFileModes.UserExecute |
60+
UnixFileModes.GroupRead |
61+
UnixFileModes.GroupWrite |
62+
UnixFileModes.GroupExecute |
63+
UnixFileModes.OtherRead |
64+
UnixFileModes.OtherWrite |
65+
UnixFileModes.OtherExecute;
6666

6767
/// <summary>
6868
/// Initializes a new instance of the <see cref="Unix" /> class.

src/Testcontainers/Configurations/UnixFileMode.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace DotNet.Testcontainers.Configurations
88
/// </summary>
99
[PublicAPI]
1010
[Flags]
11-
public enum UnixFileMode
11+
public enum UnixFileModes
1212
{
1313
/// <summary>
1414
/// No permissions.

src/Testcontainers/Configurations/Volumes/BinaryResourceMapping.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class BinaryResourceMapping : FileResourceMapping
1414
/// <param name="resourceContent">The byte array content to map in the container.</param>
1515
/// <param name="containerPath">The absolute path of a file to map in the container.</param>
1616
/// <param name="fileMode">The POSIX file mode permission.</param>
17-
public BinaryResourceMapping(byte[] resourceContent, string containerPath, UnixFileMode fileMode = Unix.FileMode644)
17+
public BinaryResourceMapping(byte[] resourceContent, string containerPath, UnixFileModes fileMode = Unix.FileMode644)
1818
: base(string.Empty, containerPath, fileMode)
1919
{
2020
_resourceContent = resourceContent;

src/Testcontainers/Configurations/Volumes/FileResourceMapping.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal class FileResourceMapping : IResourceMapping
1313
/// <param name="hostPath">The absolute path of a file to map on the host system.</param>
1414
/// <param name="containerPath">The absolute path of a file to map in the container.</param>
1515
/// <param name="fileMode">The POSIX file mode permission.</param>
16-
public FileResourceMapping(string hostPath, string containerPath, UnixFileMode fileMode = Unix.FileMode644)
16+
public FileResourceMapping(string hostPath, string containerPath, UnixFileModes fileMode = Unix.FileMode644)
1717
{
1818
Type = MountType.Bind;
1919
Source = hostPath;
@@ -35,7 +35,7 @@ public FileResourceMapping(string hostPath, string containerPath, UnixFileMode f
3535
public string Target { get; }
3636

3737
/// <inheritdoc />
38-
public UnixFileMode FileMode { get; }
38+
public UnixFileModes FileMode { get; }
3939

4040
/// <inheritdoc />
4141
public Task CreateAsync(CancellationToken ct = default)

src/Testcontainers/Configurations/Volumes/IResourceMapping.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IResourceMapping : IMount
1313
/// <summary>
1414
/// Gets the Unix file mode.
1515
/// </summary>
16-
UnixFileMode FileMode { get; }
16+
UnixFileModes FileMode { get; }
1717

1818
/// <summary>
1919
/// Gets the byte array content of the resource mapping.

src/Testcontainers/Containers/DockerContainer.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,13 @@ await UnsafeStopAsync(ct)
280280
}
281281

282282
/// <inheritdoc />
283-
public Task CopyAsync(byte[] fileContent, string filePath, UnixFileMode fileMode = Unix.FileMode644, CancellationToken ct = default)
283+
public Task CopyAsync(byte[] fileContent, string filePath, UnixFileModes fileMode = Unix.FileMode644, CancellationToken ct = default)
284284
{
285285
return _client.CopyAsync(Id, new BinaryResourceMapping(fileContent, filePath, fileMode), ct);
286286
}
287287

288288
/// <inheritdoc />
289-
public Task CopyAsync(string source, string target, UnixFileMode fileMode = Unix.FileMode644, CancellationToken ct = default)
289+
public Task CopyAsync(string source, string target, UnixFileModes fileMode = Unix.FileMode644, CancellationToken ct = default)
290290
{
291291
var fileAttributes = File.GetAttributes(source);
292292

@@ -301,13 +301,13 @@ public Task CopyAsync(string source, string target, UnixFileMode fileMode = Unix
301301
}
302302

303303
/// <inheritdoc />
304-
public Task CopyAsync(FileInfo source, string target, UnixFileMode fileMode = Unix.FileMode644, CancellationToken ct = default)
304+
public Task CopyAsync(FileInfo source, string target, UnixFileModes fileMode = Unix.FileMode644, CancellationToken ct = default)
305305
{
306306
return _client.CopyAsync(Id, source, target, fileMode, ct);
307307
}
308308

309309
/// <inheritdoc />
310-
public Task CopyAsync(DirectoryInfo source, string target, UnixFileMode fileMode = Unix.FileMode644, CancellationToken ct = default)
310+
public Task CopyAsync(DirectoryInfo source, string target, UnixFileModes fileMode = Unix.FileMode644, CancellationToken ct = default)
311311
{
312312
return _client.CopyAsync(Id, source, target, fileMode, ct);
313313
}

src/Testcontainers/Containers/IContainer.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public interface IContainer : IAsyncDisposable
174174
/// <param name="fileMode">The POSIX file mode permission.</param>
175175
/// <param name="ct">Cancellation token.</param>
176176
/// <returns></returns>
177-
Task CopyAsync(byte[] fileContent, string filePath, UnixFileMode fileMode = Unix.FileMode644, CancellationToken ct = default);
177+
Task CopyAsync(byte[] fileContent, string filePath, UnixFileModes fileMode = Unix.FileMode644, CancellationToken ct = default);
178178

179179
/// <summary>
180180
/// Copies a test host directory or file to the container.
@@ -184,7 +184,7 @@ public interface IContainer : IAsyncDisposable
184184
/// <param name="fileMode">The POSIX file mode permission.</param>
185185
/// <param name="ct">Cancellation token.</param>
186186
/// <returns>A task that completes when the directory or file has been copied.</returns>
187-
Task CopyAsync(string source, string target, UnixFileMode fileMode = Unix.FileMode644, CancellationToken ct = default);
187+
Task CopyAsync(string source, string target, UnixFileModes fileMode = Unix.FileMode644, CancellationToken ct = default);
188188

189189
/// <summary>
190190
/// Copies a test host directory to the container.
@@ -194,7 +194,7 @@ public interface IContainer : IAsyncDisposable
194194
/// <param name="fileMode">The POSIX file mode permission.</param>
195195
/// <param name="ct">Cancellation token.</param>
196196
/// <returns>A task that completes when the directory has been copied.</returns>
197-
Task CopyAsync(DirectoryInfo source, string target, UnixFileMode fileMode = Unix.FileMode644, CancellationToken ct = default);
197+
Task CopyAsync(DirectoryInfo source, string target, UnixFileModes fileMode = Unix.FileMode644, CancellationToken ct = default);
198198

199199
/// <summary>
200200
/// Copies a test host file to the container.
@@ -204,7 +204,7 @@ public interface IContainer : IAsyncDisposable
204204
/// <param name="fileMode">The POSIX file mode permission.</param>
205205
/// <param name="ct">Cancellation token.</param>
206206
/// <returns>A task that completes when the file has been copied.</returns>
207-
Task CopyAsync(FileInfo source, string target, UnixFileMode fileMode = Unix.FileMode644, CancellationToken ct = default);
207+
Task CopyAsync(FileInfo source, string target, UnixFileModes fileMode = Unix.FileMode644, CancellationToken ct = default);
208208

209209
/// <summary>
210210
/// Copies a file to the container.

src/Testcontainers/Containers/TarOutputMemoryStream.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ await CloseEntryAsync(ct)
7474
/// <param name="fileMode">The POSIX file mode permission.</param>
7575
/// <param name="ct">Cancellation token.</param>
7676
/// <returns>A task that completes when the file has been added to the archive.</returns>
77-
public Task AddAsync(FileInfo file, UnixFileMode fileMode, CancellationToken ct = default)
77+
public Task AddAsync(FileInfo file, UnixFileModes fileMode, CancellationToken ct = default)
7878
{
7979
return AddAsync(file.Directory, file, fileMode, ct);
8080
}
@@ -86,7 +86,7 @@ public Task AddAsync(FileInfo file, UnixFileMode fileMode, CancellationToken ct
8686
/// <param name="recurse">A value indicating whether the current directory and all its subdirectories are included or not.</param>
8787
/// <param name="fileMode">The POSIX file mode permission.</param>
8888
/// <param name="ct">Cancellation token.</param>
89-
public async Task AddAsync(DirectoryInfo directory, bool recurse, UnixFileMode fileMode, CancellationToken ct = default)
89+
public async Task AddAsync(DirectoryInfo directory, bool recurse, UnixFileModes fileMode, CancellationToken ct = default)
9090
{
9191
var searchOption = recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
9292

@@ -104,7 +104,7 @@ await AddAsync(directory, file, fileMode, ct)
104104
/// <param name="file">The file to add to the archive.</param>
105105
/// <param name="fileMode">The POSIX file mode permission.</param>
106106
/// <param name="ct">Cancellation token.</param>
107-
public async Task AddAsync(DirectoryInfo directory, FileInfo file, UnixFileMode fileMode, CancellationToken ct = default)
107+
public async Task AddAsync(DirectoryInfo directory, FileInfo file, UnixFileModes fileMode, CancellationToken ct = default)
108108
{
109109
using (var stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
110110
{

tests/Testcontainers.Platform.Linux.Tests/TarOutputMemoryStreamTest.cs

+51-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected TarOutputMemoryStreamTest()
1515
}
1616

1717
[Fact]
18-
public void TarFileContainsTestFile()
18+
public void TestFileExistsInTarFile()
1919
{
2020
// Given
2121
IList<string> actual = new List<string>();
@@ -47,7 +47,7 @@ public string Source
4747
public string Target
4848
=> string.Join("/", TargetDirectoryPath, _testFile.Name);
4949

50-
public UnixFileMode FileMode
50+
public UnixFileModes FileMode
5151
=> Unix.FileMode644;
5252

5353
public Task InitializeAsync()
@@ -63,14 +63,15 @@ public Task DisposeAsync()
6363
public void Dispose()
6464
{
6565
_tarOutputMemoryStream.Dispose();
66+
_testFile.Delete();
6667
}
6768

68-
public Task CreateAsync(CancellationToken ct = default)
69+
Task IFutureResource.CreateAsync(CancellationToken ct)
6970
{
7071
return Task.CompletedTask;
7172
}
7273

73-
public Task DeleteAsync(CancellationToken ct = default)
74+
Task IFutureResource.DeleteAsync(CancellationToken ct)
7475
{
7576
return Task.CompletedTask;
7677
}
@@ -79,6 +80,49 @@ public Task<byte[]> GetAllBytesAsync(CancellationToken ct = default)
7980
{
8081
return File.ReadAllBytesAsync(_testFile.FullName, ct);
8182
}
83+
84+
[Fact]
85+
public async Task TestFileExistsInContainer()
86+
{
87+
// Given
88+
var targetFilePath = string.Join("/", string.Empty, "tmp", Guid.NewGuid(), _testFile.Name);
89+
90+
var targetDirectoryPath1 = string.Join("/", string.Empty, "tmp", Guid.NewGuid());
91+
92+
var targetDirectoryPath2 = string.Join("/", string.Empty, "tmp", Guid.NewGuid());
93+
94+
IList<string> targetFilePaths = new List<string>();
95+
targetFilePaths.Add(targetFilePath);
96+
targetFilePaths.Add(string.Join("/", targetDirectoryPath1, _testFile.Name));
97+
targetFilePaths.Add(string.Join("/", targetDirectoryPath2, _testFile.Name));
98+
99+
await using var container = new ContainerBuilder()
100+
.WithImage(CommonImages.Alpine)
101+
.WithEntrypoint(CommonCommands.SleepInfinity)
102+
.Build();
103+
104+
// When
105+
var fileContent = await GetAllBytesAsync()
106+
.ConfigureAwait(false);
107+
108+
await container.StartAsync()
109+
.ConfigureAwait(false);
110+
111+
await container.CopyAsync(fileContent, targetFilePath)
112+
.ConfigureAwait(false);
113+
114+
await container.CopyAsync(_testFile, targetDirectoryPath1)
115+
.ConfigureAwait(false);
116+
117+
await container.CopyAsync(_testFile.Directory, targetDirectoryPath2)
118+
.ConfigureAwait(false);
119+
120+
// Then
121+
var execResults = await Task.WhenAll(targetFilePaths.Select(targetFilePath => container.ExecAsync(new[] { "test", "-f", targetFilePath })))
122+
.ConfigureAwait(false);
123+
124+
Assert.All(execResults, result => Assert.Equal(0, result.ExitCode));
125+
}
82126
}
83127

84128
[UsedImplicitly]
@@ -97,6 +141,7 @@ public Task DisposeAsync()
97141
public void Dispose()
98142
{
99143
_tarOutputMemoryStream.Dispose();
144+
_testFile.Delete();
100145
}
101146
}
102147

@@ -116,6 +161,7 @@ public Task DisposeAsync()
116161
public void Dispose()
117162
{
118163
_tarOutputMemoryStream.Dispose();
164+
_testFile.Delete();
119165
}
120166
}
121167

@@ -127,7 +173,7 @@ public sealed class UnixFileModeTest
127173
[InlineData(Unix.FileMode700, "700")]
128174
[InlineData(Unix.FileMode755, "755")]
129175
[InlineData(Unix.FileMode777, "777")]
130-
public void UnixFileModeResolvesToPosixFilePermission(UnixFileMode fileMode, string posixFilePermission)
176+
public void UnixFileModeResolvesToPosixFilePermission(UnixFileModes fileMode, string posixFilePermission)
131177
{
132178
Assert.Equal(Convert.ToInt32(posixFilePermission, 8), Convert.ToInt32(fileMode));
133179
}

tests/Testcontainers.Platform.Linux.Tests/Usings.cs

+2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
global using System.Collections.Generic;
33
global using System.Globalization;
44
global using System.IO;
5+
global using System.Linq;
56
global using System.Net;
67
global using System.Net.Sockets;
78
global using System.Text;
89
global using System.Threading;
910
global using System.Threading.Tasks;
1011
global using Docker.DotNet.Models;
12+
global using DotNet.Testcontainers;
1113
global using DotNet.Testcontainers.Builders;
1214
global using DotNet.Testcontainers.Commons;
1315
global using DotNet.Testcontainers.Configurations;

0 commit comments

Comments
 (0)