diff --git a/internal/config/config_file_test.go b/internal/config/config_file_test.go index edc3a6ae9..d19130bb5 100644 --- a/internal/config/config_file_test.go +++ b/internal/config/config_file_test.go @@ -7,6 +7,7 @@ import ( "reflect" "testing" + "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" ) @@ -96,16 +97,16 @@ github.com: defer StubBackupConfig()() _, err := ParseConfig("config.yml") - eq(t, err, nil) + assert.Nil(t, err) - expectedMain := "" + expectedMain := "# What protocol to use when performing git operations. Supported values: ssh, https\ngit_protocol: https\n# What editor gh should run when creating issues, pull requests, etc. If blank, will refer to environment.\neditor:\n# Aliases allow you to create nicknames for gh commands\naliases:\n co: pr checkout\n" expectedHosts := `github.com: user: keiyuri oauth_token: "123456" ` - eq(t, mainBuf.String(), expectedMain) - eq(t, hostsBuf.String(), expectedHosts) + assert.Equal(t, expectedMain, mainBuf.String()) + assert.Equal(t, expectedHosts, hostsBuf.String()) } func Test_parseConfigFile(t *testing.T) { diff --git a/internal/config/config_type.go b/internal/config/config_type.go index 8db26f965..a57d21dec 100644 --- a/internal/config/config_type.go +++ b/internal/config/config_type.go @@ -124,8 +124,50 @@ func NewConfig(root *yaml.Node) Config { func NewBlankConfig() Config { return NewConfig(&yaml.Node{ - Kind: yaml.DocumentNode, - Content: []*yaml.Node{{Kind: yaml.MappingNode}}, + Kind: yaml.DocumentNode, + Content: []*yaml.Node{ + { + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + HeadComment: "What protocol to use when performing git operations. Supported values: ssh, https", + Kind: yaml.ScalarNode, + Value: "git_protocol", + }, + { + Kind: yaml.ScalarNode, + Value: "https", + }, + { + HeadComment: "What editor gh should run when creating issues, pull requests, etc. If blank, will refer to environment.", + Kind: yaml.ScalarNode, + Value: "editor", + }, + { + Kind: yaml.ScalarNode, + Value: "", + }, + { + HeadComment: "Aliases allow you to create nicknames for gh commands", + Kind: yaml.ScalarNode, + Value: "aliases", + }, + { + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + Kind: yaml.ScalarNode, + Value: "co", + }, + { + Kind: yaml.ScalarNode, + Value: "pr checkout", + }, + }, + }, + }, + }, + }, }) } diff --git a/internal/config/config_type_test.go b/internal/config/config_type_test.go index 1c8105186..df33effa3 100644 --- a/internal/config/config_type_test.go +++ b/internal/config/config_type_test.go @@ -19,7 +19,8 @@ func Test_fileConfig_Set(t *testing.T) { assert.NoError(t, c.Set("github.com", "user", "hubot")) assert.NoError(t, c.Write()) - assert.Equal(t, "editor: nano\n", mainBuf.String()) + expected := "# What protocol to use when performing git operations. Supported values: ssh, https\ngit_protocol: https\n# What editor gh should run when creating issues, pull requests, etc. If blank, will refer to environment.\neditor: nano\n# Aliases allow you to create nicknames for gh commands\naliases:\n co: pr checkout\n" + assert.Equal(t, expected, mainBuf.String()) assert.Equal(t, `github.com: git_protocol: ssh user: hubot @@ -28,14 +29,29 @@ example.com: `, hostsBuf.String()) } -func Test_fileConfig_Write(t *testing.T) { +func Test_defaultConfig(t *testing.T) { mainBuf := bytes.Buffer{} hostsBuf := bytes.Buffer{} defer StubWriteConfig(&mainBuf, &hostsBuf)() - c := NewBlankConfig() - assert.NoError(t, c.Write()) + cfg := NewBlankConfig() + assert.NoError(t, cfg.Write()) - assert.Equal(t, "", mainBuf.String()) + expected := "# What protocol to use when performing git operations. Supported values: ssh, https\ngit_protocol: https\n# What editor gh should run when creating issues, pull requests, etc. If blank, will refer to environment.\neditor:\n# Aliases allow you to create nicknames for gh commands\naliases:\n co: pr checkout\n" + assert.Equal(t, expected, mainBuf.String()) assert.Equal(t, "", hostsBuf.String()) + + proto, err := cfg.Get("", "git_protocol") + assert.Nil(t, err) + assert.Equal(t, "https", proto) + + editor, err := cfg.Get("", "editor") + assert.Nil(t, err) + assert.Equal(t, "", editor) + + aliases, err := cfg.Aliases() + assert.Nil(t, err) + assert.Equal(t, len(aliases.All()), 1) + expansion, _ := aliases.Get("co") + assert.Equal(t, expansion, "pr checkout") }