Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -598,6 +598,8 @@ public async Task SaveToAsync_WithJustFilename_SavesInCurrentDirectory()
DataContent content = new(testData, "application/json");

string filename = $"test_{Guid.NewGuid()}.json";
string cwdBefore = Directory.GetCurrentDirectory();
string expectedAbsolute = Path.Combine(cwdBefore, filename);
string? savedPath = null;

try
Expand All @@ -606,14 +608,14 @@ 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));
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 @@ -654,6 +656,8 @@ public async Task SaveToAsync_WithEmptyPath_UsesCurrentDirectoryAndContentName()
byte[] testData = [7, 8, 9];
DataContent content = new(testData, "text/plain") { Name = $"testfile_{Guid.NewGuid()}.txt" };

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

try
Expand All @@ -662,14 +666,14 @@ 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));
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,32 @@ 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()}.bin";

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 expectedAbsolute = Path.Combine(cwdBefore, fileName);
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));

Assert.True(File.Exists(expectedAbsolute));
Assert.Equal(data, await File.ReadAllBytesAsync(expectedAbsolute));
}
finally
{
Directory.SetCurrentDirectory(originalDir);
Directory.Delete(tempDir, recursive: true);
File.Delete(expectedAbsolute);
Comment thread
jozkee marked this conversation as resolved.
}
}

Expand Down
Loading