diff --git a/src/GitVersionCore.Tests/ExecuteCoreTests.cs b/src/GitVersionCore.Tests/ExecuteCoreTests.cs index 528c75cf82..ebd57cd70a 100644 --- a/src/GitVersionCore.Tests/ExecuteCoreTests.cs +++ b/src/GitVersionCore.Tests/ExecuteCoreTests.cs @@ -116,6 +116,19 @@ public void ConfigChangeInvalidatesCache() }); } + + [Test] + public void WorkingDirectoryWithoutGit() + { + var versionAndBranchFinder = new ExecuteCore(fileSystem); + + RepositoryScope(versionAndBranchFinder, (fixture, vv) => + { + var exception = Assert.Throws(() => versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, Environment.SystemDirectory, null)); + exception.Message.ShouldContain("Can't find the .git directory in"); + }); + } + string RepositoryScope(ExecuteCore executeCore = null, Action fixtureAction = null) { // Make sure GitVersion doesn't trigger build server mode when we are running the tests diff --git a/src/GitVersionCore/GitPreparer.cs b/src/GitVersionCore/GitPreparer.cs index a94b141209..5c9a7451f4 100644 --- a/src/GitVersionCore/GitPreparer.cs +++ b/src/GitVersionCore/GitPreparer.cs @@ -97,7 +97,12 @@ public string GetDotGitDirectory() if (IsDynamicGitRepository) return DynamicGitRepositoryPath; - var dotGitDirectory = Repository.Discover(targetPath).TrimEnd('/', '\\'); + var dotGitDirectory = Repository.Discover(targetPath); + + if (String.IsNullOrEmpty(dotGitDirectory)) + throw new DirectoryNotFoundException("Can't find the .git directory in " + targetPath); + + dotGitDirectory = dotGitDirectory.TrimEnd('/', '\\'); if (string.IsNullOrEmpty(dotGitDirectory)) throw new DirectoryNotFoundException("Can't find the .git directory in " + targetPath); diff --git a/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs b/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs index a212705166..2f49b92661 100644 --- a/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs +++ b/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using GitTools.Testing; using NUnit.Framework; @@ -65,4 +66,11 @@ public void InvalidWorkingDirectoryCrashesWithInformativeMessage() var results = GitVersionHelper.ExecuteIn("InvalidDirectory", null, isTeamCity : false, logToFile : false); results.Output.ShouldContain("InvalidDirectory"); } + + [Test] + public void WorkingDirectoryWithoutGitFolderCrashesWithInformativeMessage() + { + var results = GitVersionHelper.ExecuteIn(Environment.SystemDirectory, null, isTeamCity: false, logToFile: false); + results.Output.ShouldContain("Can't find the .git directory in"); + } } \ No newline at end of file