Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock a git repo to test against

Is there an easy way to mock a git server/repo for unit testing purposes? I don't want to test git itself, rather how my code interacts with git.

I'd like to do something like

create [email protected]:myRepo.git c:/somepath
copy somefile.txt c:/somepath

and then when I

git clone [email protected]:myRepo.git

the entire contents of c:/somepath (somefile.txt) is returned, no authentication needed. I want to test how my code handles the clone, so all calls to this git repo should automatically pass authentication.

I looked at git daemon but it runs on real git, so my test code ended up have to do a lot of repo content management (adding, committing, branching etc), which seems unnecessary.

like image 449
Shukri Adams Avatar asked Oct 19 '15 23:10

Shukri Adams


People also ask

What is Git HTTP backend?

A simple CGI program to serve the contents of a Git repository to Git clients accessing the repository over http:// and https:// protocols. The program supports clients fetching using both the smart HTTP protocol and the backwards-compatible dumb HTTP protocol, as well as clients pushing using the smart HTTP protocol.

Can you test code on GitHub?

You can automatically build and test your projects with GitHub Actions.

How do I verify a Git repository?

You can inspect a Git repository by using the git status command. This command allows you to see which changes have been staged, which haven't, and which files aren't being tracked by Git. You should try and remember that status output does not show you any information regarding the committed project history.

What is test in Git?

git-test is a command-line script for running automated tests against commits in a Git repository. It is especially targeted at developers who like their tests to pass on every commit in a branch, not just the branch tip.


2 Answers

You don't need a server to clone. It will work on a normal directory.

Make a repository.

$ git init /path/to/repo

Put some stuff in it.

$ cd /path/to/repo
$ touch foo bar
$ git add .
$ git ci
[master (root-commit) 3c99023] Some files.
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bar
 create mode 100644 foo

Clone it.

$ git clone /path/to/repo /path/to/clone
$ cd /path/to/clone
$ ls
.git  bar  foo

Note that git won't like it if you push to a non-bare repository, so you might want to make the intermediate step of turning /path/to/repo into a bare repository. Simplest thing way to do that is to make a bare clone and use that for everything else to clone from.

$ git clone --bare /path/to/repo /path/to/upstream
$ git clone /path/to/upstream /path/to/clone
...use /path/to/clone as normal pushing to /path/to/upstream...

You could also just make a bare clone initially and not pre-populate it.

like image 73
Schwern Avatar answered Oct 06 '22 01:10

Schwern


Looks like you might be able to use JGit for this purpose. Here's an example of using it to create an empty repository which can be connected to over http:

https://github.com/centic9/jgit-cookbook/blob/master/httpserver/src/main/java/org/dstadler/jgit/server/Main.java

like image 29
Dan Alvizu Avatar answered Oct 05 '22 23:10

Dan Alvizu