diff --git a/src/GitVersionCore.Tests/DynamicRepositoryTests.cs b/src/GitVersionCore.Tests/DynamicRepositoryTests.cs index a8a2782ca8..76796cfae9 100644 --- a/src/GitVersionCore.Tests/DynamicRepositoryTests.cs +++ b/src/GitVersionCore.Tests/DynamicRepositoryTests.cs @@ -1,4 +1,5 @@ -using System.IO; +using System.Diagnostics; +using System.IO; using GitVersion; using GitVersionCore.Tests; using NUnit.Framework; @@ -9,38 +10,53 @@ public class DynamicRepositoryTests : TestBase string workDirectory; - [SetUp] + [OneTimeSetUp] public void CreateTemporaryRepository() { // Note: we can't use guid because paths will be too long - workDirectory = Path.Combine(Path.GetTempPath(), "DynRepoTests"); + workDirectory = Path.Combine(Path.GetTempPath(), "GV"); + + // Clean directory upfront, some build agents are having troubles + if (Directory.Exists(workDirectory)) + { + Directory.Delete(workDirectory, true); + } + + Directory.CreateDirectory(workDirectory); } - //[TearDown] - //public void Cleanup() - //{ - // Directory.Delete(workDirectory, true); - //} + [OneTimeTearDown] + public void Cleanup() + { + } + + // Note: use same name twice to see if changing commits works on same (cached) repository [Ignore("These tests are slow and fail on the second run in Test Explorer and need to be re-written")] - [TestCase("GV_master_1", "https://github.com/GitTools/GitVersion", "master", "4783d325521463cd6cf1b61074352da84451f25d", "4.0.0+1126")] - [TestCase("GV_master_2", "https://github.com/GitTools/GitVersion", "master", "3bdcd899530b4e9b37d13639f317da04a749e728", "4.0.0+1132")] + [NonParallelizable] + [TestCase("GV_master", "https://github.com/GitTools/GitVersion", "master", "4783d325521463cd6cf1b61074352da84451f25d", "4.0.0+1086")] + [TestCase("GV_master", "https://github.com/GitTools/GitVersion", "master", "3bdcd899530b4e9b37d13639f317da04a749e728", "4.0.0+1092")] + [TestCase("Ctl_develop", "https://github.com/Catel/Catel", "develop", "0e2b6c125a730d2fa5e24394ef64abe62c98e9e9", "5.12.0-alpha.188")] + [TestCase("Ctl_develop", "https://github.com/Catel/Catel", "develop", "71e71359f37581784e18c94e7a45eee72cbeeb30", "5.12.0-alpha.192")] + [TestCase("Ctl_master", "https://github.com/Catel/Catel", "master", "f5de8964c35180a5f8607f5954007d5828aa849f", "5.10.0")] + [TestCase("CtlA_develop", "https://github.com/Catel/Catel.Analyzers", "develop", "be0aa94642d6ff1df6209e2180a7fe0de9aab384", "0.1.0-alpha.21")] + [TestCase("CtlA_develop", "https://github.com/Catel/Catel.Analyzers", "develop", "0e2b6c125a730d2fa5e24394ef64abe62c98e9e9", "0.1.0-alpha.23")] public void FindsVersionInDynamicRepo(string name, string url, string targetBranch, string commitId, string expectedFullSemVer) { var root = Path.Combine(workDirectory, name); - var dynamicDirectory = Path.Combine(root, "dynamic"); - var workingDirectory = Path.Combine(root, "working"); - - // Clear upfront - if (Directory.Exists(root)) - { - Directory.Delete(root, true); - } + var dynamicDirectory = Path.Combine(root, "D"); // dynamic, keeping directory as short as possible + var workingDirectory = Path.Combine(root, "W"); // working, keeping directory as short as possible Directory.CreateDirectory(dynamicDirectory); Directory.CreateDirectory(workingDirectory); + Logger.AddLoggersTemporarily( + x => Debug.WriteLine($"[DEBUG] {x}"), + x => Debug.WriteLine($"[INFO] {x}"), + x => Debug.WriteLine($"[WARNING] {x}"), + x => Debug.WriteLine($"[ERROR] {x}")); + var executeCore = new ExecuteCore(new TestFileSystem()); var versionVariables = executeCore.ExecuteGitVersion(url, dynamicDirectory, null, targetBranch, @@ -48,4 +64,4 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer); } -} \ No newline at end of file +} diff --git a/src/GitVersionCore.Tests/IntegrationTests/RemoteRepositoryScenarios.cs b/src/GitVersionCore.Tests/IntegrationTests/RemoteRepositoryScenarios.cs index cf435a1a55..407e261cad 100644 --- a/src/GitVersionCore.Tests/IntegrationTests/RemoteRepositoryScenarios.cs +++ b/src/GitVersionCore.Tests/IntegrationTests/RemoteRepositoryScenarios.cs @@ -40,7 +40,7 @@ public void GivenARemoteGitRepositoryWithCommitsAndBranches_ThenClonedLocalShoul return repo; })) { - GitRepositoryHelper.NormalizeGitDirectory(fixture.LocalRepositoryFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty); + GitRepositoryHelper.NormalizeGitDirectory(fixture.LocalRepositoryFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty, true); fixture.AssertFullSemver("1.0.0-beta.1+5"); fixture.AssertFullSemver("1.0.0-beta.1+5", fixture.LocalRepositoryFixture.Repository); diff --git a/src/GitVersionCore/GitPreparer.cs b/src/GitVersionCore/GitPreparer.cs index cc2e6187ef..b9f9d52c97 100644 --- a/src/GitVersionCore/GitPreparer.cs +++ b/src/GitVersionCore/GitPreparer.cs @@ -20,12 +20,11 @@ public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentic { this.targetUrl = targetUrl; this.dynamicRepositoryLocation = dynamicRepositoryLocation; - this.authentication = authentication == null ? - null : + this.authentication = new AuthenticationInfo { - Username = authentication.Username, - Password = authentication.Password + Username = authentication?.Username, + Password = authentication?.Password }; this.noFetch = noFetch; this.targetPath = targetPath.TrimEnd('/', '\\'); @@ -51,7 +50,7 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch, bool sh { CleanupDuplicateOrigin(); } - GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch); + GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch, IsDynamicGitRepository); } } return; @@ -181,7 +180,7 @@ static string CreateDynamicRepository(string targetPath, AuthenticationInfo auth Logger.WriteInfo("Git repository already exists"); using (Logger.IndentLog($"Normalizing git directory for branch '{targetBranch}'")) { - GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch); + GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch, true); } return gitDirectory; @@ -192,7 +191,7 @@ static string CreateDynamicRepository(string targetPath, AuthenticationInfo auth using (Logger.IndentLog($"Normalizing git directory for branch '{targetBranch}'")) { // Normalize (download branches) before using the branch - GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch); + GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch, true); } return gitDirectory; diff --git a/src/GitVersionCore/Helpers/GitRepositoryHelper.cs b/src/GitVersionCore/Helpers/GitRepositoryHelper.cs index e5ee0cb386..923bd0f039 100644 --- a/src/GitVersionCore/Helpers/GitRepositoryHelper.cs +++ b/src/GitVersionCore/Helpers/GitRepositoryHelper.cs @@ -11,7 +11,8 @@ public static class GitRepositoryHelper /// Normalisation of a git directory turns all remote branches into local branches, turns pull request refs into a real branch and a few other things. This is designed to be run *only on the build server* which checks out repositories in different ways. /// It is not recommended to run normalisation against a local repository /// - public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo authentication, bool noFetch, string currentBranch) + public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo authentication, + bool noFetch, string currentBranch, bool isDynamicRepository) { using (var repo = new Repository(gitDirectory)) { @@ -41,7 +42,7 @@ public static void NormalizeGitDirectory(string gitDirectory, AuthenticationInfo // Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed // if this is a dynamic repository. But only allow this in case the branches are different (branch switch) if (expectedSha != repo.Head.Tip.Sha && - !expectedBranchName.IsBranch(currentBranch)) + (isDynamicRepository || !expectedBranchName.IsBranch(currentBranch))) { var newExpectedSha = repo.Head.Tip.Sha; var newExpectedBranchName = repo.Head.CanonicalName;