diff --git a/MinVerTests/AutoIncrement.cs b/MinVerTests/AutoIncrement.cs index e6704f3c..4af2ffcc 100644 --- a/MinVerTests/AutoIncrement.cs +++ b/MinVerTests/AutoIncrement.cs @@ -1,6 +1,5 @@ namespace MinVerTests { - using LibGit2Sharp; using MinVer.Lib; using MinVerTests.Infra; using Xbehave; @@ -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)); diff --git a/MinVerTests/BuildMetadata.cs b/MinVerTests/BuildMetadata.cs index bb9b358f..d53f0d63 100644 --- a/MinVerTests/BuildMetadata.cs +++ b/MinVerTests/BuildMetadata.cs @@ -1,6 +1,5 @@ namespace MinVerTests { - using LibGit2Sharp; using MinVer.Lib; using MinVerTests.Infra; using Xbehave; @@ -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())); @@ -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())); @@ -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())); @@ -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)); diff --git a/MinVerTests/DefaultPreReleasePhases.cs b/MinVerTests/DefaultPreReleasePhases.cs index 89b37a98..5d93ada9 100644 --- a/MinVerTests/DefaultPreReleasePhases.cs +++ b/MinVerTests/DefaultPreReleasePhases.cs @@ -1,6 +1,5 @@ namespace MinVerTests { - using LibGit2Sharp; using MinVer.Lib; using Infra; using Xbehave; @@ -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())); diff --git a/MinVerTests/Infra/Git.cs b/MinVerTests/Infra/Git.cs index c74d0b9e..049da86e 100644 --- a/MinVerTests/Infra/Git.cs +++ b/MinVerTests/Infra/Git.cs @@ -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 johndoe@tempuri.org", path); + Run("git", "config user.name John Doe", path); + Run("git", "config commit.gpgsign false", path); } public static Task 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 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); } } diff --git a/MinVerTests/MinMajorMinor.cs b/MinVerTests/MinMajorMinor.cs index f51075a6..cba77908 100644 --- a/MinVerTests/MinMajorMinor.cs +++ b/MinVerTests/MinMajorMinor.cs @@ -1,6 +1,5 @@ namespace MinVerTests { - using LibGit2Sharp; using MinVer.Lib; using MinVerTests.Infra; using Xbehave; @@ -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())); @@ -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())); @@ -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())); diff --git a/MinVerTests/TagPrefixes.cs b/MinVerTests/TagPrefixes.cs index 2384b3bf..ea65d410 100644 --- a/MinVerTests/TagPrefixes.cs +++ b/MinVerTests/TagPrefixes.cs @@ -1,6 +1,5 @@ namespace MinVerTests { - using LibGit2Sharp; using MinVer.Lib; using MinVerTests.Infra; using Xbehave; @@ -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())); diff --git a/MinVerTests/Versioning.cs b/MinVerTests/Versioning.cs index 687d9df5..053fb8c2 100644 --- a/MinVerTests/Versioning.cs +++ b/MinVerTests/Versioning.cs @@ -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; @@ -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)) { @@ -89,16 +88,14 @@ public static void RepoWithHistory(string name, string path, Repository repo) .x(c => { var versionCounts = new Dictionary(); - 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; @@ -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" @@ -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())); diff --git a/targets/Program.cs b/targets/Program.cs index d7ae1e24..dbf3a21a 100644 --- a/targets/Program.cs +++ b/targets/Program.cs @@ -2,7 +2,6 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -using LibGit2Sharp; using MinVerTests.Infra; using static Bullseye.Targets; using static MinVerTests.Infra.FileSystem; @@ -78,7 +77,7 @@ public static async Task Main(string[] args) async () => { // arrange - Repository.Init(testProject); + Init(testProject); var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-no-commits"); @@ -94,20 +93,17 @@ public static async Task Main(string[] args) DependsOn("test-package-no-commits"), async () => { - using (var repo = new Repository(testProject)) - { - // assert - repo.PrepareForCommits(); - Commit(testProject); + // assert + PrepareForCommits(testProject); + Commit(testProject); - var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-commit"); + var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-commit"); - // act - await CleanAndPack(testProject, output, "normal"); + // act + await CleanAndPack(testProject, output, "normal"); - // assert - AssertPackageFileNameContains("0.0.0-alpha.0.nupkg", output); - } + // assert + AssertPackageFileNameContains("0.0.0-alpha.0.nupkg", output); }); Target( @@ -115,19 +111,16 @@ public static async Task Main(string[] args) DependsOn("test-package-commit"), async () => { - using (var repo = new Repository(testProject)) - { - // arrange - repo.ApplyTag("foo"); + // arrange + Tag(testProject, "foo"); - var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-non-version-tag"); + var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-non-version-tag"); - // act - await CleanAndPack(testProject, output, default); + // act + await CleanAndPack(testProject, output, default); - // assert - AssertPackageFileNameContains("0.0.0-alpha.0.nupkg", output); - } + // assert + AssertPackageFileNameContains("0.0.0-alpha.0.nupkg", output); }); Target( @@ -135,21 +128,18 @@ public static async Task Main(string[] args) DependsOn("test-package-non-version-tag"), async () => { - using (var repo = new Repository(testProject)) - { - // arrange - Environment.SetEnvironmentVariable("MinVerTagPrefix", "v.", EnvironmentVariableTarget.Process); + // arrange + Environment.SetEnvironmentVariable("MinVerTagPrefix", "v.", EnvironmentVariableTarget.Process); - repo.ApplyTag("v.1.2.3+foo"); + Tag(testProject, "v.1.2.3+foo"); - var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-version-tag"); + var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-version-tag"); - // act - await CleanAndPack(testProject, output, "normal"); + // act + await CleanAndPack(testProject, output, "normal"); - // assert - AssertPackageFileNameContains("1.2.3.nupkg", output); - } + // assert + AssertPackageFileNameContains("1.2.3.nupkg", output); }); Target( @@ -157,19 +147,16 @@ public static async Task Main(string[] args) DependsOn("test-package-version-tag"), async () => { - using (var repo = new Repository(testProject)) - { - // arrange - Commit(testProject); + // arrange + Commit(testProject); - var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-commit-after-tag"); + var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-commit-after-tag"); - // act - await CleanAndPack(testProject, output, "detailed"); + // act + await CleanAndPack(testProject, output, "detailed"); - // assert - AssertPackageFileNameContains("1.2.4-alpha.0.1.nupkg", output); - } + // assert + AssertPackageFileNameContains("1.2.4-alpha.0.1.nupkg", output); }); Target( @@ -177,19 +164,16 @@ public static async Task Main(string[] args) DependsOn("test-package-commit-after-tag"), async () => { - using (var repo = new Repository(testProject)) - { - // arrange - Environment.SetEnvironmentVariable("MinVerAutoIncrement", "minor", EnvironmentVariableTarget.Process); + // arrange + Environment.SetEnvironmentVariable("MinVerAutoIncrement", "minor", EnvironmentVariableTarget.Process); - var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-non-default-auto-increment"); + var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-non-default-auto-increment"); - // act - await CleanAndPack(testProject, output, "diagnostic"); + // act + await CleanAndPack(testProject, output, "diagnostic"); - // assert - AssertPackageFileNameContains("1.3.0-alpha.0.1.nupkg", output); - } + // assert + AssertPackageFileNameContains("1.3.0-alpha.0.1.nupkg", output); }); Target( @@ -197,19 +181,16 @@ public static async Task Main(string[] args) DependsOn("test-package-non-default-auto-increment"), async () => { - using (var repo = new Repository(testProject)) - { - // arrange - Environment.SetEnvironmentVariable("MinVerMinimumMajorMinor", "2.0", EnvironmentVariableTarget.Process); + // arrange + Environment.SetEnvironmentVariable("MinVerMinimumMajorMinor", "2.0", EnvironmentVariableTarget.Process); - var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-minimum-major-minor"); + var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-minimum-major-minor"); - // act - await CleanAndPack(testProject, output, "diagnostic"); + // act + await CleanAndPack(testProject, output, "diagnostic"); - // assert - AssertPackageFileNameContains("2.0.0-alpha.0.1.nupkg", output); - } + // assert + AssertPackageFileNameContains("2.0.0-alpha.0.1.nupkg", output); }); Target( @@ -217,19 +198,16 @@ public static async Task Main(string[] args) DependsOn("test-package-minimum-major-minor"), async () => { - using (var repo = new Repository(testProject)) - { - // arrange - Environment.SetEnvironmentVariable("MinVerDefaultPreReleasePhase", "preview", EnvironmentVariableTarget.Process); + // arrange + Environment.SetEnvironmentVariable("MinVerDefaultPreReleasePhase", "preview", EnvironmentVariableTarget.Process); - var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-default-pre-release-phase"); + var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-default-pre-release-phase"); - // act - await CleanAndPack(testProject, output, "diagnostic"); + // act + await CleanAndPack(testProject, output, "diagnostic"); - // assert - AssertPackageFileNameContains("2.0.0-preview.0.1.nupkg", output); - } + // assert + AssertPackageFileNameContains("2.0.0-preview.0.1.nupkg", output); }); Target( @@ -237,19 +215,16 @@ public static async Task Main(string[] args) DependsOn("test-package-default-pre-release-phase"), async () => { - using (var repo = new Repository(testProject)) - { - // arrange - Environment.SetEnvironmentVariable("MinVerVersionOverride", "3.2.1-rc.4+build.5", EnvironmentVariableTarget.Process); + // arrange + Environment.SetEnvironmentVariable("MinVerVersionOverride", "3.2.1-rc.4+build.5", EnvironmentVariableTarget.Process); - var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-version-override"); + var output = Path.Combine(testPackageBaseOutput, $"{buildNumber}-test-package-version-override"); - // act - await CleanAndPack(testProject, output, "diagnostic"); + // act + await CleanAndPack(testProject, output, "diagnostic"); - // assert - AssertPackageFileNameContains("3.2.1-rc.4.nupkg", output); - } + // assert + AssertPackageFileNameContains("3.2.1-rc.4.nupkg", output); }); Target("test-package", DependsOn("test-package-version-override")); diff --git a/targets/Targets.csproj b/targets/Targets.csproj index 847dd14d..a7823d54 100644 --- a/targets/Targets.csproj +++ b/targets/Targets.csproj @@ -12,7 +12,6 @@ -