8000 Change the type of Exception which is returned when staging/unstaging… · rlazev/libgit2sharp@e32f933 · GitHub
[go: up one dir, main page]

Skip to content

Commit e32f933

Browse files
committed
Change the type of Exception which is returned when staging/unstaging/removing an empty collection of paths
1 parent e6ba4c9 commit e32f933

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ public void StageFileWithBadParamsThrows()
288288
{
289289
Assert.Throws<ArgumentException>(() => repo.Index.Stage(string.Empty));
290290
Assert.Throws<ArgumentNullException>(() => repo.Index.Stage((string)null));
291-
Assert.Throws<ArgumentNullException>(() => repo.Index.Stage(new string[] { }));
292-
Assert.Throws<ArgumentNullException>(() => repo.Index.Stage(new string[] { null }));
291+
Assert.Throws<ArgumentException>(() => repo.Index.Stage(new string[] { }));
292+
Assert.Throws<ArgumentException>(() => repo.Index.Stage(new string[] { null }));
293293
}
294294
}
295295

@@ -346,8 +346,8 @@ public void UnstagingFileWithBadParamsThrows()
346346
{
347347
Assert.Throws<ArgumentException>(() => repo.Index.Unstage(string.Empty));
348348
Assert.Throws<ArgumentNullException>(() => repo.Index.Unstage((string)null));
349-
Assert.Throws<ArgumentNullException>(() => repo.Index.Unstage(new string[] { }));
350-
Assert.Throws<ArgumentNullException>(() => repo.Index.Unstage(new string[] { null }));
349+
Assert.Throws<ArgumentException>(() => repo.Index.Unstage(new string[] { }));
350+
Assert.Throws<ArgumentException>(() => repo.Index.Unstage(new string[] { null }));
351351
}
352352
}
353353

@@ -497,6 +497,18 @@ public void RemovingAInvalidFileThrows(string filepath)
497497
}
498498
}
499499

500+
[Fact]
501+
public void RemovingFileWithBadParamsThrows()
502+
{
503+
using (var repo = new Repository(StandardTestRepoPath))
504+
{
505+
Assert.Throws<ArgumentException>(() => repo.Index.Remove(string.Empty));
506+
Assert.Throws<ArgumentNullException>(() => repo.Index.Remove((string)null));
507+
Assert.Throws<ArgumentException>(() => repo.Index.Remove(new string[] { }));
508+
Assert.Throws<ArgumentException>(() => repo.Index.Remove(new string[] { null }));
509+
}
510+
}
511+
500512
[Fact]
501513
public void PathsOfIndexEntriesAreExpressedInNativeFormat()
502514
{

LibGit2Sharp/Index.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ IEnumerator IEnumerable.GetEnumerator()
105105
/// <param name = "path">The path of the file within the working directory.</param>
106106
public void Stage(string path)
107107
{
108+
Ensure.ArgumentNotNull(path, "path");
109+
108110
Stage(new[] { path });
109111
}
110112

@@ -114,18 +116,11 @@ public void Stage(string path)
114116
/// <param name = "paths">The collection of paths of the files within the working directory.</param>
115117
public void Stage(IEnumerable<string> paths)
116118
{
117-
Ensure.ArgumentNotNull(paths, "paths");
118-
119119
//TODO: Stage() should support following use cases:
120120
// - Recursively staging the content of a directory
121121

122122
IDictionary<string, FileStatus> batch = PrepareBatch(paths);
123123

124-
if (batch.Count == 0)
125-
{
126-
throw new ArgumentNullException("paths");
127-
}
128-
129124
foreach (KeyValuePair<string, FileStatus> kvp in batch)
130125
{
131126
if (Directory.Exists(kvp.Key))
@@ -165,6 +160,8 @@ public void Stage(IEnumerable<string> paths)
165160
/// <param name = "path">The path of the file within the working directory.</param>
166161
public void Unstage(string path)
167162
{
163+
Ensure.ArgumentNotNull(path, "path");
164+
168165
Unstage(new[] { path });
169166
}
170167

@@ -174,15 +171,8 @@ public void Unstage(string path)
174171
/// <param name = "paths">The collection of paths of the files within the working directory.</param>
175172
public void Unstage(IEnumerable<string> paths)
176173
{
177-
Ensure.ArgumentNotNull(paths, "paths");
178-
179174
IDictionary<string, FileStatus> batch = PrepareBatch(paths);
180175

181-
if (batch.Count == 0)
182-
{
183-
throw new ArgumentNullException("paths");
184-
}
185-
186176
foreach (KeyValuePair<string, FileStatus> kvp in batch)
187177
{
188178
if (Directory.Exists(kvp.Key))
@@ -291,6 +281,8 @@ public void Move(IEnumerable<string> sourcePaths, IEnumerable<string> destinatio
291281
/// <param name = "path">The path of the file within the working directory.</param>
292282
public void Remove(string path)
293283
{
284+
Ensure.ArgumentNotNull(path, "path");
285+
294286
Remove(new[] { path });
295287
}
296288

@@ -304,18 +296,11 @@ public void Remove(string path)
304296
/// <param name = "paths">The collection of paths of the files within the working directory.</param>
305297
public void Remove(IEnumerable<string> paths)
306298
{
307-
Ensure.ArgumentNotNull(paths, "paths");
308-
309299
//TODO: Remove() should support following use cases:
310300
// - Removing a directory and its content
311301

312302
IDictionary<string, FileStatus> batch = PrepareBatch(paths);
313303

314-
if (batch.Count == 0)
315-
{
316-
throw new ArgumentNullException("paths");
317-
}
318-
319304
foreach (KeyValuePair<string, FileStatus> keyValuePair in batch)
320305
{
321306
if (Directory.Exists(keyValuePair.Key))
@@ -347,16 +332,28 @@ public void Remove(IEnumerable<string> paths)
347332

348333
private IDictionary<string, FileStatus> PrepareBatch(IEnumerable<string> paths)
349334
{
335+
Ensure.ArgumentNotNull(paths, "paths");
336+
350337
IDictionary<string, FileStatus> dic = new Dictionary<string, FileStatus>();
351338

352339
foreach (string path in paths)
353340
{
341+
if (string.IsNullOrEmpty(path))
342+
{
343+
throw new ArgumentException("At least one provided path is either null or empty.", "paths");
344+
}
345+
354346
string relativePath = BuildRelativePathFrom(repo, path);
355347
FileStatus fileStatus = RetrieveStatus(relativePath);
356348

357349
dic.Add(relativePath, fileStatus);
358350
}
359351

352+
if (dic.Count == 0)
353+
{
354+
throw new ArgumentException("No path has been provided.", "paths");
355+
}
356+
360357
return dic;
361358
}
362359

0 commit comments

Comments
 (0)
0