diff --git a/ini.go b/ini.go index 867455f..81aeb32 100644 --- a/ini.go +++ b/ini.go @@ -64,6 +64,7 @@ func (f File) LoadFile(file string) (err error) { if err != nil { return } + defer in.Close() return f.Load(in) } diff --git a/ini_test.go b/ini_test.go index 06a4d05..7103347 100644 --- a/ini_test.go +++ b/ini_test.go @@ -4,6 +4,7 @@ import ( "reflect" "strings" "testing" + "syscall" ) func TestLoad(t *testing.T) { @@ -87,3 +88,36 @@ func TestDefinedSectionBehaviour(t *testing.T) { "a": {"this": "that"}, }) } + +func TestLoadFile(t *testing.T) { + originalOpenFiles := numFilesOpen() + + file, err := LoadFile("test.ini") + if err != nil { + t.Fatal(err) + } + + if originalOpenFiles != numFilesOpen() { + t.Error("test.ini not closed") + } + + if !reflect.DeepEqual(file, File{"default": {"stuff": "things"}}) { + t.Error("file not read correctly") + } +} + +func numFilesOpen() (num uint64) { + var rlimit syscall.Rlimit + syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit) + maxFds := int(rlimit.Cur) + + var stat syscall.Stat_t + for i := 0; i < maxFds; i++ { + if syscall.Fstat(i, &stat) == nil { + num++ + } else { + return + } + } + return +} diff --git a/test.ini b/test.ini new file mode 100644 index 0000000..d13c999 --- /dev/null +++ b/test.ini @@ -0,0 +1,2 @@ +[default] +stuff = things