diff --git a/src/Cake.Git/GitAliases.Remote.cs b/src/Cake.Git/GitAliases.Remote.cs index a5504f8..5de1733 100644 --- a/src/Cake.Git/GitAliases.Remote.cs +++ b/src/Cake.Git/GitAliases.Remote.cs @@ -6,18 +6,19 @@ using System; using System.Collections.Generic; using System.Linq; -// ReSharper disable UnusedMember.Global +// ReSharper disable UnusedMember.Global namespace Cake.Git { public static partial class GitAliases { /// - /// Gets a git repository's remotes. + /// Gets a Git repository's remotes. /// /// /// - /// var remotes = GitRemotes("c:/temp/cake"); + /// var remotes = GitRemotes("c:/myrepo"); + /// Information("Found remotes: {0}", string.Join(", ", remotes.Select(x => x.Name + " -> " + x.Url))); /// /// /// The context. @@ -29,15 +30,9 @@ public static partial class GitAliases [CakeAliasCategory("Remotes")] public static IReadOnlyList GitRemotes(this ICakeContext context, DirectoryPath repositoryDirectoryPath) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); - if (repositoryDirectoryPath == null) - { - throw new ArgumentNullException(nameof(repositoryDirectoryPath)); - } + ArgumentNullException.ThrowIfNull(repositoryDirectoryPath); if (!context.FileSystem.Exist(repositoryDirectoryPath)) { @@ -51,11 +46,12 @@ public static IReadOnlyList GitRemotes(this ICakeContext context, Dir } /// - /// Gets the specified remote from a git repository. + /// Gets the specified remote from a Git repository. /// /// /// - /// var remotes = GitRemote("c:/temp/cake", "origin"); + /// GitRemote remote = GitRemote("c:/temp/cake", "origin"); + /// Information(remote.Url); /// /// /// The context. @@ -68,15 +64,9 @@ public static IReadOnlyList GitRemotes(this ICakeContext context, Dir [CakeAliasCategory("Remotes")] public static GitRemote GitRemote(this ICakeContext context, DirectoryPath repositoryDirectoryPath, string remoteName) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); - if (repositoryDirectoryPath == null) - { - throw new ArgumentNullException(nameof(repositoryDirectoryPath)); - } + ArgumentNullException.ThrowIfNull(repositoryDirectoryPath); if (!context.FileSystem.Exist(repositoryDirectoryPath)) { diff --git a/test.cake b/test.cake index d8b09b4..23e44ec 100644 --- a/test.cake +++ b/test.cake @@ -1025,6 +1025,68 @@ Task("Git-Fetch-Remote-Tags") } }); +Task("Git-Remotes") + .IsDependentOn("Git-Modify-Commit") + .Does(() => +{ + var originDir = testRepo.Combine(Guid.NewGuid().ToString("d")); + var testDir = testRepo.Combine(Guid.NewGuid().ToString("d")); + + try + { + // Arrange: create a repo and a clone + GitClone((IsRunningOnWindows() ? "" : "file://")+testInitialRepo.FullPath, originDir); + GitClone((IsRunningOnWindows() ? "" : "file://")+originDir.FullPath, testDir); + + // Act + var remotes = GitRemotes(testDir); + + // Assert + Information("Found remotes: {0}", string.Join(", ", remotes.Select(x => x.Name + " -> " + x.Url))); + } + finally + { + // cleanup + var settings = new DeleteDirectorySettings { + Recursive = true, + Force = true + }; + DeleteDirectory(originDir, settings); + DeleteDirectory(testDir, settings); + } +}); + +Task("Git-Remote") + .IsDependentOn("Git-Modify-Commit") + .Does(() => +{ + var originDir = testRepo.Combine(Guid.NewGuid().ToString("d")); + var testDir = testRepo.Combine(Guid.NewGuid().ToString("d")); + + try + { + // Arrange: create a repo and a clone + GitClone((IsRunningOnWindows() ? "" : "file://")+testInitialRepo.FullPath, originDir); + GitClone((IsRunningOnWindows() ? "" : "file://")+originDir.FullPath, testDir); + + // Act + var remote = GitRemote(testDir, "origin"); + + // Assert + Information("Found remote: {0}", remote.Url); + } + finally + { + // cleanup + var settings = new DeleteDirectorySettings { + Recursive = true, + Force = true + }; + DeleteDirectory(originDir, settings); + DeleteDirectory(testDir, settings); + } +}); + Task("Git-Tag") .IsDependentOn("Git-Tag-Apply") .IsDependentOn("Git-Tag-Apply-Objectish"); @@ -1120,7 +1182,9 @@ Task("Default-Tests") .IsDependentOn("Git-Clean") .IsDependentOn("Git-Config") .IsDependentOn("Git-ShortenSha") - .IsDependentOn("Git-Fetch-Remote"); + .IsDependentOn("Git-Fetch-Remote") + .IsDependentOn("Git-Remotes") + .IsDependentOn("Git-Remote"); Task("Local-Tests") .IsDependentOn("Git-Init") @@ -1160,7 +1224,9 @@ Task("Local-Tests") .IsDependentOn("Git-Clean") .IsDependentOn("Git-Config") .IsDependentOn("Git-ShortenSha") - .IsDependentOn("Git-Fetch-Remote"); + .IsDependentOn("Git-Fetch-Remote") + .IsDependentOn("Git-Remotes") + .IsDependentOn("Git-Remote"); /////////////////////////////////////////////////////////////////////////////// // EXECUTION