Skip to content

Commit

Permalink
#1833 use memory stream
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Jan 23, 2025
1 parent 37cd1df commit 8b39105
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class CreateFakeExifToolWindows
{
public readonly ImmutableArray<byte> Bytes = [..Array.Empty<byte>()];

public readonly string ExifToolPath = string.Empty;

public CreateFakeExifToolWindows()
{
var dirName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,23 @@ 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()
{
_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]
Expand All @@ -55,20 +53,15 @@ private async Task CreateStubFile(string path, string content)
await _hostFullPathFilesystem.WriteStreamAsync(stream, path);
}

private async Task<string> 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]
Expand Down Expand Up @@ -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());

Expand All @@ -158,40 +151,40 @@ 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",
"reference");

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<ArgumentException>(async () =>
await sut.RunProcessAsync("-1",
"image2"));

await file.DisposeAsync();
await sourceStream.DisposeAsync();
}

[TestMethod]
Expand Down

0 comments on commit 8b39105

Please sign in to comment.