Skip to content

Commit d08d1f7

Browse files
committed
Add unit test
1 parent ef3792e commit d08d1f7

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

LibGit2Sharp.Tests/BlobFixture.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public void CanGetBlobAsText()
1515
using (var repo = new Repository(path))
1616
{
1717
var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6");
18+
Assert.False(blob.IsMissing);
1819

1920
var text = blob.GetContentText();
2021

@@ -36,6 +37,7 @@ public void CanGetBlobAsFilteredText(string autocrlf, string expectedText)
3637
repo.Config.Set("core.autocrlf", autocrlf);
3738

3839
var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6");
40+
Assert.False(blob.IsMissing);
3941

4042
var text = blob.GetContentText(new FilteringOptions("foo.txt"));
4143

@@ -67,6 +69,7 @@ public void CanGetBlobAsTextWithVariousEncodings(string encodingName, int expect
6769
var commit = repo.Commit("bom", Constants.Signature, Constants.Signature);
6870

6971
var blob = (Blob)commit.Tree[bomFile].Target;
72+
Assert.False(blob.IsMissing);
7073
Assert.Equal(expectedContentBytes, blob.Size);
7174
using (var stream = blob.GetContentStream())
7275
{
@@ -92,6 +95,7 @@ public void CanGetBlobSize()
9295
using (var repo = new Repository(path))
9396
{
9497
var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6");
98+
Assert.False(blob.IsMissing);
9599
Assert.Equal(10, blob.Size);
96100
}
97101
}
@@ -104,6 +108,7 @@ public void CanLookUpBlob()
104108
{
105109
var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6");
106110
Assert.NotNull(blob);
111+
Assert.False(blob.IsMissing);
107112
}
108113
}
109114

@@ -114,6 +119,7 @@ public void CanReadBlobStream()
114119
using (var repo = new Repository(path))
115120
{
116121
var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6");
122+
Assert.False(blob.IsMissing);
117123

118124
var contentStream = blob.GetContentStream();
119125
Assert.Equal(blob.Size, contentStream.Length);
@@ -140,6 +146,7 @@ public void CanReadBlobFilteredStream(string autocrlf, string expectedContent)
140146
repo.Config.Set("core.autocrlf", autocrlf);
141147

142148
var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6");
149+
Assert.False(blob.IsMissing);
143150

144151
var contentStream = blob.GetContentStream(new FilteringOptions("foo.txt"));
145152
Assert.Equal(expectedContent.Length, contentStream.Length);
@@ -164,6 +171,7 @@ public void CanReadBlobFilteredStreamOfUnmodifiedBinary()
164171
using (var stream = new MemoryStream(binaryContent))
165172
{
166173
Blob blob = repo.ObjectDatabase.CreateBlob(stream);
174+
Assert.False(blob.IsMissing);
167175

168176
using (var filtered = blob.GetContentStream(new FilteringOptions("foo.txt")))
169177
{
@@ -196,6 +204,7 @@ public void CanStageAFileGeneratedFromABlobContentStream()
196204
Assert.Equal("baae1fb3760a73481ced1fa03dc15614142c19ef", entry.Id.Sha);
197205

198206
var blob = repo.Lookup<Blob>(entry.Id.Sha);
207+
Assert.False(blob.IsMissing);
199208

200209
using (Stream stream = blob.GetContentStream())
201210
using (Stream file = File.OpenWrite(Path.Combine(repo.Info.WorkingDirectory, "small.fromblob.txt")))
@@ -217,10 +226,35 @@ public void CanTellIfTheBlobContentLooksLikeBinary()
217226
using (var repo = new Repository(path))
218227
{
219228
var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6");
229+
Assert.False(blob.IsMissing);
220230
Assert.False(blob.IsBinary);
221231
}
222232
}
223233

234+
[Fact]
235+
public void CanTellIsABlobIsMissing()
236+
{
237+
string repoPath = SandboxBareTestRepo();
238+
239+
// Manually delete the objects directory to simulate a partial clone
240+
Directory.Delete(Path.Combine(repoPath, "objects", "a8"), true);
241+
242+
using (var repo = new Repository(repoPath))
243+
{
244+
// Look for the commit that reference the blob which is now missing
245+
var commit = repo.Lookup<Commit>("4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
246+
var blob = (Blob) commit.Tree["README"].Target;
247+
248+
Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", blob.Sha);
249+
Assert.NotNull(blob);
250+
Assert.True(blob.IsMissing);
251+
Assert.Throws<NotFoundException>(() => blob.Size);
252+
Assert.Throws<NotFoundException>(() => blob.IsBinary);
253+
Assert.Throws<NotFoundException>(() => blob.GetContentText());
254+
Assert.Throws<NotFoundException>(() => blob.GetContentText(new FilteringOptions("foo.txt")));
255+
}
256+
}
257+
224258
private static void SkipIfNotSupported(string autocrlf)
225259
{
226260
InconclusiveIf(() => autocrlf == "true" && Constants.IsRunningOnUnix, "Non-Windows does not support core.autocrlf = true");

LibGit2Sharp/Blob.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal Blob(Repository repo, ObjectId id)
2828
{
2929
lazySize = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_rawsize);
3030
lazyIsBinary = GitObjectLazyGroup.Singleton(repo, id, Proxy.git_blob_is_binary);
31-
lazyIsMissing = GitObjectLazyGroup.Singleton(repo, id, handle => handle == null);
31+
lazyIsMissing = GitObjectLazyGroup.Singleton(repo, id, handle => handle == null, throwsIfMissing: false);
3232
}
3333

3434
/// <summary>

LibGit2Sharp/Core/GitObjectLazyGroup.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ protected override void EvaluateInternal(Action<ObjectHandle> evaluator)
1818
using (var osw = new ObjectSafeWrapper(id, repo.Handle))
1919
{
2020
if (osw.ObjectPtr == null)
21-
throw new NotFoundException($"Object {id} is not available");
21+
throw new NotFoundException($"No valid git object identified by '{id}' exists in the repository.");
2222

2323
evaluator(osw.ObjectPtr);
2424
}
2525
}
2626

27-
public static ILazy<TResult> Singleton<TResult>(Repository repo, ObjectId id, Func<ObjectHandle, TResult> resultSelector)
27+
public static ILazy<TResult> Singleton<TResult>(Repository repo, ObjectId id, Func<ObjectHandle, TResult> resultSelector, bool throwsIfMissing = true)
2828
{
2929
return Singleton(() =>
3030
{
3131
using (var osw = new ObjectSafeWrapper(id, repo.Handle))
3232
{
33-
if (osw.ObjectPtr == null)
34-
throw new NotFoundException($"Object {id} is not available");
33+
if (throwsIfMissing && osw.ObjectPtr == null)
34+
throw new NotFoundException($"No valid git object identified by '{id}' exists in the repository.");
3535

3636
return resultSelector(osw.ObjectPtr);
3737
}

0 commit comments

Comments
 (0)