.NET 6 Coding Patterns + Unit Tests (#823)

* Refactored all files to have Interfaces within the same file. Started moving over to file-scoped namespaces.

* Refactored common methods for getting underlying file's cover, pages, and extracting into 1 interface.

* More refactoring around removing dependence on explicit filetype testing for getting information.

* Code is buildable, tests are broken. Huge refactor (not completed) which makes most of DirectoryService testable with a mock filesystem (and thus the services that utilize it).

* Finished porting DirectoryService to use mocked filesystem implementation.

* Added a null check

* Added a null check

* Finished all unit tests for DirectoryService.

* Some misc cleanup on the code

* Fixed up some bugs from refactoring scan loop.

* Implemented CleanupService testing and refactored more of DirectoryService to be non-static.

Fixed a bug where cover file cleanup wasn't properly finding files due to a regex bug.

* Fixed an issue in CleanupBackup() where we weren't properly selecting database files older than 30 days. Finished CleanupService Tests.

* Refactored Flatten and RemoveNonImages to directory service to allow CacheService to be testable.

* Finally have CacheService tested. Rewrote GetCachedPagePath() to be much more straightforward & performant.

* Updated DefaultParserTests.cs to contain all existing tests and follow new test layout format.

* All tests fixed up
This commit is contained in:
Joseph Milazzo 2021-12-05 10:58:53 -06:00 committed by GitHub
parent bf1876ff44
commit bbe8f800f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
115 changed files with 6734 additions and 5370 deletions

View file

@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using API.Services;
using Kavita.Common;
namespace API.Data
{
/// <summary>
/// A Migration to migrate config related files to the config/ directory for installs prior to v0.4.9.
/// </summary>
public static class MigrateConfigFiles
{
private static readonly List<string> LooseLeafFiles = new List<string>()
@ -31,7 +35,7 @@ namespace API.Data
/// In v0.4.8 we moved all config files to config/ to match with how docker was setup. This will move all config files from current directory
/// to config/
/// </summary>
public static void Migrate(bool isDocker)
public static void Migrate(bool isDocker, IDirectoryService directoryService)
{
Console.WriteLine("Checking if migration to config/ is needed");
@ -46,8 +50,8 @@ namespace API.Data
Console.WriteLine(
"Migrating files from pre-v0.4.8. All Kavita config files are now located in config/");
CopyAppFolders();
DeleteAppFolders();
CopyAppFolders(directoryService);
DeleteAppFolders(directoryService);
UpdateConfiguration();
@ -64,14 +68,14 @@ namespace API.Data
Console.WriteLine(
"Migrating files from pre-v0.4.8. All Kavita config files are now located in config/");
Console.WriteLine($"Creating {DirectoryService.ConfigDirectory}");
DirectoryService.ExistOrCreate(DirectoryService.ConfigDirectory);
Console.WriteLine($"Creating {directoryService.ConfigDirectory}");
directoryService.ExistOrCreate(directoryService.ConfigDirectory);
try
{
CopyLooseLeafFiles();
CopyLooseLeafFiles(directoryService);
CopyAppFolders();
CopyAppFolders(directoryService);
// Then we need to update the config file to point to the new DB file
UpdateConfiguration();
@ -84,43 +88,43 @@ namespace API.Data
// Finally delete everything in the source directory
Console.WriteLine("Removing old files");
DeleteLooseFiles();
DeleteAppFolders();
DeleteLooseFiles(directoryService);
DeleteAppFolders(directoryService);
Console.WriteLine("Removing old files...DONE");
Console.WriteLine("Migration complete. All config files are now in config/ directory");
}
private static void DeleteAppFolders()
private static void DeleteAppFolders(IDirectoryService directoryService)
{
foreach (var folderToDelete in AppFolders)
{
if (!new DirectoryInfo(Path.Join(Directory.GetCurrentDirectory(), folderToDelete)).Exists) continue;
DirectoryService.ClearAndDeleteDirectory(Path.Join(Directory.GetCurrentDirectory(), folderToDelete));
directoryService.ClearAndDeleteDirectory(Path.Join(Directory.GetCurrentDirectory(), folderToDelete));
}
}
private static void DeleteLooseFiles()
private static void DeleteLooseFiles(IDirectoryService directoryService)
{
var configFiles = LooseLeafFiles.Select(file => new FileInfo(Path.Join(Directory.GetCurrentDirectory(), file)))
.Where(f => f.Exists);
DirectoryService.DeleteFiles(configFiles.Select(f => f.FullName));
directoryService.DeleteFiles(configFiles.Select(f => f.FullName));
}
private static void CopyAppFolders()
private static void CopyAppFolders(IDirectoryService directoryService)
{
Console.WriteLine("Moving folders to config");
foreach (var folderToMove in AppFolders)
{
if (new DirectoryInfo(Path.Join(DirectoryService.ConfigDirectory, folderToMove)).Exists) continue;
if (new DirectoryInfo(Path.Join(directoryService.ConfigDirectory, folderToMove)).Exists) continue;
try
{
DirectoryService.CopyDirectoryToDirectory(
Path.Join(Directory.GetCurrentDirectory(), folderToMove),
Path.Join(DirectoryService.ConfigDirectory, folderToMove));
directoryService.CopyDirectoryToDirectory(
Path.Join(directoryService.FileSystem.Directory.GetCurrentDirectory(), folderToMove),
Path.Join(directoryService.ConfigDirectory, folderToMove));
}
catch (Exception)
{
@ -132,9 +136,9 @@ namespace API.Data
Console.WriteLine("Moving folders to config...DONE");
}
private static void CopyLooseLeafFiles()
private static void CopyLooseLeafFiles(IDirectoryService directoryService)
{
var configFiles = LooseLeafFiles.Select(file => new FileInfo(Path.Join(Directory.GetCurrentDirectory(), file)))
var configFiles = LooseLeafFiles.Select(file => new FileInfo(Path.Join(directoryService.FileSystem.Directory.GetCurrentDirectory(), file)))
.Where(f => f.Exists);
// First step is to move all the files
Console.WriteLine("Moving files to config/");
@ -142,7 +146,7 @@ namespace API.Data
{
try
{
fileInfo.CopyTo(Path.Join(DirectoryService.ConfigDirectory, fileInfo.Name));
fileInfo.CopyTo(Path.Join(directoryService.ConfigDirectory, fileInfo.Name));
}
catch (Exception)
{