6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-06 03:32:58 +00:00

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
This commit is contained in:
Grzegorz Dlugoszewski
2020-06-24 23:54:44 +02:00
committed by GitHub
parent 2ef739ea49
commit 8c132cdafa
26 changed files with 1452 additions and 1648 deletions

93
pkg/git/config_test.go Normal file
View File

@@ -0,0 +1,93 @@
package git
import (
"testing"
)
// cfgStub represents a gitconfig file but instead of using a global one, it creates a temporary git repo and uses its local gitconfig.
type cfgStub struct {
repo *testRepo
}
func newCfgStub(t *testing.T) *cfgStub {
r := testRepoEmpty(t)
return &cfgStub{
repo: r,
}
}
func (c *cfgStub) Get(key string) string {
cmd := gitCmd(c.repo.path, "config", "--local", key)
out, err := cmd.Output()
if err != nil {
return ""
}
lines := lines(out)
return lines[0]
}
func TestGitConfig(t *testing.T) {
tests := []struct {
name string
configMaker func(t *testing.T) *cfgStub
key string
want string
}{
{
name: "empty",
configMaker: makeConfigEmpty,
key: "gitget.host",
want: "",
},
{
name: "valid",
configMaker: makeConfigValid,
key: "gitget.host",
want: "github.com",
}, {
name: "only section name",
configMaker: makeConfigValid,
key: "gitget",
want: "",
}, {
name: "missing key",
configMaker: makeConfigValid,
key: "gitget.missingkey",
want: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfg := test.configMaker(t)
got := cfg.Get(test.key)
if got != test.want {
t.Errorf("expected %q; got %q", test.want, got)
}
})
}
}
func makeConfigEmpty(t *testing.T) *cfgStub {
c := newCfgStub(t)
c.repo.writeFile(".git/config", "")
return c
}
func makeConfigValid(t *testing.T) *cfgStub {
c := newCfgStub(t)
gitconfig := `
[user]
name = grdl
[gitget]
host = github.com
`
c.repo.writeFile(".git/config", gitconfig)
return c
}