using Titano.Core.IO; using Titano.Testing; namespace Titano.Core.Tests.Foundation; public sealed class AtomicFileTests(TestContext context) { [Test] public void ReplacesContentAndLeavesNoTemporaryFiles() { var dir = context.CreateTempDirectory(); var path = Path.Combine(dir, "sequence.json"); AtomicFile.WriteAllText(path, "one"); AtomicFile.WriteAllText(path, "two"); Assert.Equal("two", File.ReadAllText(path)); Assert.Count(1, Directory.GetFiles(dir)); } [Test] public async Task FailedWriteKeepsPreviousContent() { var dir = context.CreateTempDirectory(); var path = Path.Combine(dir, "project.json"); AtomicFile.WriteAllText(path, "stable"); await Assert.ThrowsAsync(() => AtomicFile.WriteAsync(path, async (s, ct) => { await s.WriteAsync("partial"u8.ToArray(), ct); throw new InvalidOperationException("boom"); })); Assert.Equal("stable", File.ReadAllText(path)); Assert.Count(1, Directory.GetFiles(dir)); } }