mock_repo.go

  1package repository
  2
  3import (
  4	"crypto/sha1"
  5	"fmt"
  6
  7	"github.com/MichaelMure/git-bug/util/git"
  8	"github.com/MichaelMure/git-bug/util/lamport"
  9)
 10
 11var _ ClockedRepo = &mockRepoForTest{}
 12var _ TestedRepo = &mockRepoForTest{}
 13
 14// mockRepoForTest defines an instance of Repo that can be used for testing.
 15type mockRepoForTest struct {
 16	config       *MemConfig
 17	globalConfig *MemConfig
 18	blobs        map[git.Hash][]byte
 19	trees        map[git.Hash]string
 20	commits      map[git.Hash]commit
 21	refs         map[string]git.Hash
 22	clocks       map[string]lamport.Clock
 23}
 24
 25type commit struct {
 26	treeHash git.Hash
 27	parent   git.Hash
 28}
 29
 30func NewMockRepoForTest() *mockRepoForTest {
 31	return &mockRepoForTest{
 32		config:       NewMemConfig(),
 33		globalConfig: NewMemConfig(),
 34		blobs:        make(map[git.Hash][]byte),
 35		trees:        make(map[git.Hash]string),
 36		commits:      make(map[git.Hash]commit),
 37		refs:         make(map[string]git.Hash),
 38		clocks:       make(map[string]lamport.Clock),
 39	}
 40}
 41
 42// LocalConfig give access to the repository scoped configuration
 43func (r *mockRepoForTest) LocalConfig() Config {
 44	return r.config
 45}
 46
 47// GlobalConfig give access to the git global configuration
 48func (r *mockRepoForTest) GlobalConfig() Config {
 49	return r.globalConfig
 50}
 51
 52// GetPath returns the path to the repo.
 53func (r *mockRepoForTest) GetPath() string {
 54	return "~/mockRepo/"
 55}
 56
 57func (r *mockRepoForTest) GetUserName() (string, error) {
 58	return "René Descartes", nil
 59}
 60
 61// GetUserEmail returns the email address that the user has used to configure git.
 62func (r *mockRepoForTest) GetUserEmail() (string, error) {
 63	return "user@example.com", nil
 64}
 65
 66// GetCoreEditor returns the name of the editor that the user has used to configure git.
 67func (r *mockRepoForTest) GetCoreEditor() (string, error) {
 68	return "vi", nil
 69}
 70
 71// GetRemotes returns the configured remotes repositories.
 72func (r *mockRepoForTest) GetRemotes() (map[string]string, error) {
 73	return map[string]string{
 74		"origin": "git://github.com/MichaelMure/git-bug",
 75	}, nil
 76}
 77
 78// PushRefs push git refs to a remote
 79func (r *mockRepoForTest) PushRefs(remote string, refSpec string) (string, error) {
 80	return "", nil
 81}
 82
 83func (r *mockRepoForTest) FetchRefs(remote string, refSpec string) (string, error) {
 84	return "", nil
 85}
 86
 87func (r *mockRepoForTest) StoreData(data []byte) (git.Hash, error) {
 88	rawHash := sha1.Sum(data)
 89	hash := git.Hash(fmt.Sprintf("%x", rawHash))
 90	r.blobs[hash] = data
 91	return hash, nil
 92}
 93
 94func (r *mockRepoForTest) ReadData(hash git.Hash) ([]byte, error) {
 95	data, ok := r.blobs[hash]
 96
 97	if !ok {
 98		return nil, fmt.Errorf("unknown hash")
 99	}
100
101	return data, nil
102}
103
104func (r *mockRepoForTest) StoreTree(entries []TreeEntry) (git.Hash, error) {
105	buffer := prepareTreeEntries(entries)
106	rawHash := sha1.Sum(buffer.Bytes())
107	hash := git.Hash(fmt.Sprintf("%x", rawHash))
108	r.trees[hash] = buffer.String()
109
110	return hash, nil
111}
112
113func (r *mockRepoForTest) StoreCommit(treeHash git.Hash) (git.Hash, error) {
114	rawHash := sha1.Sum([]byte(treeHash))
115	hash := git.Hash(fmt.Sprintf("%x", rawHash))
116	r.commits[hash] = commit{
117		treeHash: treeHash,
118	}
119	return hash, nil
120}
121
122func (r *mockRepoForTest) StoreCommitWithParent(treeHash git.Hash, parent git.Hash) (git.Hash, error) {
123	rawHash := sha1.Sum([]byte(treeHash + parent))
124	hash := git.Hash(fmt.Sprintf("%x", rawHash))
125	r.commits[hash] = commit{
126		treeHash: treeHash,
127		parent:   parent,
128	}
129	return hash, nil
130}
131
132func (r *mockRepoForTest) UpdateRef(ref string, hash git.Hash) error {
133	r.refs[ref] = hash
134	return nil
135}
136
137func (r *mockRepoForTest) RefExist(ref string) (bool, error) {
138	_, exist := r.refs[ref]
139	return exist, nil
140}
141
142func (r *mockRepoForTest) CopyRef(source string, dest string) error {
143	hash, exist := r.refs[source]
144
145	if !exist {
146		return fmt.Errorf("Unknown ref")
147	}
148
149	r.refs[dest] = hash
150	return nil
151}
152
153func (r *mockRepoForTest) ListRefs(refspec string) ([]string, error) {
154	keys := make([]string, len(r.refs))
155
156	i := 0
157	for k := range r.refs {
158		keys[i] = k
159		i++
160	}
161
162	return keys, nil
163}
164
165func (r *mockRepoForTest) ListCommits(ref string) ([]git.Hash, error) {
166	var hashes []git.Hash
167
168	hash := r.refs[ref]
169
170	for {
171		commit, ok := r.commits[hash]
172
173		if !ok {
174			break
175		}
176
177		hashes = append([]git.Hash{hash}, hashes...)
178		hash = commit.parent
179	}
180
181	return hashes, nil
182}
183
184func (r *mockRepoForTest) ListEntries(hash git.Hash) ([]TreeEntry, error) {
185	var data string
186
187	data, ok := r.trees[hash]
188
189	if !ok {
190		// Git will understand a commit hash to reach a tree
191		commit, ok := r.commits[hash]
192
193		if !ok {
194			return nil, fmt.Errorf("unknown hash")
195		}
196
197		data, ok = r.trees[commit.treeHash]
198
199		if !ok {
200			return nil, fmt.Errorf("unknown hash")
201		}
202	}
203
204	return readTreeEntries(data)
205}
206
207func (r *mockRepoForTest) FindCommonAncestor(hash1 git.Hash, hash2 git.Hash) (git.Hash, error) {
208	panic("implement me")
209}
210
211func (r *mockRepoForTest) GetTreeHash(commit git.Hash) (git.Hash, error) {
212	panic("implement me")
213}
214
215func (r *mockRepoForTest) GetOrCreateClock(name string) (lamport.Clock, error) {
216	if c, ok := r.clocks[name]; ok {
217		return c, nil
218	}
219
220	c := lamport.NewMemClock()
221	r.clocks[name] = c
222	return c, nil
223}
224
225func (r *mockRepoForTest) AddRemote(name string, url string) error {
226	panic("implement me")
227}