From 98a2b9d3ed7587fd2825d44a13ac0246deb613a2 Mon Sep 17 00:00:00 2001 From: Fesaa <77553571+Fesaa@users.noreply.github.com> Date: Wed, 19 Mar 2025 11:13:05 +0000 Subject: [PATCH] [skip ci] Fix unit tests on macOS (And probably Unix) (#3647) --- API.Tests/AbstractDbTest.cs | 32 +------- API.Tests/AbstractFsTest.cs | 43 +++++++++++ API.Tests/Helpers/CacheHelperTests.cs | 73 +++++++++++-------- API.Tests/Parsing/ParsingTests.cs | 6 +- API.Tests/Services/BackupServiceTests.cs | 28 +------ API.Tests/Services/BookmarkServiceTests.cs | 23 +----- API.Tests/Services/CacheServiceTests.cs | 24 +----- API.Tests/Services/CleanupServiceTests.cs | 3 +- API.Tests/Services/DirectoryServiceTests.cs | 54 +++++++++++--- API.Tests/Services/ParseScannedFilesTests.cs | 12 +-- API.Tests/Services/ReaderServiceTests.cs | 20 +---- .../TestCases/Flat Special - Manga.json | 8 +- 12 files changed, 154 insertions(+), 172 deletions(-) create mode 100644 API.Tests/AbstractFsTest.cs diff --git a/API.Tests/AbstractDbTest.cs b/API.Tests/AbstractDbTest.cs index 8a8186e42..c19092635 100644 --- a/API.Tests/AbstractDbTest.cs +++ b/API.Tests/AbstractDbTest.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Data.Common; +using System.IO; using System.IO.Abstractions.TestingHelpers; using System.Linq; using System.Threading.Tasks; @@ -21,24 +22,13 @@ using NSubstitute; namespace API.Tests; -public abstract class AbstractDbTest : IDisposable +public abstract class AbstractDbTest : AbstractFsTest , IDisposable { protected readonly DbConnection _connection; protected readonly DataContext _context; protected readonly IUnitOfWork _unitOfWork; protected readonly IMapper _mapper; - - protected const string CacheDirectory = "C:/kavita/config/cache/"; - protected const string CacheLongDirectory = "C:/kavita/config/cache-long/"; - protected const string CoverImageDirectory = "C:/kavita/config/covers/"; - protected const string BackupDirectory = "C:/kavita/config/backups/"; - protected const string LogDirectory = "C:/kavita/config/logs/"; - protected const string BookmarkDirectory = "C:/kavita/config/bookmarks/"; - protected const string SiteThemeDirectory = "C:/kavita/config/themes/"; - protected const string TempDirectory = "C:/kavita/config/temp/"; - protected const string DataDirectory = "C:/data/"; - protected AbstractDbTest() { var contextOptions = new DbContextOptionsBuilder() @@ -113,24 +103,6 @@ public abstract class AbstractDbTest : IDisposable protected abstract Task ResetDb(); - protected static MockFileSystem CreateFileSystem() - { - var fileSystem = new MockFileSystem(); - fileSystem.Directory.SetCurrentDirectory("C:/kavita/"); - fileSystem.AddDirectory("C:/kavita/config/"); - fileSystem.AddDirectory(CacheDirectory); - fileSystem.AddDirectory(CacheLongDirectory); - fileSystem.AddDirectory(CoverImageDirectory); - fileSystem.AddDirectory(BackupDirectory); - fileSystem.AddDirectory(BookmarkDirectory); - fileSystem.AddDirectory(SiteThemeDirectory); - fileSystem.AddDirectory(LogDirectory); - fileSystem.AddDirectory(TempDirectory); - fileSystem.AddDirectory(DataDirectory); - - return fileSystem; - } - public void Dispose() { _context.Dispose(); diff --git a/API.Tests/AbstractFsTest.cs b/API.Tests/AbstractFsTest.cs new file mode 100644 index 000000000..3341a3a7c --- /dev/null +++ b/API.Tests/AbstractFsTest.cs @@ -0,0 +1,43 @@ + + +using System.IO; +using System.IO.Abstractions.TestingHelpers; +using API.Services.Tasks.Scanner.Parser; + +namespace API.Tests; + +public abstract class AbstractFsTest +{ + + protected static readonly string Root = Parser.NormalizePath(Path.GetPathRoot(Directory.GetCurrentDirectory())); + protected static readonly string ConfigDirectory = Root + "kavita/config/"; + protected static readonly string CacheDirectory = ConfigDirectory + "cache/"; + protected static readonly string CacheLongDirectory = ConfigDirectory + "cache-long/"; + protected static readonly string CoverImageDirectory = ConfigDirectory + "covers/"; + protected static readonly string BackupDirectory = ConfigDirectory + "backups/"; + protected static readonly string LogDirectory = ConfigDirectory + "logs/"; + protected static readonly string BookmarkDirectory = ConfigDirectory + "bookmarks/"; + protected static readonly string SiteThemeDirectory = ConfigDirectory + "themes/"; + protected static readonly string TempDirectory = ConfigDirectory + "temp/"; + protected static readonly string ThemesDirectory = ConfigDirectory + "theme"; + protected static readonly string DataDirectory = Root + "data/"; + + protected static MockFileSystem CreateFileSystem() + { + var fileSystem = new MockFileSystem(); + fileSystem.Directory.SetCurrentDirectory(Root + "kavita/"); + fileSystem.AddDirectory(Root + "kavita/config/"); + fileSystem.AddDirectory(CacheDirectory); + fileSystem.AddDirectory(CacheLongDirectory); + fileSystem.AddDirectory(CoverImageDirectory); + fileSystem.AddDirectory(BackupDirectory); + fileSystem.AddDirectory(BookmarkDirectory); + fileSystem.AddDirectory(SiteThemeDirectory); + fileSystem.AddDirectory(LogDirectory); + fileSystem.AddDirectory(TempDirectory); + fileSystem.AddDirectory(DataDirectory); + fileSystem.AddDirectory(ThemesDirectory); + + return fileSystem; + } +} diff --git a/API.Tests/Helpers/CacheHelperTests.cs b/API.Tests/Helpers/CacheHelperTests.cs index 82f496a7b..93dae98d8 100644 --- a/API.Tests/Helpers/CacheHelperTests.cs +++ b/API.Tests/Helpers/CacheHelperTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.IO.Abstractions.TestingHelpers; +using System.Threading; using API.Entities; using API.Entities.Enums; using API.Helpers; @@ -11,9 +12,9 @@ using Xunit; namespace API.Tests.Helpers; -public class CacheHelperTests +public class CacheHelperTests: AbstractFsTest { - private const string TestCoverImageDirectory = @"c:\"; + private static readonly string TestCoverImageDirectory = Root; private const string TestCoverImageFile = "thumbnail.jpg"; private readonly string _testCoverPath = Path.Join(TestCoverImageDirectory, TestCoverImageFile); private const string TestCoverArchive = @"file in folder.zip"; @@ -37,24 +38,29 @@ public class CacheHelperTests [Theory] [InlineData("", false)] - [InlineData("C:/", false)] [InlineData(null, false)] public void CoverImageExists_DoesFileExist(string coverImage, bool exists) { Assert.Equal(exists, _cacheHelper.CoverImageExists(coverImage)); } + [Fact] + public void CoverImageExists_DoesFileExistRoot() + { + Assert.False(_cacheHelper.CoverImageExists(Root)); + } + [Fact] public void CoverImageExists_FileExists() { - Assert.True(_cacheHelper.CoverImageExists(TestCoverArchive)); + Assert.True(_cacheHelper.CoverImageExists(Path.Join(TestCoverImageDirectory, TestCoverArchive))); } [Fact] public void ShouldUpdateCoverImage_OnFirstRun() { - var file = new MangaFileBuilder(TestCoverArchive, MangaFormat.Archive) + var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) .WithLastModified(DateTime.Now) .Build(); Assert.True(_cacheHelper.ShouldUpdateCoverImage(null, file, DateTime.Now.Subtract(TimeSpan.FromMinutes(1)), @@ -65,7 +71,7 @@ public class CacheHelperTests public void ShouldUpdateCoverImage_ShouldNotUpdateOnSecondRunWithCoverImageSetNotLocked() { // Represents first run - var file = new MangaFileBuilder(TestCoverArchive, MangaFormat.Archive) + var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) .WithLastModified(DateTime.Now) .Build(); Assert.False(_cacheHelper.ShouldUpdateCoverImage(_testCoverPath, file, DateTime.Now.Subtract(TimeSpan.FromMinutes(1)), @@ -76,7 +82,7 @@ public class CacheHelperTests public void ShouldUpdateCoverImage_ShouldNotUpdateOnSecondRunWithCoverImageSetNotLocked_2() { // Represents first run - var file = new MangaFileBuilder(TestCoverArchive, MangaFormat.Archive) + var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) .WithLastModified(DateTime.Now) .Build(); Assert.False(_cacheHelper.ShouldUpdateCoverImage(_testCoverPath, file, DateTime.Now, @@ -87,7 +93,7 @@ public class CacheHelperTests public void ShouldUpdateCoverImage_ShouldNotUpdateOnSecondRunWithCoverImageSetLocked() { // Represents first run - var file = new MangaFileBuilder(TestCoverArchive, MangaFormat.Archive) + var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) .WithLastModified(DateTime.Now) .Build(); Assert.False(_cacheHelper.ShouldUpdateCoverImage(_testCoverPath, file, DateTime.Now.Subtract(TimeSpan.FromMinutes(1)), @@ -98,7 +104,7 @@ public class CacheHelperTests public void ShouldUpdateCoverImage_ShouldNotUpdateOnSecondRunWithCoverImageSetLocked_Modified() { // Represents first run - var file = new MangaFileBuilder(TestCoverArchive, MangaFormat.Archive) + var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) .WithLastModified(DateTime.Now) .Build(); Assert.False(_cacheHelper.ShouldUpdateCoverImage(_testCoverPath, file, DateTime.Now.Subtract(TimeSpan.FromMinutes(1)), @@ -122,7 +128,7 @@ public class CacheHelperTests var cacheHelper = new CacheHelper(fileService); var created = DateTime.Now.Subtract(TimeSpan.FromHours(1)); - var file = new MangaFileBuilder(TestCoverArchive, MangaFormat.Archive) + var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) .WithLastModified(DateTime.Now.Subtract(TimeSpan.FromMinutes(1))) .Build(); @@ -133,9 +139,10 @@ public class CacheHelperTests [Fact] public void HasFileNotChangedSinceCreationOrLastScan_NotChangedSinceCreated() { + var now = DateTimeOffset.Now; var filesystemFile = new MockFileData("") { - LastWriteTime = DateTimeOffset.Now + LastWriteTime =now, }; var fileSystem = new MockFileSystem(new Dictionary { @@ -147,12 +154,12 @@ public class CacheHelperTests var cacheHelper = new CacheHelper(fileService); var chapter = new ChapterBuilder("1") - .WithLastModified(filesystemFile.LastWriteTime.DateTime) - .WithCreated(filesystemFile.LastWriteTime.DateTime) + .WithLastModified(now.DateTime) + .WithCreated(now.DateTime) .Build(); - var file = new MangaFileBuilder(TestCoverArchive, MangaFormat.Archive) - .WithLastModified(filesystemFile.LastWriteTime.DateTime) + var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) + .WithLastModified(now.DateTime) .Build(); Assert.True(cacheHelper.IsFileUnmodifiedSinceCreationOrLastScan(chapter, false, file)); } @@ -160,9 +167,10 @@ public class CacheHelperTests [Fact] public void HasFileNotChangedSinceCreationOrLastScan_NotChangedSinceLastModified() { + var now = DateTimeOffset.Now; var filesystemFile = new MockFileData("") { - LastWriteTime = DateTimeOffset.Now + LastWriteTime = now, }; var fileSystem = new MockFileSystem(new Dictionary { @@ -174,12 +182,12 @@ public class CacheHelperTests var cacheHelper = new CacheHelper(fileService); var chapter = new ChapterBuilder("1") - .WithLastModified(filesystemFile.LastWriteTime.DateTime) - .WithCreated(filesystemFile.LastWriteTime.DateTime) + .WithLastModified(now.DateTime) + .WithCreated(now.DateTime) .Build(); - var file = new MangaFileBuilder(TestCoverArchive, MangaFormat.Archive) - .WithLastModified(filesystemFile.LastWriteTime.DateTime) + var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) + .WithLastModified(now.DateTime) .Build(); Assert.True(cacheHelper.IsFileUnmodifiedSinceCreationOrLastScan(chapter, false, file)); @@ -188,9 +196,10 @@ public class CacheHelperTests [Fact] public void HasFileNotChangedSinceCreationOrLastScan_NotChangedSinceLastModified_ForceUpdate() { + var now = DateTimeOffset.Now; var filesystemFile = new MockFileData("") { - LastWriteTime = DateTimeOffset.Now + LastWriteTime = now.DateTime, }; var fileSystem = new MockFileSystem(new Dictionary { @@ -202,12 +211,12 @@ public class CacheHelperTests var cacheHelper = new CacheHelper(fileService); var chapter = new ChapterBuilder("1") - .WithLastModified(filesystemFile.LastWriteTime.DateTime) - .WithCreated(filesystemFile.LastWriteTime.DateTime) + .WithLastModified(now.DateTime) + .WithCreated(now.DateTime) .Build(); - var file = new MangaFileBuilder(TestCoverArchive, MangaFormat.Archive) - .WithLastModified(filesystemFile.LastWriteTime.DateTime) + var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) + .WithLastModified(now.DateTime) .Build(); Assert.False(cacheHelper.IsFileUnmodifiedSinceCreationOrLastScan(chapter, true, file)); } @@ -215,10 +224,11 @@ public class CacheHelperTests [Fact] public void IsFileUnmodifiedSinceCreationOrLastScan_ModifiedSinceLastScan() { + var now = DateTimeOffset.Now; var filesystemFile = new MockFileData("") { - LastWriteTime = DateTimeOffset.Now, - CreationTime = DateTimeOffset.Now + LastWriteTime = now.DateTime, + CreationTime = now.DateTime }; var fileSystem = new MockFileSystem(new Dictionary { @@ -234,8 +244,8 @@ public class CacheHelperTests .WithCreated(DateTime.Now.Subtract(TimeSpan.FromMinutes(10))) .Build(); - var file = new MangaFileBuilder(TestCoverArchive, MangaFormat.Archive) - .WithLastModified(filesystemFile.LastWriteTime.DateTime) + var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) + .WithLastModified(now.DateTime) .Build(); Assert.False(cacheHelper.IsFileUnmodifiedSinceCreationOrLastScan(chapter, false, file)); } @@ -243,9 +253,10 @@ public class CacheHelperTests [Fact] public void HasFileNotChangedSinceCreationOrLastScan_ModifiedSinceLastScan_ButLastModifiedSame() { + var now = DateTimeOffset.Now; var filesystemFile = new MockFileData("") { - LastWriteTime = DateTimeOffset.Now + LastWriteTime =now.DateTime }; var fileSystem = new MockFileSystem(new Dictionary { @@ -262,7 +273,7 @@ public class CacheHelperTests .Build(); var file = new MangaFileBuilder(Path.Join(TestCoverImageDirectory, TestCoverArchive), MangaFormat.Archive) - .WithLastModified(filesystemFile.LastWriteTime.DateTime) + .WithLastModified(now.DateTime) .Build(); Assert.False(cacheHelper.IsFileUnmodifiedSinceCreationOrLastScan(chapter, false, file)); diff --git a/API.Tests/Parsing/ParsingTests.cs b/API.Tests/Parsing/ParsingTests.cs index a3a49762d..3c4f35973 100644 --- a/API.Tests/Parsing/ParsingTests.cs +++ b/API.Tests/Parsing/ParsingTests.cs @@ -11,9 +11,13 @@ public class ParsingTests [Fact] public void ShouldWork() { - var s = 6.5f + ""; + var s = 6.5f.ToString(CultureInfo.InvariantCulture); var a = float.Parse(s, CultureInfo.InvariantCulture); Assert.Equal(6.5f, a); + + s = 6.5f + ""; + a = float.Parse(s, CultureInfo.CurrentCulture); + Assert.Equal(6.5f, a); } // [Theory] diff --git a/API.Tests/Services/BackupServiceTests.cs b/API.Tests/Services/BackupServiceTests.cs index c4ca95a11..4a34ec3d3 100644 --- a/API.Tests/Services/BackupServiceTests.cs +++ b/API.Tests/Services/BackupServiceTests.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Data.Common; +using System.IO; using System.IO.Abstractions.TestingHelpers; using System.Linq; using System.Threading.Tasks; @@ -21,7 +22,7 @@ using Xunit; namespace API.Tests.Services; -public class BackupServiceTests +public class BackupServiceTests: AbstractFsTest { private readonly ILogger _logger = Substitute.For>(); private readonly IUnitOfWork _unitOfWork; @@ -31,13 +32,6 @@ public class BackupServiceTests private readonly DbConnection _connection; private readonly DataContext _context; - private const string CacheDirectory = "C:/kavita/config/cache/"; - private const string CoverImageDirectory = "C:/kavita/config/covers/"; - private const string BackupDirectory = "C:/kavita/config/backups/"; - private const string LogDirectory = "C:/kavita/config/logs/"; - private const string ConfigDirectory = "C:/kavita/config/"; - private const string BookmarkDirectory = "C:/kavita/config/bookmarks"; - private const string ThemesDirectory = "C:/kavita/config/theme"; public BackupServiceTests() { @@ -82,7 +76,7 @@ public class BackupServiceTests _context.ServerSetting.Update(setting); _context.Library.Add(new LibraryBuilder("Manga") - .WithFolderPath(new FolderPathBuilder("C:/data/").Build()) + .WithFolderPath(new FolderPathBuilder(Root + "data/").Build()) .Build()); return await _context.SaveChangesAsync() > 0; } @@ -94,22 +88,6 @@ public class BackupServiceTests await _context.SaveChangesAsync(); } - private static MockFileSystem CreateFileSystem() - { - var fileSystem = new MockFileSystem(); - fileSystem.Directory.SetCurrentDirectory("C:/kavita/"); - fileSystem.AddDirectory("C:/kavita/config/"); - fileSystem.AddDirectory(CacheDirectory); - fileSystem.AddDirectory(CoverImageDirectory); - fileSystem.AddDirectory(BackupDirectory); - fileSystem.AddDirectory(LogDirectory); - fileSystem.AddDirectory(ThemesDirectory); - fileSystem.AddDirectory(BookmarkDirectory); - fileSystem.AddDirectory("C:/data/"); - - return fileSystem; - } - #endregion diff --git a/API.Tests/Services/BookmarkServiceTests.cs b/API.Tests/Services/BookmarkServiceTests.cs index 80a483833..9c7d87737 100644 --- a/API.Tests/Services/BookmarkServiceTests.cs +++ b/API.Tests/Services/BookmarkServiceTests.cs @@ -25,17 +25,12 @@ using Xunit; namespace API.Tests.Services; -public class BookmarkServiceTests +public class BookmarkServiceTests: AbstractFsTest { private readonly IUnitOfWork _unitOfWork; private readonly DbConnection _connection; private readonly DataContext _context; - private const string CacheDirectory = "C:/kavita/config/cache/"; - private const string CoverImageDirectory = "C:/kavita/config/covers/"; - private const string BackupDirectory = "C:/kavita/config/backups/"; - private const string BookmarkDirectory = "C:/kavita/config/bookmarks/"; - public BookmarkServiceTests() { @@ -88,7 +83,7 @@ Substitute.For()); _context.ServerSetting.Update(setting); _context.Library.Add(new LibraryBuilder("Manga") - .WithFolderPath(new FolderPathBuilder("C:/data/").Build()) + .WithFolderPath(new FolderPathBuilder(Root + "data/").Build()) .Build()); return await _context.SaveChangesAsync() > 0; } @@ -102,20 +97,6 @@ Substitute.For()); await _context.SaveChangesAsync(); } - private static MockFileSystem CreateFileSystem() - { - var fileSystem = new MockFileSystem(); - fileSystem.Directory.SetCurrentDirectory("C:/kavita/"); - fileSystem.AddDirectory("C:/kavita/config/"); - fileSystem.AddDirectory(CacheDirectory); - fileSystem.AddDirectory(CoverImageDirectory); - fileSystem.AddDirectory(BackupDirectory); - fileSystem.AddDirectory(BookmarkDirectory); - fileSystem.AddDirectory("C:/data/"); - - return fileSystem; - } - #endregion #region BookmarkPage diff --git a/API.Tests/Services/CacheServiceTests.cs b/API.Tests/Services/CacheServiceTests.cs index ba06525a3..72b3015b8 100644 --- a/API.Tests/Services/CacheServiceTests.cs +++ b/API.Tests/Services/CacheServiceTests.cs @@ -62,7 +62,7 @@ internal class MockReadingItemServiceForCacheService : IReadingItemService throw new System.NotImplementedException(); } } -public class CacheServiceTests +public class CacheServiceTests: AbstractFsTest { private readonly ILogger _logger = Substitute.For>(); private readonly IUnitOfWork _unitOfWork; @@ -71,11 +71,6 @@ public class CacheServiceTests private readonly DbConnection _connection; private readonly DataContext _context; - private const string CacheDirectory = "C:/kavita/config/cache/"; - private const string CoverImageDirectory = "C:/kavita/config/covers/"; - private const string BackupDirectory = "C:/kavita/config/backups/"; - private const string DataDirectory = "C:/data/"; - public CacheServiceTests() { var contextOptions = new DbContextOptionsBuilder() @@ -118,7 +113,7 @@ public class CacheServiceTests _context.ServerSetting.Update(setting); _context.Library.Add(new LibraryBuilder("Manga") - .WithFolderPath(new FolderPathBuilder("C:/data/").Build()) + .WithFolderPath(new FolderPathBuilder(Root + "data/").Build()) .Build()); return await _context.SaveChangesAsync() > 0; } @@ -130,19 +125,6 @@ public class CacheServiceTests await _context.SaveChangesAsync(); } - private static MockFileSystem CreateFileSystem() - { - var fileSystem = new MockFileSystem(); - fileSystem.Directory.SetCurrentDirectory("C:/kavita/"); - fileSystem.AddDirectory("C:/kavita/config/"); - fileSystem.AddDirectory(CacheDirectory); - fileSystem.AddDirectory(CoverImageDirectory); - fileSystem.AddDirectory(BackupDirectory); - fileSystem.AddDirectory(DataDirectory); - - return fileSystem; - } - #endregion #region Ensure @@ -263,7 +245,7 @@ public class CacheServiceTests .WithFile(new MangaFileBuilder($"{DataDirectory}2.epub", MangaFormat.Epub).Build()) .Build(); cs.GetCachedFile(c); - Assert.Same($"{DataDirectory}1.epub", cs.GetCachedFile(c)); + Assert.Equal($"{DataDirectory}1.epub", cs.GetCachedFile(c)); } #endregion diff --git a/API.Tests/Services/CleanupServiceTests.cs b/API.Tests/Services/CleanupServiceTests.cs index ef80ad850..d355378a7 100644 --- a/API.Tests/Services/CleanupServiceTests.cs +++ b/API.Tests/Services/CleanupServiceTests.cs @@ -30,11 +30,10 @@ public class CleanupServiceTests : AbstractDbTest private readonly IEventHub _messageHub = Substitute.For(); private readonly IReaderService _readerService; - public CleanupServiceTests() : base() { _context.Library.Add(new LibraryBuilder("Manga") - .WithFolderPath(new FolderPathBuilder("C:/data/").Build()) + .WithFolderPath(new FolderPathBuilder(Root + "data/").Build()) .Build()); _readerService = new ReaderService(_unitOfWork, Substitute.For>(), Substitute.For(), diff --git a/API.Tests/Services/DirectoryServiceTests.cs b/API.Tests/Services/DirectoryServiceTests.cs index 737779f0f..c5216bebf 100644 --- a/API.Tests/Services/DirectoryServiceTests.cs +++ b/API.Tests/Services/DirectoryServiceTests.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.IO.Abstractions.TestingHelpers; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using API.Services; @@ -10,12 +12,19 @@ using Kavita.Common.Helpers; using Microsoft.Extensions.Logging; using NSubstitute; using Xunit; +using Xunit.Abstractions; namespace API.Tests.Services; -public class DirectoryServiceTests +public class DirectoryServiceTests: AbstractFsTest { private readonly ILogger _logger = Substitute.For>(); + private readonly ITestOutputHelper _testOutputHelper; + + public DirectoryServiceTests(ITestOutputHelper testOutputHelper) + { + _testOutputHelper = testOutputHelper; + } #region TraverseTreeParallelForEach @@ -373,9 +382,16 @@ public class DirectoryServiceTests #endregion #region IsDriveMounted + // The root directory (/) is always mounted on non windows [Fact] public void IsDriveMounted_DriveIsNotMounted() { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + _testOutputHelper.WriteLine("Skipping test on non Windows platform"); + return; + } + const string testDirectory = "c:/manga/"; var fileSystem = new MockFileSystem(); fileSystem.AddFile($"{testDirectory}data-0.txt", new MockFileData("abc")); @@ -387,6 +403,12 @@ public class DirectoryServiceTests [Fact] public void IsDriveMounted_DriveIsMounted() { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + _testOutputHelper.WriteLine("Skipping test on non Windows platform"); + return; + } + const string testDirectory = "c:/manga/"; var fileSystem = new MockFileSystem(); fileSystem.AddFile($"{testDirectory}data-0.txt", new MockFileData("abc")); @@ -900,12 +922,14 @@ public class DirectoryServiceTests #region GetHumanReadableBytes [Theory] - [InlineData(1200, "1.17 KB")] - [InlineData(1, "1 B")] - [InlineData(10000000, "9.54 MB")] - [InlineData(10000000000, "9.31 GB")] - public void GetHumanReadableBytesTest(long bytes, string expected) + [InlineData(1200, 1.17, " KB")] + [InlineData(1, 1, " B")] + [InlineData(10000000, 9.54, " MB")] + [InlineData(10000000000, 9.31, " GB")] + public void GetHumanReadableBytesTest(long bytes, float number, string suffix) { + // GetHumanReadableBytes is user facing, should be in CultureInfo.CurrentCulture + var expected = number.ToString(CultureInfo.CurrentCulture) + suffix; Assert.Equal(expected, DirectoryService.GetHumanReadableBytes(bytes)); } #endregion @@ -1041,11 +1065,14 @@ public class DirectoryServiceTests #region GetParentDirectory [Theory] - [InlineData(@"C:/file.txt", "C:/")] - [InlineData(@"C:/folder/file.txt", "C:/folder")] - [InlineData(@"C:/folder/subfolder/file.txt", "C:/folder/subfolder")] + [InlineData(@"file.txt", "")] + [InlineData(@"folder/file.txt", "folder")] + [InlineData(@"folder/subfolder/file.txt", "folder/subfolder")] public void GetParentDirectoryName_ShouldFindParentOfFiles(string path, string expected) { + path = Root + path; + expected = Root + expected; + var fileSystem = new MockFileSystem(new Dictionary { { path, new MockFileData(string.Empty)} @@ -1055,11 +1082,14 @@ public class DirectoryServiceTests Assert.Equal(expected, ds.GetParentDirectoryName(path)); } [Theory] - [InlineData(@"C:/folder", "C:/")] - [InlineData(@"C:/folder/subfolder", "C:/folder")] - [InlineData(@"C:/folder/subfolder/another", "C:/folder/subfolder")] + [InlineData(@"folder", "")] + [InlineData(@"folder/subfolder", "folder")] + [InlineData(@"folder/subfolder/another", "folder/subfolder")] public void GetParentDirectoryName_ShouldFindParentOfDirectories(string path, string expected) { + path = Root + path; + expected = Root + expected; + var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(path); diff --git a/API.Tests/Services/ParseScannedFilesTests.cs b/API.Tests/Services/ParseScannedFilesTests.cs index 26065f151..f34711305 100644 --- a/API.Tests/Services/ParseScannedFilesTests.cs +++ b/API.Tests/Services/ParseScannedFilesTests.cs @@ -204,11 +204,11 @@ public class ParseScannedFilesTests : AbstractDbTest public async Task ScanLibrariesForSeries_ShouldFindFiles() { var fileSystem = new MockFileSystem(); - fileSystem.AddDirectory("C:/Data/"); - fileSystem.AddFile("C:/Data/Accel World v1.cbz", new MockFileData(string.Empty)); - fileSystem.AddFile("C:/Data/Accel World v2.cbz", new MockFileData(string.Empty)); - fileSystem.AddFile("C:/Data/Accel World v2.pdf", new MockFileData(string.Empty)); - fileSystem.AddFile("C:/Data/Nothing.pdf", new MockFileData(string.Empty)); + fileSystem.AddDirectory(Root + "Data/"); + fileSystem.AddFile(Root + "Data/Accel World v1.cbz", new MockFileData(string.Empty)); + fileSystem.AddFile(Root + "Data/Accel World v2.cbz", new MockFileData(string.Empty)); + fileSystem.AddFile(Root + "Data/Accel World v2.pdf", new MockFileData(string.Empty)); + fileSystem.AddFile(Root + "Data/Nothing.pdf", new MockFileData(string.Empty)); var ds = new DirectoryService(Substitute.For>(), fileSystem); var psf = new ParseScannedFiles(Substitute.For>(), ds, @@ -221,7 +221,7 @@ public class ParseScannedFilesTests : AbstractDbTest Assert.NotNull(library); library.Type = LibraryType.Manga; - var parsedSeries = await psf.ScanLibrariesForSeries(library, new List() {"C:/Data/"}, false, + var parsedSeries = await psf.ScanLibrariesForSeries(library, new List() {Root + "Data/"}, false, await _unitOfWork.SeriesRepository.GetFolderPathMap(1)); diff --git a/API.Tests/Services/ReaderServiceTests.cs b/API.Tests/Services/ReaderServiceTests.cs index 468c22681..3dd929a4b 100644 --- a/API.Tests/Services/ReaderServiceTests.cs +++ b/API.Tests/Services/ReaderServiceTests.cs @@ -32,18 +32,13 @@ using Xunit.Abstractions; namespace API.Tests.Services; -public class ReaderServiceTests +public class ReaderServiceTests: AbstractFsTest { private readonly ITestOutputHelper _testOutputHelper; private readonly IUnitOfWork _unitOfWork; private readonly DataContext _context; private readonly ReaderService _readerService; - private const string CacheDirectory = "C:/kavita/config/cache/"; - private const string CoverImageDirectory = "C:/kavita/config/covers/"; - private const string BackupDirectory = "C:/kavita/config/backups/"; - private const string DataDirectory = "C:/data/"; - public ReaderServiceTests(ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; @@ -101,19 +96,6 @@ public class ReaderServiceTests await _context.SaveChangesAsync(); } - private static MockFileSystem CreateFileSystem() - { - var fileSystem = new MockFileSystem(); - fileSystem.Directory.SetCurrentDirectory("C:/kavita/"); - fileSystem.AddDirectory("C:/kavita/config/"); - fileSystem.AddDirectory(CacheDirectory); - fileSystem.AddDirectory(CoverImageDirectory); - fileSystem.AddDirectory(BackupDirectory); - fileSystem.AddDirectory(DataDirectory); - - return fileSystem; - } - #endregion #region FormatBookmarkFolderPath diff --git a/API.Tests/Services/Test Data/ScannerService/TestCases/Flat Special - Manga.json b/API.Tests/Services/Test Data/ScannerService/TestCases/Flat Special - Manga.json index d864da284..3fa9eebf7 100644 --- a/API.Tests/Services/Test Data/ScannerService/TestCases/Flat Special - Manga.json +++ b/API.Tests/Services/Test Data/ScannerService/TestCases/Flat Special - Manga.json @@ -1,5 +1,5 @@ [ - "Uzaki-chan Wants to Hang Out!\\Uzaki-chan Wants to Hang Out! - 2022 New Years Special SP01.cbz", - "Uzaki-chan Wants to Hang Out!\\Uzaki-chan Wants to Hang Out! - Ch. 103 - Kouhai and Control.cbz", - "Uzaki-chan Wants to Hang Out!\\Uzaki-chan Wants to Hang Out! v01 (2019) (Digital) (danke-Empire).cbz" -] \ No newline at end of file + "Uzaki-chan Wants to Hang Out!/Uzaki-chan Wants to Hang Out! - 2022 New Years Special SP01.cbz", + "Uzaki-chan Wants to Hang Out!/Uzaki-chan Wants to Hang Out! - Ch. 103 - Kouhai and Control.cbz", + "Uzaki-chan Wants to Hang Out!/Uzaki-chan Wants to Hang Out! v01 (2019) (Digital) (danke-Empire).cbz" +]