Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement GetRandomFileName for simulated Path #569

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions Source/Testably.Abstractions.Testing/Helpers/Execute.MacPath.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System.Linq;

namespace Testably.Abstractions.Testing.Helpers;
namespace Testably.Abstractions.Testing.Helpers;

internal partial class Execute
{
private sealed class MacPath(MockFileSystem fileSystem) : LinuxPath(fileSystem)
{
private readonly MockFileSystem _fileSystem = fileSystem;

private string? _tempPath;

/// <inheritdoc cref="IPath.GetTempPath()" />
Expand All @@ -16,12 +12,5 @@ public override string GetTempPath()
_tempPath ??= $"/var/folders/{RandomString(2)}/{RandomString(2)}_{RandomString(27)}/T/";
return _tempPath;
}

private string RandomString(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[_fileSystem.RandomSystem.Random.Shared.Next(s.Length)]).ToArray());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
#if FEATURE_FILESYSTEM_NET7
using Testably.Abstractions.Testing.Storage;
Expand Down Expand Up @@ -283,7 +283,7 @@ public ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path)

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

#if FEATURE_PATH_RELATIVE
/// <inheritdoc cref="IPath.GetRelativePath(string, string)" />
Expand Down Expand Up @@ -489,6 +489,13 @@ private string JoinInternal(string?[] paths)
}
#endif

protected string RandomString(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[fileSystem.RandomSystem.Random.Shared.Next(s.Length)]).ToArray());
}

private bool TryGetExtensionIndex(string path, [NotNullWhen(true)] out int? dotIndex)
{
for (int i = path.Length - 1; i >= 0; i--)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ public abstract partial class GetRandomFileNameTests<TFileSystem>
: FileSystemTestBase<TFileSystem>
where TFileSystem : IFileSystem
{
[SkippableFact]
public void GetRandomFileName_ShouldMatch8Dot3Pattern()
{
string result = FileSystem.Path.GetRandomFileName();

result.Should().Match("????????.???");
}

[SkippableFact]
public void GetRandomFileName_ShouldReturnRandomFileNameWithExtension()
{
Expand Down
Loading