[skip ci] Fix unit tests on macOS (And probably Unix) (#3647)

This commit is contained in:
Fesaa 2025-03-19 11:13:05 +00:00 committed by GitHub
parent e897fb9a12
commit 98a2b9d3ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 154 additions and 172 deletions

View File

@ -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<DataContext>()
@ -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();

View File

@ -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;
}
}

View File

@ -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<string, MockFileData>
{
@ -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<string, MockFileData>
{
@ -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<string, MockFileData>
{
@ -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<string, MockFileData>
{
@ -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<string, MockFileData>
{
@ -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));

View File

@ -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]

View File

@ -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<BackupService> _logger = Substitute.For<ILogger<BackupService>>();
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

View File

@ -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<IMediaConversionService>());
_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<IMediaConversionService>());
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

View File

@ -62,7 +62,7 @@ internal class MockReadingItemServiceForCacheService : IReadingItemService
throw new System.NotImplementedException();
}
}
public class CacheServiceTests
public class CacheServiceTests: AbstractFsTest
{
private readonly ILogger<CacheService> _logger = Substitute.For<ILogger<CacheService>>();
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

View File

@ -30,11 +30,10 @@ public class CleanupServiceTests : AbstractDbTest
private readonly IEventHub _messageHub = Substitute.For<IEventHub>();
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<ILogger<ReaderService>>(), Substitute.For<IEventHub>(),

View File

@ -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<DirectoryService> _logger = Substitute.For<ILogger<DirectoryService>>();
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<string, MockFileData>
{
{ 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);

View File

@ -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<ILogger<DirectoryService>>(), fileSystem);
var psf = new ParseScannedFiles(Substitute.For<ILogger<ParseScannedFiles>>(), ds,
@ -221,7 +221,7 @@ public class ParseScannedFilesTests : AbstractDbTest
Assert.NotNull(library);
library.Type = LibraryType.Manga;
var parsedSeries = await psf.ScanLibrariesForSeries(library, new List<string>() {"C:/Data/"}, false,
var parsedSeries = await psf.ScanLibrariesForSeries(library, new List<string>() {Root + "Data/"}, false,
await _unitOfWork.SeriesRepository.GetFolderPathMap(1));

View File

@ -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

View File

@ -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"
]
"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"
]