-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
git_manager.go
46 lines (39 loc) · 1.73 KB
/
git_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package test
import (
"fmt"
"path/filepath"
"github.com/git-town/git-town/v8/test/helpers"
)
// GitManager manages the Git setup for the entire test suite.
// For each scenario, it provides a standardized, empty GitEnvironment consisting of a local and remote Git repository.
//
// Setting up a GitEnvironment is an expensive operation and has to be done for every scenario.
// As a performance optimization, GitManager creates a fully set up GitEnvironment (including the main branch and configuration)
// (the "memoized" environment) at the beginning of the test suite and makes copies of it for each scenario.
// Making copies of a fully set up Git repo is much faster than creating it from scratch.
// End-to-end tests run multi-threaded, all threads share a global GitManager instance.
type GitManager struct {
counter helpers.Counter
// path of the folder that this class operates in
dir string
// the memoized environment
memoized GitEnvironment
}
// NewGitManager provides a new GitManager instance operating in the given directory.
func NewGitManager(dir string) (GitManager, error) {
memoized, err := NewStandardGitEnvironment(filepath.Join(dir, "memoized"))
if err != nil {
return GitManager{}, fmt.Errorf("cannot create memoized environment: %w", err)
}
return GitManager{
counter: helpers.Counter{},
dir: dir,
memoized: memoized,
}, nil
}
// CreateScenarioEnvironment provides a new GitEnvironment for the scenario with the given name.
func (manager *GitManager) CreateScenarioEnvironment(scenarioName string) (GitEnvironment, error) {
envDirName := helpers.FolderName(scenarioName) + "_" + manager.counter.ToString()
envPath := filepath.Join(manager.dir, envDirName)
return CloneGitEnvironment(manager.memoized, envPath)
}