Skip to content

Commit 7821e23

Browse files
committed
cleanup GitPreparer
1 parent 5b21b1c commit 7821e23

File tree

13 files changed

+284
-244
lines changed

13 files changed

+284
-244
lines changed

src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text;
44
using GitTools.Testing;
55
using GitVersion;
6+
using GitVersion.BuildAgents;
67
using GitVersion.Configuration;
78
using GitVersion.Logging;
89
using GitVersion.Model.Configuration;
@@ -36,18 +37,23 @@ public void CacheKeySameAfterReNormalizing()
3637

3738
var gitVersionOptions = new GitVersionOptions
3839
{
39-
RepositoryInfo = { TargetUrl = targetUrl },
40-
WorkingDirectory = fixture.RepositoryPath
40+
RepositoryInfo = { TargetUrl = targetUrl, TargetBranch = targetBranch },
41+
WorkingDirectory = fixture.RepositoryPath,
42+
Settings = { NoNormalize = false }
4143
};
4244

43-
sp = GetServiceProvider(gitVersionOptions);
45+
var environment = new TestEnvironment();
46+
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
4447

45-
var preparer = sp.GetService<IGitPreparer>() as GitPreparer;
48+
sp = GetServiceProvider(gitVersionOptions, environment:environment);
4649

47-
preparer?.PrepareInternal(true, targetBranch);
50+
var preparer = sp.GetService<IGitPreparer>();
51+
52+
preparer.Prepare();
4853
var cacheKeyFactory = sp.GetService<IGitVersionCacheKeyFactory>();
4954
var cacheKey1 = cacheKeyFactory.Create(null);
50-
preparer?.PrepareInternal(true, targetBranch);
55+
preparer.Prepare();
56+
5157
var cacheKey2 = cacheKeyFactory.Create(null);
5258

5359
cacheKey2.Value.ShouldBe(cacheKey1.Value);
@@ -89,11 +95,14 @@ public void CacheKeyForWorktree()
8995

9096
var gitVersionOptions = new GitVersionOptions
9197
{
92-
RepositoryInfo = { TargetUrl = targetUrl },
98+
RepositoryInfo = { TargetUrl = targetUrl, TargetBranch = "master"},
9399
WorkingDirectory = worktreePath
94100
};
95101

96102
sp = GetServiceProvider(gitVersionOptions);
103+
104+
var preparer = sp.GetService<IGitPreparer>();
105+
preparer.Prepare();
97106
var cacheKey = sp.GetService<IGitVersionCacheKeyFactory>().Create(null);
98107
cacheKey.Value.ShouldNotBeEmpty();
99108
}
@@ -460,11 +469,9 @@ public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
460469
public void GetDotGitDirectoryNoWorktree()
461470
{
462471
using var fixture = new EmptyRepositoryFixture();
463-
var targetUrl = "https://github.com/GitTools/GitVersion.git";
464472

465473
var gitVersionOptions = new GitVersionOptions
466474
{
467-
RepositoryInfo = { TargetUrl = targetUrl },
468475
WorkingDirectory = fixture.RepositoryPath
469476
};
470477

@@ -489,11 +496,8 @@ public void GetDotGitDirectoryWorktree()
489496
var repo = new Repository(fixture.RepositoryPath);
490497
repo.Worktrees.Add("worktree", worktreePath, false);
491498

492-
var targetUrl = "https://github.com/GitTools/GitVersion.git";
493-
494499
var gitVersionOptions = new GitVersionOptions
495500
{
496-
RepositoryInfo = { TargetUrl = targetUrl },
497501
WorkingDirectory = worktreePath
498502
};
499503

@@ -520,13 +524,14 @@ private IGitVersionTool GetGitVersionCalculator(GitVersionOptions gitVersionOpti
520524
return sp.GetService<IGitVersionTool>();
521525
}
522526

523-
private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog log = null, IRepository repository = null, IFileSystem fileSystem = null)
527+
private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog log = null, IRepository repository = null, IFileSystem fileSystem = null, IEnvironment environment = null)
524528
{
525529
return ConfigureServices(services =>
526530
{
527531
if (log != null) services.AddSingleton(log);
528532
if (fileSystem != null) services.AddSingleton(fileSystem);
529533
if (repository != null) services.AddSingleton(repository);
534+
if (environment != null) services.AddSingleton(environment);
530535
services.AddSingleton(Options.Create(gitVersionOptions));
531536
});
532537
}

src/GitVersionCore.Tests/Extensions/GitToolsTestingExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using GitTools.Testing;
33
using GitVersion;
4+
using GitVersion.BuildAgents;
45
using GitVersion.Configuration;
56
using GitVersion.Extensions;
67
using GitVersion.Model.Configuration;
@@ -97,13 +98,17 @@ public static void InitializeRepo(this RemoteRepositoryFixture fixture)
9798
};
9899
var options = Options.Create(gitVersionOptions);
99100

101+
var environment = new TestEnvironment();
102+
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
103+
100104
var serviceProvider = ConfigureServices(services =>
101105
{
102106
services.AddSingleton(options);
107+
services.AddSingleton(environment);
103108
});
104109

105-
var gitPreparer = serviceProvider.GetService<IGitPreparer>() as GitPreparer;
106-
gitPreparer?.PrepareInternal(true, null);
110+
var gitPreparer = serviceProvider.GetService<IGitPreparer>();
111+
gitPreparer.Prepare();
107112
}
108113

109114
private static IServiceProvider ConfigureServices(Action<IServiceCollection> servicesOverrides = null)

src/GitVersionCore.Tests/IntegrationTests/RemoteRepositoryScenarios.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
22
using GitTools.Testing;
33
using GitVersion;
4-
using GitVersion.Helpers;
5-
using GitVersion.Logging;
4+
using GitVersion.BuildAgents;
65
using GitVersionCore.Tests.Helpers;
76
using LibGit2Sharp;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Options;
89
using NUnit.Framework;
910
using Shouldly;
1011

@@ -24,6 +25,7 @@ public void GivenARemoteGitRepositoryWithCommitsThenClonedLocalShouldMatchRemote
2425
[Test]
2526
public void GivenARemoteGitRepositoryWithCommitsAndBranchesThenClonedLocalShouldMatchRemoteVersion()
2627
{
28+
var targetBranch = "release-1.0";
2729
using var fixture = new RemoteRepositoryFixture(
2830
path =>
2931
{
@@ -34,14 +36,41 @@ public void GivenARemoteGitRepositoryWithCommitsAndBranchesThenClonedLocalShould
3436
repo.MakeCommits(5);
3537

3638
repo.CreateBranch("develop");
37-
repo.CreateBranch("release-1.0");
39+
repo.CreateBranch(targetBranch);
3840

39-
Commands.Checkout(repo, "release-1.0");
41+
Commands.Checkout(repo, targetBranch);
4042
repo.MakeCommits(5);
4143

4244
return repo;
4345
});
44-
GitRepositoryHelper.NormalizeGitDirectory(new NullLog(), new TestEnvironment(), fixture.LocalRepositoryFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty, isDynamicRepository: true);
46+
47+
var gitVersionOptions = new GitVersionOptions
48+
{
49+
WorkingDirectory = fixture.LocalRepositoryFixture.RepositoryPath,
50+
RepositoryInfo =
51+
{
52+
TargetBranch = targetBranch
53+
},
54+
55+
Settings =
56+
{
57+
NoNormalize = false,
58+
NoFetch = false
59+
}
60+
};
61+
var options = Options.Create(gitVersionOptions);
62+
var environment = new TestEnvironment();
63+
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
64+
65+
var sp = ConfigureServices(services =>
66+
{
67+
services.AddSingleton(options);
68+
services.AddSingleton<IEnvironment>(environment);
69+
});
70+
71+
var gitPreparer = sp.GetService<IGitPreparer>();
72+
73+
gitPreparer.Prepare();
4574

4675
fixture.AssertFullSemver("1.0.0-beta.1+5");
4776
fixture.AssertFullSemver("1.0.0-beta.1+5", repository: fixture.LocalRepositoryFixture.Repository);

src/GitVersionCore/Core/BuildAgentResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public BuildAgentResolver(IEnumerable<IBuildAgent> buildAgents, ILog log)
1717
public ICurrentBuildAgent Resolve()
1818
{
1919
ICurrentBuildAgent instance = null;
20-
foreach (IBuildAgent buildAgent in buildAgents)
20+
foreach (var buildAgent in buildAgents)
2121
{
2222
var agentName = buildAgent.GetType().Name;
2323
try

0 commit comments

Comments
 (0)