Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
fix tests 🔨
Browse files Browse the repository at this point in the history
  • Loading branch information
profclems committed Sep 20, 2020
1 parent e9bcd5c commit fb011e3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 130 deletions.
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,14 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de h1:ikNHVSjEfnvz6sxdSPCaPt572qowuyMDMJLLm3Db3ig=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 h1:XQyxROzUlZH+WIQwySDgnISgOivlhjIEwaQaJEJrrN0=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -287,6 +289,7 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 h1:AeiKBIuRw3UomYXSbLy0Mc2dDLfdtbT/IVn4keq83P0=
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
Expand All @@ -301,6 +304,7 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTm
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
95 changes: 21 additions & 74 deletions internal/config/config_new.go → internal/config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,49 +123,6 @@ func parseConfigData(data []byte) (*yaml.Node, error) {
return &root, nil
}

func isLegacy(root *yaml.Node) bool {
for _, v := range root.Content[0].Content {
if v.Value == "gitlab.com" {
return true
}
}

return false
}

func migrateConfig(filename string) error {
b, err := ReadConfigFile(filename)
if err != nil {
return err
}

var hosts map[string][]yaml.Node
err = yaml.Unmarshal(b, &hosts)
if err != nil {
return fmt.Errorf("error decoding legacy format: %w", err)
}

cfg := NewBlankConfig()
for hostname, entries := range hosts {
if len(entries) < 1 {
continue
}
mapContent := entries[0].Content
for i := 0; i < len(mapContent)-1; i += 2 {
if err := cfg.Set(hostname, mapContent[i].Value, mapContent[i+1].Value); err != nil {
return err
}
}
}

err = BackupConfigFile(filename)
if err != nil {
return fmt.Errorf("failed to back up existing config: %w", err)
}

return cfg.Write()
}

func ParseConfig(filename string) (Config, error) {
_, root, err := parseConfigFile(filename)
var confError error
Expand All @@ -177,44 +134,34 @@ func ParseConfig(filename string) (Config, error) {
return nil, err
}
}
if isLegacy(root) {

err = migrateConfig(filename)
if err != nil {
return nil, fmt.Errorf("error migrating legacy config: %w", err)
}

_, root, err = parseConfigFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to reparse migrated config: %w", err)
}
} else {
// Load local config file
if _, localRoot, err := parseConfigFile(localConfigFile()); err == nil {
if len(localRoot.Content[0].Content) > 0 {
newContent := []*yaml.Node{
{Value: "local"},
localRoot.Content[0],
}
restContent := root.Content[0].Content
root.Content[0].Content = append(newContent, restContent...)
// Load local config file
if _, localRoot, err := parseConfigFile(localConfigFile()); err == nil {
if len(localRoot.Content[0].Content) > 0 {
newContent := []*yaml.Node{
{Value: "local"},
localRoot.Content[0],
}
restContent := root.Content[0].Content
root.Content[0].Content = append(newContent, restContent...)
}
// Load aliases config file
if _, aliasesRoot, err := parseConfigFile(aliasesConfigFile()); err == nil {
if len(aliasesRoot.Content[0].Content) > 0 {
newContent := []*yaml.Node{
{Value: "aliases"},
aliasesRoot.Content[0],
}
restContent := root.Content[0].Content
root.Content[0].Content = append(newContent, restContent...)
}

// Load aliases config file
if _, aliasesRoot, err := parseConfigFile(aliasesConfigFile()); err == nil {
if len(aliasesRoot.Content[0].Content) > 0 {
newContent := []*yaml.Node{
{Value: "aliases"},
aliasesRoot.Content[0],
}
} else if !errors.Is(err, os.ErrNotExist) {
return nil, err
restContent := root.Content[0].Content
root.Content[0].Content = append(newContent, restContent...)
}
} else if !errors.Is(err, os.ErrNotExist) {
return nil, err
}


return NewConfig(root), confError
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"bytes"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -72,11 +71,13 @@ hosts:
}
}

func Test_parseConfig_AliasesFile(t *testing.T) {
defer StubConfig("", `---
gitlab.com:
username: monalisa
token: OTOKEN
func Test_parseConfig_Hosts(t *testing.T) {
defer StubConfig(`---
hosts:
gitlab.com:
username: monalisa
token: OTOKEN
`, `
`)()
// prevent using env variable for test
envToken := os.Getenv("GITLAB_TOKEN")
Expand All @@ -96,17 +97,36 @@ gitlab.com:
}
}

func Test_parseConfig_AliasesFile(t *testing.T) {
defer StubConfig("", `---
ci: pipeline ci
co: mr checkout
`)()
config, err := ParseConfig("aliases.yml")
eq(t, err, nil)
aliases, err := config.Aliases()
eq(t, err, nil)
a, isAlias := aliases.Get("ci")
eq(t, isAlias, true)
eq(t, a, "pipeline ci")
b, isAlias := aliases.Get("co")
eq(t, isAlias, true)
eq(t, b, "mr checkout")
eq(t, len(aliases.All()), 2)
}

func Test_parseConfig_hostFallback(t *testing.T) {
defer StubConfig(`---
git_protocol: ssh
`, `---
gitlab.com:
hosts:
gitlab.com:
username: monalisa
token: OTOKEN
gitlab.example.com:
gitlab.example.com:
username: wrongusername
token: NOTTHIS
git_protocol: https
`, `
`)()
config, err := ParseConfig("config.yml")
eq(t, err, nil)
Expand All @@ -121,32 +141,6 @@ gitlab.example.com:
eq(t, val, "ssh")
}

func Test_ParseConfig_migrateConfig(t *testing.T) {
defer StubConfig(`---
gitlab.com:
- username: keiyuri
token: 123456
`, `ci: pipeline ci
co: mr checkout
`)()

mainBuf := bytes.Buffer{}
aliasesBuf := bytes.Buffer{}
defer StubWriteConfig(&mainBuf, &aliasesBuf)()
defer StubBackupConfig()()

_, err := ParseConfig("config.yml")
assert.Nil(t, err)

expectedMain := "# What protocol to use when performing git operations. Supported values: ssh, https\ngit_protocol: ssh\n# What editor glab should run when creating issues, merge requests, etc. This is a global config that cannot be overridden by hostname.\neditor:\n# What browser glab should run when opening links. This is a global config that cannot be overridden by hostname.\nbrowser:\n# Git remote alias which glab should use when fetching the remote url. This can be overridden by hostname\nremote_alias: origin\n# Set your desired markdown renderer style. Available options are [dark, light, notty] or set a custom style. Refer to https://github.com/charmbracelet/glamour#styles\nglamour_style: dark\n# Allow glab to automatically check for updates and notify you when there are new updates\ncheck_update: false\n# configuration specific for gitlab instances\nhosts:\n gitlab.com:\n # What protocol to use to access the api endpoint. Supported values: http, https\n protocol: https\n # Your GitLab access token. Get an access token at https://gitlab.com/profile/personal_access_tokens\n token: 123456\n username: keiyuri\n"
expectedAliases := `ci: pipeline ci
co: mr checkout
`

assert.Equal(t, expectedMain, mainBuf.String())
assert.Equal(t, expectedAliases, aliasesBuf.String())
}

func Test_parseConfigFile(t *testing.T) {
tests := []struct {
contents string
Expand Down
18 changes: 9 additions & 9 deletions internal/config/config_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ func TestIsSelfHosted(t *testing.T) {

func Test_fileConfig_Set(t *testing.T) {
mainBuf := bytes.Buffer{}
hostsBuf := bytes.Buffer{}
defer StubWriteConfig(&mainBuf, &hostsBuf)()
aliasesBuf := bytes.Buffer{}
defer StubWriteConfig(&mainBuf, &aliasesBuf)()

c := NewBlankConfig()
assert.NoError(t, c.Set("", "editor", "nano"))
assert.NoError(t, c.Set("gitlab.com", "git_protocol", "ssh"))
assert.NoError(t, c.Set("example.com", "editor", "vim"))
assert.NoError(t, c.Set("gitlab.com", "username", "hubot"))
assert.NoError(t, c.Write())
assert.NoError(t, c.WriteAll())
//a, _ := c.Aliases()
//assert.NoError(t, a.Set("co", "mr checkout"))
//assert.NoError(t, a.Write())

expected := "# What protocol to use when performing git operations. Supported values: ssh, https\ngit_protocol: ssh\n# What editor glab should run when creating issues, merge requests, etc. This is a global config that cannot be overridden by hostname.\neditor: nano\n# What browser glab should run when opening links. This is a global config that cannot be overridden by hostname.\nbrowser:\n# Git remote alias which glab should use when fetching the remote url. This can be overridden by hostname\nremote_alias: origin\n# Set your desired markdown renderer style. Available options are [dark, light, notty] or set a custom style. Refer to https://github.com/charmbracelet/glamour#styles\nglamour_style: dark\n# Allow glab to automatically check for updates and notify you when there are new updates\ncheck_update: false\n# configuration specific for gitlab instances\nhosts:\n gitlab.com:\n # What protocol to use to access the api endpoint. Supported values: http, https\n protocol: https\n # Your GitLab access token. Get an access token at https://gitlab.com/profile/personal_access_tokens\n token:\n git_protocol: ssh\n username: hubot\n example.com:\n editor: vim\n"
assert.Equal(t, expected, mainBuf.String())
assert.Equal(t, `gitlab.com:
git_protocol: ssh
username: hubot
gitlab.example.com:
editor: vim
`, hostsBuf.String())
assert.Equal(t, `ci: pipeline ci
co: mr checkout
`, aliasesBuf.String())
}

func Test_defaultConfig(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion internal/config/local_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func localConfigFile() (conf string) {
UseGlobalConfig = false
conf = path.Join(ConfigFile())
UseGlobalConfig = useGlobalConfigDefaultValue
fmt.Println(conf)
return
}


func (a *LocalConfig) Get(key string) (string, bool) {
key = ConfigKeyEquivalence(key)
if a.Empty() {
return "", false
}
Expand All @@ -32,6 +32,7 @@ func (a *LocalConfig) Get(key string) (string, bool) {
}

func (a *LocalConfig) Set(key, value string) error {
key = ConfigKeyEquivalence(key)
err := a.SetStringValue(key, value)
if err != nil {
return fmt.Errorf("failed to update config: %w", err)
Expand Down
11 changes: 0 additions & 11 deletions internal/config/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ import (
"path"
)

func StubBackupConfig() func() {
orig := BackupConfigFile
BackupConfigFile = func(_ string) error {
return nil
}

return func() {
BackupConfigFile = orig
}
}

func StubWriteConfig(wc io.Writer, wh io.Writer) func() {
orig := WriteConfigFile
WriteConfigFile = func(fn string, data []byte) error {
Expand Down

0 comments on commit fb011e3

Please sign in to comment.