state: LocalState allows file to not exist

This commit is contained in:
Mitchell Hashimoto 2015-02-21 16:06:27 -08:00
parent 579f102f37
commit 34864a64a5
2 changed files with 17 additions and 0 deletions

View File

@ -44,6 +44,12 @@ func (s *LocalState) PersistState() error {
func (s *LocalState) RefreshState() error {
f, err := os.Open(s.Path)
if err != nil {
// It is okay if the file doesn't exist, we treat that as a nil state
if os.IsNotExist(err) {
s.state = nil
return nil
}
return err
}
defer f.Close()

View File

@ -14,6 +14,17 @@ func TestLocalState(t *testing.T) {
TestState(t, ls)
}
func TestLocalState_nonExist(t *testing.T) {
ls := &LocalState{Path: "ishouldntexist"}
if err := ls.RefreshState(); err != nil {
t.Fatalf("err: %s", err)
}
if state := ls.State(); state != nil {
t.Fatalf("bad: %#v", state)
}
}
func TestLocalState_impl(t *testing.T) {
var _ StateReader = new(LocalState)
var _ StateWriter = new(LocalState)