git_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package git
  2. /*
  3. git_test.go
  4. Shared test helpers: an in-memory CredentialDatabase and small utilities for
  5. building throwaway repositories under t.TempDir().
  6. */
  7. import (
  8. "encoding/json"
  9. "errors"
  10. "os"
  11. "path/filepath"
  12. "sync"
  13. "testing"
  14. )
  15. // fakeDatabase is an in-memory stand-in for the ArozOS system database. It
  16. // stores marshalled JSON exactly like the real bolt-backed implementation, so
  17. // the ListTable code path is exercised for real.
  18. type fakeDatabase struct {
  19. mutex sync.Mutex
  20. tables map[string]map[string][]byte
  21. }
  22. func newFakeDatabase() *fakeDatabase {
  23. return &fakeDatabase{tables: map[string]map[string][]byte{}}
  24. }
  25. func (f *fakeDatabase) NewTable(tableName string) error {
  26. f.mutex.Lock()
  27. defer f.mutex.Unlock()
  28. if _, ok := f.tables[tableName]; !ok {
  29. f.tables[tableName] = map[string][]byte{}
  30. }
  31. return nil
  32. }
  33. func (f *fakeDatabase) Write(tableName string, key string, value interface{}) error {
  34. f.mutex.Lock()
  35. defer f.mutex.Unlock()
  36. table, ok := f.tables[tableName]
  37. if !ok {
  38. return errors.New("table not exists")
  39. }
  40. encoded, err := json.Marshal(value)
  41. if err != nil {
  42. return err
  43. }
  44. table[key] = encoded
  45. return nil
  46. }
  47. func (f *fakeDatabase) Read(tableName string, key string, assignee interface{}) error {
  48. f.mutex.Lock()
  49. defer f.mutex.Unlock()
  50. table, ok := f.tables[tableName]
  51. if !ok {
  52. return errors.New("table not exists")
  53. }
  54. encoded, ok := table[key]
  55. if !ok {
  56. return errors.New("key not exists")
  57. }
  58. return json.Unmarshal(encoded, assignee)
  59. }
  60. func (f *fakeDatabase) KeyExists(tableName string, key string) bool {
  61. f.mutex.Lock()
  62. defer f.mutex.Unlock()
  63. table, ok := f.tables[tableName]
  64. if !ok {
  65. return false
  66. }
  67. _, ok = table[key]
  68. return ok
  69. }
  70. func (f *fakeDatabase) Delete(tableName string, key string) error {
  71. f.mutex.Lock()
  72. defer f.mutex.Unlock()
  73. table, ok := f.tables[tableName]
  74. if !ok {
  75. return errors.New("table not exists")
  76. }
  77. delete(table, key)
  78. return nil
  79. }
  80. func (f *fakeDatabase) ListTable(tableName string) ([][][]byte, error) {
  81. f.mutex.Lock()
  82. defer f.mutex.Unlock()
  83. table, ok := f.tables[tableName]
  84. if !ok {
  85. return nil, errors.New("table not exists")
  86. }
  87. results := [][][]byte{}
  88. for key, value := range table {
  89. results = append(results, [][]byte{[]byte(key), value})
  90. }
  91. return results, nil
  92. }
  93. // newTestManager builds a Manager backed by a fake database and a temporary
  94. // key store.
  95. func newTestManager(t *testing.T) *Manager {
  96. t.Helper()
  97. manager, err := NewManager(Options{
  98. Database: newFakeDatabase(),
  99. KeyStorePath: filepath.Join(t.TempDir(), "keystore"),
  100. })
  101. if err != nil {
  102. t.Fatalf("NewManager() returned error: %v", err)
  103. }
  104. return manager
  105. }
  106. // newTestRepo initialises an empty repository in a fresh temp folder and
  107. // returns its path.
  108. func newTestRepo(t *testing.T, manager *Manager) string {
  109. t.Helper()
  110. repoPath := filepath.Join(t.TempDir(), "repo")
  111. if err := manager.Init(repoPath); err != nil {
  112. t.Fatalf("Init() returned error: %v", err)
  113. }
  114. return repoPath
  115. }
  116. // writeFile creates or overwrites a file inside a repository.
  117. func writeFile(t *testing.T, repoPath string, name string, content string) {
  118. t.Helper()
  119. fullPath := filepath.Join(repoPath, filepath.FromSlash(name))
  120. if err := os.MkdirAll(filepath.Dir(fullPath), 0775); err != nil {
  121. t.Fatalf("cannot create folder for %s: %v", name, err)
  122. }
  123. if err := os.WriteFile(fullPath, []byte(content), 0664); err != nil {
  124. t.Fatalf("cannot write %s: %v", name, err)
  125. }
  126. }
  127. // commitFile writes a file and commits it, returning the commit hash.
  128. func commitFile(t *testing.T, manager *Manager, repoPath string, name string, content string, message string) string {
  129. t.Helper()
  130. writeFile(t, repoPath, name, content)
  131. hash, err := manager.Commit(repoPath, &CommitRequest{
  132. Message: message,
  133. Files: []string{name},
  134. Name: "Test User",
  135. Email: "test@arozos.local",
  136. })
  137. if err != nil {
  138. t.Fatalf("Commit(%s) returned error: %v", name, err)
  139. }
  140. return hash
  141. }