Skip to content

Commit

Permalink
test: add tests for DiffService
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed Jul 5, 2023
1 parent c628c30 commit 956e07d
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.IO;
using System.Text;
using LibGit2Sharp;
using NSubstitute;
using NUnit.Framework;
using UnityEngine;
using UnityGit.Core.Services;

namespace UnityGit.Tests.Core.Services
{
[TestFixture]
public class DiffServiceTests
{
private const string FilePath = "test.txt";

private DiffService _diffService;
private IRepository _repository;
private DiffService.InvokeDiffToolDelegate _invokeDiffDelegate;
private TreeEntry _treeEntry;

[SetUp]
public void Setup()
{
_repository = Substitute.For<IRepository>();

var head = Substitute.For<Branch>();
_repository.Head.Returns(head);
var tip = Substitute.For<Commit>();
head.Tip.Returns(tip);
_treeEntry = Substitute.For<TreeEntry>();
tip[FilePath].Returns(_treeEntry);

_invokeDiffDelegate = Substitute.For<DiffService.InvokeDiffToolDelegate>();
_diffService = new DiffService(_invokeDiffDelegate);
}

[Test]
public void DiffFile_WhenBlobIsNull_ShouldNotInvokeDiffTool()
{
_treeEntry.Target.Returns((Blob) null);

_diffService.DiffFile(_repository, FilePath);

_invokeDiffDelegate.DidNotReceive().Invoke(
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<string>()
);
}

[Test]
public void DiffFile_WhenBlobExists_ShouldInvokeDiffTool()
{
var repositoryInfo = Substitute.For<RepositoryInformation>();
repositoryInfo.WorkingDirectory.Returns("/repo");
_repository.Info.Returns(repositoryInfo);

var blob = Substitute.For<Blob>();
_treeEntry.Target.Returns(blob);

var contentStream = new MemoryStream(Encoding.UTF8.GetBytes("Test Content"));
blob.GetContentStream().Returns(contentStream);

_diffService.DiffFile(_repository, FilePath);

var headFilePath = Path.Combine(Application.temporaryCachePath, "HEAD");
var currentFilePath = Path.Combine("/repo", FilePath);

_invokeDiffDelegate.Received(1).Invoke(
"HEAD",
headFilePath,
"Current",
currentFilePath,
null,
null
);
}

[Test]
public void DiffFile_ShouldWriteBlobContentToFile()
{
var blob = Substitute.For<Blob>();
_treeEntry.Target.Returns(blob);

var contentStream = new MemoryStream(Encoding.UTF8.GetBytes("Test Content"));
blob.GetContentStream().Returns(contentStream);

_diffService.DiffFile(_repository, FilePath);

var headFilePath = Path.Combine(Application.temporaryCachePath, "HEAD");
Assert.IsTrue(File.Exists(headFilePath));
var fileContent = File.ReadAllText(headFilePath);
Assert.AreEqual("Test Content", fileContent);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion Packages/io.savolainen.unitygit/Core/Services/DiffService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Text;
using JetBrains.Annotations;
using LibGit2Sharp;
using UnityEditor;
using UnityEngine;
Expand All @@ -13,6 +14,25 @@ public interface IDiffService

public sealed class DiffService : IDiffService
{
public delegate string InvokeDiffToolDelegate(
string leftTitle,
string leftFile,
string rightTitle,
string rightFile,
string ancestorTitle,
string ancestorFile
);

private readonly InvokeDiffToolDelegate _invokeDiffDelegate;

[PublicAPI]
public DiffService() : this(EditorUtility.InvokeDiffTool) {}

public DiffService(InvokeDiffToolDelegate invokeDiffDelegate)
{
_invokeDiffDelegate = invokeDiffDelegate;
}

public void DiffFile(IRepository repository, string filePath)
{
var headFilePath = Path.Combine(Application.temporaryCachePath, "HEAD");
Expand All @@ -29,7 +49,7 @@ public void DiffFile(IRepository repository, string filePath)

var currentFilePath = Path.Combine(repository.Info.WorkingDirectory, filePath);

EditorUtility.InvokeDiffTool(
_invokeDiffDelegate(
"HEAD",
headFilePath,
"Current",
Expand Down

0 comments on commit 956e07d

Please sign in to comment.