Skip to content

Commit

Permalink
refactor: fix sonar issues (#463)
Browse files Browse the repository at this point in the history
Fix `System.IO.Path` usage and sonar issues
  • Loading branch information
vbreuss authored Feb 26, 2024
1 parent 514706d commit d037fd4
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private DriveInfoMock(string driveName, MockFileSystem fileSystem)
if (driveName.IsUncPath(_fileSystem))
{
IsUncPath = true;
driveName = PathHelper.UncPrefix +
driveName = new string(fileSystem.Path.DirectorySeparatorChar, 2) +
GetTopmostParentDirectory(driveName.Substring(2));
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ public IFileSystem FileSystem
/// <inheritdoc cref="IFileSystemWatcher.Filter" />
public string Filter
{
get => _filters.Count == 0
? _fileSystem.Execute.IsNetFramework ? "*.*" : "*"
: _filters [0];
get
{
if (_filters.Count == 0)
{
return _fileSystem.Execute.IsNetFramework ? "*.*" : "*";
}
return _filters[0];
}
set
{
_filters.Clear();
Expand Down
4 changes: 2 additions & 2 deletions Source/Testably.Abstractions.Testing/FileSystem/PathMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public override string GetRelativePath(string relativeTo, string path)
relativeTo.EnsureValidArgument(_fileSystem, nameof(relativeTo));
path.EnsureValidArgument(_fileSystem, nameof(path));

relativeTo = FileSystem.Path.GetFullPath(relativeTo);
path = FileSystem.Path.GetFullPath(path);
relativeTo = _fileSystem.Path.GetFullPath(relativeTo);
path = _fileSystem.Path.GetFullPath(path);

return Path.GetRelativePath(relativeTo, path);
}
Expand Down
27 changes: 11 additions & 16 deletions Source/Testably.Abstractions.Testing/Helpers/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ internal class Execute
/// </summary>
public static Execute Default { get; } = new();

private bool? _isNetFramework;
public Execute()
{
IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
IsMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
IsNetFramework = RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework");
}

/// <summary>
/// The default <see cref="StringComparison" /> used for comparing paths.
Expand All @@ -24,36 +30,25 @@ internal class Execute
/// <summary>
/// Flag indicating if the code runs on <see cref="OSPlatform.Linux" />.
/// </summary>
public bool IsLinux
=> RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
public bool IsLinux { get; }

/// <summary>
/// Flag indicating if the code runs on <see cref="OSPlatform.OSX" />.
/// </summary>
public bool IsMac
=> RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
public bool IsMac { get; }

/// <summary>
/// Flag indicating if the code runs in .NET Framework.
/// </summary>
/// <remarks>
/// <see href="https://stackoverflow.com/a/53675231" />
/// </remarks>
public bool IsNetFramework
{
get
{
_isNetFramework ??= RuntimeInformation
.FrameworkDescription.StartsWith(".NET Framework");
return _isNetFramework.Value;
}
}
public bool IsNetFramework { get; }

/// <summary>
/// Flag indicating if the code runs on <see cref="OSPlatform.Windows" />.
/// </summary>
public bool IsWindows
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public bool IsWindows { get; }

/// <summary>
/// The <paramref name="callback" /> is executed when the code runs not in .NET Framework.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ internal static class FilePlatformIndependenceExtensions

return fileSystem.Execute.OnWindows(
() => path
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar),
.Replace(fileSystem.Path.AltDirectorySeparatorChar, fileSystem.Path.DirectorySeparatorChar),
() => PathTransformRegex
.Replace(path, "${path}")
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar));
.Replace(fileSystem.Path.AltDirectorySeparatorChar, fileSystem.Path.DirectorySeparatorChar));
}

/// <summary>
Expand All @@ -46,7 +46,7 @@ internal static class FilePlatformIndependenceExtensions
return path;
}

if (Path.IsPathRooted(path))
if (fileSystem.Path.IsPathRooted(path))
{
return path;
}
Expand Down
10 changes: 3 additions & 7 deletions Source/Testably.Abstractions.Testing/Helpers/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ internal static class PathHelper
'*', '?'
};

internal static readonly string UncPrefix = new(Path.DirectorySeparatorChar, 2);

internal static readonly string UncAltPrefix = new(Path.AltDirectorySeparatorChar, 2);

/// <summary>
/// Determines whether the given path contains illegal characters.
/// </summary>
Expand All @@ -37,7 +33,7 @@ internal static bool HasIllegalCharacters(this string path, MockFileSystem fileS
/// For unix, this is empty or null. For Windows, this is empty, null, or
/// just spaces ((char)32).
/// </summary>
internal static bool IsEffectivelyEmpty(this string path, MockFileSystem fileSystem)
internal static bool IsEffectivelyEmpty(this string? path, MockFileSystem fileSystem)
{
if (string.IsNullOrEmpty(path))
{
Expand Down Expand Up @@ -70,8 +66,8 @@ internal static bool IsUncPath([NotNullWhen(true)] this string? path, MockFileSy
}

return fileSystem.Execute.OnWindows(
() => path.StartsWith(UncPrefix) || path.StartsWith(UncAltPrefix),
() => path.StartsWith(UncPrefix));
() => path.StartsWith(new string(fileSystem.Path.DirectorySeparatorChar, 2)) || path.StartsWith(new string(fileSystem.Path.AltDirectorySeparatorChar, 2)),
() => path.StartsWith(new string(fileSystem.Path.DirectorySeparatorChar, 2)));
}

internal static string EnsureValidFormat(
Expand Down
13 changes: 6 additions & 7 deletions Source/Testably.Abstractions.Testing/Storage/InMemoryLocation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using Testably.Abstractions.Testing.Helpers;

namespace Testably.Abstractions.Testing.Storage;
Expand Down Expand Up @@ -83,8 +82,8 @@ public override int GetHashCode()
/// <inheritdoc cref="IStorageLocation.GetParent()" />
public IStorageLocation? GetParent()
{
string? parentPath = Path.GetDirectoryName(FullPath);
if (Path.GetPathRoot(FullPath) == FullPath || parentPath == null)
string? parentPath = _fileSystem.Path.GetDirectoryName(FullPath);
if (_fileSystem.Path.GetPathRoot(FullPath) == FullPath || parentPath == null)
{
return null;
}
Expand All @@ -98,7 +97,7 @@ public override int GetHashCode()

private string GetFriendlyNameParent(string parentPath)
=> _fileSystem.Execute.OnNetFramework(
() => Path.GetFileName(parentPath),
() => _fileSystem.Path.GetFileName(parentPath),
() => parentPath);

#endregion
Expand Down Expand Up @@ -133,13 +132,13 @@ public override string ToString()
private static string NormalizeKey(MockFileSystem fileSystem, string fullPath)
{
#if FEATURE_PATH_ADVANCED
return Path.TrimEndingDirectorySeparator(fullPath);
return fileSystem.Path.TrimEndingDirectorySeparator(fullPath);
#else
return FileFeatureExtensionMethods.TrimEndingDirectorySeparator(
fileSystem,
fullPath,
Path.DirectorySeparatorChar,
Path.AltDirectorySeparatorChar);
fileSystem.Path.DirectorySeparatorChar,
fileSystem.Path.AltDirectorySeparatorChar);
#endif
}
}
12 changes: 6 additions & 6 deletions Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,12 @@ private void CreateParents(MockFileSystem fileSystem, IStorageLocation location)
}

_containers.TryAdd(source, sourceContainer);
throw ExceptionFactory.CannotCreateFileWhenAlreadyExists(
sourceType == FileSystemTypes.Directory
? -2147024891
: _fileSystem.Execute.IsWindows
? -2147024713
: 17);
int hResult = -2147024891;
if (sourceType != FileSystemTypes.Directory)
{
hResult = _fileSystem.Execute.IsWindows ? -2147024713 : 17;
}
throw ExceptionFactory.CannotCreateFileWhenAlreadyExists(hResult);
}
}

Expand Down

0 comments on commit d037fd4

Please sign in to comment.