|
| 1 | +using System; |
| 2 | +using GitTools.Testing.Internal; |
| 3 | +using LibGit2Sharp; |
| 4 | + |
| 5 | +namespace GitTools.Testing |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Fixture abstracting a git repository |
| 9 | + /// </summary> |
| 10 | + public abstract class RepositoryFixtureBase : IDisposable |
| 11 | + { |
| 12 | + readonly SequenceDiagram _sequenceDiagram; |
| 13 | + |
| 14 | + protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder) |
| 15 | + : this(repoBuilder(PathHelper.GetTempPath())) |
| 16 | + { |
| 17 | + } |
| 18 | + |
| 19 | + protected RepositoryFixtureBase(IRepository repository) |
| 20 | + { |
| 21 | + _sequenceDiagram = new SequenceDiagram(); |
| 22 | + Repository = repository; |
| 23 | + Repository.Config.Set("user.name", "Test"); |
| 24 | + Repository.Config.Set("user.email", "[email protected]"); |
| 25 | + } |
| 26 | + |
| 27 | + public IRepository Repository { get; private set; } |
| 28 | + |
| 29 | + public string RepositoryPath |
| 30 | + { |
| 31 | + get { return Repository.Info.WorkingDirectory.TrimEnd('\\'); } |
| 32 | + } |
| 33 | + |
| 34 | + public SequenceDiagram SequenceDiagram |
| 35 | + { |
| 36 | + get { return _sequenceDiagram; } |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| 41 | + /// </summary> |
| 42 | + public virtual void Dispose() |
| 43 | + { |
| 44 | + Repository.Dispose(); |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + DirectoryHelper.DeleteDirectory(RepositoryPath); |
| 49 | + } |
| 50 | + catch (Exception e) |
| 51 | + { |
| 52 | + Console.WriteLine("Failed to clean up repository path at {0}. Received exception: {1}", RepositoryPath, |
| 53 | + e.Message); |
| 54 | + } |
| 55 | + |
| 56 | + _sequenceDiagram.End(); |
| 57 | + Console.WriteLine("**Visualisation of test:**"); |
| 58 | + Console.WriteLine(string.Empty); |
| 59 | + Console.WriteLine(_sequenceDiagram.GetDiagram()); |
| 60 | + } |
| 61 | + |
| 62 | + public void Checkout(string branch) |
| 63 | + { |
| 64 | + Commands.Checkout(Repository, branch); |
| 65 | + } |
| 66 | + |
| 67 | + public void MakeATaggedCommit(string tag) |
| 68 | + { |
| 69 | + MakeACommit(); |
| 70 | + ApplyTag(tag); |
| 71 | + } |
| 72 | + |
| 73 | + public void ApplyTag(string tag) |
| 74 | + { |
| 75 | + _sequenceDiagram.ApplyTag(tag, Repository.Head.FriendlyName); |
| 76 | + Repository.ApplyTag(tag); |
| 77 | + } |
| 78 | + |
| 79 | + public void BranchTo(string branchName, string @as = null) |
| 80 | + { |
| 81 | + _sequenceDiagram.BranchTo(branchName, Repository.Head.FriendlyName, @as); |
| 82 | + var branch = Repository.CreateBranch(branchName); |
| 83 | + Commands.Checkout(Repository, branch); |
| 84 | + } |
| 85 | + |
| 86 | + public void BranchToFromTag(string branchName, string fromTag, string onBranch, string @as = null) |
| 87 | + { |
| 88 | + _sequenceDiagram.BranchToFromTag(branchName, fromTag, onBranch, @as); |
| 89 | + var branch = Repository.CreateBranch(branchName); |
| 90 | + Commands.Checkout(Repository, branch); |
| 91 | + } |
| 92 | + |
| 93 | + public void MakeACommit() |
| 94 | + { |
| 95 | + var to = Repository.Head.FriendlyName; |
| 96 | + _sequenceDiagram.MakeACommit(to); |
| 97 | + Repository.MakeACommit(); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Merges (no-ff) specified branch into the current HEAD of this repository |
| 102 | + /// </summary> |
| 103 | + public void MergeNoFF(string mergeSource) |
| 104 | + { |
| 105 | + _sequenceDiagram.Merge(mergeSource, Repository.Head.FriendlyName); |
| 106 | + Repository.MergeNoFF(mergeSource, Generate.SignatureNow()); |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Clones the repository managed by this fixture into another LocalRepositoryFixture |
| 111 | + /// </summary> |
| 112 | + public LocalRepositoryFixture CloneRepository() |
| 113 | + { |
| 114 | + var localPath = PathHelper.GetTempPath(); |
| 115 | + LibGit2Sharp.Repository.Clone(RepositoryPath, localPath); |
| 116 | + return new LocalRepositoryFixture(new Repository(localPath)); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments