| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "io/ioutil" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/go-git/go-git/v5" |
| 11 | "github.com/isacikgoz/gitbatch/internal/testlib" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | ) |
| 14 | |
| 15 | type TestHelper struct { |
| 16 | Repository *Repository |
| 17 | RepoPath string |
| 18 | } |
| 19 | |
| 20 | func InitTestRepositoryFromLocal(t *testing.T) *TestHelper { |
| 21 | testPathDir, err := ioutil.TempDir("", "gitbatch") |
| 22 | require.NoError(t, err) |
| 23 | |
| 24 | p, err := testlib.ExtractTestRepository(testPathDir) |
| 25 | require.NoError(t, err) |
| 26 | |
| 27 | r, err := InitializeRepo(p) |
| 28 | require.NoError(t, err) |
| 29 | |
| 30 | return &TestHelper{ |
| 31 | Repository: r, |
| 32 | RepoPath: p, |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func InitTestRepository(t *testing.T) *TestHelper { |
| 37 | testRepoDir, err := ioutil.TempDir("", "test-data") |
| 38 | require.NoError(t, err) |
| 39 | |
| 40 | testRepoURL := "https://gitlab.com/isacikgoz/test-data.git" |
| 41 | _, err = git.PlainClone(testRepoDir, false, &git.CloneOptions{ |
| 42 | URL: testRepoURL, |
| 43 | RecurseSubmodules: git.DefaultSubmoduleRecursionDepth, |
| 44 | }) |
| 45 | |
| 46 | time.Sleep(time.Second) |
| 47 | if err != nil && err != git.NoErrAlreadyUpToDate { |
| 48 | require.FailNow(t, err.Error()) |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | r, err := InitializeRepo(testRepoDir) |
| 53 | require.NoError(t, err) |
| 54 | |
| 55 | return &TestHelper{ |
| 56 | Repository: r, |
| 57 | RepoPath: testRepoDir, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func (h *TestHelper) CleanUp(t *testing.T) { |
| 62 | err := os.RemoveAll(filepath.Dir(h.RepoPath)) |
| 63 | require.NoError(t, err) |
| 64 | } |
| 65 | |
| 66 | func (h *TestHelper) DirtyRepoPath() string { |
| 67 | return filepath.Join(h.RepoPath, "dirty-repo") |
| 68 | } |
| 69 | |
| 70 | func (h *TestHelper) BasicRepoPath() string { |
| 71 | return filepath.Join(h.RepoPath, "basic-repo") |
| 72 | } |
| 73 | |
| 74 | func (h *TestHelper) NonRepoPath() string { |
| 75 | return filepath.Join(h.RepoPath, "non-repo") |
| 76 | } |