Skip to content

Commit 917a646

Browse files
committed
Merge pull request #712 from okb/feature/invalid-directory-crash
More informative crash on invalid working directory
2 parents f5af64d + cbd3ea9 commit 917a646

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

src/GitVersionCore/GitPreparer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@ public string GetDotGitDirectory()
9797
public string GetProjectRootDirectory()
9898
{
9999
if (IsDynamicGitRepository)
100-
return targetPath;
100+
return this.targetPath;
101101

102-
return Directory.GetParent(GitDirFinder.TreeWalkForDotGitDir(targetPath)).FullName;
102+
var gitDir = GitDirFinder.TreeWalkForDotGitDir(this.targetPath);
103+
104+
if (String.IsNullOrEmpty(gitDir))
105+
throw new DirectoryNotFoundException("Can't find the .git directory in " + targetPath);
106+
107+
return Directory.GetParent(gitDir).FullName;
103108
}
104109

105110
static string CreateDynamicRepository(string targetPath, Authentication authentication, string repositoryUrl, string targetBranch, bool noFetch)

src/GitVersionCore/Helpers/ProcessHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ public static int Run(Action<string> output, Action<string> errorOutput, TextRea
5050
{
5151
if (String.IsNullOrEmpty(exe))
5252
throw new ArgumentNullException("exe");
53+
5354
if (output == null)
5455
throw new ArgumentNullException("output");
5556

5657
workingDirectory = workingDirectory ?? Environment.CurrentDirectory;
5758

59+
if (!Directory.Exists(workingDirectory))
60+
{
61+
errorOutput(string.Format("The directory {0} doesn't exist.", workingDirectory));
62+
return 1;
63+
}
64+
5865
var psi = new ProcessStartInfo
5966
{
6067
UseShellExecute = false,

src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
using System.IO;
2+
23
using GitVersion;
4+
35
using NUnit.Framework;
6+
47
using Shouldly;
58

69
[TestFixture]
710
public class ExecCmdLineArgumentTest
811
{
912
const string MsBuild = @"c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe";
1013

14+
1115
[Test]
1216
public void RunExecViaCommandLine()
1317
{
@@ -32,6 +36,7 @@ public void RunExecViaCommandLine()
3236
}
3337
}
3438

39+
3540
[Test]
3641
public void InvalidArgumentsExitCodeShouldNotBeZero()
3742
{
@@ -49,13 +54,14 @@ public void InvalidArgumentsExitCodeShouldNotBeZero()
4954
</Target>
5055
</Project>";
5156
File.WriteAllText(buildFile, buildFileContent);
52-
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " /invalid-argument");
57+
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments : " /invalid-argument");
5358

5459
result.ExitCode.ShouldBe(1);
5560
result.Output.ShouldContain("Failed to parse arguments");
5661
}
5762
}
5863

64+
5965
[Test]
6066
public void UsesGitVersionConfigWhenCreatingDynamicRepository()
6167
{
@@ -82,4 +88,12 @@ public void UsesGitVersionConfigWhenCreatingDynamicRepository()
8288
DeleteHelper.DeleteGitRepository(repoBasePath);
8389
}
8490
}
91+
92+
93+
[Test]
94+
public void InvalidWorkingDirectoryCrashesWithInformativeMessage()
95+
{
96+
var results = GitVersionHelper.ExecuteIn("InvalidDirectory", null, isTeamCity : false, logToFile : false);
97+
results.Output.ShouldContain("InvalidDirectory");
98+
}
8599
}

src/GitVersionExe.Tests/GitVersionHelper.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Text;
5+
56
using GitVersion.Helpers;
67

78
public static class GitVersionHelper
89
{
910
public static ExecutionResults ExecuteIn(string workingDirectory,
10-
string exec = null, string execArgs = null, string projectFile = null, string projectArgs = null,
11-
bool isTeamCity = false)
11+
string exec = null,
12+
string execArgs = null,
13+
string projectFile = null,
14+
string projectArgs = null,
15+
bool isTeamCity = false,
16+
bool logToFile = true)
1217
{
13-
var logFile = Path.Combine(workingDirectory, "log.txt");
18+
var logFile = logToFile ? Path.Combine(workingDirectory, "log.txt") : null;
1419
var args = new ArgumentBuilder(workingDirectory, exec, execArgs, projectFile, projectArgs, logFile, isTeamCity);
1520
return ExecuteIn(args);
1621
}
1722

18-
public static ExecutionResults ExecuteIn(string workingDirectory, string arguments, bool isTeamCity = false)
23+
24+
public static ExecutionResults ExecuteIn(string workingDirectory, string arguments, bool isTeamCity = false, bool logToFile = true)
1925
{
20-
var logFile = Path.Combine(workingDirectory, "log.txt");
26+
var logFile = logToFile ? Path.Combine(workingDirectory, "log.txt") : null;
2127
var args = new ArgumentBuilder(workingDirectory, arguments, isTeamCity, logFile);
2228
return ExecuteIn(args);
2329
}
2430

31+
2532
static ExecutionResults ExecuteIn(ArgumentBuilder arguments)
2633
{
2734
var gitHubFlowVersion = Path.Combine(PathHelper.GetCurrentDirectory(), "GitVersion.exe");

0 commit comments

Comments
 (0)