Skip to content

Commit

Permalink
Integrate tests repo and improve test coverage (#28)
Browse files Browse the repository at this point in the history
Integrate the tests repo with the main repository and take the opportunity to streamline setup and improve test coverage.

- Integrate the tests repo
- Improve test coverage
- Streamline test setup with AutoFixture
- Implement `System.IO.Abstractions` for FileSystem mocking
- Refactor where needed to improve testability
  • Loading branch information
Erik-White authored Jun 19, 2021
1 parent 7b8a1ab commit af58f8e
Show file tree
Hide file tree
Showing 29 changed files with 1,273 additions and 228 deletions.
14 changes: 14 additions & 0 deletions Mosey.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mosey", "Mosey\Mosey.csproj
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3D609DE7-CA92-45B0-A4A9-93975FA50AA9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mosey.Tests", "MoseyTests\Mosey.Tests.csproj", "{A6FA9705-788E-4712-BE02-E13D8A652020}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -28,6 +30,18 @@ Global
{B1DD8B7D-FFF0-406C-9765-14848EAB126F}.Release|x64.Build.0 = Release|Any CPU
{B1DD8B7D-FFF0-406C-9765-14848EAB126F}.Release|x86.ActiveCfg = Release|Any CPU
{B1DD8B7D-FFF0-406C-9765-14848EAB126F}.Release|x86.Build.0 = Release|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Debug|x64.ActiveCfg = Debug|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Debug|x64.Build.0 = Debug|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Debug|x86.ActiveCfg = Debug|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Debug|x86.Build.0 = Debug|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Release|Any CPU.Build.0 = Release|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Release|x64.ActiveCfg = Release|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Release|x64.Build.0 = Release|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Release|x86.ActiveCfg = Release|Any CPU
{A6FA9705-788E-4712-BE02-E13D8A652020}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 6 additions & 1 deletion Mosey/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.IO;
using System.IO.Abstractions;
using System.Windows;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -12,6 +14,7 @@
using Mosey.Services.Imaging;
using Mosey.ViewModels;

[assembly: InternalsVisibleTo("Mosey.Tests")]
namespace Mosey
{
/// <summary>
Expand Down Expand Up @@ -60,9 +63,11 @@ protected override void OnStartup(StartupEventArgs e)
})

// Services
.AddTransient<IIntervalTimer, IntervalTimer>()
.AddSingleton<IFileSystem, FileSystem>()
.AddTransient<IFactory<IIntervalTimer>, IntervalTimerFactory>()
.AddTransient<IFolderBrowserDialog, FolderBrowserDialog>()
.AddScoped<IDialogManager, DialogManager>()
.AddTransient<ISystemDevices, SystemDevices>()
.AddTransient<IImagingDevice, ScanningDevice>()
.AddSingleton<IImagingDevices<IImagingDevice>, ScanningDevices>()

Expand Down
1 change: 0 additions & 1 deletion Mosey/Configuration/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mosey.Models;
using Mosey.Services;
using Mosey.Services.Imaging;
Expand Down
14 changes: 0 additions & 14 deletions Mosey/Models/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ public interface IDeviceCollection<T> where T : IDevice
/// </summary>
IEnumerable<T> Devices { get; }

/// <summary>
/// Retrieve the devices in the collection where their <see cref="IDevice.IsEnabled"/> property is equal to <paramref name="enabled"/>.
/// </summary>
/// <param name="enabled">The specified <see cref="IDevice.IsEnabled"/> property</param>
/// <returns>Devices where the <see cref="IDevice.IsEnabled"/> is equal to <paramref name="enabled"/></returns>
IEnumerable<T> GetByEnabled(bool enabled);

/// <summary>
/// Add a <see cref="IDevice"/> to the collection.
/// </summary>
Expand All @@ -36,13 +29,6 @@ public interface IDeviceCollection<T> where T : IDevice
/// </summary>
void DisableAll();

/// <summary>
/// Set the <see cref="IDevice.IsEnabled"/> property of a specific <see cref="IDevice"/> instance.
/// </summary>
/// <param name="device">A <see cref="IDevice"/> instance</param>
/// <param name="enabled">Sets the <see cref="IDevice.IsEnabled"/> property</param>
void SetDeviceEnabled(T device, bool enabled);

/// <summary>
/// Set the <see cref="IDevice.IsEnabled"/> property of a device in the collection.
/// </summary>
Expand Down
19 changes: 9 additions & 10 deletions Mosey/Models/FileSystemExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;

namespace Mosey.Models
Expand All @@ -14,9 +13,9 @@ public static class FileSystemExtensions
/// <returns>A <see cref="DriveInfo"/> instance that represents the logical drive <paramref name="driveName"/></returns>
/// <exception cref="IOException"></exception>
/// <exception cref="UnauthorizedAccessException"></exception>
public static DriveInfo GetDriveInfo(string driveName)
public static IDriveInfo GetDriveInfo(string driveName, IFileSystem fileSystem)
{
return DriveInfo.GetDrives().Where(drive => drive.Name == driveName).FirstOrDefault();
return fileSystem.DriveInfo.GetDrives().Where(drive => drive.Name == driveName).FirstOrDefault();
}

/// <summary>
Expand All @@ -26,25 +25,25 @@ public static DriveInfo GetDriveInfo(string driveName)
/// <returns>The available free space, in bytes</returns>
/// <exception cref="IOException"></exception>
/// <exception cref="UnauthorizedAccessException"></exception>
public static long AvailableFreeSpace(string driveName)
public static long AvailableFreeSpace(string driveName, IFileSystem fileSystem)
{
return GetDriveInfo(driveName).AvailableFreeSpace;
return GetDriveInfo(driveName, fileSystem).AvailableFreeSpace;
}

/// <summary>
/// Check is a file path is a Universal Naming Convention (UNC) network path
/// </summary>
/// <param name="path">The path to verify</param>
/// <returns><see langword="True"/> if <paramref name="path"/> is a UNC path</returns>
public static bool IsNetworkPath(string path)
public static bool IsNetworkPath(string path, IFileSystem fileSystem)
{
if (!path.StartsWith(@"/") && !path.StartsWith(@"\"))
{
// Path may not start with a slash, but could be a network drive
string rootPath = Path.GetPathRoot(path);
DriveInfo driveInfo = new DriveInfo(rootPath);
string rootPath = fileSystem.Path.GetPathRoot(path);
var driveInfo = fileSystem.DriveInfo.FromDriveName(rootPath);

return driveInfo.DriveType == DriveType.Network;
return driveInfo.DriveType == System.IO.DriveType.Network;
}

return true;
Expand Down
2 changes: 2 additions & 0 deletions Mosey/Mosey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>9.0</LangVersion>
<UseWPF>true</UseWPF>
<ApplicationIcon>Mosey.ico</ApplicationIcon>
<Win32Resource />
Expand Down Expand Up @@ -46,6 +47,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.3" />
<PackageReference Include="NReco.Logging.File" Version="1.0.5" />
<PackageReference Include="System.Drawing.Common" Version="5.0.0-preview.4.20251.6" />
<PackageReference Include="System.IO.Abstractions" Version="13.2.38" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Mosey/Services/Imaging/Extensions/ImageFormatExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static ImageFormat ToDrawingImageFormat(this ScanningDevice.ImageFormat v
/// Convert to a <see cref="DNTScanner.Core.WiaImageFormat"/> instance.
/// </summary>
/// <param name="value"></param>
/// <returns>A <see cref="ImageFormat"/> instance</returns>
/// <returns>A <see cref="DNTScanner.Core.WiaImageFormat"/> instance</returns>
public static DNTScanner.Core.WiaImageFormat ToWIAImageFormat(this ScanningDevice.ImageFormat value)
{
return (DNTScanner.Core.WiaImageFormat)typeof(DNTScanner.Core.WiaImageFormat)
Expand Down
51 changes: 51 additions & 0 deletions Mosey/Services/Imaging/ISystemDevices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using DNTScanner.Core;
using Mosey.Models;

namespace Mosey.Services.Imaging
{
/// <summary>
/// Provides access to the WIA driver devices via DNTScanner.Core
/// </summary>
public interface ISystemDevices
{
/// <summary>
/// Retrieve image(s) from a scanner.
/// </summary>
/// <param name="settings">A <see cref="ScannerSettings"/> instance representing a physical device</param>
/// <param name="config">Device settings used when capturing an image</param>
/// <param name="format">The image format used internally for storing the image</param>
/// <returns>A list of retrieved images as byte arrays, in <paramref name="format"/></returns>
IEnumerable<byte[]> PerformScan(ScannerSettings settings, IImagingDeviceConfig config, ScanningDevice.ImageFormat format);

/// <inheritdoc cref="PerformScan(ScannerSettings, IImagingDeviceConfig, ScanningDevice.ImageFormat)"/>
/// <param name="connectRetries">The number of attempts to try connecting to the WIA driver, after <paramref name="delay"/></param>
/// <param name="delay">The time in millseconds between <paramref name="connectRetries"/> attempts</param>
IEnumerable<byte[]> PerformScan(ScannerSettings settings, IImagingDeviceConfig config, ScanningDevice.ImageFormat format, int connectRetries, int delay);

/// <summary>
/// Lists the static properties of scanners connected to the system.
/// <para/>
/// Use the <see cref="ScannerDevices"/> function to retrieve full device instances.
/// </summary>
/// <remarks>
/// Static device properties are limited, but can be retrieved without establishing a connection to the device.
/// </remarks>
/// <returns>A list of the static device properties</returns>
public IList<IDictionary<string, object>> ScannerProperties();

/// <inheritdoc cref="ScannerProperties"/>
/// <param name="connectRetries">The number of retry attempts allowed if connecting to the WIA driver was unsuccessful</param>
public IList<IDictionary<string, object>> ScannerProperties(int connectRetries);

/// <summary>
/// A collection of <see cref="ScannerSettings"/> representing physical devices connected to the system.
/// </summary>
/// <returns>A collection of <see cref="ScannerSettings"/> representing physical devices connected to the system.</returns>
public IEnumerable<ScannerSettings> ScannerSettings();

/// <inheritdoc cref="ScannerSettings"/>
/// <param name="connectRetries">The number of retry attempts allowed if connecting to the WIA driver was unsuccessful</param>
public IEnumerable<ScannerSettings> ScannerSettings(int connectRetries);
}
}
Loading

0 comments on commit af58f8e

Please sign in to comment.