Skip to content

Commit

Permalink
Cleanup code and add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed Dec 14, 2024
1 parent 8b43ab0 commit 31a0c56
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 23 deletions.
32 changes: 11 additions & 21 deletions src/Cake.Git/GitAliases.Remote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// Gets a git repository's remotes.
/// Gets a Git repository's remotes.
/// </summary>
/// <example>
/// <code>
/// var remotes = GitRemotes("c:/temp/cake");
/// var remotes = GitRemotes("c:/myrepo");
/// Information("Found remotes: {0}", string.Join(", ", remotes.Select(x => x.Name + " -> " + x.Url)));
/// </code>
/// </example>
/// <param name="context">The context.</param>
Expand All @@ -29,15 +30,9 @@ public static partial class GitAliases
[CakeAliasCategory("Remotes")]
public static IReadOnlyList<GitRemote> 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))
{
Expand All @@ -51,11 +46,12 @@ public static IReadOnlyList<GitRemote> GitRemotes(this ICakeContext context, Dir
}

/// <summary>
/// Gets the specified remote from a git repository.
/// Gets the specified remote from a Git repository.
/// </summary>
/// <example>
/// <code>
/// var remotes = GitRemote("c:/temp/cake", "origin");
/// GitRemote remote = GitRemote("c:/temp/cake", "origin");
/// Information(remote.Url);
/// </code>
/// </example>
/// <param name="context">The context.</param>
Expand All @@ -68,15 +64,9 @@ public static IReadOnlyList<GitRemote> 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))
{
Expand Down
70 changes: 68 additions & 2 deletions test.cake
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 31a0c56

Please sign in to comment.