Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 346bfe1

Browse files
committed
Factor out Guard.ArgumentIsGitPath
1 parent f685a22 commit 346bfe1

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/GitHub.Exports/Services/GitService.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public Task<Patch> Compare(
239239
Guard.ArgumentNotNull(repository, nameof(repository));
240240
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
241241
Guard.ArgumentNotEmptyString(sha2, nameof(sha2));
242-
Guard.ArgumentNotEmptyString(path, nameof(path));
242+
Guard.ArgumentIsGitPath(path, nameof(path));
243243

244244
return Task.Run(() =>
245245
{
@@ -266,11 +266,7 @@ public Task<ContentChanges> CompareWith(IRepository repository, string sha1, str
266266
Guard.ArgumentNotNull(repository, nameof(repository));
267267
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
268268
Guard.ArgumentNotEmptyString(sha2, nameof(sha1));
269-
Guard.ArgumentNotEmptyString(path, nameof(path));
270-
if (path.Contains('\\'))
271-
{
272-
throw new ArgumentException($"The value for '{nameof(path)}' must use '/' not '\\' as directory separator", nameof(path));
273-
}
269+
Guard.ArgumentIsGitPath(path, nameof(path));
274270

275271
return Task.Run(() =>
276272
{

src/GitHub.Extensions/Guard.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
2+
using System.IO;
43
using System.Globalization;
54
using System.Linq;
65

76
namespace GitHub.Extensions
87
{
98
public static class Guard
109
{
10+
public static void ArgumentIsGitPath(string value, string name)
11+
{
12+
ArgumentNotNull(value, name);
13+
14+
if (value.Contains('\\'))
15+
{
16+
throw new ArgumentException($"The value '{value}' must use '/' not '\\' as directory separator", name);
17+
}
18+
19+
if (Path.IsPathRooted(value))
20+
{
21+
throw new ArgumentException($"The value '{value}' must not be rooted", name);
22+
}
23+
}
24+
1125
public static void ArgumentNotNull(object value, string name)
1226
{
1327
if (value != null) return;

test/GitHub.Exports.UnitTests/GitServiceIntegrationTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ public async Task One_File_Is_Modified(string content1, string content2)
5858
Assert.That(treeChanges.Modified.FirstOrDefault()?.Path, Is.EqualTo(path));
5959
}
6060
}
61+
62+
63+
[Test]
64+
public void Path_Must_Not_Use_Windows_Directory_Separator()
65+
{
66+
using (var temp = new TempRepository())
67+
{
68+
var path = @"dir\foo.txt";
69+
var oldContent = "oldContent";
70+
var newContent = "newContent";
71+
var commit1 = AddCommit(temp.Repository, path, oldContent);
72+
var commit2 = AddCommit(temp.Repository, path, newContent);
73+
var target = new GitService(new RepositoryFacade());
74+
75+
Assert.ThrowsAsync<ArgumentException>(() =>
76+
target.Compare(temp.Repository, commit1.Sha, commit2.Sha, path));
77+
}
78+
}
6179
}
6280

6381
public class TheCompareWithMethod

0 commit comments

Comments
 (0)