< Summary - Results for net8.0, Release

Information
Class: LockCheck.Windows.Extensions
Assembly: LockCheck
File(s): D:\a\LockCheck\LockCheck\src\LockCheck\Windows\Extensions.cs
Tag: 117_11660770947
Line coverage
100%
Covered lines: 8
Uncovered lines: 0
Coverable lines: 8
Total lines: 36
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsFileLocked(...)100%88100%

File(s)

D:\a\LockCheck\LockCheck\src\LockCheck\Windows\Extensions.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3
 4namespace LockCheck.Windows;
 5
 6internal static class Extensions
 7{
 8    public static bool IsFileLocked(Exception exception)
 9    {
 210        if (exception == null)
 211            throw new ArgumentNullException(nameof(exception));
 12
 213        if (exception is IOException ioException)
 14        {
 15            // Generally it is not safe / stable to convert HRESULTs to Win32 error codes. It works here,
 16            // because we exactly know where we're at. So resist refactoring the following code into an
 17            // (maybe even externally visible) method.
 218            int errorCode = ioException.HResult & ((1 << 16) - 1);
 19
 20            // Code coverage note: causing a ERROR_LOCK_VIOLATION is rather hard to achieve in a test.
 21            // Basically, you will mostly (always?) get a ERROR_SHARING_VIOLATION, unless you would
 22            // do the test via the network (e.g. using a share). Note that using the "\\<computer>\C$"
 23            // share will not cut it.
 24            //
 25            // Also note, that as of current (fall 2024), the .NET runtime does not raise IOException
 26            // with ERROR_LOCK_VIOLATION. Since technically, this error is a potential result of a
 27            // locking issue, we check for it anyway.
 228            if (errorCode == NativeMethods.ERROR_LOCK_VIOLATION ||
 229                errorCode == NativeMethods.ERROR_SHARING_VIOLATION)
 30            {
 231                return true;
 32            }
 33        }
 234        return false;
 35    }
 36}

Methods/Properties

IsFileLocked(System.Exception)