test: Unit test fixes for GitRepository

GitRepository now lazily-constructs its internal Repository object. This
makes the class testable since it no longer does a lot of work in its
constructor. Previously, the string passed to it had to be valid for the
Repository object to accept it.
pull/47/head
Robert Dailey 2 years ago
parent 258ac508d5
commit 2d21ffeb38

@ -1,16 +1,35 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Autofac;
using Autofac.Core;
using FluentAssertions;
using NUnit.Framework;
using VersionControl;
namespace Trash.Tests;
public record ServiceFactoryWrapper(Type Service, Action<ILifetimeScope> Instantiate);
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class CompositionRootTest
{
private static class FactoryForService<TService>
{
public static ServiceFactoryWrapper WithArgs<TP1>(TP1 arg1 = default!)
{
return new ServiceFactoryWrapper(typeof(TService),
c => c.Resolve<Func<TP1, TService>>().Invoke(arg1));
}
}
private static readonly List<ServiceFactoryWrapper> FactoryTests = new()
{
FactoryForService<IGitRepository>.WithArgs("path")
};
private sealed class ConcreteTypeEnumerator : IEnumerable
{
private readonly IContainer _container;
@ -25,12 +44,25 @@ public class CompositionRootTest
return _container.ComponentRegistry.Registrations
.SelectMany(x => x.Services)
.OfType<TypedService>()
.Where(x => FactoryTests.All(y => y.Service != x.ServiceType))
.GetEnumerator();
}
}
[TestCaseSource(typeof(CompositionRootTest), nameof(FactoryTests))]
public void Service_requiring_factory_should_be_instantiable(ServiceFactoryWrapper service)
{
var act = () =>
{
using var container = CompositionRoot.Setup();
service.Instantiate(container);
};
act.Should().NotThrow();
}
[TestCaseSource(typeof(ConcreteTypeEnumerator))]
public void Resolve_ICommandConcreteClasses(Service service)
public void Service_should_be_instantiable(Service service)
{
using var container = CompositionRoot.Setup();
container.Invoking(c => c.ResolveService(service))

@ -1,3 +1,4 @@
using System;
using System.Linq;
using LibGit2Sharp;
@ -5,21 +6,26 @@ namespace VersionControl;
public sealed class GitRepository : IGitRepository
{
private readonly Repository _repo;
private readonly Lazy<Repository> _repo;
public GitRepository(string repoPath)
{
_repo = new Repository(repoPath);
// Lazily construct the Repository object because it does too much work in its constructor
// We want to keep our own constructor here as thin as possible for DI and testability.
_repo = new Lazy<Repository>(() => new Repository(repoPath));
}
public void Dispose()
{
_repo.Dispose();
if (_repo.IsValueCreated)
{
_repo.Value.Dispose();
}
}
public void ForceCheckout(string branch)
{
Commands.Checkout(_repo, branch, new CheckoutOptions
Commands.Checkout(_repo.Value, branch, new CheckoutOptions
{
CheckoutModifiers = CheckoutModifiers.Force
});
@ -27,13 +33,13 @@ public sealed class GitRepository : IGitRepository
public void Fetch(string remote = "origin")
{
var origin = _repo.Network.Remotes[remote];
Commands.Fetch(_repo, origin.Name, origin.FetchRefSpecs.Select(s => s.Specification), null, "");
var origin = _repo.Value.Network.Remotes[remote];
Commands.Fetch(_repo.Value, origin.Name, origin.FetchRefSpecs.Select(s => s.Specification), null, "");
}
public void ResetHard(string toBranch)
{
var commit = _repo.Branches[toBranch].Tip;
_repo.Reset(ResetMode.Hard, commit);
var commit = _repo.Value.Branches[toBranch].Tip;
_repo.Value.Reset(ResetMode.Hard, commit);
}
}

Loading…
Cancel
Save