mirror of
https://github.com/cyanfish/naps2.git
synced 2024-09-11 15:26:55 +03:00
Improve test isolation
This commit is contained in:
parent
8a2ab077c7
commit
0ed3fdd5ec
@ -4,12 +4,11 @@ using System.Linq;
|
||||
using NAPS2.Config;
|
||||
using NAPS2.Config.Experimental;
|
||||
using NAPS2.Serialization;
|
||||
using NAPS2.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace NAPS2.Sdk.Tests.Config
|
||||
{
|
||||
public class CommonConfigTests
|
||||
public class CommonConfigTests : ContextualTexts
|
||||
{
|
||||
[Fact]
|
||||
public void CanSerialize()
|
||||
|
@ -5,12 +5,11 @@ using System.Xml.Linq;
|
||||
using NAPS2.Config;
|
||||
using NAPS2.Scan;
|
||||
using NAPS2.Serialization;
|
||||
using NAPS2.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace NAPS2.Sdk.Tests.Config
|
||||
{
|
||||
public class ProfileSerializerTests
|
||||
public class ProfileSerializerTests : ContextualTexts
|
||||
{
|
||||
[Fact]
|
||||
public void Serialization()
|
||||
|
81
NAPS2.Sdk.Tests/ContextualTexts.cs
Normal file
81
NAPS2.Sdk.Tests/ContextualTexts.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using NAPS2.Config;
|
||||
using NAPS2.Images;
|
||||
using NAPS2.Images.Storage;
|
||||
using NAPS2.ImportExport.Images;
|
||||
using NAPS2.ImportExport.Pdf;
|
||||
using NAPS2.Logging;
|
||||
using NAPS2.Ocr;
|
||||
using NAPS2.Operation;
|
||||
using NAPS2.Util;
|
||||
using NAPS2.WinForms;
|
||||
|
||||
namespace NAPS2.Sdk.Tests
|
||||
{
|
||||
public class ContextualTexts : IDisposable
|
||||
{
|
||||
private RecoveryStorageManager rsm;
|
||||
|
||||
public ContextualTexts()
|
||||
{
|
||||
FolderPath = $"naps2_test_temp_{Path.GetRandomFileName()}";
|
||||
Folder = Directory.CreateDirectory(FolderPath);
|
||||
var tempPath = Path.Combine(FolderPath, "temp");
|
||||
Directory.CreateDirectory(tempPath);
|
||||
FileStorageManager.Current = new FileStorageManager(tempPath);
|
||||
|
||||
Log.Logger = new NullLogger();
|
||||
Log.EventLogger = new NullEventLogger();
|
||||
ProfileManager.Current = new StubProfileManager();
|
||||
UserConfig.Manager = new StubConfigManager<UserConfig>(UserConfig.Create());
|
||||
AppConfig.Manager = new StubConfigManager<AppConfig>(AppConfig.Create());
|
||||
|
||||
var componentsPath = Path.Combine(FolderPath, "components");
|
||||
GhostscriptManager.BasePath = componentsPath;
|
||||
var ocrEngineManager = new OcrEngineManager(componentsPath);
|
||||
OcrEngineManager.Default = ocrEngineManager;
|
||||
|
||||
ErrorOutput.Default = new StubErrorOutput();
|
||||
var operationProgress = new StubOperationProgress();
|
||||
OperationProgress.Default = operationProgress;
|
||||
OverwritePrompt.Default = new StubOverwritePrompt();
|
||||
DialogHelper.Default = new StubDialogHelper();
|
||||
BlankDetector.Default = new ThresholdBlankDetector();
|
||||
ImageSettingsProvider.Default = ImageSettingsProvider.Wrap(new ImageSettings());
|
||||
PdfSettingsProvider.Default = PdfSettingsProvider.Wrap(new PdfSettings());
|
||||
PdfExporter.Default = new PdfSharpExporter();
|
||||
OcrRequestQueue.Default = new OcrRequestQueue(ocrEngineManager, operationProgress);
|
||||
|
||||
StorageManager.ConfigureImageType<GdiImage>();
|
||||
StorageManager.ConfigureBackingStorage<IStorage>();
|
||||
StorageManager.ImageMetadataFactory = new StubImageMetadataFactory();
|
||||
}
|
||||
|
||||
public string FolderPath { get; }
|
||||
|
||||
public DirectoryInfo Folder { get; }
|
||||
|
||||
public void UseFileStorage()
|
||||
{
|
||||
StorageManager.ConfigureBackingStorage<FileStorage>();
|
||||
}
|
||||
|
||||
public void UseRecovery()
|
||||
{
|
||||
var recoveryFolderPath = Path.Combine(FolderPath, "recovery", Path.GetRandomFileName());
|
||||
rsm = new RecoveryStorageManager(recoveryFolderPath);
|
||||
FileStorageManager.Current = rsm;
|
||||
StorageManager.ConfigureBackingStorage<FileStorage>();
|
||||
StorageManager.ImageMetadataFactory = rsm;
|
||||
}
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
rsm?.ForceReleaseLock();
|
||||
Directory.Delete(FolderPath, true);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using NAPS2.Images.Storage;
|
||||
|
||||
namespace NAPS2.Sdk.Tests
|
||||
{
|
||||
public class FileSystemTests : IDisposable
|
||||
{
|
||||
public FileSystemTests()
|
||||
{
|
||||
FolderPath = $"naps2_test_temp_{Path.GetRandomFileName()}";
|
||||
Folder = Directory.CreateDirectory(FolderPath);
|
||||
FileStorageManager.Current = new FileStorageManager(FolderPath);
|
||||
}
|
||||
|
||||
public string FolderPath { get; }
|
||||
|
||||
public DirectoryInfo Folder { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Directory.Delete(FolderPath, true);
|
||||
}
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ using Xunit;
|
||||
|
||||
namespace NAPS2.Sdk.Tests.Images
|
||||
{
|
||||
public class ScannedImageSinkTests
|
||||
public class ScannedImageSinkTests : ContextualTexts
|
||||
{
|
||||
[Fact]
|
||||
public async Task NoImages()
|
||||
|
@ -4,7 +4,7 @@ using System.Linq;
|
||||
|
||||
namespace NAPS2.Sdk.Tests.Images
|
||||
{
|
||||
public class StorageTests
|
||||
public class StorageTests : ContextualTexts
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -91,7 +91,6 @@
|
||||
<Compile Include="Asserts\PdfAsserts.cs" />
|
||||
<Compile Include="Config\CommonConfigTests.cs" />
|
||||
<Compile Include="Config\ProfileSerializerTests.cs" />
|
||||
<Compile Include="FileSystemTests.cs" />
|
||||
<Compile Include="GlobalSuppressions.cs" />
|
||||
<Compile Include="Images\ScannedImageSinkTests.cs" />
|
||||
<Compile Include="Images\StorageTests.cs" />
|
||||
@ -102,6 +101,7 @@
|
||||
<DesignTime>True</DesignTime>
|
||||
<DependentUpon>ProfileSerializerTestsData.resx</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ContextualTexts.cs" />
|
||||
<Compile Include="Util\NaturalStringComparerTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Scan\SaneOptionParserTests.cs" />
|
||||
|
@ -21,7 +21,7 @@ using Xunit;
|
||||
|
||||
namespace NAPS2.Sdk.Tests.Scan
|
||||
{
|
||||
public class AutoSaveTests : FileSystemTests
|
||||
public class AutoSaveTests : ContextualTexts
|
||||
{
|
||||
[Fact]
|
||||
public async Task NoImages()
|
||||
|
@ -18,7 +18,7 @@ using Xunit;
|
||||
|
||||
namespace NAPS2.Sdk.Tests.Worker
|
||||
{
|
||||
public class WorkerChannelTests : FileSystemTests
|
||||
public class WorkerChannelTests : ContextualTexts
|
||||
{
|
||||
private Channel Start(ITwainWrapper twainWrapper = null, ThumbnailRenderer thumbnailRenderer = null, IMapiWrapper mapiWrapper = null, ServerCredentials serverCreds = null, ChannelCredentials clientCreds = null)
|
||||
{
|
||||
@ -90,48 +90,21 @@ namespace NAPS2.Sdk.Tests.Worker
|
||||
public async Task TwainScanWithMemoryStorage()
|
||||
{
|
||||
StorageManager.ConfigureBackingStorage<GdiImage>();
|
||||
try
|
||||
{
|
||||
await TwainScanInternalTest();
|
||||
}
|
||||
finally
|
||||
{
|
||||
StorageManager.ConfigureBackingStorage<IStorage>();
|
||||
}
|
||||
await TwainScanInternalTest();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TwainScanWithFileStorage()
|
||||
{
|
||||
StorageManager.ConfigureBackingStorage<FileStorage>();
|
||||
try
|
||||
{
|
||||
await TwainScanInternalTest();
|
||||
}
|
||||
finally
|
||||
{
|
||||
StorageManager.ConfigureBackingStorage<IStorage>();
|
||||
}
|
||||
UseFileStorage();
|
||||
await TwainScanInternalTest();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TwainScanWithRecovery()
|
||||
{
|
||||
StorageManager.ConfigureBackingStorage<FileStorage>();
|
||||
var rsm = new RecoveryStorageManager(FolderPath);
|
||||
StorageManager.ImageMetadataFactory = rsm;
|
||||
FileStorageManager.Current = rsm;
|
||||
try
|
||||
{
|
||||
await TwainScanInternalTest();
|
||||
}
|
||||
finally
|
||||
{
|
||||
// TODO: Set all static things in a base class constructor so we don't have any fragility here
|
||||
StorageManager.ConfigureBackingStorage<IStorage>();
|
||||
StorageManager.ImageMetadataFactory = new StubImageMetadataFactory();
|
||||
rsm.ForceReleaseLock();
|
||||
}
|
||||
UseRecovery();
|
||||
await TwainScanInternalTest();
|
||||
}
|
||||
|
||||
private async Task TwainScanInternalTest()
|
||||
|
@ -148,7 +148,7 @@ namespace NAPS2.Worker
|
||||
IncludeThumbnail = true,
|
||||
RenderedFilePath = imagePathDict.Get(image)
|
||||
})
|
||||
});
|
||||
}).Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user