summaryrefslogtreecommitdiffstatshomepage
path: root/repository/mock_repo.go
diff options
context:
space:
mode:
Diffstat (limited to 'repository/mock_repo.go')
-rw-r--r--repository/mock_repo.go166
1 files changed, 118 insertions, 48 deletions
diff --git a/repository/mock_repo.go b/repository/mock_repo.go
index 576e984eb..628939aab 100644
--- a/repository/mock_repo.go
+++ b/repository/mock_repo.go
@@ -5,6 +5,8 @@ import (
"fmt"
"strings"
+ "github.com/99designs/keyring"
+
"github.com/MichaelMure/git-bug/util/lamport"
)
@@ -13,85 +15,143 @@ var _ TestedRepo = &mockRepoForTest{}
// mockRepoForTest defines an instance of Repo that can be used for testing.
type mockRepoForTest struct {
- config *MemConfig
- globalConfig *MemConfig
- blobs map[Hash][]byte
- trees map[Hash]string
- commits map[Hash]commit
- refs map[string]Hash
- clocks map[string]lamport.Clock
-}
-
-type commit struct {
- treeHash Hash
- parent Hash
+ *mockRepoConfig
+ *mockRepoKeyring
+ *mockRepoCommon
+ *mockRepoData
+ *mockRepoClock
}
func NewMockRepoForTest() *mockRepoForTest {
return &mockRepoForTest{
- config: NewMemConfig(),
+ mockRepoConfig: NewMockRepoConfig(),
+ mockRepoKeyring: NewMockRepoKeyring(),
+ mockRepoCommon: NewMockRepoCommon(),
+ mockRepoData: NewMockRepoData(),
+ mockRepoClock: NewMockRepoClock(),
+ }
+}
+
+var _ RepoConfig = &mockRepoConfig{}
+
+type mockRepoConfig struct {
+ localConfig *MemConfig
+ globalConfig *MemConfig
+}
+
+func NewMockRepoConfig() *mockRepoConfig {
+ return &mockRepoConfig{
+ localConfig: NewMemConfig(),
globalConfig: NewMemConfig(),
- blobs: make(map[Hash][]byte),
- trees: make(map[Hash]string),
- commits: make(map[Hash]commit),
- refs: make(map[string]Hash),
- clocks: make(map[string]lamport.Clock),
}
}
// LocalConfig give access to the repository scoped configuration
-func (r *mockRepoForTest) LocalConfig() Config {
- return r.config
+func (r *mockRepoConfig) LocalConfig() Config {
+ return r.localConfig
}
// GlobalConfig give access to the git global configuration
-func (r *mockRepoForTest) GlobalConfig() Config {
+func (r *mockRepoConfig) GlobalConfig() Config {
return r.globalConfig
}
+// AnyConfig give access to a merged local/global configuration
+func (r *mockRepoConfig) AnyConfig() ConfigRead {
+ return mergeConfig(r.localConfig, r.globalConfig)
+}
+
+var _ RepoKeyring = &mockRepoKeyring{}
+
+type mockRepoKeyring struct {
+ keyring *keyring.ArrayKeyring
+}
+
+func NewMockRepoKeyring() *mockRepoKeyring {
+ return &mockRepoKeyring{
+ keyring: keyring.NewArrayKeyring(nil),
+ }
+}
+
+// Keyring give access to a user-wide storage for secrets
+func (r *mockRepoKeyring) Keyring() Keyring {
+ return r.keyring
+}
+
+var _ RepoCommon = &mockRepoCommon{}
+
+type mockRepoCommon struct{}
+
+func NewMockRepoCommon() *mockRepoCommon {
+ return &mockRepoCommon{}
+}
+
// GetPath returns the path to the repo.
-func (r *mockRepoForTest) GetPath() string {
+func (r *mockRepoCommon) GetPath() string {
return "~/mockRepo/"
}
-func (r *mockRepoForTest) GetUserName() (string, error) {
+func (r *mockRepoCommon) GetUserName() (string, error) {
return "René Descartes", nil
}
// GetUserEmail returns the email address that the user has used to configure git.
-func (r *mockRepoForTest) GetUserEmail() (string, error) {
+func (r *mockRepoCommon) GetUserEmail() (string, error) {
return "user@example.com", nil
}
// GetCoreEditor returns the name of the editor that the user has used to configure git.
-func (r *mockRepoForTest) GetCoreEditor() (string, error) {
+func (r *mockRepoCommon) GetCoreEditor() (string, error) {
return "vi", nil
}
// GetRemotes returns the configured remotes repositories.
-func (r *mockRepoForTest) GetRemotes() (map[string]string, error) {
+func (r *mockRepoCommon) GetRemotes() (map[string]string, error) {
return map[string]string{
"origin": "git://github.com/MichaelMure/git-bug",
}, nil
}
+var _ RepoData = &mockRepoData{}
+
+type commit struct {
+ treeHash Hash
+ parent Hash
+}
+
+type mockRepoData struct {
+ blobs map[Hash][]byte
+ trees map[Hash]string
+ commits map[Hash]commit
+ refs map[string]Hash
+}
+
+func NewMockRepoData() *mockRepoData {
+ return &mockRepoData{
+ blobs: make(map[Hash][]byte),
+ trees: make(map[Hash]string),
+ commits: make(map[Hash]commit),
+ refs: make(map[string]Hash),
+ }
+}
+
// PushRefs push git refs to a remote
-func (r *mockRepoForTest) PushRefs(remote string, refSpec string) (string, error) {
+func (r *mockRepoData) PushRefs(remote string, refSpec string) (string, error) {
return "", nil
}
-func (r *mockRepoForTest) FetchRefs(remote string, refSpec string) (string, error) {
+func (r *mockRepoData) FetchRefs(remote string, refSpec string) (string, error) {
return "", nil
}
-func (r *mockRepoForTest) StoreData(data []byte) (Hash, error) {
+func (r *mockRepoData) StoreData(data []byte) (Hash, error) {
rawHash := sha1.Sum(data)
hash := Hash(fmt.Sprintf("%x", rawHash))
r.blobs[hash] = data
return hash, nil
}
-func (r *mockRepoForTest) ReadData(hash Hash) ([]byte, error) {
+func (r *mockRepoData) ReadData(hash Hash) ([]byte, error) {
data, ok := r.blobs[hash]
if !ok {
@@ -101,7 +161,7 @@ func (r *mockRepoForTest) ReadData(hash Hash) ([]byte, error) {
return data, nil
}
-func (r *mockRepoForTest) StoreTree(entries []TreeEntry) (Hash, error) {
+func (r *mockRepoData) StoreTree(entries []TreeEntry) (Hash, error) {
buffer := prepareTreeEntries(entries)
rawHash := sha1.Sum(buffer.Bytes())
hash := Hash(fmt.Sprintf("%x", rawHash))
@@ -110,7 +170,7 @@ func (r *mockRepoForTest) StoreTree(entries []TreeEntry) (Hash, error) {
return hash, nil
}
-func (r *mockRepoForTest) StoreCommit(treeHash Hash) (Hash, error) {
+func (r *mockRepoData) StoreCommit(treeHash Hash) (Hash, error) {
rawHash := sha1.Sum([]byte(treeHash))
hash := Hash(fmt.Sprintf("%x", rawHash))
r.commits[hash] = commit{
@@ -119,7 +179,7 @@ func (r *mockRepoForTest) StoreCommit(treeHash Hash) (Hash, error) {
return hash, nil
}
-func (r *mockRepoForTest) StoreCommitWithParent(treeHash Hash, parent Hash) (Hash, error) {
+func (r *mockRepoData) StoreCommitWithParent(treeHash Hash, parent Hash) (Hash, error) {
rawHash := sha1.Sum([]byte(treeHash + parent))
hash := Hash(fmt.Sprintf("%x", rawHash))
r.commits[hash] = commit{
@@ -129,22 +189,22 @@ func (r *mockRepoForTest) StoreCommitWithParent(treeHash Hash, parent Hash) (Has
return hash, nil
}
-func (r *mockRepoForTest) UpdateRef(ref string, hash Hash) error {
+func (r *mockRepoData) UpdateRef(ref string, hash Hash) error {
r.refs[ref] = hash
return nil
}
-func (r *mockRepoForTest) RemoveRef(ref string) error {
+func (r *mockRepoData) RemoveRef(ref string) error {
delete(r.refs, ref)
return nil
}
-func (r *mockRepoForTest) RefExist(ref string) (bool, error) {
+func (r *mockRepoData) RefExist(ref string) (bool, error) {
_, exist := r.refs[ref]
return exist, nil
}
-func (r *mockRepoForTest) CopyRef(source string, dest string) error {
+func (r *mockRepoData) CopyRef(source string, dest string) error {
hash, exist := r.refs[source]
if !exist {
@@ -155,11 +215,11 @@ func (r *mockRepoForTest) CopyRef(source string, dest string) error {
return nil
}
-func (r *mockRepoForTest) ListRefs(refspec string) ([]string, error) {
+func (r *mockRepoData) ListRefs(refPrefix string) ([]string, error) {
var keys []string
for k := range r.refs {
- if strings.HasPrefix(k, refspec) {
+ if strings.HasPrefix(k, refPrefix) {
keys = append(keys, k)
}
}
@@ -167,7 +227,7 @@ func (r *mockRepoForTest) ListRefs(refspec string) ([]string, error) {
return keys, nil
}
-func (r *mockRepoForTest) ListCommits(ref string) ([]Hash, error) {
+func (r *mockRepoData) ListCommits(ref string) ([]Hash, error) {
var hashes []Hash
hash := r.refs[ref]
@@ -186,7 +246,7 @@ func (r *mockRepoForTest) ListCommits(ref string) ([]Hash, error) {
return hashes, nil
}
-func (r *mockRepoForTest) ReadTree(hash Hash) ([]TreeEntry, error) {
+func (r *mockRepoData) ReadTree(hash Hash) ([]TreeEntry, error) {
var data string
data, ok := r.trees[hash]
@@ -209,7 +269,7 @@ func (r *mockRepoForTest) ReadTree(hash Hash) ([]TreeEntry, error) {
return readTreeEntries(data)
}
-func (r *mockRepoForTest) FindCommonAncestor(hash1 Hash, hash2 Hash) (Hash, error) {
+func (r *mockRepoData) FindCommonAncestor(hash1 Hash, hash2 Hash) (Hash, error) {
ancestor1 := []Hash{hash1}
for hash1 != "" {
@@ -241,7 +301,7 @@ func (r *mockRepoForTest) FindCommonAncestor(hash1 Hash, hash2 Hash) (Hash, erro
}
}
-func (r *mockRepoForTest) GetTreeHash(commit Hash) (Hash, error) {
+func (r *mockRepoData) GetTreeHash(commit Hash) (Hash, error) {
c, ok := r.commits[commit]
if !ok {
return "", fmt.Errorf("unknown commit")
@@ -250,7 +310,21 @@ func (r *mockRepoForTest) GetTreeHash(commit Hash) (Hash, error) {
return c.treeHash, nil
}
-func (r *mockRepoForTest) GetOrCreateClock(name string) (lamport.Clock, error) {
+func (r *mockRepoData) AddRemote(name string, url string) error {
+ panic("implement me")
+}
+
+type mockRepoClock struct {
+ clocks map[string]lamport.Clock
+}
+
+func NewMockRepoClock() *mockRepoClock {
+ return &mockRepoClock{
+ clocks: make(map[string]lamport.Clock),
+ }
+}
+
+func (r *mockRepoClock) GetOrCreateClock(name string) (lamport.Clock, error) {
if c, ok := r.clocks[name]; ok {
return c, nil
}
@@ -259,7 +333,3 @@ func (r *mockRepoForTest) GetOrCreateClock(name string) (lamport.Clock, error) {
r.clocks[name] = c
return c, nil
}
-
-func (r *mockRepoForTest) AddRemote(name string, url string) error {
- panic("implement me")
-}