Skip to content

Commit 0580cf0

Browse files
committed
Rewrite tests to use clean repoes
1 parent 8acade2 commit 0580cf0

File tree

2 files changed

+149
-31
lines changed

2 files changed

+149
-31
lines changed

cmd/git-goopen/git-helpers.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"path"
8+
"path/filepath"
9+
"time"
10+
11+
"github.com/go-git/go-git/v5"
12+
13+
"github.com/go-git/go-git/v5/config"
14+
"github.com/go-git/go-git/v5/plumbing"
15+
"github.com/go-git/go-git/v5/plumbing/object"
16+
)
17+
18+
// Helpers is based on
19+
// - https://github.com/thecjharries/go-git-ref-bug/
20+
21+
func newRepo(directory, repoURL, branch string) (*git.Repository, error) {
22+
23+
r, err := git.PlainInit(directory, false)
24+
if git.ErrRepositoryAlreadyExists == err {
25+
r, err = git.PlainOpen(directory)
26+
}
27+
if err != nil {
28+
return nil, err
29+
}
30+
makeACommit(r, directory, "master")
31+
_, err = r.CreateRemote(&config.RemoteConfig{
32+
Name: "example",
33+
URLs: []string{"https://github.com/git-fixtures/basic.git"},
34+
})
35+
if branch != "master" {
36+
createNewBranch(r, branch)
37+
}
38+
return r, nil
39+
}
40+
41+
func makeACommit(repo *git.Repository, directory string, random_stuff string) {
42+
// Pulled from the commit demo
43+
w, err := repo.Worktree()
44+
if err != nil {
45+
log.Fatalf("Broke: %v", err)
46+
}
47+
// Info("echo \"hello world!\" > example-git-file")
48+
filename := filepath.Join(directory, "example-git-file")
49+
err = ioutil.WriteFile(filename, []byte(random_stuff), 0644)
50+
if err != nil {
51+
log.Fatalf("Broke: %v", err)
52+
}
53+
// Info("git add example-git-file")
54+
_, err = w.Add("example-git-file")
55+
if err != nil {
56+
log.Fatalf("Broke: %v", err)
57+
}
58+
// Info("git status --porcelain")
59+
status, err := w.Status()
60+
if err != nil {
61+
log.Fatalf("Broke: %v", err)
62+
}
63+
fmt.Println(status)
64+
// Info("git commit -m \"example go-git commit\"")
65+
commit, err := w.Commit("example go-git commit", &git.CommitOptions{
66+
Author: &object.Signature{
67+
Name: "John Doe",
68+
69+
When: time.Now(),
70+
},
71+
})
72+
if err != nil {
73+
log.Fatalf("Broke: %v", err)
74+
}
75+
// Info("git show -s")
76+
obj, err := repo.CommitObject(commit)
77+
if err != nil {
78+
log.Fatalf("Broke: %v", err)
79+
}
80+
fmt.Println(obj)
81+
}
82+
83+
func createNewBranch(repo *git.Repository, branchName string) {
84+
// Pulled from the branch demo
85+
repoConfig, err := repo.Config()
86+
if err != nil {
87+
log.Fatalf("Broke: %v", err)
88+
}
89+
headRef, err := repo.Head()
90+
if err != nil {
91+
log.Fatalf("Broke: %v", err)
92+
}
93+
newRef := plumbing.NewHashReference(plumbing.NewBranchReferenceName(branchName), headRef.Hash())
94+
95+
w, err := repo.Worktree()
96+
if err != nil {
97+
log.Fatalf("Broke: %v", err)
98+
}
99+
err = w.Checkout(&git.CheckoutOptions{
100+
Branch: newRef.Name(),
101+
Keep: true,
102+
Create: true,
103+
})
104+
if err != nil {
105+
log.Fatalf("Broke: %v", err)
106+
}
107+
err = repo.Storer.SetReference(newRef)
108+
if err != nil {
109+
log.Fatalf("Broke: %v", err)
110+
}
111+
err = repo.Storer.SetConfig(repoConfig)
112+
if err != nil {
113+
log.Fatalf("Broke: %v", err)
114+
}
115+
makeACommit(repo, w.Filesystem.Root(), branchName)
116+
}
117+
118+
func saveGitConfig(directory string, conf *config.Config) error {
119+
serializedConfig, err := conf.Marshal()
120+
if err != nil {
121+
return fmt.Errorf("cannot serialize git configuration. Error: %w", err)
122+
}
123+
err = ioutil.WriteFile(path.Join(directory, ".git", "config"), serializedConfig, 0644)
124+
return err
125+
}

cmd/git-goopen/main_test.go

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package main
22

33
import (
4+
"io/ioutil"
45
"log"
6+
"os"
57
"testing"
6-
7-
"github.com/go-git/go-billy/v5/memfs"
8-
"github.com/go-git/go-git/v5"
9-
"github.com/go-git/go-git/v5/plumbing"
10-
"github.com/go-git/go-git/v5/plumbing/format/config"
11-
"github.com/go-git/go-git/v5/storage/memory"
128
)
139

1410
func Test_getURLFromGitRepo(t *testing.T) {
@@ -44,17 +40,13 @@ func Test_getURLFromGitRepo(t *testing.T) {
4440
for _, tt := range tests {
4541
t.Run(tt.name, func(t *testing.T) {
4642

47-
fs := memfs.New()
48-
// Git objects storer based on memory
49-
storer := memory.NewStorage()
43+
dir, err := ioutil.TempDir("", "clone-example-"+tt.name)
44+
if err != nil {
45+
log.Fatal(err)
46+
}
47+
defer os.RemoveAll(dir) // clean up
5048

51-
// Clones the repository into the worktree (fs) and storer all the .git
52-
// content into the storer
53-
gitRepo, err := git.Clone(storer, fs, &git.CloneOptions{
54-
URL: tt.args.gitRemote,
55-
SingleBranch: true,
56-
ReferenceName: plumbing.NewBranchReferenceName(tt.args.gitBranch),
57-
})
49+
gitRepo, err := newRepo(dir, tt.args.gitRemote, tt.args.gitBranch)
5850
if err != nil {
5951
log.Fatal(err)
6052
}
@@ -82,17 +74,17 @@ func Test_getOverwriteDomain(t *testing.T) {
8274
want string
8375
}{
8476
{
85-
name: "simple",
77+
name: "simple with overwrite",
8678
args: args{
87-
gitRemote: "git@github.com:git-fixtures/basic.git",
79+
gitRemote: "https://github.com/git-fixtures/basic",
8880
openDomain: "myrepo.com",
8981
},
9082
want: "myrepo.com",
9183
},
9284
{
93-
name: "simple",
85+
name: "simple without overwrite",
9486
args: args{
95-
gitRemote: "git@github.com:git-fixtures/basic.git",
87+
gitRemote: "https://github.com/git-fixtures/basic",
9688
openDomain: "",
9789
},
9890
want: "",
@@ -101,20 +93,21 @@ func Test_getOverwriteDomain(t *testing.T) {
10193
for _, tt := range tests {
10294
t.Run(tt.name, func(t *testing.T) {
10395

104-
fs := memfs.New()
105-
// Git objects storer based on memory
106-
storer := memory.NewStorage()
96+
dir, err := ioutil.TempDir("", "clone-example-"+tt.name)
97+
if err != nil {
98+
log.Fatal(err)
99+
}
100+
defer os.RemoveAll(dir) // clean up
107101

108-
// Clones the repository into the worktree (fs) and storer all the .git
109-
// content into the storer
110-
gitRepo, err := git.Clone(storer, fs, &git.CloneOptions{
111-
URL: tt.args.gitRemote,
112-
SingleBranch: true,
113-
ReferenceName: plumbing.NewBranchReferenceName("master"),
114-
})
102+
gitRepo, err := newRepo(dir, tt.args.gitRemote, "master")
103+
if err != nil {
104+
log.Fatal(err)
105+
}
115106

107+
// Add git config open.domain
116108
c, _ := gitRepo.Config()
117-
c.Raw.Sections = append(c.Raw.Sections, &config.Section{Name: "open", Options: config.Options{&config.Option{Key: "domain", Value: tt.args.openDomain}}})
109+
c.Raw.AddOption("open", "", "domain", tt.args.openDomain)
110+
saveGitConfig(dir, c)
118111

119112
if err != nil {
120113
log.Fatal(err)

0 commit comments

Comments
 (0)