From ecb64c048179fbc6086e7eff23136802720c8972 Mon Sep 17 00:00:00 2001 From: Wes Morgan Date: Mon, 6 Apr 2020 10:32:06 -0600 Subject: Add Merged config ...for reading and writing global (~/.git/config) and reading system (/etc/gitconfig) configs in addition to local repo config --- config/config_test.go | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) (limited to 'config/config_test.go') diff --git a/config/config_test.go b/config/config_test.go index e5e3be5..a2ece2a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -3,6 +3,7 @@ package config import ( . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/plumbing" + format "github.com/go-git/go-git/v5/plumbing/format/config" ) type ConfigSuite struct{} @@ -60,6 +61,76 @@ func (s *ConfigSuite) TestUnmarshal(c *C) { c.Assert(cfg.Branches["master"].Merge, Equals, plumbing.ReferenceName("refs/heads/master")) } +func (s *ConfigSuite) TestMergedUnmarshal(c *C) { + localInput := []byte(`[core] + bare = true + worktree = foo + commentchar = bar +[pack] + window = 20 +[remote "origin"] + url = git@github.com:mcuadros/go-git.git + fetch = +refs/heads/*:refs/remotes/origin/* +[remote "alt"] + url = git@github.com:mcuadros/go-git.git + url = git@github.com:src-d/go-git.git + fetch = +refs/heads/*:refs/remotes/origin/* + fetch = +refs/pull/*:refs/remotes/origin/pull/* +[remote "win-local"] + url = X:\\Git\\ +[submodule "qux"] + path = qux + url = https://github.com/foo/qux.git + branch = bar +[branch "master"] + remote = origin + merge = refs/heads/master +[user] + name = Override +`) + + globalInput := []byte(` +[user] + name = Soandso + email = soandso@example.com +[core] + editor = nvim +[push] + default = simple +`) + + cfg := NewConfig() + + err := cfg.UnmarshalScoped(format.LocalScope, localInput) + c.Assert(err, IsNil) + + err = cfg.UnmarshalScoped(format.GlobalScope, globalInput) + c.Assert(err, IsNil) + + c.Assert(cfg.Core.IsBare, Equals, true) + c.Assert(cfg.Core.Worktree, Equals, "foo") + c.Assert(cfg.Core.CommentChar, Equals, "bar") + c.Assert(cfg.Pack.Window, Equals, uint(20)) + c.Assert(cfg.Remotes, HasLen, 3) + c.Assert(cfg.Remotes["origin"].Name, Equals, "origin") + c.Assert(cfg.Remotes["origin"].URLs, DeepEquals, []string{"git@github.com:mcuadros/go-git.git"}) + c.Assert(cfg.Remotes["origin"].Fetch, DeepEquals, []RefSpec{"+refs/heads/*:refs/remotes/origin/*"}) + c.Assert(cfg.Remotes["alt"].Name, Equals, "alt") + c.Assert(cfg.Remotes["alt"].URLs, DeepEquals, []string{"git@github.com:mcuadros/go-git.git", "git@github.com:src-d/go-git.git"}) + c.Assert(cfg.Remotes["alt"].Fetch, DeepEquals, []RefSpec{"+refs/heads/*:refs/remotes/origin/*", "+refs/pull/*:refs/remotes/origin/pull/*"}) + c.Assert(cfg.Remotes["win-local"].Name, Equals, "win-local") + c.Assert(cfg.Remotes["win-local"].URLs, DeepEquals, []string{"X:\\Git\\"}) + c.Assert(cfg.Submodules, HasLen, 1) + c.Assert(cfg.Submodules["qux"].Name, Equals, "qux") + c.Assert(cfg.Submodules["qux"].URL, Equals, "https://github.com/foo/qux.git") + c.Assert(cfg.Submodules["qux"].Branch, Equals, "bar") + c.Assert(cfg.Branches["master"].Remote, Equals, "origin") + c.Assert(cfg.Branches["master"].Merge, Equals, plumbing.ReferenceName("refs/heads/master")) + c.Assert(cfg.Merged.Section("user").Option("name"), Equals, "Override") + c.Assert(cfg.Merged.Section("user").Option("email"), Equals, "soandso@example.com") + c.Assert(cfg.Merged.Section("push").Option("default"), Equals, "simple") +} + func (s *ConfigSuite) TestMarshal(c *C) { output := []byte(`[core] bare = true @@ -119,6 +190,95 @@ func (s *ConfigSuite) TestMarshal(c *C) { c.Assert(string(b), Equals, string(output)) } +func (s *ConfigSuite) TestMergedMarshal(c *C) { + localOutput := []byte(`[user] + name = Override +[custom] + key = value +[core] + bare = true + worktree = bar +[pack] + window = 20 +[remote "alt"] + url = git@github.com:mcuadros/go-git.git + url = git@github.com:src-d/go-git.git + fetch = +refs/heads/*:refs/remotes/origin/* + fetch = +refs/pull/*:refs/remotes/origin/pull/* +[remote "origin"] + url = git@github.com:mcuadros/go-git.git +[remote "win-local"] + url = "X:\\Git\\" +[submodule "qux"] + url = https://github.com/foo/qux.git +[branch "master"] + remote = origin + merge = refs/heads/master +`) + + globalOutput := []byte(`[user] + name = Soandso + email = soandso@example.com +[core] + editor = nvim +[push] + default = simple +`) + + cfg := NewConfig() + + cfg.Core.IsBare = true + cfg.Core.Worktree = "bar" + cfg.Pack.Window = 20 + cfg.Remotes["origin"] = &RemoteConfig{ + Name: "origin", + URLs: []string{"git@github.com:mcuadros/go-git.git"}, + } + + cfg.Remotes["alt"] = &RemoteConfig{ + Name: "alt", + URLs: []string{"git@github.com:mcuadros/go-git.git", "git@github.com:src-d/go-git.git"}, + Fetch: []RefSpec{"+refs/heads/*:refs/remotes/origin/*", "+refs/pull/*:refs/remotes/origin/pull/*"}, + } + + cfg.Remotes["win-local"] = &RemoteConfig{ + Name: "win-local", + URLs: []string{"X:\\Git\\"}, + } + + cfg.Submodules["qux"] = &Submodule{ + Name: "qux", + URL: "https://github.com/foo/qux.git", + } + + cfg.Branches["master"] = &Branch{ + Name: "master", + Remote: "origin", + Merge: "refs/heads/master", + } + + cfg.Merged.GlobalConfig().Section("user").SetOption("name", "Soandso") + cfg.Merged.LocalConfig().Section("user").SetOption("name", "Override") + cfg.Merged.GlobalConfig().Section("user").SetOption("email", "soandso@example.com") + cfg.Merged.GlobalConfig().Section("core").AddOption("editor", "nvim") + cfg.Merged.LocalConfig().Section("custom").SetOption("key", "value") + cfg.Merged.GlobalConfig().Section("push").AddOption("default", "simple") + + c.Assert(cfg.Merged.Section("user").Option("name"), Equals, "Override") + + localBytes, err := cfg.Marshal() + c.Assert(err, IsNil) + c.Assert(string(localBytes), Equals, string(localOutput)) + + globalBytes, err := cfg.MarshalScope(format.GlobalScope) + c.Assert(err, IsNil) + c.Assert(string(globalBytes), Equals, string(globalOutput)) + + systemBytes, err := cfg.MarshalScope(format.SystemScope) + c.Assert(err, IsNil) + c.Assert(string(systemBytes), Equals, "") +} + func (s *ConfigSuite) TestUnmarshalMarshal(c *C) { input := []byte(`[core] bare = true -- cgit