Integration Tests with a Local Git Server

Powerful Command-Line Applications in Go — by Ricardo Gerardi (67 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Running Commands with Contexts | TOC | Testing Commands with Mock Reso urces 👉

When you’re writing tests for your application, you need to ensure that the tests run on a reproducible environment, to guarantee that the results match the expected values. This is a challenge when executing external commands that modify the state of an external resource, as the test conditions will be different the second time you execute the tests. The first strategy you’ll apply to handle this issue involves instantiating a local Git server by using a test helper function, similarly to what you did in Testing with the Help of Test Helpers.

The test helper function setupGit uses the git command to create a Bare Git repository that works like an external Git service such as GitLab or GitHub. A bare Git repository is a repository that contains only the git data but no working directory so it cannot be used to make local modifications to the code. This characteristic makes it well suited to serve as a remote repository. For more information about it, consult the official Git book.[35]

At a high level, the helper function will perform the following steps:

  1. Create a temporary directory.
  2. Create a Bare Git repository on this temporary directory.
  3. Initialize a Git repository on the target…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.