Skip to content

Commit e855d61

Browse files
authored
fix: sonar issues (#690)
Fix sonar issues detected after updating the build pipeline * Move unused code in correct #if statements * Avoid using `Thread.Sleep` in tests
1 parent 77ff0ad commit e855d61

File tree

8 files changed

+46
-33
lines changed

8 files changed

+46
-33
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ protected override bool IsDirectorySeparator(char c)
104104
protected override bool IsEffectivelyEmpty(string path)
105105
=> string.IsNullOrEmpty(path);
106106

107+
#if FEATURE_PATH_RELATIVE
107108
/// <summary>
108109
/// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Unix.cs#L77
109110
/// </summary>
110111
protected override bool IsPartiallyQualified(string path)
111112
=> !IsPathRooted(path);
113+
#endif
112114

113115
/// <summary>
114116
/// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Unix.cs#L39

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ string NormalizePath(string path)
550550
return sb.ToString();
551551
}
552552

553+
#if FEATURE_PATH_RELATIVE
553554
/// <summary>
554555
/// We have the same root, we need to calculate the difference now using the
555556
/// common Length and Segment count past the length.
@@ -606,7 +607,9 @@ private string CreateRelativePath(string relativeTo, string path, int commonLeng
606607

607608
return sb.ToString();
608609
}
610+
#endif
609611

612+
#if FEATURE_PATH_RELATIVE
610613
/// <summary>
611614
/// Get the common path length from the start of the string.
612615
/// </summary>
@@ -653,12 +656,15 @@ private int GetCommonPathLength(string first, string second,
653656

654657
return commonChars;
655658
}
659+
#endif
656660

657661
protected abstract int GetRootLength(string path);
658662
protected abstract bool IsDirectorySeparator(char c);
659663
protected abstract bool IsEffectivelyEmpty(string path);
660664

665+
#if FEATURE_PATH_RELATIVE
661666
protected abstract bool IsPartiallyQualified(string path);
667+
#endif
662668

663669
#if FEATURE_PATH_JOIN || FEATURE_PATH_ADVANCED
664670
private string JoinInternal(string?[] paths)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ protected override bool IsEffectivelyEmpty(string path)
251251
return path.All(c => c == ' ');
252252
}
253253

254+
#if FEATURE_PATH_RELATIVE
254255
/// <summary>
255256
/// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L250
256257
/// </summary>
@@ -279,6 +280,7 @@ protected override bool IsPartiallyQualified(string path)
279280
// not qualified if you don't have a valid drive. "=:\" is the "=" file's default data stream.
280281
&& IsValidDriveChar(path[0]));
281282
}
283+
#endif
282284

283285
/// <summary>
284286
/// Returns true if the given character is a valid drive letter

Tests/Testably.Abstractions.Testing.Tests/NotificationTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public void AwaitableCallback_Amount_ShouldOnlyReturnAfterNumberOfCallbacks()
1919
}
2020
});
2121

22-
_ = Task.Run(() =>
22+
_ = Task.Run(async () =>
2323
{
24-
Thread.Sleep(10);
24+
await Task.Delay(10);
2525
for (int i = 1; i <= 10; i++)
2626
{
2727
timeSystem.Thread.Sleep(i);
28-
Thread.Sleep(1);
28+
await Task.Delay(1);
2929
}
3030
});
3131

@@ -82,13 +82,13 @@ public void AwaitableCallback_Filter_ShouldOnlyUpdateAfterFilteredValue()
8282
receivedCount++;
8383
});
8484

85-
_ = Task.Run(() =>
85+
_ = Task.Run(async () =>
8686
{
87-
Thread.Sleep(10);
87+
await Task.Delay(10);
8888
for (int i = 1; i <= 10; i++)
8989
{
9090
timeSystem.Thread.Sleep(i);
91-
Thread.Sleep(1);
91+
await Task.Delay(1);
9292
}
9393
});
9494

@@ -107,16 +107,16 @@ public void AwaitableCallback_Predicate_ShouldOnlyUpdateAfterFilteredValue()
107107
receivedCount++;
108108
}, t => t.TotalMilliseconds > 6);
109109

110-
_ = Task.Run(() =>
110+
_ = Task.Run(async () =>
111111
{
112112
// ReSharper disable once AccessToDisposedClosure
113113
try
114114
{
115-
Thread.Sleep(10);
115+
await Task.Delay(10);
116116
for (int i = 1; i <= 10; i++)
117117
{
118118
timeSystem.Thread.Sleep(i);
119-
Thread.Sleep(1);
119+
await Task.Delay(1);
120120
}
121121

122122
ms.Set();
@@ -145,15 +145,15 @@ public void AwaitableCallback_ShouldWaitForCallbackExecution()
145145
isCalled = true;
146146
});
147147

148-
_ = Task.Run(() =>
148+
_ = Task.Run(async () =>
149149
{
150150
// ReSharper disable once AccessToDisposedClosure
151151
try
152152
{
153153
while (!ms.IsSet)
154154
{
155155
timeSystem.Thread.Sleep(1);
156-
Thread.Sleep(1);
156+
await Task.Delay(1);
157157
}
158158
}
159159
catch (ObjectDisposedException)

Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileSystemWatcherStatisticsTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using Testably.Abstractions.Testing.Statistics;
66
using Testably.Abstractions.Testing.Tests.TestHelpers;
7+
// ReSharper disable MethodSupportsCancellation
78

89
namespace Testably.Abstractions.Testing.Tests.Statistics.FileSystem;
910

@@ -46,11 +47,11 @@ public void Method_WaitForChanged_WatcherChangeTypes_Int_ShouldRegisterCall()
4647
// Changes in the background are necessary, so that FileSystemWatcher.WaitForChanged returns.
4748
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(30));
4849
CancellationToken token = cts.Token;
49-
_ = Task.Run(() =>
50+
_ = Task.Run(async () =>
5051
{
5152
while (!token.IsCancellationRequested)
5253
{
53-
Thread.Sleep(10);
54+
await Task.Delay(10);
5455
sut.Directory.CreateDirectory(sut.Path.Combine("foo", "some-directory"));
5556
sut.Directory.Delete(sut.Path.Combine("foo", "some-directory"));
5657
}
@@ -76,11 +77,11 @@ public void Method_WaitForChanged_WatcherChangeTypes_ShouldRegisterCall()
7677
// Changes in the background are necessary, so that FileSystemWatcher.WaitForChanged returns.
7778
using CancellationTokenSource cts = new();
7879
CancellationToken token = cts.Token;
79-
_ = Task.Run(() =>
80+
_ = Task.Run(async () =>
8081
{
8182
while (!token.IsCancellationRequested)
8283
{
83-
Thread.Sleep(10);
84+
await Task.Delay(10);
8485
sut.Directory.CreateDirectory(sut.Path.Combine("foo", "some-directory"));
8586
sut.Directory.Delete(sut.Path.Combine("foo", "some-directory"));
8687
}
@@ -105,11 +106,11 @@ public void Method_WaitForChanged_WatcherChangeTypes_TimeSpan_ShouldRegisterCall
105106
// Changes in the background are necessary, so that FileSystemWatcher.WaitForChanged returns.
106107
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(30));
107108
CancellationToken token = cts.Token;
108-
_ = Task.Run(() =>
109+
_ = Task.Run(async () =>
109110
{
110111
while (!token.IsCancellationRequested)
111112
{
112-
Thread.Sleep(10);
113+
await Task.Delay(10);
113114
sut.Directory.CreateDirectory(sut.Path.Combine("foo", "some-directory"));
114115
sut.Directory.Delete(sut.Path.Combine("foo", "some-directory"));
115116
}

Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/EventTests.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.IO;
33
using System.Threading;
44
using System.Threading.Tasks;
5+
// ReSharper disable MethodSupportsCancellation
6+
// ReSharper disable MethodHasAsyncOverloadWithCancellation
57

68
namespace Testably.Abstractions.Tests.FileSystem.FileSystemWatcher;
79

@@ -41,7 +43,7 @@ void FileSystemWatcherOnChanged(object sender, FileSystemEventArgs e)
4143

4244
try
4345
{
44-
_ = Task.Run(() =>
46+
_ = Task.Run(async () =>
4547
{
4648
// ReSharper disable once AccessToDisposedClosure
4749
try
@@ -51,7 +53,7 @@ void FileSystemWatcherOnChanged(object sender, FileSystemEventArgs e)
5153
{
5254
string content = i++.ToString(CultureInfo.InvariantCulture);
5355
FileSystem.File.WriteAllText(path, content);
54-
Thread.Sleep(10);
56+
await Task.Delay(10);
5557
ms1.Set();
5658
}
5759
}
@@ -114,7 +116,7 @@ void FileSystemWatcherOnCreated(object sender, FileSystemEventArgs e)
114116

115117
try
116118
{
117-
_ = Task.Run(() =>
119+
_ = Task.Run(async () =>
118120
{
119121
// ReSharper disable once AccessToDisposedClosure
120122
try
@@ -123,7 +125,7 @@ void FileSystemWatcherOnCreated(object sender, FileSystemEventArgs e)
123125
{
124126
FileSystem.Directory.CreateDirectory(path);
125127
FileSystem.Directory.Delete(path);
126-
Thread.Sleep(10);
128+
await Task.Delay(10);
127129
ms1.Set();
128130
}
129131
}
@@ -184,7 +186,7 @@ void FileSystemWatcherOnDeleted(object sender, FileSystemEventArgs e)
184186

185187
try
186188
{
187-
_ = Task.Run(() =>
189+
_ = Task.Run(async () =>
188190
{
189191
// ReSharper disable once AccessToDisposedClosure
190192
try
@@ -193,7 +195,7 @@ void FileSystemWatcherOnDeleted(object sender, FileSystemEventArgs e)
193195
{
194196
FileSystem.Directory.CreateDirectory(path);
195197
FileSystem.Directory.Delete(path);
196-
Thread.Sleep(10);
198+
await Task.Delay(10);
197199
ms1.Set();
198200
}
199201
}
@@ -255,7 +257,7 @@ void FileSystemWatcherOnRenamed(object sender, FileSystemEventArgs e)
255257

256258
try
257259
{
258-
_ = Task.Run(() =>
260+
_ = Task.Run(async () =>
259261
{
260262
// ReSharper disable once AccessToDisposedClosure
261263
try
@@ -265,7 +267,7 @@ void FileSystemWatcherOnRenamed(object sender, FileSystemEventArgs e)
265267
while (!token.IsCancellationRequested)
266268
{
267269
FileSystem.File.Move($"path-{i}", $"path-{++i}");
268-
Thread.Sleep(10);
270+
await Task.Delay(10);
269271
ms1.Set();
270272
}
271273
}

Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/Tests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public void BeginInit_ShouldStopListening(string path)
2828
fileSystemWatcher.EnableRaisingEvents.Should().BeTrue();
2929
try
3030
{
31-
_ = Task.Run(() =>
31+
_ = Task.Run(async () =>
3232
{
3333
// ReSharper disable once AccessToDisposedClosure
3434
try
3535
{
3636
while (!ms.IsSet)
3737
{
38-
Thread.Sleep(10);
38+
await Task.Delay(10);
3939
FileSystem.Directory.CreateDirectory(path);
4040
FileSystem.Directory.Delete(path);
4141
}
@@ -86,14 +86,14 @@ public void EndInit_ShouldRestartListening(string path)
8686
fileSystemWatcher.EnableRaisingEvents.Should().BeTrue();
8787
try
8888
{
89-
_ = Task.Run(() =>
89+
_ = Task.Run(async () =>
9090
{
9191
// ReSharper disable once AccessToDisposedClosure
9292
try
9393
{
9494
while (!ms.IsSet)
9595
{
96-
Thread.Sleep(10);
96+
await Task.Delay(10);
9797
FileSystem.Directory.CreateDirectory(path);
9898
FileSystem.Directory.Delete(path);
9999
}

Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/WaitForChangedTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public void WaitForChanged_ShouldBlockUntilEventHappens(string path)
1919
FileSystem.FileSystemWatcher.New(BasePath);
2020
try
2121
{
22-
_ = Task.Run(() =>
22+
_ = Task.Run(async () =>
2323
{
2424
// ReSharper disable once AccessToDisposedClosure
2525
try
2626
{
2727
while (!ms.IsSet)
2828
{
29-
Thread.Sleep(10);
29+
await Task.Delay(10);
3030
FileSystem.Directory.CreateDirectory(path);
3131
FileSystem.Directory.Delete(path);
3232
}
@@ -68,14 +68,14 @@ public void WaitForChanged_Timeout_ShouldReturnTimedOut(string path,
6868
try
6969
{
7070
fileSystemWatcher.EnableRaisingEvents = true;
71-
_ = Task.Run(() =>
71+
_ = Task.Run(async () =>
7272
{
7373
// ReSharper disable once AccessToDisposedClosure
7474
try
7575
{
7676
while (!ms.IsSet)
7777
{
78-
Thread.Sleep(10);
78+
await Task.Delay(10);
7979
FileSystem.Directory.CreateDirectory(fullPath);
8080
FileSystem.Directory.Delete(fullPath);
8181
}

0 commit comments

Comments
 (0)