aboutsummaryrefslogtreecommitdiffstats
path: root/options_test.go
blob: 86d725ac96c1ec8ac67e7649444f578361d0aa71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package git

import (
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/go-git/go-git/v5/config"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/plumbing/object"
	. "gopkg.in/check.v1"
)

type OptionsSuite struct {
	BaseSuite
}

var _ = Suite(&OptionsSuite{})

func (s *OptionsSuite) TestCommitOptionsParentsFromHEAD(c *C) {
	o := CommitOptions{Author: &object.Signature{}}
	err := o.Validate(s.Repository)
	c.Assert(err, IsNil)
	c.Assert(o.Parents, HasLen, 1)
}

func (s *OptionsSuite) TestCommitOptionsCommitter(c *C) {
	sig := &object.Signature{}

	o := CommitOptions{Author: sig}
	err := o.Validate(s.Repository)
	c.Assert(err, IsNil)

	c.Assert(o.Committer, Equals, o.Author)
}

func (s *OptionsSuite) TestCommitOptionsLoadGlobalConfigUser(c *C) {
	cfg := config.NewConfig()
	cfg.User.Name = "foo"
	cfg.User.Email = "foo@foo.com"

	s.writeGlobalConfig(c, cfg)
	defer s.clearGlobalConfig(c)

	o := CommitOptions{}
	err := o.Validate(s.Repository)
	c.Assert(err, IsNil)

	c.Assert(o.Author.Name, Equals, "foo")
	c.Assert(o.Author.Email, Equals, "foo@foo.com")
	c.Assert(o.Committer.Name, Equals, "foo")
	c.Assert(o.Committer.Email, Equals, "foo@foo.com")
}

func (s *OptionsSuite) TestCommitOptionsLoadGlobalCommitter(c *C) {
	cfg := config.NewConfig()
	cfg.User.Name = "foo"
	cfg.User.Email = "foo@foo.com"
	cfg.Committer.Name = "bar"
	cfg.Committer.Email = "bar@bar.com"

	s.writeGlobalConfig(c, cfg)
	defer s.clearGlobalConfig(c)

	o := CommitOptions{}
	err := o.Validate(s.Repository)
	c.Assert(err, IsNil)

	c.Assert(o.Author.Name, Equals, "foo")
	c.Assert(o.Author.Email, Equals, "foo@foo.com")
	c.Assert(o.Committer.Name, Equals, "bar")
	c.Assert(o.Committer.Email, Equals, "bar@bar.com")
}

func (s *OptionsSuite) TestCreateTagOptionsLoadGlobal(c *C) {
	cfg := config.NewConfig()
	cfg.User.Name = "foo"
	cfg.User.Email = "foo@foo.com"

	s.writeGlobalConfig(c, cfg)
	defer s.clearGlobalConfig(c)

	o := CreateTagOptions{
		Message: "foo",
	}

	err := o.Validate(s.Repository, plumbing.ZeroHash)
	c.Assert(err, IsNil)

	c.Assert(o.Tagger.Name, Equals, "foo")
	c.Assert(o.Tagger.Email, Equals, "foo@foo.com")
}

func (s *OptionsSuite) writeGlobalConfig(c *C, cfg *config.Config) {
	tmp, err := ioutil.TempDir("", "test-options")
	c.Assert(err, IsNil)

	err = os.Mkdir(filepath.Join(tmp, "git"), 0777)
	c.Assert(err, IsNil)

	os.Setenv("XDG_CONFIG_HOME", tmp)

	content, err := cfg.Marshal()
	c.Assert(err, IsNil)

	cfgFile := filepath.Join(tmp, "git/config")
	err = ioutil.WriteFile(cfgFile, content, 0777)
	c.Assert(err, IsNil)
}

func (s *OptionsSuite) clearGlobalConfig(c *C) {
	os.Setenv("XDG_CONFIG_HOME", "")
}