forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitToolsTestingExtensions.cs
159 lines (133 loc) · 6.09 KB
/
GitToolsTestingExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using GitTools.Testing;
using GitVersion.BuildAgents;
using GitVersion.Configuration;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Model.Configuration;
using GitVersion.OutputVariables;
using GitVersion.VersionCalculation;
using LibGit2Sharp;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NSubstitute;
using Shouldly;
namespace GitVersion.Core.Tests;
public static class GitToolsTestingExtensions
{
private static int commitCount = 1;
private static readonly DateTimeOffset when = DateTimeOffset.Now;
public static ICommit CreateMockCommit()
{
var objectId = Substitute.For<IObjectId>();
objectId.Sha.Returns(Guid.NewGuid().ToString("n") + "00000000");
var commit = Substitute.For<ICommit>();
commit.Id.Returns(objectId);
commit.Sha.Returns(objectId.Sha);
commit.Message.Returns("Commit " + commitCount++);
commit.Parents.Returns(Enumerable.Empty<ICommit>());
commit.When.Returns(when.AddSeconds(1));
return commit;
}
public static IBranch CreateMockBranch(string name, params ICommit[] commits)
{
var branch = Substitute.For<IBranch>();
branch.Name.Returns(new ReferenceName(name));
branch.IsTracking.Returns(true);
branch.IsRemote.Returns(false);
branch.IsDetachedHead.Returns(false);
branch.Tip.Returns(commits.FirstOrDefault());
var commitsCollection = Substitute.For<ICommitCollection>();
commitsCollection.GetEnumerator().Returns(_ => ((IEnumerable<ICommit>)commits).GetEnumerator());
commitsCollection.GetCommitsPriorTo(Arg.Any<DateTimeOffset>()).Returns(commits);
branch.Commits.Returns(commitsCollection);
return branch;
}
public static IBranch? FindBranch(this IGitRepository repository, string branchName) => repository.Branches.FirstOrDefault(x => x.Name.WithoutRemote == branchName);
public static void DumpGraph(this IGitRepository repository, Action<string>? writer = null, int? maxCommits = null) => GitExtensions.DumpGraph(repository.Path, writer, maxCommits);
public static void DumpGraph(this IRepository repository, Action<string>? writer = null, int? maxCommits = null) => GitExtensions.DumpGraph(repository.ToGitRepository().Path, writer, maxCommits);
public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config? configuration = null, IRepository? repository = null, string? commitId = null, bool onlyTrackedBranches = true, string? branch = null)
{
configuration ??= new ConfigurationBuilder().Build();
repository ??= fixture.Repository;
var options = Options.Create(new GitVersionOptions
{
WorkingDirectory = repository.Info.WorkingDirectory,
ConfigInfo = { OverrideConfig = configuration },
RepositoryInfo =
{
TargetBranch = branch,
CommitId = commitId
},
Settings = { OnlyTrackedBranches = onlyTrackedBranches }
});
var sp = ConfigureServices(services => services.AddSingleton(options));
var variableProvider = sp.GetRequiredService<IVariableProvider>();
var nextVersionCalculator = sp.GetRequiredService<INextVersionCalculator>();
var contextOptions = sp.GetRequiredService<Lazy<GitVersionContext>>();
var context = contextOptions.Value;
try
{
var nextVersion = nextVersionCalculator.FindVersion();
var variables = variableProvider.GetVariablesFor(nextVersion.IncrementedVersion, nextVersion.Configuration, context.IsCurrentCommitTagged);
return variables;
}
catch (Exception)
{
Console.WriteLine("Test failing, dumping repository graph");
repository.DumpGraph();
throw;
}
}
public static void WriteVersionVariables(this RepositoryFixtureBase fixture, string versionFile)
{
var versionInfo = fixture.GetVersion();
using var stream = File.Open(versionFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
using var writer = new StreamWriter(stream);
writer.Write(versionInfo.ToString());
}
public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver, Config? configuration = null, IRepository? repository = null, string? commitId = null, bool onlyTrackedBranches = true, string? targetBranch = null)
{
Console.WriteLine("---------");
try
{
var variables = fixture.GetVersion(configuration, repository, commitId, onlyTrackedBranches, targetBranch);
variables.FullSemVer.ShouldBe(fullSemver);
}
catch (Exception)
{
(repository ?? fixture.Repository).DumpGraph();
throw;
}
if (commitId == null)
{
fixture.SequenceDiagram.NoteOver(fullSemver, fixture.Repository.Head.FriendlyName, color: "#D3D3D3");
}
}
/// <summary>
/// Simulates running on build server
/// </summary>
public static void InitializeRepo(this RemoteRepositoryFixture fixture)
{
var gitVersionOptions = new GitVersionOptions
{
WorkingDirectory = fixture.LocalRepositoryFixture.RepositoryPath
};
var options = Options.Create(gitVersionOptions);
var environment = new TestEnvironment();
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
var serviceProvider = ConfigureServices(services =>
{
services.AddSingleton(options);
services.AddSingleton(environment);
});
var gitPreparer = serviceProvider.GetRequiredService<IGitPreparer>();
gitPreparer.Prepare();
}
private static IServiceProvider ConfigureServices(Action<IServiceCollection>? servicesOverrides = null)
{
var services = new ServiceCollection()
.AddModule(new GitVersionCoreTestModule());
servicesOverrides?.Invoke(services);
return services.BuildServiceProvider();
}
}