Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,9 @@ public async Task SaveToAsync_WithJustFilename_SavesInCurrentDirectory()
byte[] testData = [1, 2, 3];
DataContent content = new(testData, "application/json");

string filename = $"test_{Guid.NewGuid()}.json";
string filename = $"test_{Guid.NewGuid():N}.json";
Comment thread
jozkee marked this conversation as resolved.
Outdated
string cwdBefore = Directory.GetCurrentDirectory();
string expectedAbsolute = Path.Combine(cwdBefore, filename);
string? savedPath = null;

try
Expand All @@ -606,14 +608,17 @@ public async Task SaveToAsync_WithJustFilename_SavesInCurrentDirectory()

// The returned path should be in the current directory
Assert.Equal(filename, Path.GetFileName(savedPath));
Assert.True(File.Exists(savedPath));
Assert.Equal(testData, await File.ReadAllBytesAsync(savedPath));

// Verify the file actually landed in cwd-at-call-time using an absolute path,
// so the assertion is independent of cwd at check time.
Comment thread
jozkee marked this conversation as resolved.
Outdated
Assert.True(File.Exists(expectedAbsolute));
Assert.Equal(testData, await File.ReadAllBytesAsync(expectedAbsolute));
}
finally
{
if (savedPath is not null && File.Exists(savedPath))
if (File.Exists(expectedAbsolute))
{
File.Delete(savedPath);
File.Delete(expectedAbsolute);
}
}
}
Expand Down Expand Up @@ -652,8 +657,10 @@ public async Task SaveToAsync_WithEmptyPath_UsesCurrentDirectoryAndContentName()
{
// Test that providing an empty string path uses current directory with name from content
byte[] testData = [7, 8, 9];
DataContent content = new(testData, "text/plain") { Name = $"testfile_{Guid.NewGuid()}.txt" };
DataContent content = new(testData, "text/plain") { Name = $"testfile_{Guid.NewGuid():N}.txt" };
Comment thread
jozkee marked this conversation as resolved.
Outdated

string cwdBefore = Directory.GetCurrentDirectory();
string expectedAbsolute = Path.Combine(cwdBefore, content.Name!);
string? savedPath = null;

try
Expand All @@ -662,14 +669,17 @@ public async Task SaveToAsync_WithEmptyPath_UsesCurrentDirectoryAndContentName()

// The returned path should be in the current directory using content's name
Assert.Equal(content.Name, Path.GetFileName(savedPath));
Assert.True(File.Exists(savedPath));
Assert.Equal(testData, await File.ReadAllBytesAsync(savedPath));

// Verify the file actually landed in cwd-at-call-time using an absolute path,
// so the assertion is independent of cwd at check time.
Comment thread
jozkee marked this conversation as resolved.
Outdated
Assert.True(File.Exists(expectedAbsolute));
Assert.Equal(testData, await File.ReadAllBytesAsync(expectedAbsolute));
}
finally
{
if (savedPath is not null && File.Exists(savedPath))
if (File.Exists(expectedAbsolute))
{
File.Delete(savedPath);
File.Delete(expectedAbsolute);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,27 +197,36 @@ public async Task DownloadToAsync_DirectoryPath_NoFileName_UsesFileId()
public async Task DownloadToAsync_EmptyDestinationPath_UsesCurrentDirectory()
{
var data = new byte[] { 42, 43 };
var originalDir = Directory.GetCurrentDirectory();
var tempDir = Path.Combine(Path.GetTempPath(), $"dlcwd-{Guid.NewGuid()}");
Directory.CreateDirectory(tempDir);
var fileName = $"output_{Guid.NewGuid():N}.bin";
Comment thread
jozkee marked this conversation as resolved.
Outdated

using var client = new TestHostedFileClient
{
DownloadAsyncCallback = (fileId, options, ct) =>
Task.FromResult<HostedFileDownloadStream>(new TestHostedFileDownloadStream(data, fileName: "output.bin"))
Task.FromResult<HostedFileDownloadStream>(new TestHostedFileDownloadStream(data, fileName: fileName))
};

// Capture cwd at call time so the assertion doesn't depend on cwd at check time.
string cwdBefore = Directory.GetCurrentDirectory();
string? savedPath = null;
Comment thread
jozkee marked this conversation as resolved.
try
{
Directory.SetCurrentDirectory(tempDir);
var savedPath = await client.DownloadToAsync("file-cwd", string.Empty);
Assert.Equal("output.bin", savedPath);
Assert.Equal(data, await File.ReadAllBytesAsync(Path.Combine(tempDir, savedPath)));
savedPath = await client.DownloadToAsync("file-cwd", string.Empty);

// Empty destination path => file is written to cwd; returned path is just the filename (relative).
Assert.Equal(fileName, savedPath);
Assert.False(Path.IsPathRooted(savedPath));

string expectedAbsolute = Path.Combine(cwdBefore, savedPath);
Comment thread
jozkee marked this conversation as resolved.
Outdated
Assert.True(File.Exists(expectedAbsolute));
Assert.Equal(data, await File.ReadAllBytesAsync(expectedAbsolute));
}
finally
{
Directory.SetCurrentDirectory(originalDir);
Directory.Delete(tempDir, recursive: true);
if (savedPath is not null)
{
string absolute = Path.Combine(cwdBefore, savedPath);
if (File.Exists(absolute)) File.Delete(absolute);
}
Comment thread
jozkee marked this conversation as resolved.
Outdated
}
}

Expand Down
Loading