1package git
2
3import (
4 "os"
5 "testing"
6
7 "github.com/go-git/go-billy/v5/util"
8 "github.com/go-git/go-git/v5/config"
9 "github.com/go-git/go-git/v5/plumbing"
10 "github.com/go-git/go-git/v5/plumbing/object"
11 "github.com/stretchr/testify/suite"
12)
13
14type OptionsSuite struct {
15 suite.Suite
16 BaseSuite
17}
18
19func TestOptionsSuite(t *testing.T) {
20 suite.Run(t, new(OptionsSuite))
21}
22
23func (s *OptionsSuite) TestCommitOptionsParentsFromHEAD() {
24 o := CommitOptions{Author: &object.Signature{}}
25 err := o.Validate(s.Repository)
26 s.NoError(err)
27 s.Len(o.Parents, 1)
28}
29
30func (s *OptionsSuite) TestResetOptionsCommitNotFound() {
31 o := ResetOptions{Commit: plumbing.NewHash("ab1b15c6f6487b4db16f10d8ec69bb8bf91dcabd")}
32 err := o.Validate(s.Repository)
33 s.NotNil(err)
34}
35
36func (s *OptionsSuite) TestCommitOptionsCommitter() {
37 sig := &object.Signature{}
38
39 o := CommitOptions{Author: sig}
40 err := o.Validate(s.Repository)
41 s.NoError(err)
42
43 s.Equal(o.Author, o.Committer)
44}
45
46func (s *OptionsSuite) TestCommitOptionsLoadGlobalConfigUser() {
47 cfg := config.NewConfig()
48 cfg.User.Name = "foo"
49 cfg.User.Email = "foo@foo.com"
50
51 clean := s.writeGlobalConfig(cfg)
52 defer clean()
53
54 o := CommitOptions{}
55 err := o.Validate(s.Repository)
56 s.NoError(err)
57
58 s.Equal("foo", o.Author.Name)
59 s.Equal("foo@foo.com", o.Author.Email)
60 s.Equal("foo", o.Committer.Name)
61 s.Equal("foo@foo.com", o.Committer.Email)
62}
63
64func (s *OptionsSuite) TestCommitOptionsLoadGlobalCommitter() {
65 cfg := config.NewConfig()
66 cfg.User.Name = "foo"
67 cfg.User.Email = "foo@foo.com"
68 cfg.Committer.Name = "bar"
69 cfg.Committer.Email = "bar@bar.com"
70
71 clean := s.writeGlobalConfig(cfg)
72 defer clean()
73
74 o := CommitOptions{}
75 err := o.Validate(s.Repository)
76 s.NoError(err)
77
78 s.Equal("foo", o.Author.Name)
79 s.Equal("foo@foo.com", o.Author.Email)
80 s.Equal("bar", o.Committer.Name)
81 s.Equal("bar@bar.com", o.Committer.Email)
82}
83
84func (s *OptionsSuite) TestCreateTagOptionsLoadGlobal() {
85 cfg := config.NewConfig()
86 cfg.User.Name = "foo"
87 cfg.User.Email = "foo@foo.com"
88
89 clean := s.writeGlobalConfig(cfg)
90 defer clean()
91
92 o := CreateTagOptions{
93 Message: "foo",
94 }
95
96 err := o.Validate(s.Repository, plumbing.ZeroHash)
97 s.NoError(err)
98
99 s.Equal("foo", o.Tagger.Name)
100 s.Equal("foo@foo.com", o.Tagger.Email)
101}
102
103func (s *OptionsSuite) writeGlobalConfig(cfg *config.Config) func() {
104 fs := s.TemporalFilesystem()
105
106 tmp, err := util.TempDir(fs, "", "test-options")
107 s.NoError(err)
108
109 err = fs.MkdirAll(fs.Join(tmp, "git"), 0777)
110 s.NoError(err)
111
112 os.Setenv("XDG_CONFIG_HOME", fs.Join(fs.Root(), tmp))
113
114 content, err := cfg.Marshal()
115 s.NoError(err)
116
117 cfgFile := fs.Join(tmp, "git/config")
118 err = util.WriteFile(fs, cfgFile, content, 0777)
119 s.NoError(err)
120
121 return func() {
122 os.Setenv("XDG_CONFIG_HOME", "")
123
124 }
125}