Skip to content

Commit

Permalink
Merge pull request #257 from adamralph/independent-tests
Browse files Browse the repository at this point in the history
remove use of LibGit2Sharp from test assertion
  • Loading branch information
adamralph authored Aug 14, 2019
2 parents ff24807 + 38fe67a commit 21add10
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 140 deletions.
7 changes: 3 additions & 4 deletions MinVerTests/AutoIncrement.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace MinVerTests
{
using LibGit2Sharp;
using MinVer.Lib;
using MinVerTests.Infra;
using Xbehave;
Expand All @@ -15,13 +14,13 @@ public static class AutoIncrement
[Example("1.2.3", VersionPart.Major, "2.0.0-alpha.0.1")]
[Example("1.2.3", VersionPart.Minor, "1.3.0-alpha.0.1")]
[Example("1.2.3", VersionPart.Patch, "1.2.4-alpha.0.1")]
public static void RtmVersionIncrement(string tag, VersionPart autoIncrement, string expectedVersion, string path, Repository repo, Version actualVersion)
public static void RtmVersionIncrement(string tag, VersionPart autoIncrement, string expectedVersion, string path, Version actualVersion)
{
$"Given a git repository with a commit in '{path = GetScenarioDirectory($"rtm-auto-increment-{tag}")}'"
.x(c => repo = EnsureEmptyRepositoryAndCommit(path).Using(c));
.x(() => EnsureEmptyRepositoryAndCommit(path));

$"And the commit is tagged '{tag}'"
.x(() => repo.ApplyTag(tag));
.x(() => Tag(path, tag));

$"And another commit"
.x(() => Commit(path));
Expand Down
17 changes: 8 additions & 9 deletions MinVerTests/BuildMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace MinVerTests
{
using LibGit2Sharp;
using MinVer.Lib;
using MinVerTests.Infra;
using Xbehave;
Expand All @@ -17,7 +16,7 @@ public static class BuildMetadata
public static void NoCommits(string buildMetadata, string expectedVersion, string path, Version actualVersion)
{
$"Given an empty git repository in '{path = GetScenarioDirectory($"build-metadata-no-tag-{buildMetadata}")}'"
.x(c => EnsureEmptyRepository(path).Using(c));
.x(() => EnsureEmptyRepository(path));

$"When the version is determined using build metadata '{buildMetadata}'"
.x(() => actualVersion = Versioner.GetVersion(path, default, default, buildMetadata, default, default, new TestLogger()));
Expand All @@ -32,7 +31,7 @@ public static void NoCommits(string buildMetadata, string expectedVersion, strin
public static void NoTag(string buildMetadata, string expectedVersion, string path, Version actualVersion)
{
$"Given a git repository with a commit in '{path = GetScenarioDirectory($"build-metadata-no-tag-{buildMetadata}")}'"
.x(c => EnsureEmptyRepositoryAndCommit(path).Using(c));
.x(() => EnsureEmptyRepositoryAndCommit(path));

$"When the version is determined using build metadata '{buildMetadata}'"
.x(() => actualVersion = Versioner.GetVersion(path, default, default, buildMetadata, default, default, new TestLogger()));
Expand All @@ -48,13 +47,13 @@ public static void NoTag(string buildMetadata, string expectedVersion, string pa
[Example("1.2.3-pre+a", default, "1.2.3-pre+a")]
[Example("1.2.3-pre", "b", "1.2.3-pre+b")]
[Example("1.2.3-pre+a", "b", "1.2.3-pre+a.b")]
public static void CurrentTag(string tag, string buildMetadata, string expectedVersion, string path, Repository repo, Version actualVersion)
public static void CurrentTag(string tag, string buildMetadata, string expectedVersion, string path, Version actualVersion)
{
$"Given a git repository with a commit in '{path = GetScenarioDirectory($"build-metadata-current-tag-{tag}-{buildMetadata}")}'"
.x(c => repo = EnsureEmptyRepositoryAndCommit(path).Using(c));
.x(() => EnsureEmptyRepositoryAndCommit(path));

$"And the commit is tagged '{tag}'"
.x(() => repo.ApplyTag(tag));
.x(() =>Tag(path, tag));

$"When the version is determined using build metadata '{buildMetadata}'"
.x(() => actualVersion = Versioner.GetVersion(path, default, default, buildMetadata, default, default, new TestLogger()));
Expand All @@ -70,13 +69,13 @@ public static void CurrentTag(string tag, string buildMetadata, string expectedV
[Example("1.2.3-pre+a", default, "1.2.3-pre.1")]
[Example("1.2.3-pre", "b", "1.2.3-pre.1+b")]
[Example("1.2.3-pre+a", "b", "1.2.3-pre.1+b")]
public static void PreviousTag(string tag, string buildMetadata, string expectedVersion, string path, Repository repo, Version actualVersion)
public static void PreviousTag(string tag, string buildMetadata, string expectedVersion, string path, Version actualVersion)
{
$"Given a git repository with a commit in '{path = GetScenarioDirectory($"build-metadata-previous-tag-{tag}-{buildMetadata}")}'"
.x(c => repo = EnsureEmptyRepositoryAndCommit(path).Using(c));
.x(() => EnsureEmptyRepositoryAndCommit(path));

$"And the commit is tagged '{tag}'"
.x(() => repo.ApplyTag(tag));
.x(() => Tag(path, tag));

$"And another commit"
.x(() => Commit(path));
Expand Down
5 changes: 2 additions & 3 deletions MinVerTests/DefaultPreReleasePhases.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace MinVerTests
{
using LibGit2Sharp;
using MinVer.Lib;
using Infra;
using Xbehave;
Expand All @@ -15,10 +14,10 @@ public static class DefaultPreReleasePhases
[Example(default, "0.0.0-alpha.0")]
[Example("", "0.0.0-alpha.0")]
[Example("preview", "0.0.0-preview.0")]
public static void DefaultPreReleasePhase(string phase, string expectedVersion, string path, Repository repo, Version actualVersion)
public static void DefaultPreReleasePhase(string phase, string expectedVersion, string path, Version actualVersion)
{
$"Given a git repository with a commit in '{path = GetScenarioDirectory($"default-pre-release-phase-{phase}")}'"
.x(c => repo = EnsureEmptyRepositoryAndCommit(path).Using(c));
.x(() => EnsureEmptyRepositoryAndCommit(path));

$"When the version is determined using the default pre-release phase '{phase}'"
.x(() => actualVersion = Versioner.GetVersion(path, default, default, default, default, phase, new TestLogger()));
Expand Down
40 changes: 23 additions & 17 deletions MinVerTests/Infra/Git.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
namespace MinVerTests.Infra
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using LibGit2Sharp;
using static MinVerTests.Infra.FileSystem;
using static SimpleExec.Command;

public static class Git
{
public static Repository EnsureEmptyRepositoryAndCommit(string path)
public static void EnsureEmptyRepositoryAndCommit(string path)
{
var repo = EnsureEmptyRepository(path);

EnsureEmptyRepository(path);
Commit(path);

return repo;
}

public static void Commit(string path) => Run("git", "commit -m '.' --allow-empty", path);

public static Repository EnsureEmptyRepository(string path)
public static void EnsureEmptyRepository(string path)
{
EnsureEmptyDirectory(path);

Repository.Init(path);

return new Repository(path).PrepareForCommits();
Init(path);
PrepareForCommits(path);
}

public static Repository PrepareForCommits(this Repository repo)
{
repo.Config.Set("user.email", "johndoe @tempuri.org");
repo.Config.Set("user.name", "John Doe");
repo.Config.Set("commit.gpgsign", "false");
public static void Init(string path) => Run("git", "init", path);

return repo;
public static void PrepareForCommits(string path)
{
Run("git", "config user.email [email protected]", path);
Run("git", "config user.name John Doe", path);
Run("git", "config commit.gpgsign false", path);
}

public static Task<string> GetGraph(string path) => ReadAsync("git", "log --graph --pretty=format:'%d'", path);

internal static void Tag(string path, string tag) => Run("git", $"tag {tag}", path);

internal static void Tag(string path, string tagName, string sha) => Run("git", $"tag {tagName} {sha}", path);

internal static IEnumerable<string> GetCommitShas(string path) =>
Read("git", "log --pretty=format:\"%H\"", path, noEcho: true)
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

internal static void Checkout(string path, string sha) => Run("git", $"checkout {sha}", path);
}
}
11 changes: 5 additions & 6 deletions MinVerTests/MinMajorMinor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace MinVerTests
{
using LibGit2Sharp;
using MinVer.Lib;
using MinVerTests.Infra;
using Xbehave;
Expand All @@ -15,7 +14,7 @@ public static class MinMajorMinor
public static void NoCommits(string path, Version actualVersion)
{
$"Given an empty git repository in '{path = GetScenarioDirectory($"minimum-major-minor-not-tagged")}'"
.x(c => EnsureEmptyRepository(path).Using(c));
.x(() => EnsureEmptyRepository(path));

"When the version is determined using minimum major minor '1.2'"
.x(() => actualVersion = Versioner.GetVersion(path, default, new MajorMinor(1, 2), default, default, default, new TestLogger()));
Expand All @@ -28,13 +27,13 @@ public static void NoCommits(string path, Version actualVersion)
[Example("2.0.0", 1, 0, "2.0.0", true)]
[Example("2.0.0", 2, 0, "2.0.0", true)]
[Example("2.0.0", 3, 0, "3.0.0-alpha.0", false)]
public static void Tagged(string tag, int major, int minor, string expectedVersion, bool isRedundant, string path, Repository repo, TestLogger logger, Version actualVersion)
public static void Tagged(string tag, int major, int minor, string expectedVersion, bool isRedundant, string path, TestLogger logger, Version actualVersion)
{
$"Given a git repository with a commit in '{path = GetScenarioDirectory($"minimum-major-minor-tagged-{tag}-{major}-{minor}")}'"
.x(c => repo = EnsureEmptyRepositoryAndCommit(path).Using(c));
.x(() => EnsureEmptyRepositoryAndCommit(path));

$"And the commit is tagged '{tag}'"
.x(() => repo.ApplyTag(tag));
.x(() => Tag(path, tag));

$"When the version is determined using minimum major minor '{major}.{minor}'"
.x(() => actualVersion = Versioner.GetVersion(path, default, new MajorMinor(major, minor), default, default, default, logger = new TestLogger()));
Expand All @@ -53,7 +52,7 @@ public static void Tagged(string tag, int major, int minor, string expectedVersi
public static void NotTagged(string path, Version actualVersion)
{
$"Given a git repository with a commit in '{path = GetScenarioDirectory($"minimum-major-minor-not-tagged")}'"
.x(c => EnsureEmptyRepositoryAndCommit(path).Using(c));
.x(() => EnsureEmptyRepositoryAndCommit(path));

"When the version is determined using minimum major minor '1.0'"
.x(() => actualVersion = Versioner.GetVersion(path, default, new MajorMinor(1, 0), default, default, default, new TestLogger()));
Expand Down
7 changes: 3 additions & 4 deletions MinVerTests/TagPrefixes.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace MinVerTests
{
using LibGit2Sharp;
using MinVer.Lib;
using MinVerTests.Infra;
using Xbehave;
Expand All @@ -16,13 +15,13 @@ public static class TagPrefixes
[Example("2.3.4", "", "2.3.4")]
[Example("v3.4.5", "v", "3.4.5")]
[Example("version5.6.7", "version", "5.6.7")]
public static void TagPrefix(string tag, string prefix, string expectedVersion, string path, Repository repo, Version actualVersion)
public static void TagPrefix(string tag, string prefix, string expectedVersion, string path, Version actualVersion)
{
$"Given a git repository with a commit in '{path = GetScenarioDirectory($"tag-prefixes-{tag}")}'"
.x(c => repo = EnsureEmptyRepositoryAndCommit(path).Using(c));
.x(() => EnsureEmptyRepositoryAndCommit(path));

$"And the commit is tagged '{tag}'"
.x(() => repo.ApplyTag(tag));
.x(() => Tag(path, tag));

$"When the version is determined using the tag prefix '{prefix}'"
.x(() => actualVersion = Versioner.GetVersion(path, prefix, default, default, default, default, new TestLogger()));
Expand Down
20 changes: 8 additions & 12 deletions MinVerTests/Versioning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace MinVerTests
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LibGit2Sharp;
using MinVer.Lib;
using MinVerTests.Infra;
using Xbehave;
Expand Down Expand Up @@ -70,12 +69,12 @@ git tag 1.1.0

[Scenario]
[Example("general")]
public static void RepoWithHistory(string name, string path, Repository repo)
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 c =>
.x(async () =>
{
repo = EnsureEmptyRepositoryAndCommit(path).Using(c);
EnsureEmptyRepositoryAndCommit(path);

foreach (var command in historicalCommands[name].Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
{
Expand All @@ -89,16 +88,14 @@ public static void RepoWithHistory(string name, string path, Repository repo)
.x(c =>
{
var versionCounts = new Dictionary<string, int>();
foreach (var commit in repo.Commits)
foreach (var sha in GetCommitShas(path))
{
Commands.Checkout(repo, commit);
Checkout(path, sha);

var version = Versioner.GetVersion(path, default, default, default, default, default, new TestLogger());
var versionString = version.ToString();
var tagName = $"v/{versionString}";

if (!repo.Tags.Any(tag => tag.Target.Sha == commit.Sha && tag.FriendlyName == tagName))
{
versionCounts.TryGetValue(versionString, out var oldVersionCount);
var versionCount = oldVersionCount + 1;
versionCounts[versionString] = versionCount;
Expand All @@ -107,11 +104,10 @@ public static void RepoWithHistory(string name, string path, Repository repo)
? $"v({versionCount})/{versionString}"
: tagName;

repo.Tags.Add(tagName, commit);
}
Tag(path, tagName, sha);
}

Commands.Checkout(repo, repo.Branches["master"]);
Checkout(path, "master");
});

"Then the versions are as expected"
Expand All @@ -122,7 +118,7 @@ public static void RepoWithHistory(string name, string path, Repository repo)
public static void EmptyRepo(string path, Version version)
{
$"Given an empty git repository in '{path = GetScenarioDirectory("versioning-empty-repo")}'"
.x(c => EnsureEmptyRepository(path).Using(c));
.x(() => EnsureEmptyRepository(path));

"When the version is determined"
.x(() => version = Versioner.GetVersion(path, default, default, default, default, default, new TestLogger()));
Expand Down
Loading

0 comments on commit 21add10

Please sign in to comment.