From 8b39105c02f0a0b4c215d47965e612db71275e24 Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 23 Jan 2025 09:34:40 +0100 Subject: [PATCH] #1833 use memory stream --- .../CreateFakeExifToolWindows.cs | 4 +- .../ExifToolStreamToStreamRunnerTests.cs | 37 ++++++++----------- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/starsky/starskytest/FakeCreateAn/CreateFakeExifToolWindows/CreateFakeExifToolWindows.cs b/starsky/starskytest/FakeCreateAn/CreateFakeExifToolWindows/CreateFakeExifToolWindows.cs index b11d75180..c0b08002e 100644 --- a/starsky/starskytest/FakeCreateAn/CreateFakeExifToolWindows/CreateFakeExifToolWindows.cs +++ b/starsky/starskytest/FakeCreateAn/CreateFakeExifToolWindows/CreateFakeExifToolWindows.cs @@ -11,6 +11,8 @@ public class CreateFakeExifToolWindows { public readonly ImmutableArray Bytes = [..Array.Empty()]; + public readonly string ExifToolPath = string.Empty; + public CreateFakeExifToolWindows() { var dirName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); @@ -26,8 +28,6 @@ public CreateFakeExifToolWindows() Bytes = [..StreamToBytes(path)]; } - public string ExifToolPath { get; set; } - private static byte[] StreamToBytes(string path) { var input = new StorageHostFullPathFilesystem(new FakeIWebLogger()).ReadStream(path); diff --git a/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolStreamToStreamRunnerTests.cs b/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolStreamToStreamRunnerTests.cs index eb49541a5..a61229f8b 100644 --- a/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolStreamToStreamRunnerTests.cs +++ b/starsky/starskytest/starsky.foundation.writemeta/Helpers/ExifToolStreamToStreamRunnerTests.cs @@ -17,9 +17,7 @@ namespace starskytest.starsky.foundation.writemeta.Helpers; public class ExifToolStreamToStreamRunnerTests { private readonly AppSettings _appSettingsWithExifTool; - private readonly string _exifToolExe; private readonly string _exifToolExePosix; - private readonly string _exifToolExeWindows; private readonly StorageHostFullPathFilesystem _hostFullPathFilesystem; public ExifToolStreamToStreamRunnerTests() @@ -27,15 +25,15 @@ public ExifToolStreamToStreamRunnerTests() _hostFullPathFilesystem = new StorageHostFullPathFilesystem(new FakeIWebLogger()); - _exifToolExeWindows = new CreateFakeExifToolWindows().ExifToolPath; + var exifToolExeWindows = new CreateFakeExifToolWindows().ExifToolPath; _hostFullPathFilesystem.CreateDirectory(Path.Combine(new CreateAnImage().BasePath, "ExifToolStreamToStreamRunnerTests")); _exifToolExePosix = Path.Combine(new CreateAnImage().BasePath, "ExifToolStreamToStreamRunnerTests", "exiftool"); - _exifToolExe = new AppSettings().IsWindows ? _exifToolExeWindows : _exifToolExePosix; - _appSettingsWithExifTool = new AppSettings { ExifToolPath = _exifToolExe }; + var exifToolExe = new AppSettings().IsWindows ? exifToolExeWindows : _exifToolExePosix; + _appSettingsWithExifTool = new AppSettings { ExifToolPath = exifToolExe }; } [ClassCleanup] @@ -55,20 +53,15 @@ private async Task CreateStubFile(string path, string content) await _hostFullPathFilesystem.WriteStreamAsync(stream, path); } - private async Task SetupFakeExifToolExecutable(int i) + private async Task SetupFakeExifToolExecutable() { - var readFile = Path.Combine(new CreateAnImage().BasePath, - "FfmpegStreamToStreamRunnerTests", "read_file."); - await CreateStubFile(_exifToolExePosix, "#!/bin/bash\necho Fake Executable"); - await CreateStubFile(readFile + i, "test_content"); await new FfMpegChmod(new FakeSelectorStorage(_hostFullPathFilesystem), new FakeIWebLogger()) .Chmod( _exifToolExePosix); - return readFile + i; } [TestMethod] @@ -132,10 +125,10 @@ public async Task ExifTool_RunProcessAsync_Fuzzing(string argument) [TestMethod] public async Task ExifTool_RunProcessAsync_HappyFlow() { - var readFile = await SetupFakeExifToolExecutable(0); + await SetupFakeExifToolExecutable(); + + var sourceStream = new MemoryStream([0x01, 0x02, 0x03]); - var hostStorage = new StorageHostFullPathFilesystem(new FakeIWebLogger()); - var sourceStream = hostStorage.ReadStream(readFile); var sut = new ExifToolStreamToStreamRunner(_appSettingsWithExifTool, sourceStream, new FakeIWebLogger()); @@ -158,14 +151,14 @@ public async Task ExifTool_RunProcessAsync_ExitCode() return; } - var readFile = await SetupFakeExifToolExecutable(1); + await SetupFakeExifToolExecutable(); + var sourceStream = new MemoryStream([0x01, 0x02, 0x03]); // overwrite the file with a bash script that will exit with code 1 await CreateStubFile(_exifToolExePosix, "#!/bin/bash\ntest_content\n exit 1"); - var file = new StorageHostFullPathFilesystem(new FakeIWebLogger()).ReadStream(readFile); - var sut = new ExifToolStreamToStreamRunner(_appSettingsWithExifTool, file, + var sut = new ExifToolStreamToStreamRunner(_appSettingsWithExifTool, sourceStream, new FakeIWebLogger()); var stream = await sut.RunProcessAsync("arg1", @@ -173,25 +166,25 @@ await CreateStubFile(_exifToolExePosix, Assert.IsNotNull(stream); - await file.DisposeAsync(); + await sourceStream.DisposeAsync(); await stream.DisposeAsync(); } [TestMethod] public async Task ExifTool_RunProcessAsync_WithInvalidFfmpegPath() { - var readFile = await SetupFakeExifToolExecutable(2); + await SetupFakeExifToolExecutable(); + var sourceStream = new MemoryStream([0x01, 0x02, 0x03]); - var file = new StorageHostFullPathFilesystem(new FakeIWebLogger()).ReadStream(readFile); var sut = new ExifToolStreamToStreamRunner(new AppSettings { ExifToolPath = "invalid" }, - file, + sourceStream, new FakeIWebLogger()); await Assert.ThrowsExceptionAsync(async () => await sut.RunProcessAsync("-1", "image2")); - await file.DisposeAsync(); + await sourceStream.DisposeAsync(); } [TestMethod]