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