Skip to content

Commit 3487b77

Browse files
committed
Fix codacy smells
1 parent dba9d80 commit 3487b77

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

src/TestableIO.System.IO.Abstractions.TestingHelpers/Events/MockFileSystemEvents.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public class MockFileSystemEvents
7474
public IDisposable Subscribe(Action<FileSystemOperationEventArgs> handler)
7575
{
7676
if (handler == null)
77+
{
7778
throw new ArgumentNullException(nameof(handler));
79+
}
7880

7981
lock (LockObject)
8082
{
@@ -116,7 +118,9 @@ public IDisposable Subscribe(FileOperation[] operations, Action<FileSystemOperat
116118
if (handler == null)
117119
throw new ArgumentNullException(nameof(handler));
118120
if (operations == null)
121+
{
119122
throw new ArgumentNullException(nameof(operations));
123+
}
120124

121125
lock (LockObject)
122126
{
@@ -144,7 +148,9 @@ public IDisposable Subscribe(FileOperation[] operations, Action<FileSystemOperat
144148
public T WithEvents<T>(string path, FileOperation operation, ResourceType resourceType, Func<T> func)
145149
{
146150
if (!isEnabled)
151+
{
147152
return func();
153+
}
148154

149155
RaiseOperation(path, operation, resourceType, OperationPhase.Before);
150156

@@ -228,7 +234,9 @@ internal void RaiseOperation(
228234
lock (LockObject)
229235
{
230236
if (subscriptions.Count == 0)
237+
{
231238
return;
239+
}
232240
currentSubscriptions = subscriptions.ToArray();
233241
}
234242

@@ -249,7 +257,9 @@ internal void RaiseOperation(
249257
if (exceptions.Count > 0)
250258
{
251259
if (exceptions.Count == 1)
260+
{
252261
throw exceptions[0];
262+
}
253263
throw new AggregateException(
254264
$"One or more event handlers failed for {operation} operation on {path}",
255265
exceptions);
@@ -282,17 +292,15 @@ internal void RaiseOperation(
282292
/// Removes the specified subscription from the list of active subscriptions.
283293
/// </summary>
284294
/// <param name="subscription">The subscription to be removed.</param>
285-
/// <returns>True if the subscription was successfully removed; otherwise, false.</returns>
286-
private bool RemoveSubscription(Subscription subscription)
295+
private void RemoveSubscription(Subscription subscription)
287296
{
288297
lock (LockObject)
289298
{
290299
if (!subscriptions.Remove(subscription))
291300
{
292-
return false;
301+
return;
293302
}
294303
Interlocked.Increment(ref subscriptionVersion);
295-
return true;
296304
}
297305
}
298306

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystemOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ public class MockFileSystemOptions
1919
/// Flag indicating whether file system events should be enabled.
2020
/// When false (default), the event system has zero overhead.
2121
/// </summary>
22-
public bool EnableEvents { get; init; } = false;
22+
public bool EnableEvents { get; init; }
2323
}

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemCreativeEventsTests.cs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public void Events_FileSystemOperationTracker_ShouldCreateOperationLog()
5656

5757
using (fileSystem.Events.Subscribe(args =>
5858
{
59-
if (args.Phase != OperationPhase.After) return;
59+
if (args.Phase != OperationPhase.After)
60+
{
61+
return;
62+
}
6063
var logEntry = args.Operation switch
6164
{
6265
FileOperation.Create => $"Created new file at {args.Path}",
@@ -161,13 +164,19 @@ public void Events_FileSystemProgressTracker_ShouldTrackExperiencePoints()
161164
// Milestone tracking
162165
var opsPerformed = (int)playerStats["operations_performed"];
163166
if (opsPerformed == 10 && !milestones.Contains("Completed 10 operations"))
167+
{
164168
milestones.Add("Completed 10 operations");
169+
}
165170

166171
if (args.Operation == FileOperation.Delete && !milestones.Contains("First file deletion"))
172+
{
167173
milestones.Add("First file deletion");
174+
}
168175

169176
if (args.Path.EndsWith(".exe") && !milestones.Contains("Handled executable file"))
177+
{
170178
milestones.Add("Handled executable file");
179+
}
171180
}
172181
}))
173182
{
@@ -329,9 +338,15 @@ public void Events_QuantumFileSystem_ShouldImplementSuperpositionPattern()
329338

330339
using (fileSystem.Events.Subscribe(args =>
331340
{
332-
if (args.Phase != OperationPhase.Before || args.Operation != FileOperation.Read) return;
341+
if (args.Phase != OperationPhase.Before || args.Operation != FileOperation.Read)
342+
{
343+
return;
344+
}
333345
// Quantum files exist in superposition until observed (read)
334-
if (!args.Path.Contains("quantum") || quantumStates.ContainsKey(args.Path)) return;
346+
if (!args.Path.Contains("quantum") || quantumStates.ContainsKey(args.Path))
347+
{
348+
return;
349+
}
335350
// Collapse the wave function deterministically
336351
var exists = deterministicRandom.NextDouble() > 0.5;
337352
quantumStates[args.Path] = exists;
@@ -420,7 +435,10 @@ public void Events_AutoCorrectFileSystem_ShouldDetectAndSuggestTypoCorrections()
420435

421436
using (fileSystem.Events.Subscribe(args =>
422437
{
423-
if (args.Phase != OperationPhase.Before || args.Operation != FileOperation.Create) return;
438+
if (args.Phase != OperationPhase.Before || args.Operation != FileOperation.Create)
439+
{
440+
return;
441+
}
424442
var fileName = fileSystem.Path.GetFileNameWithoutExtension(args.Path);
425443

426444
// Check for potential typos
@@ -484,7 +502,10 @@ public void Events_ReliabilityTestingSystem_ShouldSimulateControlledFailures()
484502

485503
using (fileSystem.Events.Subscribe(args =>
486504
{
487-
if (args.Phase != OperationPhase.Before) return;
505+
if (args.Phase != OperationPhase.Before)
506+
{
507+
return;
508+
}
488509
operationCount++;
489510

490511
// Simulate controlled failure scenarios
@@ -587,7 +608,10 @@ public void Events_AccessPatternMonitor_ShouldTrackSuspiciousActivity()
587608

588609
using (fileSystem.Events.Subscribe(args =>
589610
{
590-
if (args.Phase != OperationPhase.Before || args.Operation != FileOperation.Read) return;
611+
if (args.Phase != OperationPhase.Before || args.Operation != FileOperation.Read)
612+
{
613+
return;
614+
}
591615
accessCounts[args.Path] = (accessCounts.TryGetValue(args.Path, out var count) ? count : 0) + 1;
592616
var accesses = accessCounts[args.Path];
593617

@@ -613,6 +637,9 @@ public void Events_AccessPatternMonitor_ShouldTrackSuspiciousActivity()
613637
Exception = new UnauthorizedAccessException("Access denied: Suspicious activity pattern detected")
614638
});
615639
break;
640+
default:
641+
// No action for access counts > 4
642+
break;
616643
}
617644
}))
618645
{
@@ -690,11 +717,16 @@ public void Events_PerformanceProfiler_ShouldTrackOperationMetrics()
690717
else if (args.Phase == OperationPhase.After)
691718
{
692719
var beforeKey = key.Replace("_After", "_Before");
693-
if (!operationTimestamps.TryGetValue(beforeKey, out var timestamp)) return;
720+
if (!operationTimestamps.TryGetValue(beforeKey, out var timestamp))
721+
{
722+
return;
723+
}
694724
var duration = (DateTime.UtcNow - timestamp).Ticks;
695725

696726
if (!performanceMetrics.ContainsKey(args.Operation))
727+
{
697728
performanceMetrics[args.Operation] = new List<long>();
729+
}
698730

699731
performanceMetrics[args.Operation].Add(duration);
700732
operationTimestamps.Remove(beforeKey);
@@ -759,7 +791,9 @@ public void Events_PerformanceProfiler_ShouldTrackOperationMetrics()
759791
private static bool IsLikelyTypo(string input, string target)
760792
{
761793
if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(target))
794+
{
762795
return false;
796+
}
763797

764798
// Simple Levenshtein distance check for typo detection
765799
var distance = CalculateLevenshteinDistance(input.ToLowerInvariant(), target.ToLowerInvariant());
@@ -775,15 +809,25 @@ private static bool IsLikelyTypo(string input, string target)
775809
/// <returns>The Levenshtein distance as an integer, representing the number of edits required to transform the source string into the target string.</returns>
776810
private static int CalculateLevenshteinDistance(string source, string target)
777811
{
778-
if (source.Length == 0) return target.Length;
779-
if (target.Length == 0) return source.Length;
812+
if (source.Length == 0)
813+
{
814+
return target.Length;
815+
}
816+
if (target.Length == 0)
817+
{
818+
return source.Length;
819+
}
780820

781821
var matrix = new int[source.Length + 1, target.Length + 1];
782822

783823
for (var i = 0; i <= source.Length; i++)
824+
{
784825
matrix[i, 0] = i;
826+
}
785827
for (var j = 0; j <= target.Length; j++)
828+
{
786829
matrix[0, j] = j;
830+
}
787831

788832
for (var i = 1; i <= source.Length; i++)
789833
{

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemEventsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,11 @@ public void Events_ExceptionInHandler_DoesNotAffectOtherHandlers()
480480
var fileSystem = new MockFileSystem(new MockFileSystemOptions { EnableEvents = true });
481481
var handler2Called = false;
482482

483-
using (fileSystem.Events.Subscribe(args => throw new Exception("Handler 1 error")))
483+
using (fileSystem.Events.Subscribe(args => throw new InvalidOperationException("Handler 1 error")))
484484
using (fileSystem.Events.Subscribe(args => handler2Called = true))
485485
{
486486
// The operation should throw the handler exception
487-
Assert.Throws<Exception>(() => fileSystem.File.Create(XFS.Path(@"C:\test.txt")));
487+
Assert.Throws<InvalidOperationException>(() => fileSystem.File.Create(XFS.Path(@"C:\test.txt")));
488488

489489
// But handler2 should have been called
490490
Assert.That(handler2Called, Is.True);

0 commit comments

Comments
 (0)