58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
using FluentAssertions;
|
|
using FreeCode.Tools;
|
|
using FreeCode.Tests.Unit.Helpers;
|
|
using Xunit;
|
|
|
|
namespace FreeCode.Tests.Unit.Tools;
|
|
|
|
public sealed class FileWriteToolTests : IDisposable
|
|
{
|
|
private readonly string _tempDirectory = Path.Combine(Path.GetTempPath(), "free-code-tests", Guid.NewGuid().ToString("N"));
|
|
private readonly FileWriteTool _sut = new();
|
|
|
|
public FileWriteToolTests()
|
|
{
|
|
Directory.CreateDirectory(_tempDirectory);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteAsync_CreatesNewFile()
|
|
{
|
|
var path = Path.Combine(_tempDirectory, "nested", "sample.txt");
|
|
|
|
var result = await _sut.ExecuteAsync(new FileWriteToolInput(path, "created"), TestHelper.CreateContext());
|
|
|
|
result.IsError.Should().BeFalse();
|
|
File.Exists(path).Should().BeTrue();
|
|
(await File.ReadAllTextAsync(path)).Should().Be("created");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteAsync_OverwritesExistingFile()
|
|
{
|
|
var path = Path.Combine(_tempDirectory, "sample.txt");
|
|
await File.WriteAllTextAsync(path, "old");
|
|
|
|
await _sut.ExecuteAsync(new FileWriteToolInput(path, "new"), TestHelper.CreateContext());
|
|
|
|
(await File.ReadAllTextAsync(path)).Should().Be("new");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidateInputAsync_WithNullPathLikeValue_IsInvalid()
|
|
{
|
|
var result = await _sut.ValidateInputAsync(new FileWriteToolInput(string.Empty, "content"));
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
result.Errors.Should().Contain("FilePath is required.");
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(_tempDirectory))
|
|
{
|
|
Directory.Delete(_tempDirectory, recursive: true);
|
|
}
|
|
}
|
|
}
|