Skip to content

Commit 8eb3fd1

Browse files
authoredApr 19, 2024··
feat: implement GetRandomFileName for simulated Path (#569)
Add GetRandomFileName
1 parent bfaa9f3 commit 8eb3fd1

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using System.Linq;
2-
3-
namespace Testably.Abstractions.Testing.Helpers;
1+
namespace Testably.Abstractions.Testing.Helpers;
42

53
internal partial class Execute
64
{
75
private sealed class MacPath(MockFileSystem fileSystem) : LinuxPath(fileSystem)
86
{
9-
private readonly MockFileSystem _fileSystem = fileSystem;
10-
117
private string? _tempPath;
128

139
/// <inheritdoc cref="IPath.GetTempPath()" />
@@ -16,12 +12,5 @@ public override string GetTempPath()
1612
_tempPath ??= $"/var/folders/{RandomString(2)}/{RandomString(2)}_{RandomString(27)}/T/";
1713
return _tempPath;
1814
}
19-
20-
private string RandomString(int length)
21-
{
22-
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
23-
return new string(Enumerable.Repeat(chars, length)
24-
.Select(s => s[_fileSystem.RandomSystem.Random.Shared.Next(s.Length)]).ToArray());
25-
}
2615
}
2716
}

‎Source/Testably.Abstractions.Testing/Helpers/Execute.SimulatedPath.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Diagnostics.CodeAnalysis;
3+
using System.Linq;
44
using System.Text;
55
#if FEATURE_FILESYSTEM_NET7
66
using Testably.Abstractions.Testing.Storage;
@@ -283,7 +283,7 @@ public ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path)
283283

284284
/// <inheritdoc cref="IPath.GetRandomFileName()" />
285285
public string GetRandomFileName()
286-
=> System.IO.Path.GetRandomFileName();
286+
=> $"{RandomString(8)}.{RandomString(3)}";
287287

288288
#if FEATURE_PATH_RELATIVE
289289
/// <inheritdoc cref="IPath.GetRelativePath(string, string)" />
@@ -489,6 +489,13 @@ private string JoinInternal(string?[] paths)
489489
}
490490
#endif
491491

492+
protected string RandomString(int length)
493+
{
494+
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
495+
return new string(Enumerable.Repeat(chars, length)
496+
.Select(s => s[fileSystem.RandomSystem.Random.Shared.Next(s.Length)]).ToArray());
497+
}
498+
492499
private bool TryGetExtensionIndex(string path, [NotNullWhen(true)] out int? dotIndex)
493500
{
494501
for (int i = path.Length - 1; i >= 0; i--)

‎Tests/Testably.Abstractions.Tests/FileSystem/Path/GetRandomFileNameTests.cs

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ public abstract partial class GetRandomFileNameTests<TFileSystem>
88
: FileSystemTestBase<TFileSystem>
99
where TFileSystem : IFileSystem
1010
{
11+
[SkippableFact]
12+
public void GetRandomFileName_ShouldMatch8Dot3Pattern()
13+
{
14+
string result = FileSystem.Path.GetRandomFileName();
15+
16+
result.Should().Match("????????.???");
17+
}
18+
1119
[SkippableFact]
1220
public void GetRandomFileName_ShouldReturnRandomFileNameWithExtension()
1321
{

0 commit comments

Comments
 (0)
Please sign in to comment.