Skip to content

Commit d037fd4

Browse files
authored
refactor: fix sonar issues (#463)
Fix `System.IO.Path` usage and sonar issues
1 parent 514706d commit d037fd4

File tree

8 files changed

+40
-45
lines changed

8 files changed

+40
-45
lines changed

Source/Testably.Abstractions.Testing/FileSystem/DriveInfoMock.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private DriveInfoMock(string driveName, MockFileSystem fileSystem)
4040
if (driveName.IsUncPath(_fileSystem))
4141
{
4242
IsUncPath = true;
43-
driveName = PathHelper.UncPrefix +
43+
driveName = new string(fileSystem.Path.DirectorySeparatorChar, 2) +
4444
GetTopmostParentDirectory(driveName.Substring(2));
4545
}
4646
else

Source/Testably.Abstractions.Testing/FileSystem/FileSystemWatcherMock.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,14 @@ public IFileSystem FileSystem
6565
/// <inheritdoc cref="IFileSystemWatcher.Filter" />
6666
public string Filter
6767
{
68-
get => _filters.Count == 0
69-
? _fileSystem.Execute.IsNetFramework ? "*.*" : "*"
70-
: _filters [0];
68+
get
69+
{
70+
if (_filters.Count == 0)
71+
{
72+
return _fileSystem.Execute.IsNetFramework ? "*.*" : "*";
73+
}
74+
return _filters[0];
75+
}
7176
set
7277
{
7378
_filters.Clear();

Source/Testably.Abstractions.Testing/FileSystem/PathMock.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public override string GetRelativePath(string relativeTo, string path)
6464
relativeTo.EnsureValidArgument(_fileSystem, nameof(relativeTo));
6565
path.EnsureValidArgument(_fileSystem, nameof(path));
6666

67-
relativeTo = FileSystem.Path.GetFullPath(relativeTo);
68-
path = FileSystem.Path.GetFullPath(path);
67+
relativeTo = _fileSystem.Path.GetFullPath(relativeTo);
68+
path = _fileSystem.Path.GetFullPath(path);
6969

7070
return Path.GetRelativePath(relativeTo, path);
7171
}

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

+11-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ internal class Execute
1212
/// </summary>
1313
public static Execute Default { get; } = new();
1414

15-
private bool? _isNetFramework;
15+
public Execute()
16+
{
17+
IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
18+
IsMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
19+
IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
20+
IsNetFramework = RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework");
21+
}
1622

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

3035
/// <summary>
3136
/// Flag indicating if the code runs on <see cref="OSPlatform.OSX" />.
3237
/// </summary>
33-
public bool IsMac
34-
=> RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
38+
public bool IsMac { get; }
3539

3640
/// <summary>
3741
/// Flag indicating if the code runs in .NET Framework.
3842
/// </summary>
3943
/// <remarks>
4044
/// <see href="https://stackoverflow.com/a/53675231" />
4145
/// </remarks>
42-
public bool IsNetFramework
43-
{
44-
get
45-
{
46-
_isNetFramework ??= RuntimeInformation
47-
.FrameworkDescription.StartsWith(".NET Framework");
48-
return _isNetFramework.Value;
49-
}
50-
}
46+
public bool IsNetFramework { get; }
5147

5248
/// <summary>
5349
/// Flag indicating if the code runs on <see cref="OSPlatform.Windows" />.
5450
/// </summary>
55-
public bool IsWindows
56-
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
51+
public bool IsWindows { get; }
5752

5853
/// <summary>
5954
/// The <paramref name="callback" /> is executed when the code runs not in .NET Framework.

Source/Testably.Abstractions.Testing/Helpers/FilePlatformIndependenceExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ internal static class FilePlatformIndependenceExtensions
2929

3030
return fileSystem.Execute.OnWindows(
3131
() => path
32-
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar),
32+
.Replace(fileSystem.Path.AltDirectorySeparatorChar, fileSystem.Path.DirectorySeparatorChar),
3333
() => PathTransformRegex
3434
.Replace(path, "${path}")
35-
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar));
35+
.Replace(fileSystem.Path.AltDirectorySeparatorChar, fileSystem.Path.DirectorySeparatorChar));
3636
}
3737

3838
/// <summary>
@@ -46,7 +46,7 @@ internal static class FilePlatformIndependenceExtensions
4646
return path;
4747
}
4848

49-
if (Path.IsPathRooted(path))
49+
if (fileSystem.Path.IsPathRooted(path))
5050
{
5151
return path;
5252
}

Source/Testably.Abstractions.Testing/Helpers/PathHelper.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ internal static class PathHelper
1111
'*', '?'
1212
};
1313

14-
internal static readonly string UncPrefix = new(Path.DirectorySeparatorChar, 2);
15-
16-
internal static readonly string UncAltPrefix = new(Path.AltDirectorySeparatorChar, 2);
17-
1814
/// <summary>
1915
/// Determines whether the given path contains illegal characters.
2016
/// </summary>
@@ -37,7 +33,7 @@ internal static bool HasIllegalCharacters(this string path, MockFileSystem fileS
3733
/// For unix, this is empty or null. For Windows, this is empty, null, or
3834
/// just spaces ((char)32).
3935
/// </summary>
40-
internal static bool IsEffectivelyEmpty(this string path, MockFileSystem fileSystem)
36+
internal static bool IsEffectivelyEmpty(this string? path, MockFileSystem fileSystem)
4137
{
4238
if (string.IsNullOrEmpty(path))
4339
{
@@ -70,8 +66,8 @@ internal static bool IsUncPath([NotNullWhen(true)] this string? path, MockFileSy
7066
}
7167

7268
return fileSystem.Execute.OnWindows(
73-
() => path.StartsWith(UncPrefix) || path.StartsWith(UncAltPrefix),
74-
() => path.StartsWith(UncPrefix));
69+
() => path.StartsWith(new string(fileSystem.Path.DirectorySeparatorChar, 2)) || path.StartsWith(new string(fileSystem.Path.AltDirectorySeparatorChar, 2)),
70+
() => path.StartsWith(new string(fileSystem.Path.DirectorySeparatorChar, 2)));
7571
}
7672

7773
internal static string EnsureValidFormat(

Source/Testably.Abstractions.Testing/Storage/InMemoryLocation.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.IO;
32
using Testably.Abstractions.Testing.Helpers;
43

54
namespace Testably.Abstractions.Testing.Storage;
@@ -83,8 +82,8 @@ public override int GetHashCode()
8382
/// <inheritdoc cref="IStorageLocation.GetParent()" />
8483
public IStorageLocation? GetParent()
8584
{
86-
string? parentPath = Path.GetDirectoryName(FullPath);
87-
if (Path.GetPathRoot(FullPath) == FullPath || parentPath == null)
85+
string? parentPath = _fileSystem.Path.GetDirectoryName(FullPath);
86+
if (_fileSystem.Path.GetPathRoot(FullPath) == FullPath || parentPath == null)
8887
{
8988
return null;
9089
}
@@ -98,7 +97,7 @@ public override int GetHashCode()
9897

9998
private string GetFriendlyNameParent(string parentPath)
10099
=> _fileSystem.Execute.OnNetFramework(
101-
() => Path.GetFileName(parentPath),
100+
() => _fileSystem.Path.GetFileName(parentPath),
102101
() => parentPath);
103102

104103
#endregion
@@ -133,13 +132,13 @@ public override string ToString()
133132
private static string NormalizeKey(MockFileSystem fileSystem, string fullPath)
134133
{
135134
#if FEATURE_PATH_ADVANCED
136-
return Path.TrimEndingDirectorySeparator(fullPath);
135+
return fileSystem.Path.TrimEndingDirectorySeparator(fullPath);
137136
#else
138137
return FileFeatureExtensionMethods.TrimEndingDirectorySeparator(
139138
fileSystem,
140139
fullPath,
141-
Path.DirectorySeparatorChar,
142-
Path.AltDirectorySeparatorChar);
140+
fileSystem.Path.DirectorySeparatorChar,
141+
fileSystem.Path.AltDirectorySeparatorChar);
143142
#endif
144143
}
145144
}

Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -688,12 +688,12 @@ private void CreateParents(MockFileSystem fileSystem, IStorageLocation location)
688688
}
689689

690690
_containers.TryAdd(source, sourceContainer);
691-
throw ExceptionFactory.CannotCreateFileWhenAlreadyExists(
692-
sourceType == FileSystemTypes.Directory
693-
? -2147024891
694-
: _fileSystem.Execute.IsWindows
695-
? -2147024713
696-
: 17);
691+
int hResult = -2147024891;
692+
if (sourceType != FileSystemTypes.Directory)
693+
{
694+
hResult = _fileSystem.Execute.IsWindows ? -2147024713 : 17;
695+
}
696+
throw ExceptionFactory.CannotCreateFileWhenAlreadyExists(hResult);
697697
}
698698
}
699699

0 commit comments

Comments
 (0)