Skip to content

Commit

Permalink
Merge pull request #31 from adamralph/subdirectory
Browse files Browse the repository at this point in the history
MSBuildIntegration.Subdirectory()
  • Loading branch information
adamralph authored Oct 27, 2018
2 parents 21b864d + f887b1f commit 6cc449c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
24 changes: 15 additions & 9 deletions MinVer/Versioner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@ public static Version GetVersion(string path)
throw new Exception($"Path '{path}' doesn't point at a valid workdir.");
}

try
var testPath = path;
while (testPath != null)
{
using (var repo = new Repository(path))
try
{
return GetVersion(repo.Commits.FirstOrDefault(), repo.Tags.ToList());
using (var repo = new Repository(testPath))
{
return GetVersion(repo.Commits.FirstOrDefault(), repo.Tags.ToList());
}
}
catch (RepositoryNotFoundException)
{
testPath = Directory.GetParent(testPath)?.FullName;
}
}
catch (RepositoryNotFoundException)
{
// Includes substring of RepositoryNotFoundException.Message $"Path '{path}' doesn't point at a valid Git repository or workdir."
Log($"WARNING: Using default version. Path '{path}' doesn't point at a valid Git repository.");
return new Version();
}

// Includes substring of RepositoryNotFoundException.Message $"Path '{path}' doesn't point at a valid Git repository or workdir."
Log($"WARNING: Using default version. Path '{path}' doesn't point at a valid Git repository.");
return new Version();
}

private static Version GetVersion(Commit commit, List<Tag> tags)
Expand Down
10 changes: 10 additions & 0 deletions MinVerTests/Infra/Git.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ namespace MinVerTests.Infra

public static class Git
{
public static async Task EnsureRepositoryWithACommit(string path)
{
await EnsureEmptyRepository(path);

await RunAsync("git", @"config user.email 'johndoe @tempuri.org'", path);
await RunAsync("git", @"config user.name 'John Doe'", path);
await RunAsync("git", @"config commit.gpgsign false", path);
await RunAsync("git", @"commit --allow-empty -m '.'", path);
}

public static async Task EnsureEmptyRepository(string path)
{
EnsureEmptyDirectory(path);
Expand Down
32 changes: 32 additions & 0 deletions MinVerTests/MSBuildIntegration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace MinVerTests
{
using System.IO;
using MinVer;
using Xbehave;
using Xunit;
using static MinVerTests.Infra.Git;
using static MinVerTests.Infra.FileSystem;
using static SimpleExec.Command;

public static class MSBuildIntegration
{
[Scenario]
public static void Subdirectory(string path, Version version)
{
$"Given a git repository with a commit in '{path = GetScenarioDirectory("msbuild-integration-subdirectory")}'"
.x(async () => await EnsureRepositoryWithACommit(path));

"And the git repository has a subdirectory"
.x(() => EnsureEmptyDirectory(path = Path.Combine(path, "subdirectory")));

"And the current commit is tagged 1.0.0"
.x(async () => await RunAsync("git", @"tag 1.0.0", path));

"When the version is determined using the subdirectory"
.x(() => version = Versioner.GetVersion(path));

"Then the version is 1.0.0"
.x(() => Assert.Equal("1.0.0", version.ToString()));
}
}
}
9 changes: 2 additions & 7 deletions MinVerTests/Versioning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ public static void RepoWithHistory(string name, string path)
$"Given a git repository in '{path = GetScenarioDirectory("versioning-repo-with-history-" + name)}' with a history of branches and/or tags"
.x(async () =>
{
await EnsureEmptyRepository(path);
await RunAsync("git", @"config user.email 'johndoe @tempuri.org'", path);
await RunAsync("git", @"config user.name 'John Doe'", path);
await RunAsync("git", @"config commit.gpgsign false", path);
await RunAsync("git", @"commit --allow-empty -m '.'", path);
await EnsureRepositoryWithACommit(path);
foreach (var command in historicalCommands[name].Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
{
Expand Down Expand Up @@ -129,7 +124,7 @@ public static void RepoWithHistory(string name, string path)
[Scenario]
public static void EmptyRepo(string path, MinVer.Version version)
{
$"Given an empty repo git repository in '{path = GetScenarioDirectory("versioning-empty-repo")}'"
$"Given an empty git repository in '{path = GetScenarioDirectory("versioning-empty-repo")}'"
.x(async () => await EnsureEmptyRepository(path));

"When the version is determined"
Expand Down

0 comments on commit 6cc449c

Please sign in to comment.