Skip to content

Commit bf27a22

Browse files
authored
refactor: add missing tests from simulation mode implementation (#585)
* Add tests for Execute and ExecuteExtensions * Add test for uncovered code in ExceptionFactory * Improve implementation and test coverage of SimulatedPaths and NativePath * Run ReSharper cleanup * Add dispose test for Notification * Use feature flag `CAN_SIMULATE_OTHER_OS` instead of `!NETFRAMEWORK`
1 parent 615a74e commit bf27a22

File tree

30 files changed

+342
-186
lines changed

30 files changed

+342
-186
lines changed

Source/Testably.Abstractions.Interface/Helpers/GuidSystemBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Guid Parse(string input)
4141
#endif
4242

4343
#if FEATURE_GUID_PARSE
44-
#pragma warning disable MA0011
44+
#pragma warning disable MA0011
4545
/// <inheritdoc cref="IGuid.Parse(ReadOnlySpan{char})" />
4646
public Guid Parse(ReadOnlySpan<char> input)
4747
=> Guid.Parse(input);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ private bool TryMakeRenamedEventArgs(
595595
.GetField("_oldFullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
596596
.SetValue(eventArgs, oldFullPath);
597597
}
598+
598599
return _fileSystem.Execute.Path.GetDirectoryName(changeDescription.Path)?
599600
.Equals(_fileSystem.Execute.Path.GetDirectoryName(changeDescription.OldPath),
600601
_fileSystem.Execute.StringComparisonMode) ?? true;

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,15 @@ public string GetFullPath(string path)
132132
string? pathRoot = System.IO.Path.GetPathRoot(path);
133133
string? directoryRoot =
134134
System.IO.Path.GetPathRoot(fileSystem.Storage.CurrentDirectory);
135-
if (!string.IsNullOrEmpty(pathRoot) && !string.IsNullOrEmpty(directoryRoot))
135+
if (pathRoot?.Length < directoryRoot?.Length)
136136
{
137-
if (char.ToUpperInvariant(pathRoot[0]) != char.ToUpperInvariant(directoryRoot[0]))
137+
if (pathRoot.Length > 0 &&
138+
char.ToUpperInvariant(pathRoot[0]) != char.ToUpperInvariant(directoryRoot[0]))
138139
{
139140
return System.IO.Path.GetFullPath(path);
140141
}
141142

142-
if (pathRoot.Length < directoryRoot.Length)
143-
{
144-
path = path.Substring(pathRoot.Length);
145-
}
143+
path = path.Substring(pathRoot.Length);
146144
}
147145

148146
return System.IO.Path.GetFullPath(System.IO.Path.Combine(
@@ -183,7 +181,6 @@ public string GetRandomFileName()
183181
public string GetRelativePath(string relativeTo, string path)
184182
{
185183
relativeTo.EnsureValidArgument(fileSystem, nameof(relativeTo));
186-
path.EnsureValidArgument(fileSystem, nameof(path));
187184

188185
relativeTo = fileSystem.Execute.Path.GetFullPath(relativeTo);
189186
path = fileSystem.Execute.Path.GetFullPath(path);

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

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,10 @@ public string GetRandomFileName()
290290
public string GetRelativePath(string relativeTo, string path)
291291
{
292292
relativeTo.EnsureValidArgument(fileSystem, nameof(relativeTo));
293-
path.EnsureValidArgument(fileSystem, nameof(path));
294293

295294
relativeTo = fileSystem.Execute.Path.GetFullPath(relativeTo);
296295
path = fileSystem.Execute.Path.GetFullPath(path);
297296

298-
// Need to check if the roots are different- if they are we need to return the "to" path.
299-
if (!AreRootsEqual(relativeTo, path, fileSystem.Execute.StringComparisonMode))
300-
{
301-
return path;
302-
}
303-
304297
Func<char, char, bool> charComparer = (c1, c2) => c1 == c2;
305298
if (fileSystem.Execute.StringComparisonMode == StringComparison.OrdinalIgnoreCase)
306299
{
@@ -505,35 +498,10 @@ public bool TryJoin(ReadOnlySpan<char> path1,
505498

506499
#endregion
507500

508-
/// <summary>
509-
/// Returns true if the two paths have the same root
510-
/// </summary>
511-
private bool AreRootsEqual(string first, string second, StringComparison comparisonType)
512-
{
513-
int firstRootLength = GetRootLength(first);
514-
int secondRootLength = GetRootLength(second);
515-
516-
return firstRootLength == secondRootLength
517-
&& string.Compare(
518-
strA: first,
519-
indexA: 0,
520-
strB: second,
521-
indexB: 0,
522-
length: firstRootLength,
523-
comparisonType: comparisonType) == 0;
524-
}
525-
526501
private string CombineInternal(string[] paths)
527502
{
528-
string NormalizePath(string path, bool ignoreStartingSeparator)
503+
string NormalizePath(string path)
529504
{
530-
if (!ignoreStartingSeparator && (
531-
path[0] == DirectorySeparatorChar ||
532-
path[0] == AltDirectorySeparatorChar))
533-
{
534-
path = path.Substring(1);
535-
}
536-
537505
if (path[path.Length - 1] == DirectorySeparatorChar ||
538506
path[path.Length - 1] == AltDirectorySeparatorChar)
539507
{
@@ -550,7 +518,6 @@ string NormalizePath(string path, bool ignoreStartingSeparator)
550518

551519
StringBuilder sb = new();
552520

553-
bool isFirst = true;
554521
bool endsWithDirectorySeparator = false;
555522
foreach (string path in paths)
556523
{
@@ -567,10 +534,9 @@ string NormalizePath(string path, bool ignoreStartingSeparator)
567534
if (IsPathRooted(path))
568535
{
569536
sb.Clear();
570-
isFirst = true;
571537
}
572538

573-
sb.Append(NormalizePath(path, isFirst));
539+
sb.Append(NormalizePath(path));
574540
sb.Append(DirectorySeparatorChar);
575541
endsWithDirectorySeparator = path.EndsWith(DirectorySeparatorChar) ||
576542
path.EndsWith(AltDirectorySeparatorChar);
@@ -650,7 +616,7 @@ private int GetCommonPathLength(string first, string second,
650616
int commonChars = 0;
651617
for (; commonChars < first.Length; commonChars++)
652618
{
653-
if (second.Length < commonChars)
619+
if (second.Length <= commonChars)
654620
{
655621
break;
656622
}
@@ -668,13 +634,13 @@ private int GetCommonPathLength(string first, string second,
668634
}
669635

670636
// Or we're a full string and equal length or match to a separator
671-
if (commonChars == first.Length
672-
&& (commonChars == second.Length || IsDirectorySeparator(second[commonChars])))
637+
if (commonChars == first.Length && commonChars == second.Length)
673638
{
674639
return commonChars;
675640
}
676641

677-
if (commonChars == second.Length && IsDirectorySeparator(first[commonChars]))
642+
if ((second.Length > commonChars && IsDirectorySeparator(second[commonChars])) ||
643+
IsDirectorySeparator(first[commonChars - 1]))
678644
{
679645
return commonChars;
680646
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ internal partial class Execute
4343
#if !CAN_SIMULATE_OTHER_OS
4444
[Obsolete("Simulating other operating systems is not supported on .NET Framework")]
4545
#endif
46-
internal Execute(MockFileSystem fileSystem, SimulationMode simulationMode)
46+
internal Execute(MockFileSystem fileSystem, SimulationMode simulationMode,
47+
bool isNetFramework = false)
4748
{
4849
IsLinux = simulationMode == SimulationMode.Linux;
4950
IsMac = simulationMode == SimulationMode.MacOS;
5051
IsWindows = simulationMode == SimulationMode.Windows;
51-
IsNetFramework = false;
52+
IsNetFramework = IsWindows && isNetFramework;
5253
StringComparisonMode = IsLinux
5354
? StringComparison.Ordinal
5455
: StringComparison.OrdinalIgnoreCase;
@@ -67,7 +68,7 @@ internal Execute(MockFileSystem fileSystem, SimulationMode simulationMode)
6768
else
6869
{
6970
throw new NotSupportedException(
70-
"The operating system must be one of Linux, OSX or Windows");
71+
"The operating system must be one of Linux, MacOS or Windows");
7172
}
7273
}
7374

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ internal static string GetSubdirectoryPath(this MockFileSystem fileSystem,
6666
{
6767
fullFilePath = fullFilePath.Substring(currentDirectory.Length);
6868
}
69-
else if (fullFilePath.StartsWith(currentDirectory + fileSystem.Execute.Path.DirectorySeparatorChar,
69+
else if (fullFilePath.StartsWith(
70+
currentDirectory + fileSystem.Execute.Path.DirectorySeparatorChar,
7071
fileSystem.Execute.StringComparisonMode))
7172
{
7273
fullFilePath = fullFilePath.Substring(currentDirectory.Length + 1);
@@ -75,11 +76,13 @@ internal static string GetSubdirectoryPath(this MockFileSystem fileSystem,
7576
{
7677
string? parentName = currentDirectory;
7778
while (parentName != null &&
78-
!fullFilePath.StartsWith(parentName + fileSystem.Execute.Path.DirectorySeparatorChar,
79+
!fullFilePath.StartsWith(
80+
parentName + fileSystem.Execute.Path.DirectorySeparatorChar,
7981
fileSystem.Execute.StringComparisonMode))
8082
{
8183
parentName = fileSystem.Execute.Path.GetDirectoryName(parentName);
82-
int lastIndex = givenPath.LastIndexOf(fileSystem.Execute.Path.DirectorySeparatorChar);
84+
int lastIndex =
85+
givenPath.LastIndexOf(fileSystem.Execute.Path.DirectorySeparatorChar);
8386
if (lastIndex >= 0)
8487
{
8588
givenPath = givenPath.Substring(0, lastIndex);

Source/Testably.Abstractions.Testing/Statistics/CallStatistics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public CallStatistics(IStatisticsGate statisticsGate, string name)
1616
_name = name;
1717
}
1818

19-
#region IStatistics Members
19+
#region IStatistics<TType> Members
2020

2121
/// <inheritdoc cref="IStatistics.Methods" />
2222
public MethodStatistic[] Methods => _methods.ToArray();

Source/Testably.Abstractions.Testing/Statistics/PathStatistics.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
namespace Testably.Abstractions.Testing.Statistics;
77

8-
internal class PathStatistics<TFactory, TType> : CallStatistics<TFactory>, IPathStatistics<TFactory, TType>
8+
internal class PathStatistics<TFactory, TType> : CallStatistics<TFactory>,
9+
IPathStatistics<TFactory, TType>
910
{
1011
private readonly MockFileSystem _fileSystem;
1112

@@ -26,7 +27,7 @@ public PathStatistics(
2627
_fileSystem = fileSystem;
2728
}
2829

29-
#region IPathStatistics Members
30+
#region IPathStatistics<TFactory,TType> Members
3031

3132
/// <inheritdoc cref="IPathStatistics{TFactory,TType}.this[string]" />
3233
public IStatistics<TType> this[string path]

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,11 @@ internal FileAttributes AdjustAttributes(FileAttributes attributes)
290290
return attributes;
291291
}
292292

293-
private bool CanGetAccess(FileAccess access, FileShare share, bool deleteAccess, bool ignoreFileShare)
293+
private bool CanGetAccess(
294+
FileAccess access,
295+
FileShare share,
296+
bool deleteAccess,
297+
bool ignoreFileShare)
294298
{
295299
foreach (KeyValuePair<Guid, FileHandle> fileHandle in _fileHandles)
296300
{
@@ -386,7 +390,11 @@ public void Dispose()
386390

387391
#endregion
388392

389-
public bool GrantAccess(FileAccess access, FileShare share, bool deleteAccess, bool ignoreFileShare)
393+
public bool GrantAccess(
394+
FileAccess access,
395+
FileShare share,
396+
bool deleteAccess,
397+
bool ignoreFileShare)
390398
{
391399
FileShare usedShare = share;
392400
FileShare currentShare = Share;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ public static AdjustedLocation AdjustLocationFromSearchPattern(
2727
StringBuilder givenPathPrefix = new();
2828

2929
while (searchPattern.StartsWith(
30-
".." + fileSystem.Execute.Path.DirectorySeparatorChar, StringComparison.Ordinal) ||
30+
".." + fileSystem.Execute.Path.DirectorySeparatorChar,
31+
StringComparison.Ordinal) ||
3132
searchPattern.StartsWith(
32-
".." + fileSystem.Execute.Path.AltDirectorySeparatorChar, StringComparison.Ordinal))
33+
".." + fileSystem.Execute.Path.AltDirectorySeparatorChar,
34+
StringComparison.Ordinal))
3335
{
3436
fileSystem.Execute.OnNetFramework(
3537
() => throw ExceptionFactory.SearchPatternCannotContainTwoDots());

0 commit comments

Comments
 (0)