Skip to content

Commit

Permalink
Fixes #309 - FolderFileStorage doesn't overwrite existing content (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski authored Sep 14, 2024
1 parent 3c6ef87 commit ad61485
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,28 @@ public virtual void CanUseDataDirectory()
Assert.NotEqual(DATA_DIRECTORY_QUEUE_FOLDER, storage.Folder);
Assert.True(storage.Folder.EndsWith("Queue" + Path.DirectorySeparatorChar), storage.Folder);
}

public virtual async Task CanSaveOverExistingStoredContent()
{
using var storage = GetStorage();
if (storage == null)
return;

await ResetAsync(storage);

var shortIdInfo = new PostInfo { ProjectId = "123" };
var longIdInfo = new PostInfo { ProjectId = "1234567890" };

string path = "test.json";
await storage.SaveObjectAsync(path, longIdInfo);
await storage.SaveObjectAsync(path, shortIdInfo);

var actualInfo = await storage.GetObjectAsync<PostInfo>(path);
Assert.Equal(shortIdInfo, actualInfo);
}
}

public class PostInfo
public record PostInfo
{
public int ApiVersion { get; set; }
public string CharSet { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Foundatio/Storage/FolderFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Task<Stream> GetFileStreamAsync(string path, StreamMode streamMode, Cance
string fullPath = Path.Combine(Folder, normalizedPath);
EnsureDirectory(fullPath);

var stream = streamMode == StreamMode.Read ? File.OpenRead(fullPath) : File.OpenWrite(fullPath);
var stream = streamMode == StreamMode.Read ? File.OpenRead(fullPath) : new FileStream(fullPath, FileMode.Create, FileAccess.Write);
return Task.FromResult<Stream>(stream);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/Foundatio.Tests/Storage/FolderFileStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public override Task WillWriteStreamContentAsync()
return base.WillWriteStreamContentAsync();
}

[Fact]
public override Task CanSaveOverExistingStoredContent()
{
return base.CanSaveOverExistingStoredContent();
}

[Fact]
public async Task WillNotReturnDirectoryInGetPagedFileListAsync()
{
Expand Down
6 changes: 6 additions & 0 deletions tests/Foundatio.Tests/Storage/InMemoryFileStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,10 @@ public override Task WillWriteStreamContentAsync()
{
return base.WillWriteStreamContentAsync();
}

[Fact]
public override Task CanSaveOverExistingStoredContent()
{
return base.CanSaveOverExistingStoredContent();
}
}
6 changes: 6 additions & 0 deletions tests/Foundatio.Tests/Storage/ScopedFolderFileStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ public override Task WillWriteStreamContentAsync()
return base.WillWriteStreamContentAsync();
}

[Fact]
public override Task CanSaveOverExistingStoredContent()
{
return base.CanSaveOverExistingStoredContent();
}

[Fact]
public async Task WillNotReturnDirectoryInGetPagedFileListAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,10 @@ public override Task WillWriteStreamContentAsync()
{
return base.WillWriteStreamContentAsync();
}

[Fact]
public override Task CanSaveOverExistingStoredContent()
{
return base.CanSaveOverExistingStoredContent();
}
}

0 comments on commit ad61485

Please sign in to comment.