Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to other locations when ComicInfo.xml not at root of archive #1551

Merged
merged 3 commits into from
Sep 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Better ComicInfo test coverage and benchmarks
tjarls committed Sep 21, 2022
commit 37dfa837196805653931b6ab45ea425c30a8873a
22 changes: 21 additions & 1 deletion API.Benchmark/ArchiveServiceBenchmark.cs
Original file line number Diff line number Diff line change
@@ -3,10 +3,14 @@
using Microsoft.Extensions.Logging.Abstractions;
using API.Services;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;

namespace API.Benchmark;

[StopOnFirstError]
[MemoryDiagnoser]
[RankColumn]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[SimpleJob(launchCount: 1, warmupCount: 5, targetCount: 20)]
public class ArchiveServiceBenchmark
{
@@ -21,14 +25,30 @@ public ArchiveServiceBenchmark()
_archiveService = new ArchiveService(new NullLogger<ArchiveService>(), _directoryService, _imageService);
}

[Benchmark(Baseline = true)]
public void TestGetComicInfo_baseline()
{
if (_archiveService.GetComicInfo("Data/ComicInfo.zip") == null) {
throw new Exception("ComicInfo not found");
}
}

[Benchmark]
public void TestGetComicInfo()
public void TestGetComicInfo_duplicate()
{
if (_archiveService.GetComicInfo("Data/ComicInfo_duplicateInfos.zip") == null) {
throw new Exception("ComicInfo not found");
}
}

[Benchmark]
public void TestGetComicInfo_outside_root()
{
if (_archiveService.GetComicInfo("Data/ComicInfo_outside_root.zip") == null) {
throw new Exception("ComicInfo not found");
}
}

// Benchmark to test default GetNumberOfPages from archive
// vs a new method where I try to open the archive and return said stream
}
19 changes: 16 additions & 3 deletions API.Tests/Services/ArchiveServiceTests.cs
Original file line number Diff line number Diff line change
@@ -256,17 +256,30 @@ public void ShouldHaveComicInfo_WithAuthors()
Assert.Equal("Junya Inoue", comicInfo.Writer);
}

[Fact]
public void ShouldHaveComicInfo_TopLevelFileOnly()
[Theory]
[InlineData("ComicInfo_duplicateInfos.zip")]
[InlineData("ComicInfo_duplicateInfos_reversed.zip")]
public void ShouldHaveComicInfo_TopLevelFileOnly(string filename)
{
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/ComicInfos");
var archive = Path.Join(testDirectory, "ComicInfo_duplicateInfos.zip");
var archive = Path.Join(testDirectory, filename);

var comicInfo = _archiveService.GetComicInfo(archive);
Assert.NotNull(comicInfo);
Assert.Equal("BTOOOM!", comicInfo.Series);
}

[Fact]
public void ShouldHaveComicInfo_OutsideRoot()
{
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/ComicInfos");
var archive = Path.Join(testDirectory, "ComicInfo_outside_root.zip");

var comicInfo = _archiveService.GetComicInfo(archive);
Assert.NotNull(comicInfo);
Assert.Equal("BTOOOM! - Duplicate", comicInfo.Series);
}

#endregion

#region CanParseComicInfo
Binary file not shown.
Binary file not shown.
8 changes: 4 additions & 4 deletions API/Services/ArchiveService.cs
Original file line number Diff line number Diff line change
@@ -328,7 +328,7 @@ public bool IsValidArchive(string archivePath)
return false;
}

private static bool BackupComicInfoArchiveEntry(string fullName, string name)
private static bool IsComicInfoArchiveEntry(string fullName, string name)
{
return !Tasks.Scanner.Parser.Parser.HasBlacklistedFolderInPath(fullName)
&& name.Equals(ComicInfoFilename, StringComparison.OrdinalIgnoreCase)
@@ -355,8 +355,8 @@ private static bool BackupComicInfoArchiveEntry(string fullName, string name)
{
using var archive = ZipFile.OpenRead(archivePath);

var entry = archive.Entries.FirstOrDefault(x => x.FullName == ComicInfoFilename) ??
archive.Entries.FirstOrDefault(x => BackupComicInfoArchiveEntry(x.FullName, x.Name));
var entry = archive.Entries.FirstOrDefault(x => (x.FullName ?? x.Name) == ComicInfoFilename) ??
archive.Entries.FirstOrDefault(x => IsComicInfoArchiveEntry(x.FullName, x.Name));
if (entry != null)
{
using var stream = entry.Open();
@@ -373,7 +373,7 @@ private static bool BackupComicInfoArchiveEntry(string fullName, string name)
using var archive = ArchiveFactory.Open(archivePath);
var entry = archive.Entries.FirstOrDefault(entry => entry.Key == ComicInfoFilename) ??
archive.Entries.FirstOrDefault(entry =>
BackupComicInfoArchiveEntry(Path.GetDirectoryName(entry.Key), entry.Key));
IsComicInfoArchiveEntry(Path.GetDirectoryName(entry.Key), entry.Key));

if (entry != null)
{