public void Finds_GitDirectory() { try { executeCore.ExecuteGitVersion(null, null, null, null, true, workDirectory, null); } catch (Exception ex) { // `RepositoryNotFoundException` means that it couldn't find the .git directory, // any other exception means that the .git was found but there was some other issue that this test doesn't care about. Assert.IsNotAssignableFrom <RepositoryNotFoundException>(ex); } }
public void ConfigChangeInvalidatesCache() { const string versionCacheFileContent = @" Major: 4 Minor: 10 Patch: 3 PreReleaseTag: test.19 PreReleaseTagWithDash: -test.19 PreReleaseLabel: test PreReleaseNumber: 19 WeightedPreReleaseNumber: 19 BuildMetaData: BuildMetaDataPadded: FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f MajorMinorPatch: 4.10.3 SemVer: 4.10.3-test.19 LegacySemVer: 4.10.3-test19 LegacySemVerPadded: 4.10.3-test0019 AssemblySemVer: 4.10.3.0 AssemblySemFileVer: 4.10.3.0 FullSemVer: 4.10.3-test.19 InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f BranchName: feature/test Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f ShortSha: dd2a29af NuGetVersionV2: 4.10.3-test0019 NuGetVersion: 4.10.3-test0019 NuGetPreReleaseTagV2: test0019 NuGetPreReleaseTag: test0019 VersionSourceSha: 4.10.2 CommitsSinceVersionSource: 19 CommitsSinceVersionSourcePadded: 0019 CommitDate: 2015-11-10 "; var versionAndBranchFinder = new ExecuteCore(fileSystem); RepositoryScope(versionAndBranchFinder, (fixture, vv) => { fileSystem.WriteAllText(vv.FileName, versionCacheFileContent); vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null); vv.AssemblySemVer.ShouldBe("4.10.3.0"); var configPath = Path.Combine(fixture.RepositoryPath, "GitVersionConfig.yaml"); fileSystem.WriteAllText(configPath, "next-version: 5.0"); vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null); vv.AssemblySemVer.ShouldBe("5.0.0.0"); }); }
string RepositoryScope(ExecuteCore executeCore = null, Action <EmptyRepositoryFixture, VersionVariables> fixtureAction = null) { // Make sure GitVersion doesn't trigger build server mode when we are running the tests Environment.SetEnvironmentVariable("APPVEYOR", null); var infoBuilder = new StringBuilder(); Action <string> infoLogger = s => { infoBuilder.AppendLine(s); Console.WriteLine(s); }; executeCore = executeCore ?? new ExecuteCore(fileSystem); Logger.SetLoggers(infoLogger, Console.WriteLine, Console.WriteLine); using (var fixture = new EmptyRepositoryFixture()) { fixture.Repository.MakeACommit(); var vv = executeCore.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null); vv.AssemblySemVer.ShouldBe("0.1.0.0"); vv.FileName.ShouldNotBeNullOrEmpty(); if (fixtureAction != null) { fixtureAction(fixture, vv); } } return(infoBuilder.ToString()); }
public static VersionVariables GetVersion(string directory, Authentication authentication, bool noFetch, IFileSystem fileSystem) { var gitDir = GitDirFinder.TreeWalkForDotGitDir(directory); using (var repo = RepositoryLoader.GetRepo(gitDir)) { var ticks = DirectoryDateFinder.GetLastDirectoryWrite(directory); var key = string.Format("{0}:{1}:{2}", repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks); CachedVersion result; if (versionCacheVersions.TryGetValue(key, out result)) { if (result.Timestamp != ticks) { Logger.WriteInfo("Change detected. flushing cache."); result.VersionVariables = ExecuteCore.ExecuteGitVersion(fileSystem, null, null, authentication, null, noFetch, directory, null); } return(result.VersionVariables); } Logger.WriteInfo("Version not in cache. Calculating version."); return((versionCacheVersions[key] = new CachedVersion { VersionVariables = ExecuteCore.ExecuteGitVersion(fileSystem, null, null, authentication, null, noFetch, directory, null), Timestamp = ticks }).VersionVariables); } }
public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory() { var versionAndBranchFinder = new ExecuteCore(fileSystem); RepositoryScope(versionAndBranchFinder, (fixture, vv) => { versionAndBranchFinder.ExecuteGitVersion("https://github.com/GitTools/GitVersion.git", null, new Authentication(), "refs/head/master", false, fixture.RepositoryPath, null); }); }
public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDynamicallyCalculatedWithoutSavingInCache() { const string versionCacheFileContent = @" Major: 4 Minor: 10 Patch: 3 PreReleaseTag: test.19 PreReleaseTagWithDash: -test.19 PreReleaseLabel: test PreReleaseNumber: 19 BuildMetaData: BuildMetaDataPadded: FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f MajorMinorPatch: 4.10.3 SemVer: 4.10.3-test.19 LegacySemVer: 4.10.3-test19 LegacySemVerPadded: 4.10.3-test0019 AssemblySemVer: 4.10.3.0 AssemblySemFileVer: 4.10.3.0 FullSemVer: 4.10.3-test.19 InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f BranchName: feature/test Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f ShortSha: dd2a29af NuGetVersionV2: 4.10.3-test0019 NuGetVersion: 4.10.3-test0019 NuGetPreReleaseTagV2: test0019 NuGetPreReleaseTag: test0019 CommitsSinceVersionSource: 19 CommitsSinceVersionSourcePadded: 0019 CommitDate: 2015-11-10 "; var versionAndBranchFinder = new ExecuteCore(fileSystem); RepositoryScope(versionAndBranchFinder, (fixture, vv) => { fileSystem.WriteAllText(vv.FileName, versionCacheFileContent); var gitPreparer = new GitPreparer(null, null, null, false, fixture.RepositoryPath); var cacheDirectory = GitVersionCache.GetCacheDirectory(gitPreparer); var cacheDirectoryTimestamp = fileSystem.GetLastDirectoryWrite(cacheDirectory); vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null, new Config() { TagPrefix = "prefix" }); vv.AssemblySemVer.ShouldBe("0.1.0.0"); var cachedDirectoryTimestampAfter = fileSystem.GetLastDirectoryWrite(cacheDirectory); cachedDirectoryTimestampAfter.ShouldBe(cacheDirectoryTimestamp, () => "Cache was updated when override config was set"); }); // TODO info.ShouldContain("Override config from command line", () => info); }
public void WorkingDirectoryWithoutGit() { var versionAndBranchFinder = new ExecuteCore(fileSystem); RepositoryScope(versionAndBranchFinder, (fixture, vv) => { var exception = Assert.Throws <DirectoryNotFoundException>(() => versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, Environment.SystemDirectory, null)); exception.Message.ShouldContain("Can't find the .git directory in"); }); }
LogMessages RepositoryScope(ExecuteCore executeCore = null, Action <EmptyRepositoryFixture, VersionVariables> fixtureAction = null) { // Make sure GitVersion doesn't trigger build server mode when we are running the tests Environment.SetEnvironmentVariable(AppVeyor.EnvironmentVariableName, null); Environment.SetEnvironmentVariable(TravisCI.EnvironmentVariableName, null); Environment.SetEnvironmentVariable(VsoAgent.EnvironmentVariableName, null); var debugBuilder = new StringBuilder(); Action <string> debugLogger = s => { debugBuilder.AppendLine(s); }; var infoBuilder = new StringBuilder(); Action <string> infoLogger = s => { infoBuilder.AppendLine(s); }; var warnBuilder = new StringBuilder(); Action <string> warnLogger = s => { warnBuilder.AppendLine(s); }; var errorBuilder = new StringBuilder(); Action <string> errorLogger = s => { errorBuilder.AppendLine(s); }; executeCore = executeCore ?? new ExecuteCore(fileSystem); using (Logger.AddLoggersTemporarily(debugLogger, infoLogger, warnLogger, errorLogger)) using (var fixture = new EmptyRepositoryFixture()) { fixture.Repository.MakeACommit(); var vv = executeCore.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null); vv.AssemblySemVer.ShouldBe("0.1.0.0"); vv.FileName.ShouldNotBeNullOrEmpty(); if (fixtureAction != null) { fixtureAction(fixture, vv); } } return(new LogMessages { Debug = debugBuilder.ToString(), Info = infoBuilder.ToString(), Warn = warnBuilder.ToString(), Error = errorBuilder.ToString() }); }
public void FindsVersionInDynamicRepo(string name, string url, string targetBranch, string commitId, string expectedFullSemVer) { var root = Path.Combine(workDirectory, name); 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); var executeCore = new ExecuteCore(new TestFileSystem(), new TestEnvironment(), new NullLog()); var versionVariables = executeCore.ExecuteGitVersion(url, dynamicDirectory, null, targetBranch, false, workingDirectory, commitId); Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer); }
public void CacheFileExistsOnDisk() { const string versionCacheFileContent = @" Major: 4 Minor: 10 Patch: 3 PreReleaseTag: test.19 PreReleaseTagWithDash: -test.19 PreReleaseLabel: test PreReleaseNumber: 19 WeightedPreReleaseNumber: 19 BuildMetaData: BuildMetaDataPadded: FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f MajorMinorPatch: 4.10.3 SemVer: 4.10.3-test.19 LegacySemVer: 4.10.3-test19 LegacySemVerPadded: 4.10.3-test0019 AssemblySemVer: 4.10.3.0 AssemblySemFileVer: 4.10.3.0 FullSemVer: 4.10.3-test.19 InformationalVersion: 4.10.3-test.19+Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f BranchName: feature/test Sha: dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f ShortSha: dd2a29af NuGetVersionV2: 4.10.3-test0019 NuGetVersion: 4.10.3-test0019 NuGetPreReleaseTagV2: test0019 NuGetPreReleaseTag: test0019 VersionSourceSha: 4.10.2 CommitsSinceVersionSource: 19 CommitsSinceVersionSourcePadded: 0019 CommitDate: 2015-11-10 "; var versionAndBranchFinder = new ExecuteCore(fileSystem, environment); var logs = RepositoryScope(versionAndBranchFinder, (fixture, vv) => { fileSystem.WriteAllText(vv.FileName, versionCacheFileContent); vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null); vv.AssemblySemVer.ShouldBe("4.10.3.0"); }); logs.Info.ShouldContain("Deserializing version variables from cache file", () => logs.Info); }
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); } Directory.CreateDirectory(dynamicDirectory); Directory.CreateDirectory(workingDirectory); var executeCore = new ExecuteCore(new TestFileSystem()); var versionVariables = executeCore.ExecuteGitVersion(url, dynamicDirectory, null, targetBranch, false, workingDirectory, commitId); Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer); }
public void FindsVersionInDynamicRepo(string name, string url, string targetBranch, string commitId, string expectedFullSemVer) { var root = Path.Combine(workDirectory, name); 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, false, workingDirectory, commitId); Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer); }