Skip to content

Commit 456b70a

Browse files
authored
fix: Mocked TagClient.GetAsync(TagQuery query) must handle searching "^term$" (#1076)
fix: Mocked TagClient.GetAsync(TagQuery query) now handles searches with both ^ and $
1 parent 78259f1 commit 456b70a

3 files changed

Lines changed: 50 additions & 24 deletions

File tree

NGitLab.Mock.Tests/TagTests.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public async Task SearchTags()
9797
.WithUser("user1", isDefault: true)
9898
.WithProject("test-project", id: 1, addDefaultUserAsMaintainer: true, configure: project => project
9999
.WithCommit("First Tag", tags: ["v0.5"])
100-
.WithCommit("Second Tag", tags: ["v0.6"]))
100+
.WithCommit("Second Tag", tags: ["v0.5.1"])
101+
.WithCommit("Third Tag", tags: ["v0.6"]))
101102
.BuildServer();
102103

103104
var client = server.CreateClient();
@@ -108,20 +109,21 @@ public async Task SearchTags()
108109
// You can use "^term" and "term$" to find tags that begin and end with "term". No other regular expressions are supported.
109110
// The search expression is case-insensitive.
110111
// https://docs.gitlab.com/api/tags/#list-all-project-repository-tags
111-
("^v0.5", 1),
112-
("^v0", 2),
113-
("^v", 2),
114-
("^V", 2),
112+
("^v0.5", 2),
113+
("^v0", 3),
114+
("^v", 3),
115+
("^V", 3),
115116
("^v1", 0),
116-
("0.5", 1),
117-
("0", 2),
117+
("0.5", 2),
118+
("0", 3),
118119
("6", 1),
119-
("V", 2),
120+
("V", 3),
120121
("0.5$", 1),
121122
(".5$", 1),
122123
("\\.5$", 0),
123124
(".[0-9]$", 0),
124125
("0\\.", 0),
126+
("^v0.5$", 1),
125127
];
126128

127129
foreach (var (searchExpression, expectedCount) in testCases)

NGitLab.Mock/Clients/TagClient.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,30 @@ public Tag ToTagClient(LibGit2Sharp.Tag tag)
9999
// First, filter by search term if provided
100100
if (!string.IsNullOrEmpty(query.Search))
101101
{
102-
tags = query.Search switch
102+
var search = query.Search;
103+
var startsWithCaret = search.StartsWith("^", StringComparison.Ordinal);
104+
var endsWithDollar = search.EndsWith("$", StringComparison.Ordinal);
105+
106+
if (!startsWithCaret && !endsWithDollar)
107+
{
108+
tags = tags.Where(t => t.FriendlyName.Contains(search, StringComparison.OrdinalIgnoreCase));
109+
}
110+
else
103111
{
104-
string search when search.StartsWith("^", StringComparison.Ordinal) =>
105-
tags.Where(t => t.FriendlyName.StartsWith(search[1..], StringComparison.OrdinalIgnoreCase)),
106-
string search when search.EndsWith("$", StringComparison.Ordinal) =>
107-
tags.Where(t => t.FriendlyName.EndsWith(search[..^1], StringComparison.OrdinalIgnoreCase)),
108-
_ => tags.Where(t => t.FriendlyName.Contains(query.Search, StringComparison.OrdinalIgnoreCase)),
109-
};
112+
// Remove the special characters for the actual search term
113+
if (startsWithCaret)
114+
search = search[1..];
115+
116+
if (endsWithDollar)
117+
search = search[..^1];
118+
119+
// Search with the appropriate conditions based on the presence of ^ and $
120+
if (startsWithCaret)
121+
tags = tags.Where(t => t.FriendlyName.StartsWith(search, StringComparison.OrdinalIgnoreCase));
122+
123+
if (endsWithDollar)
124+
tags = tags.Where(t => t.FriendlyName.EndsWith(search, StringComparison.OrdinalIgnoreCase));
125+
}
110126
}
111127

112128
var orderBy = query.OrderBy?.ToLowerInvariant();

NGitLab.Tests/TagTests.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,21 @@ public async Task SearchTags()
4545
tagClient.Create(new TagCreate
4646
{
4747
Name = "v0.5",
48-
Message = "Test message",
48+
Message = "First tag",
49+
Ref = project.DefaultBranch,
50+
});
51+
52+
tagClient.Create(new TagCreate
53+
{
54+
Name = "v0.5.1",
55+
Message = "Second tag",
4956
Ref = project.DefaultBranch,
5057
});
5158

5259
tagClient.Create(new TagCreate
5360
{
5461
Name = "v0.6",
55-
Message = "Test second message",
62+
Message = "Third tag",
5663
Ref = project.DefaultBranch,
5764
});
5865

@@ -61,20 +68,21 @@ public async Task SearchTags()
6168
// You can use "^term" and "term$" to find tags that begin and end with "term". No other regular expressions are supported.
6269
// The search expression is case-insensitive.
6370
// https://docs.gitlab.com/api/tags/#list-all-project-repository-tags
64-
("^v0.5", 1),
65-
("^v0", 2),
66-
("^v", 2),
67-
("^V", 2),
71+
("^v0.5", 2),
72+
("^v0", 3),
73+
("^v", 3),
74+
("^V", 3),
6875
("^v1", 0),
69-
("0.5", 1),
70-
("0", 2),
76+
("0.5", 2),
77+
("0", 3),
7178
("6", 1),
72-
("V", 2),
79+
("V", 3),
7380
("0.5$", 1),
7481
(".5$", 1),
7582
("\\.5$", 0),
7683
(".[0-9]$", 0),
7784
("0\\.", 0),
85+
("^v0.5$", 1),
7886
];
7987

8088
foreach (var (searchExpression, expectedCount) in testCases)

0 commit comments

Comments
 (0)