forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGitRepositoryTestingExtensions.cs
More file actions
194 lines (164 loc) · 7.69 KB
/
GitRepositoryTestingExtensions.cs
File metadata and controls
194 lines (164 loc) · 7.69 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using GitVersion.Agents;
using GitVersion.Configuration;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Git;
using GitVersion.Helpers;
using GitVersion.Logging;
using GitVersion.OutputVariables;
using GitVersion.VersionCalculation;
using LibGit2Sharp;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace GitVersion.Core.Tests;
public static class GitRepositoryTestingExtensions
{
private static int commitCount = 1;
private static readonly DateTimeOffset when = DateTimeOffset.Now;
public static ICommit CreateMockCommit()
{
var objectId = Substitute.For<IObjectId>();
var sha = Guid.NewGuid().ToString("n") + "00000000";
objectId.Sha.Returns(sha);
var commit = Substitute.For<ICommit>();
commit.Id.Returns(objectId);
commit.Sha.Returns(sha);
commit.Message.Returns("Commit " + commitCount++);
commit.Parents.Returns([]);
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.MockCollectionReturn(commits);
commitsCollection.GetCommitsPriorTo(Arg.Any<DateTimeOffset>()).Returns(commits);
branch.Commits.Returns(commitsCollection);
return branch;
}
public static void DiscoverRepository(this IServiceProvider sp)
{
var gitRepository = sp.GetRequiredService<IGitRepository>();
var gitRepositoryInfo = sp.GetRequiredService<IGitRepositoryInfo>();
gitRepository.DiscoverRepository(gitRepositoryInfo.GitRootPath);
}
public static IBranch FindBranch(this IGitRepository repository, string branchName)
=> repository.Branches.FirstOrDefault(branch => branch.Name.WithoutOrigin == branchName)
?? throw new GitVersionException($"Branch {branchName} not found");
public static void DumpGraph(this IGitRepository repository, Action<string>? writer = null, int? maxCommits = null)
=> DumpGraph(repository.Path, writer, maxCommits);
public static void DumpGraph(this IRepository repository, Action<string>? writer = null, int? maxCommits = null)
=> DumpGraph(repository.ToGitRepository().Path, writer, maxCommits);
public static void RenameRemote(this LibGit2Sharp.RemoteCollection remotes, string oldName, string newName)
{
if (oldName.IsEquivalentTo(newName)) return;
if (remotes.Any(remote => remote.Name == newName))
{
throw new InvalidOperationException($"A remote with the name '{newName}' already exists.");
}
if (!remotes.Any(remote => remote.Name == oldName))
{
throw new InvalidOperationException($"A remote with the name '{oldName}' does not exist.");
}
remotes.Add(newName, remotes[oldName].Url);
remotes.Remove(oldName);
}
public static GitVersionVariables GetVersion(this RepositoryFixtureBase fixture, IGitVersionConfiguration? configuration = null,
IRepository? repository = null, string? commitId = null, bool onlyTrackedBranches = true, string? targetBranch = null)
{
repository ??= fixture.Repository;
configuration ??= GitFlowConfigurationBuilder.New.Build();
var overrideConfiguration = new Dictionary<object, object?>();
var options = Options.Create(new GitVersionOptions
{
WorkingDirectory = repository.Info.WorkingDirectory,
ConfigurationInfo = { OverrideConfiguration = overrideConfiguration },
RepositoryInfo =
{
TargetBranch = targetBranch,
CommitId = commitId
},
Settings = { OnlyTrackedBranches = onlyTrackedBranches }
});
try
{
var configurationProviderMock = Substitute.For<IConfigurationProvider>();
configurationProviderMock.Provide(overrideConfiguration).Returns(configuration);
var sp = ConfigureServices(services =>
{
services.AddSingleton(options);
services.AddSingleton(configurationProviderMock);
});
sp.DiscoverRepository();
var variableProvider = sp.GetRequiredService<IVariableProvider>();
var nextVersionCalculator = sp.GetRequiredService<INextVersionCalculator>();
var contextOptions = sp.GetRequiredService<Lazy<GitVersionContext>>();
var context = contextOptions.Value;
var semanticVersion = nextVersionCalculator.FindVersion();
var effectiveConfiguration = context.Configuration.GetEffectiveConfiguration(context.CurrentBranch.Name);
return variableProvider.GetVariablesFor(semanticVersion, context.Configuration, effectiveConfiguration.PreReleaseWeight);
}
catch (Exception)
{
repository.DumpGraph();
throw;
}
}
public static void WriteVersionVariables(this RepositoryFixtureBase fixture, string versionFile)
{
var versionVariables = fixture.GetVersion();
FileSystemHelper.File.WriteAllText(versionFile, versionVariables.ToJson());
}
public static void AssertFullSemver(this RepositoryFixtureBase fixture, string fullSemver,
IGitVersionConfiguration? configuration = null, IRepository? repository = null, string? commitId = null, bool onlyTrackedBranches = true, string? targetBranch = null)
{
repository ??= fixture.Repository;
var variables = GetVersion(fixture, configuration, repository, commitId, onlyTrackedBranches, targetBranch);
variables.FullSemVer.ShouldBe(fullSemver);
if (commitId == null)
{
fixture.SequenceDiagram.NoteOver(fullSemver, repository.Head.FriendlyName, color: "#D3D3D3");
}
}
/// <summary>
/// Simulates running on build server
/// </summary>
public static void InitializeRepository(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();
}
internal static IGitRepository ToGitRepository(this IRepository repository)
{
var gitRepository = new GitRepository(new NullLog());
gitRepository.DiscoverRepository(repository.Info.Path);
return gitRepository;
}
private static ServiceProvider ConfigureServices(Action<IServiceCollection>? servicesOverrides = null)
{
var services = new ServiceCollection()
.AddModule(new GitVersionCoreTestModule());
servicesOverrides?.Invoke(services);
return services.BuildServiceProvider();
}
private static void DumpGraph(string workingDirectory, Action<string>? writer = null, int? maxCommits = null)
=> GitTestExtensions.ExecuteGitCmd(GitExtensions.CreateGitLogArgs(maxCommits), workingDirectory, writer);
}