Merge pull request #258 from TheDahv/feat-config-snapshot
Fix bug in Config LookupString
This commit is contained in:
commit
22da351b1e
|
@ -115,18 +115,20 @@ func (c *Config) LookupInt64(name string) (int64, error) {
|
|||
}
|
||||
|
||||
func (c *Config) LookupString(name string) (string, error) {
|
||||
var ptr *C.char
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
|
||||
valBuf := C.git_buf{}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
if ret := C.git_config_get_string(&ptr, c.ptr, cname); ret < 0 {
|
||||
if ret := C.git_config_get_string_buf(&valBuf, c.ptr, cname); ret < 0 {
|
||||
return "", MakeGitError(ret)
|
||||
}
|
||||
defer C.git_buf_free(&valBuf)
|
||||
|
||||
return C.GoString(ptr), nil
|
||||
return C.GoString(valBuf.ptr), nil
|
||||
}
|
||||
|
||||
func (c *Config) LookupBool(name string) (bool, error) {
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var tempConfig = "./temp.gitconfig"
|
||||
|
||||
func setupConfig() (*Config, error) {
|
||||
var (
|
||||
c *Config
|
||||
err error
|
||||
)
|
||||
|
||||
c, err = OpenOndisk(nil, tempConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.SetString("foo.bar", "baz")
|
||||
c.SetBool("foo.bool", true)
|
||||
c.SetInt32("foo.int32", 32)
|
||||
c.SetInt64("foo.int64", 64)
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
func cleanupConfig() {
|
||||
os.Remove(tempConfig)
|
||||
}
|
||||
|
||||
type TestRunner func(*Config, *testing.T)
|
||||
|
||||
var tests = []TestRunner{
|
||||
// LookupString
|
||||
func(c *Config, t *testing.T) {
|
||||
val, err := c.LookupString("foo.bar")
|
||||
if err != nil {
|
||||
t.Errorf("Got LookupString error: '%v', expected none\n", err)
|
||||
}
|
||||
if val != "baz" {
|
||||
t.Errorf("Got '%s' from LookupString, expected 'bar'\n", val)
|
||||
}
|
||||
},
|
||||
// LookupBool
|
||||
func(c *Config, t *testing.T) {
|
||||
val, err := c.LookupBool("foo.bool")
|
||||
if err != nil {
|
||||
t.Errorf("Got LookupBool error: '%v', expected none\n", err)
|
||||
}
|
||||
if !val {
|
||||
t.Errorf("Got %b from LookupBool, expected 'false'\n", val)
|
||||
}
|
||||
},
|
||||
// LookupInt32
|
||||
func(c *Config, t *testing.T) {
|
||||
val, err := c.LookupInt32("foo.int32")
|
||||
if err != nil {
|
||||
t.Errorf("Got LookupInt32 error: '%v', expected none\n", err)
|
||||
}
|
||||
if val != 32 {
|
||||
t.Errorf("Got %v, expected 32\n", val)
|
||||
}
|
||||
},
|
||||
// LookupInt64
|
||||
func(c *Config, t *testing.T) {
|
||||
val, err := c.LookupInt64("foo.int64")
|
||||
if err != nil {
|
||||
t.Errorf("Got LookupInt64 error: '%v', expected none\n", err)
|
||||
}
|
||||
if val != 64 {
|
||||
t.Errorf("Got %v, expected 64\n", val)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func TestConfigLookups(t *testing.T) {
|
||||
var (
|
||||
err error
|
||||
c *Config
|
||||
)
|
||||
|
||||
c, err = setupConfig()
|
||||
defer cleanupConfig()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Setup error: '%v'. Expected none\n", err)
|
||||
}
|
||||
defer c.Free()
|
||||
|
||||
for _, test := range tests {
|
||||
test(c, t)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue