-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix project and group resolution in mocks (#676)
* Pass the unescaped project id to all usages of ProjectExtensions.FindProject() FindProject() compares the specified ProjectId value to the namespace and project names of mock projects. This was failing because most usages were passing the value of ProjectId.ValueAsUriParameter() which is the URL-escaped form of the project path. When the project path contained a "/", no matching project could be found, causing a NullReferenceException when trying to get a project client from the GitLabClient mock. * Pass the unescaped group id to all usages of GroupExtensions.FindGroup() FindGroup() compares the specified GroupId value to the namespace and names of mock groups. This was failing because most usages were passing the value of GroupId.ValueAsUriParameter() which is the URL-escaped form of the project path. When the group path contained a "/", no matching group could be found, causing a NullReferenceException when trying to get a group client from the GitLabClient mock. * Fix typo in GitLabClientMockTest Co-authored-by: Thomas Cortes <[email protected]> * Include hint about exclusion from test cases in constructor of MergeRequestClient mock --------- Co-authored-by: Thomas Cortes <[email protected]>
- Loading branch information
Showing
26 changed files
with
211 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using NGitLab.Mock.Config; | ||
using NGitLab.Models; | ||
using NUnit.Framework; | ||
|
||
namespace NGitLab.Mock.Tests; | ||
|
||
|
||
public class GitLabClientMockTest | ||
{ | ||
public static IEnumerable ProjectClientTestCases | ||
{ | ||
get | ||
{ | ||
static TestCaseData TestCase(string name, Func<IGitLabClient, ProjectId, object> getClient) => new TestCaseData(getClient).SetArgDisplayNames(name); | ||
|
||
// Create a test case for each method in IGitLabClient that has a single parameter of type ProjectId | ||
foreach (var method in GetMethods<ProjectId>()) | ||
{ | ||
yield return TestCase(method.Name, (client, id) => method.Invoke(client, new object[] { id })); | ||
} | ||
} | ||
} | ||
|
||
public static IEnumerable GroupClientTestCases | ||
{ | ||
get | ||
{ | ||
static TestCaseData TestCase(string name, Func<IGitLabClient, GroupId, object> getClient) => new TestCaseData(getClient).SetArgDisplayNames(name); | ||
|
||
// Create a test case for each method in IGitLabClient that has a single parameter of type GroupId | ||
foreach (var method in GetMethods<GroupId>()) | ||
{ | ||
// GetGroupMergeRequest() is not implemented in the mock MergeRequestClient and will throw a NotImplementedException | ||
// To avoid a test failure, exclude this method from the test cases (this is the only such case) | ||
// This can be removed if a mock for group merge requests is implemented | ||
if (string.Equals(method.Name, nameof(IGitLabClient.GetGroupMergeRequest), StringComparison.Ordinal)) | ||
{ | ||
continue; | ||
} | ||
|
||
yield return TestCase(method.Name, (client, id) => method.Invoke(client, new object[] { id })); | ||
} | ||
|
||
} | ||
} | ||
|
||
[TestCaseSource(nameof(ProjectClientTestCases))] | ||
public void Test_can_get_project_client(Func<IGitLabClient, ProjectId, object> getClient) | ||
{ | ||
using var server = new GitLabConfig() | ||
.WithUser("user1", isDefault: true) | ||
.WithGroup("test-group") | ||
.WithProject("test-project", id: 1, @namespace: "test-group") | ||
.BuildServer(); | ||
|
||
var client = server.CreateClient(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(getClient(client, 1), Is.Not.Null); | ||
Assert.That(getClient(client, "test-group/test-project"), Is.Not.Null); | ||
}); | ||
} | ||
|
||
[TestCaseSource(nameof(GroupClientTestCases))] | ||
public void Test_can_get_group_client(Func<IGitLabClient, GroupId, object> getClient) | ||
{ | ||
using var server = new GitLabConfig() | ||
.WithUser("user1", isDefault: true) | ||
.WithGroup("test-group", group => | ||
{ | ||
group.Namespace = "parent-group"; | ||
group.Id = 1; | ||
}) | ||
.BuildServer(); | ||
|
||
var client = server.CreateClient(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(getClient(client, 1), Is.Not.Null); | ||
Assert.That(getClient(client, "parent-group/test-group"), Is.Not.Null); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Test_getting_MergeRequestClient_for_group_is_not_implemented() | ||
{ | ||
// GetGroupMergeRequest() is not implemented in the mock MergeRequestClient and will throw a NotImplementedException | ||
// For that reason, this method is excluded from the test cases retruend by GroupClientTestCases | ||
// | ||
// This test checkes that this assumption still holds true and the method is rightly is skipped in GroupClientTestCases | ||
// When a mock for GetGroupMergeRequest(), this test will fail. | ||
// In this case, this test special logic for GetGroupMergeRequest() in GroupClientTestCases can be removed | ||
|
||
using var server = new GitLabConfig() | ||
.WithUser("user1", isDefault: true) | ||
.WithGroup("test-group", group => | ||
{ | ||
group.Namespace = "parent-group"; | ||
group.Id = 1; | ||
}) | ||
.BuildServer(); | ||
|
||
var client = server.CreateClient(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.Throws<NotImplementedException>(() => client.GetGroupMergeRequest(1)); | ||
Assert.Throws<NotImplementedException>(() => client.GetGroupMergeRequest("parent-group/test-group")); | ||
}); | ||
} | ||
|
||
private static IEnumerable<MethodInfo> GetMethods<TParameter>() | ||
{ | ||
return typeof(IGitLabClient) | ||
.GetMethods() | ||
.Where(method => method.IsPublic) | ||
.Where(method => method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(TParameter)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NGitLab.Models; | ||
|
||
namespace NGitLab.Mock.Clients; | ||
|
||
internal sealed class ProtectedTagClient : ClientBase, IProtectedTagClient | ||
{ | ||
private readonly int _projectId; | ||
|
||
public ProtectedTagClient(ClientContext context, ProjectId projectId) | ||
: base(context) | ||
{ | ||
_projectId = Server.AllProjects.FindProject(projectId.ValueAsString()).Id; | ||
} | ||
|
||
public ProtectedTag GetProtectedTag(string name) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public Task<ProtectedTag> GetProtectedTagAsync(string name, CancellationToken cancellationToken = default) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public IEnumerable<ProtectedTag> GetProtectedTags() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public GitLabCollectionResponse<ProtectedTag> GetProtectedTagsAsync() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public ProtectedTag ProtectTag(TagProtect protect) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public Task<ProtectedTag> ProtectTagAsync(TagProtect protect, CancellationToken cancellationToken = default) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public void UnprotectTag(string name) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public Task UnprotectTagAsync(string name, CancellationToken cancellationToken = default) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.