Skip to content

Commit 302c594

Browse files
committed
Cleanup code and add test cases
1 parent 8b43ab0 commit 302c594

File tree

2 files changed

+79
-23
lines changed

2 files changed

+79
-23
lines changed

src/Cake.Git/GitAliases.Remote.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Linq;
9-
// ReSharper disable UnusedMember.Global
109

10+
// ReSharper disable UnusedMember.Global
1111
namespace Cake.Git
1212
{
1313
public static partial class GitAliases
1414
{
1515
/// <summary>
16-
/// Gets a git repository's remotes.
16+
/// Gets a Git repository's remotes.
1717
/// </summary>
1818
/// <example>
1919
/// <code>
20-
/// var remotes = GitRemotes("c:/temp/cake");
20+
/// var remotes = GitRemotes("c:/myrepo");
21+
/// Information("Found remotes: {0}", string.Join(", ", remotes.Select(x => x.Name + " -> " + x.Url)));
2122
/// </code>
2223
/// </example>
2324
/// <param name="context">The context.</param>
@@ -29,15 +30,9 @@ public static partial class GitAliases
2930
[CakeAliasCategory("Remotes")]
3031
public static IReadOnlyList<GitRemote> GitRemotes(this ICakeContext context, DirectoryPath repositoryDirectoryPath)
3132
{
32-
if (context == null)
33-
{
34-
throw new ArgumentNullException(nameof(context));
35-
}
33+
ArgumentNullException.ThrowIfNull(context);
3634

37-
if (repositoryDirectoryPath == null)
38-
{
39-
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
40-
}
35+
ArgumentNullException.ThrowIfNull(repositoryDirectoryPath);
4136

4237
if (!context.FileSystem.Exist(repositoryDirectoryPath))
4338
{
@@ -51,11 +46,12 @@ public static IReadOnlyList<GitRemote> GitRemotes(this ICakeContext context, Dir
5146
}
5247

5348
/// <summary>
54-
/// Gets the specified remote from a git repository.
49+
/// Gets the specified remote from a Git repository.
5550
/// </summary>
5651
/// <example>
5752
/// <code>
58-
/// var remotes = GitRemote("c:/temp/cake", "origin");
53+
/// GitRemote remote = GitRemote("c:/temp/cake", "origin");
54+
/// Information(remote.Url);
5955
/// </code>
6056
/// </example>
6157
/// <param name="context">The context.</param>
@@ -68,15 +64,9 @@ public static IReadOnlyList<GitRemote> GitRemotes(this ICakeContext context, Dir
6864
[CakeAliasCategory("Remotes")]
6965
public static GitRemote GitRemote(this ICakeContext context, DirectoryPath repositoryDirectoryPath, string remoteName)
7066
{
71-
if (context == null)
72-
{
73-
throw new ArgumentNullException(nameof(context));
74-
}
67+
ArgumentNullException.ThrowIfNull(context);
7568

76-
if (repositoryDirectoryPath == null)
77-
{
78-
throw new ArgumentNullException(nameof(repositoryDirectoryPath));
79-
}
69+
ArgumentNullException.ThrowIfNull(repositoryDirectoryPath);
8070

8171
if (!context.FileSystem.Exist(repositoryDirectoryPath))
8272
{

test.cake

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,68 @@ Task("Git-Fetch-Remote-Tags")
10251025
}
10261026
});
10271027

1028+
Task("Git-Remotes")
1029+
.IsDependentOn("Git-Modify-Commit")
1030+
.Does(() =>
1031+
{
1032+
var originDir = testRepo.Combine(Guid.NewGuid().ToString("d"));
1033+
var testDir = testRepo.Combine(Guid.NewGuid().ToString("d"));
1034+
1035+
try
1036+
{
1037+
// Arrange: create a repo and a clone
1038+
GitClone((IsRunningOnWindows() ? "" : "file://")+testInitialRepo.FullPath, originDir);
1039+
GitClone((IsRunningOnWindows() ? "" : "file://")+originDir.FullPath, testDir);
1040+
1041+
// Act
1042+
var remotes = GitRemotes(testDir);
1043+
1044+
// Assert
1045+
Information("Found remotes: {0}", string.Join(", ", remotes.Select(x => x.Name + " -> " + x.Url)));
1046+
}
1047+
finally
1048+
{
1049+
// cleanup
1050+
var settings = new DeleteDirectorySettings {
1051+
Recursive = true,
1052+
Force = true
1053+
};
1054+
DeleteDirectory(originDir, settings);
1055+
DeleteDirectory(testDir, settings);
1056+
}
1057+
});
1058+
1059+
Task("Git-Remote")
1060+
.IsDependentOn("Git-Modify-Commit")
1061+
.Does(() =>
1062+
{
1063+
var originDir = testRepo.Combine(Guid.NewGuid().ToString("d"));
1064+
var testDir = testRepo.Combine(Guid.NewGuid().ToString("d"));
1065+
1066+
try
1067+
{
1068+
// Arrange: create a repo and a clone
1069+
GitClone((IsRunningOnWindows() ? "" : "file://")+testInitialRepo.FullPath, originDir);
1070+
GitClone((IsRunningOnWindows() ? "" : "file://")+originDir.FullPath, testDir);
1071+
1072+
// Act
1073+
var remote = GitRemote(testDir, "origin");
1074+
1075+
// Assert
1076+
Information("Found remote: {0}", remote.Url);
1077+
}
1078+
finally
1079+
{
1080+
// cleanup
1081+
var settings = new DeleteDirectorySettings {
1082+
Recursive = true,
1083+
Force = true
1084+
};
1085+
DeleteDirectory(originDir, settings);
1086+
DeleteDirectory(testDir, settings);
1087+
}
1088+
});
1089+
10281090
Task("Git-Tag")
10291091
.IsDependentOn("Git-Tag-Apply")
10301092
.IsDependentOn("Git-Tag-Apply-Objectish");
@@ -1120,7 +1182,9 @@ Task("Default-Tests")
11201182
.IsDependentOn("Git-Clean")
11211183
.IsDependentOn("Git-Config")
11221184
.IsDependentOn("Git-ShortenSha")
1123-
.IsDependentOn("Git-Fetch-Remote");
1185+
.IsDependentOn("Git-Fetch-Remote")
1186+
.IsDependentOn("Git-Remotes")
1187+
.IsDependentOn("Git-Remote");
11241188

11251189
Task("Local-Tests")
11261190
.IsDependentOn("Git-Init")
@@ -1160,7 +1224,9 @@ Task("Local-Tests")
11601224
.IsDependentOn("Git-Clean")
11611225
.IsDependentOn("Git-Config")
11621226
.IsDependentOn("Git-ShortenSha")
1163-
.IsDependentOn("Git-Fetch-Remote");
1227+
.IsDependentOn("Git-Fetch-Remote")
1228+
.IsDependentOn("Git-Remotes")
1229+
.IsDependentOn("Git-Remote");
11641230

11651231
///////////////////////////////////////////////////////////////////////////////
11661232
// EXECUTION

0 commit comments

Comments
 (0)