libgit2 / libgit2/libgit2sharp

Diff class not mockable

Open
#1,856 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
3.5k
Forks
925
PR merge metrics
No merged PRs in 30d

Description

The Diff class is not very mockable due to its dependency on a private Repository field that is only set from an internal constructor. I encountered this when attempting to mock a repository and its related object graph and then invoking Commands.Stage with that mocked repository. It resulted in a null ref because the Diff.repo field wasn't set.

Commands.Stage ends up calling the internal Diff.Compare method:
https://github.com/libgit2/libgit2sharp/blob/3d7276275e8ea9dce324e437760c31e69b5ec10c/LibGit2Sharp/Commands/Stage.cs#L77

Compare ends up passing the Diff.repo field to WorkdirToIndex:
https://github.com/libgit2/libgit2sharp/blob/3d7276275e8ea9dce324e437760c31e69b5ec10c/LibGit2Sharp/Diff.cs#L472

Which then blows up because repo is null:
https://github.com/libgit2/libgit2sharp/blob/3d7276275e8ea9dce324e437760c31e69b5ec10c/LibGit2Sharp/Diff.cs#L506

The repo field is null because it only gets set by an internal constructor:
https://github.com/libgit2/libgit2sharp/blob/3d7276275e8ea9dce324e437760c31e69b5ec10c/LibGit2Sharp/Diff.cs#L90

This prevents a good mocking solution because the internal constructor can't be called without reflection.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with LibGit2Sharp/Diff.cs, especially the internal constructor, Compare, and WorkdirToIndex, then trace the call from LibGit2Sharp/Commands/Stage.cs. Reproduce the mocked-repository Commands.Stage scenario described in the issue. Done means the path no longer depends on reflection to construct a usable Diff and does not fail because its repository is null.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.