Skip to content

More informative crash on invalid working directory #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/GitVersionCore/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,14 @@ public string GetDotGitDirectory()
public string GetProjectRootDirectory()
{
if (IsDynamicGitRepository)
return targetPath;
return this.targetPath;

return Directory.GetParent(GitDirFinder.TreeWalkForDotGitDir(targetPath)).FullName;
var gitDir = GitDirFinder.TreeWalkForDotGitDir(this.targetPath);

if (String.IsNullOrEmpty(gitDir))
throw new DirectoryNotFoundException("Can't find the .git directory in " + targetPath);

return Directory.GetParent(gitDir).FullName;
}

static string CreateDynamicRepository(string targetPath, Authentication authentication, string repositoryUrl, string targetBranch, bool noFetch)
Expand Down
7 changes: 7 additions & 0 deletions src/GitVersionCore/Helpers/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ public static int Run(Action<string> output, Action<string> errorOutput, TextRea
{
if (String.IsNullOrEmpty(exe))
throw new ArgumentNullException("exe");

if (output == null)
throw new ArgumentNullException("output");

workingDirectory = workingDirectory ?? Environment.CurrentDirectory;

if (!Directory.Exists(workingDirectory))
{
errorOutput(string.Format("The directory {0} doesn't exist.", workingDirectory));
return 1;
}

var psi = new ProcessStartInfo
{
UseShellExecute = false,
Expand Down
16 changes: 15 additions & 1 deletion src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using System.IO;

using GitVersion;

using NUnit.Framework;

using Shouldly;

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


[Test]
public void RunExecViaCommandLine()
{
Expand All @@ -32,6 +36,7 @@ public void RunExecViaCommandLine()
}
}


[Test]
public void InvalidArgumentsExitCodeShouldNotBeZero()
{
Expand All @@ -49,13 +54,14 @@ public void InvalidArgumentsExitCodeShouldNotBeZero()
</Target>
</Project>";
File.WriteAllText(buildFile, buildFileContent);
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " /invalid-argument");
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments : " /invalid-argument");

result.ExitCode.ShouldBe(1);
result.Output.ShouldContain("Failed to parse arguments");
}
}


[Test]
public void UsesGitVersionConfigWhenCreatingDynamicRepository()
{
Expand All @@ -82,4 +88,12 @@ public void UsesGitVersionConfigWhenCreatingDynamicRepository()
DeleteHelper.DeleteGitRepository(repoBasePath);
}
}


[Test]
public void InvalidWorkingDirectoryCrashesWithInformativeMessage()
{
var results = GitVersionHelper.ExecuteIn("InvalidDirectory", null, isTeamCity : false, logToFile : false);
results.Output.ShouldContain("InvalidDirectory");
}
}
17 changes: 12 additions & 5 deletions src/GitVersionExe.Tests/GitVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@
using System.Collections.Generic;
using System.IO;
using System.Text;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this r# splitting it up? We should add the setting to stop this automatically happening (for consistency reasons) into the solution DotSettings file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I'm actually not entirely sure if it is my machine-level R# doing this or the local ("team shared") sln.DotSettings file. Either way, I agree that the local one should specify this explicitly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Have you already read my new article? I think that's what you need at the moment, please, read it here http://describe.jamiecooks.com/e2lndncz

[email protected]

using GitVersion.Helpers;

public static class GitVersionHelper
{
public static ExecutionResults ExecuteIn(string workingDirectory,
string exec = null, string execArgs = null, string projectFile = null, string projectArgs = null,
bool isTeamCity = false)
string exec = null,
string execArgs = null,
string projectFile = null,
string projectArgs = null,
bool isTeamCity = false,
bool logToFile = true)
{
var logFile = Path.Combine(workingDirectory, "log.txt");
var logFile = logToFile ? Path.Combine(workingDirectory, "log.txt") : null;
var args = new ArgumentBuilder(workingDirectory, exec, execArgs, projectFile, projectArgs, logFile, isTeamCity);
return ExecuteIn(args);
}

public static ExecutionResults ExecuteIn(string workingDirectory, string arguments, bool isTeamCity = false)

public static ExecutionResults ExecuteIn(string workingDirectory, string arguments, bool isTeamCity = false, bool logToFile = true)
{
var logFile = Path.Combine(workingDirectory, "log.txt");
var logFile = logToFile ? Path.Combine(workingDirectory, "log.txt") : null;
var args = new ArgumentBuilder(workingDirectory, arguments, isTeamCity, logFile);
return ExecuteIn(args);
}


static ExecutionResults ExecuteIn(ArgumentBuilder arguments)
{
var gitHubFlowVersion = Path.Combine(PathHelper.GetCurrentDirectory(), "GitVersion.exe");
Expand Down