Skip to content

Commit

Permalink
test: add tests for the majority of the Core assembly (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed Jul 29, 2023
1 parent e690b8d commit 73ce729
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 60 deletions.
1 change: 1 addition & 0 deletions Core/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("UnityGit.Tests.Core")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
14 changes: 12 additions & 2 deletions Core/Internal/GitProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace UnityGit.Core.Internal
{
internal sealed class GitProcess
public class GitProcess
{
private readonly Process _process;

Expand Down Expand Up @@ -64,7 +64,17 @@ public void SetTimeout(int timeoutMilliseconds)
_timeoutMs = timeoutMilliseconds;
}

public async Task<GitProcessResult> Run()
public int GetTimeout()
{
return _timeoutMs;
}

public string GetWorkingDirectory()
{
return _process.StartInfo.WorkingDirectory;
}

public virtual async Task<GitProcessResult> Run()
{
using (_process)
{
Expand Down
7 changes: 7 additions & 0 deletions Core/Services/CommandsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface ICommandsService
void Stage(IRepository repository, string filePath);

Commit Commit(IRepository repository, string message, Signature author);

MergeResult Pull(Repository repository, Signature signature, PullOptions pullOptions);
}

/// <summary>Acts as a wrapper for LibGit2Sharp.Commands.</summary>
Expand All @@ -30,5 +32,10 @@ public Commit Commit(IRepository repository, string message, Signature author)
{
return repository.Commit(message, author, author);
}

public MergeResult Pull(Repository repository, Signature signature, PullOptions pullOptions)
{
return Commands.Pull(repository, signature, pullOptions);
}
}
}
3 changes: 2 additions & 1 deletion Core/Services/DiffService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using LibGit2Sharp;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;

namespace UnityGit.Core.Services
{
Expand All @@ -25,7 +26,7 @@ string ancestorFile

private readonly InvokeDiffToolDelegate _invokeDiffDelegate;

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

public DiffService(InvokeDiffToolDelegate invokeDiffDelegate)
Expand Down
19 changes: 19 additions & 0 deletions Core/Services/FileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.IO;
using UnityEngine.TestTools;

namespace UnityGit.Core.Services
{
internal interface IFileService
{
void Delete(string path);
}

[ExcludeFromCoverage]
internal sealed class FileService : IFileService
{
public void Delete(string path)
{
File.Delete(path);
}
}
}
3 changes: 3 additions & 0 deletions Core/Services/FileService.cs.meta

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

48 changes: 32 additions & 16 deletions Core/Services/GitCommandService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using UIComponents;
using UnityGit.Core.Data;
using UnityGit.Core.Internal;
using Debug = UnityEngine.Debug;

namespace UnityGit.Core.Services
{
Expand All @@ -30,6 +29,8 @@ public sealed partial class GitCommandService : Service, IGitCommandService

public event CommandFinishedDelegate CommandFinished;

public delegate GitProcess CreateProcessDelegate(GitCommandInfo info);

[Provide]
private ILogService _logService;

Expand All @@ -39,6 +40,30 @@ public sealed partial class GitCommandService : Service, IGitCommandService
private GitProcess _currentProcess;

private int _progressId;

private readonly CreateProcessDelegate _createProcessDelegate;

public static GitProcess CreateGitProcess(GitCommandInfo info)
{
var process = new Process();
process.StartInfo.FileName = "git";
process.StartInfo.Arguments = info.Arguments;
process.StartInfo.WorkingDirectory = info.Repository.Info.WorkingDirectory;

var gitProcess = new GitProcess(process);

if (info.TimeoutMs > 0)
gitProcess.SetTimeout(info.TimeoutMs);

return gitProcess;
}

public GitCommandService() : this(CreateGitProcess) {}

public GitCommandService(CreateProcessDelegate createDelegate)
{
_createProcessDelegate = createDelegate;
}

[CanBeNull]
public async Task<GitProcessResult> Run(GitCommandInfo info)
Expand All @@ -56,7 +81,7 @@ public async Task<GitProcessResult> Run(GitCommandInfo info)
new ProgressOptions { Sticky = true }
);

_currentProcess = CreateGitProcess(info);
_currentProcess = _createProcessDelegate(info);

try
{
Expand All @@ -76,20 +101,11 @@ public async Task<GitProcessResult> Run(GitCommandInfo info)

return result;
}

private static GitProcess CreateGitProcess(GitCommandInfo info)

/// <summary>Used for unit testing.</summary>
internal void SetIsRunning(bool isRunning)
{
var process = new Process();
process.StartInfo.FileName = "git";
process.StartInfo.Arguments = info.Arguments;
process.StartInfo.WorkingDirectory = info.Repository.Info.WorkingDirectory;

var gitProcess = new GitProcess(process);

if (info.TimeoutMs > 0)
gitProcess.SetTimeout(info.TimeoutMs);

return gitProcess;
IsRunning = isRunning;
}

private void FinishProcessProgress(GitProcessResult result)
Expand All @@ -107,7 +123,7 @@ private void FinishProcessProgress(GitProcessResult result)
else if (result.Timeout)
errorMessage = "Process timed out";
else
errorMessage = $"Failed with exit code {result.ExitCode}";
errorMessage = $"Process ended with exit code {result.ExitCode}";

_progressService.FinishWithError(_progressId, errorMessage);
}
Expand Down
2 changes: 2 additions & 0 deletions Core/Services/ProgressService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEditor;
using UnityEngine.TestTools;

namespace UnityGit.Core.Services
{
Expand All @@ -18,6 +19,7 @@ public interface IProgressService
}

/// <summary>Acts as a wrapper for Unity's Progress API.</summary>
[ExcludeFromCoverage]
public sealed class ProgressService : IProgressService
{
public int Start(string name, string description, ProgressOptions options)
Expand Down
6 changes: 5 additions & 1 deletion Core/Services/PullService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface IPullService

[Dependency(typeof(ICredentialsService), provide: typeof(CredentialsService))]
[Dependency(typeof(ISignatureService), provide: typeof(SignatureService))]
[Dependency(typeof(ICommandsService), provide: typeof(CommandsService))]
public sealed partial class PullService : Service, IPullService
{
[Provide]
Expand All @@ -18,6 +19,9 @@ public sealed partial class PullService : Service, IPullService
[Provide]
private ISignatureService _signatureService;

[Provide]
private ICommandsService _commandsService;

public MergeResult Pull(Repository repository)
{
var fetchOptions = new FetchOptions();
Expand All @@ -33,7 +37,7 @@ public MergeResult Pull(Repository repository)
FetchOptions = fetchOptions
};

return Commands.Pull(repository, _signatureService.GetSignature(), pullOptions);
return _commandsService.Pull(repository, _signatureService.GetSignature(), pullOptions);
}
}
}
27 changes: 20 additions & 7 deletions Core/Services/RepositoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace UnityGit.Core.Services
{
public interface IRepositoryService
{
IRepository GetRepository(string directoryPath);

IRepository GetProjectRepository();

List<IRepository> GetPackageRepositories();
Expand All @@ -20,32 +22,38 @@ public interface IRepositoryService
string GetRepositoryName(IRepository repository);

bool AreRepositoriesEqual(IRepository repositoryOne, IRepository repositoryTwo);

bool IsValid(IRepository repository);
}

public sealed class RepositoryService : IRepositoryService
{
[CanBeNull]
public IRepository GetProjectRepository()
public IRepository GetRepository(string repositoryPath)
{
var projectFullPath = Directory.GetCurrentDirectory();
var gitRepositoryPath = Path.Combine(projectFullPath, ".git");

if (!Directory.Exists(gitRepositoryPath))
if (!Directory.Exists(repositoryPath))
return null;

try
{
if (!Repository.IsValid(gitRepositoryPath))
if (!Repository.IsValid(repositoryPath))
return null;
}
catch
{
return null;
}

return new Repository(gitRepositoryPath);
return new Repository(repositoryPath);
}

[CanBeNull]
public IRepository GetProjectRepository()
{
var gitRepositoryPath = Path.Combine(Directory.GetCurrentDirectory(), ".git");
return GetRepository(gitRepositoryPath);
}

public List<IRepository> GetPackageRepositories()
{
var repositories = new List<IRepository>();
Expand Down Expand Up @@ -109,5 +117,10 @@ public bool AreRepositoriesEqual(IRepository repositoryOne, IRepository reposito

return repositoryOne.Info.Path.Equals(repositoryTwo.Info.Path);
}

public bool IsValid(IRepository repository)
{
return Repository.IsValid(repository.Info.Path);
}
}
}
28 changes: 17 additions & 11 deletions Core/Services/RestoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public interface IRestoreService

event FileRestoredDelegate FileRestored;

void RestoreFile(IRepository repository, string filePath);
bool RestoreFile(IRepository repository, string filePath);
}

[Dependency(typeof(IDialogService), provide: typeof(DialogService))]
[Dependency(typeof(ICheckoutService), provide: typeof(CheckoutService))]
[Dependency(typeof(ILogService), provide: typeof(UnityGitLogService))]
[Dependency(typeof(IFileService), provide: typeof(FileService))]
public sealed partial class RestoreService : Service, IRestoreService
{
public event IRestoreService.FileRestoredDelegate FileRestored;
Expand All @@ -28,29 +29,33 @@ public sealed partial class RestoreService : Service, IRestoreService
private ICheckoutService _checkoutService;
[Provide]
private ILogService _logService;
[Provide]
private IFileService _fileService;

public void RestoreFile(IRepository repository, string filePath)
public bool RestoreFile(IRepository repository, string filePath)
{
var status = repository.RetrieveStatus(filePath);

if (status == FileStatus.Nonexistent)
return;
return false;

if (FileStatusUtilities.IsNew(status))
{
if (!TryDeleteFile(repository, filePath))
return;
return false;
}
else
{
if (!TryRestoreFile(repository, filePath))
return;
return false;
}

FileRestored?.Invoke(repository, filePath);

return true;
}

private bool TryDeleteFile(IRepository repository, string filePath)
public bool TryDeleteFile(IRepository repository, string filePath)
{
if (!_dialogService.Confirm($"Are you sure you want to delete {filePath}?"))
return false;
Expand All @@ -62,26 +67,27 @@ private bool TryDeleteFile(IRepository repository, string filePath)

try
{
File.Delete(path);
_fileService.Delete(path);
success = true;
_logService.LogMessage("File deleted.");
} catch (Exception exception)
{
_dialogService.Error($"Failed to delete {filePath}.");
_dialogService.Error($"Failed to delete {path}.");
_logService.LogException(exception);
}

return success;
}

private bool TryRestoreFile(IRepository repository, string filePath)
public bool TryRestoreFile(IRepository repository, string filePath)
{
if (!_dialogService.Confirm($"Are you sure you want to restore {filePath}?"))
return false;

var success = false;

_logService.LogMessage($"Restoring file {filePath}...");
var path = Path.Combine(repository.Info.WorkingDirectory, filePath);
_logService.LogMessage($"Restoring file {path}...");

try
{
Expand All @@ -91,7 +97,7 @@ private bool TryRestoreFile(IRepository repository, string filePath)
}
catch (Exception exception)
{
_dialogService.Error($"Failed to restore file {filePath}.");
_dialogService.Error($"Failed to restore {path}.");
_logService.LogException(exception);
}

Expand Down
Loading

0 comments on commit 73ce729

Please sign in to comment.