6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 15:39:46 +00:00
Files
git-get/pkg/cfg/config_test.go
Grzegorz Dlugoszewski 8c132cdafa Remove gogit and major refactoring (#2)
* Fix typo in readme

* Reimplement all git methods without go-git

* Rename repo pkg to git, add gitconfig methods

* Improve tests for configuration reading

* Rename package file to io and move RepoFinder there

* Refactor printers

- Remove smart printer
- Decouple printers from git repos with interfaces
- Update printer functions
- Remove unnecessary flags
- Add better remote URL detection

* Update readme and go.mod

* Add author to git commit in tests

Otherwise tests will fail in CI.

* Install git before running tests and don't use cgo

* Add better error message, revert installing git

* Ensure commit message is in quotes

* Set up git config before running tests
2020-06-24 23:54:44 +02:00

118 lines
2.4 KiB
Go

package cfg
import (
"fmt"
"os"
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
envVarName = strings.ToUpper(fmt.Sprintf("%s_%s", GitgetPrefix, KeyDefaultHost))
fromGitconfig = "value.from.gitconfig"
fromEnv = "value.from.env"
fromFlag = "value.from.flag"
)
func TestConfig(t *testing.T) {
tests := []struct {
name string
configMaker func(*testing.T)
key string
want string
}{
{
name: "no config",
configMaker: testConfigEmpty,
key: KeyDefaultHost,
want: DefDefaultHost,
},
{
name: "value only in gitconfig",
configMaker: testConfigOnlyInGitconfig,
key: KeyDefaultHost,
want: fromGitconfig,
},
{
name: "value only in env var",
configMaker: testConfigOnlyInEnvVar,
key: KeyDefaultHost,
want: fromEnv,
},
{
name: "value in gitconfig and env var",
configMaker: testConfigInGitconfigAndEnvVar,
key: KeyDefaultHost,
want: fromEnv,
},
{
name: "value in flag",
configMaker: testConfigInFlag,
key: KeyDefaultHost,
want: fromFlag,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.configMaker(t)
got := viper.GetString(test.key)
if got != test.want {
t.Errorf("expected %q; got %q", test.want, got)
}
// Clear env variables and reset viper registry after each test so they impact other tests.
os.Clearenv()
viper.Reset()
})
}
}
type gitconfigEmpty struct{}
func (c *gitconfigEmpty) Get(key string) string {
return ""
}
type gitconfigValid struct{}
func (c *gitconfigValid) Get(key string) string {
return fromGitconfig
}
func testConfigEmpty(t *testing.T) {
Init(&gitconfigEmpty{})
}
func testConfigOnlyInGitconfig(t *testing.T) {
Init(&gitconfigValid{})
}
func testConfigOnlyInEnvVar(t *testing.T) {
os.Setenv(envVarName, fromEnv)
Init(&gitconfigEmpty{})
}
func testConfigInGitconfigAndEnvVar(t *testing.T) {
os.Setenv(envVarName, fromEnv)
Init(&gitconfigValid{})
}
func testConfigInFlag(t *testing.T) {
os.Setenv(envVarName, fromEnv)
cmd := cobra.Command{}
cmd.PersistentFlags().String(KeyDefaultHost, DefDefaultHost, "")
viper.BindPFlag(KeyDefaultHost, cmd.PersistentFlags().Lookup(KeyDefaultHost))
cmd.SetArgs([]string{"--" + KeyDefaultHost, fromFlag})
cmd.Execute()
Init(&gitconfigValid{})
}