Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

2.5 KB
Raw Blame
using Grand.Business.Storage.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grand.Business.Storage.Tests.Services;

[TestClass]
public class AtomicFileTests
{
    private string _dir;

    [TestInitialize]
    public void Init()
    {
        _dir = Path.Combine(Path.GetTempPath(), "atomicfile-" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(_dir);
    }

    [TestCleanup]
    public void Cleanup()
    {
        Directory.Delete(_dir, true);
    }

    [TestMethod]
    public async Task WriteAllBytes_NewFile_WritesContentAndLeavesNoTempFile()
    {
        //Arrange
        var path = Path.Combine(_dir, "thumb.jpg");
        //Act
        await AtomicFile.WriteAllBytesAsync(path, [1, 2, 3]);
        //Assert
        CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, File.ReadAllBytes(path));
        Assert.HasCount(1, Directory.GetFiles(_dir));
    }

    [TestMethod]
    public async Task WriteAllTextAsync_ExistingFile_OverwritesAndLeavesNoTempFile()
    {
        //Arrange
        var path = Path.Combine(_dir, "sitemap.xml");
        await File.WriteAllTextAsync(path, "old content that is longer than the new one");
        //Act
        await AtomicFile.WriteAllTextAsync(path, "new");
        //Assert
        Assert.AreEqual("new", await File.ReadAllTextAsync(path));
        Assert.HasCount(1, Directory.GetFiles(_dir));
    }

    [TestMethod]
    public async Task WriteAllTextAsync_WritesUtf8WithoutBom()
    {
        //Arrange
        var path = Path.Combine(_dir, "firebase-messaging-sw.js");
        //Act
        await AtomicFile.WriteAllTextAsync(path, "żółw");
        //Assert
        CollectionAssert.AreEqual(new System.Text.UTF8Encoding(false).GetBytes("żółw"), await File.ReadAllBytesAsync(path));
    }

    [TestMethod]
    public async Task WriteAllBytes_TargetHeldOpenForReading_ReplacesOnceReaderCloses()
    {
        //Arrange - the static file middleware opens files with FileShare.ReadWrite
        var path = Path.Combine(_dir, "sitemap.xml");
        File.WriteAllBytes(path, [9]);
        var reader = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        var closeReader = Task.Delay(200).ContinueWith(_ => reader.Dispose());

        //Act
        await AtomicFile.WriteAllBytesAsync(path, [1, 2]);
        await closeReader;

        //Assert
        CollectionAssert.AreEqual(new byte[] { 1, 2 }, File.ReadAllBytes(path));
        Assert.HasCount(1, Directory.GetFiles(_dir));
    }
}