From 34864a64a5fef59ed0cc196c1bfb1196d7fbd583 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 21 Feb 2015 16:06:27 -0800 Subject: [PATCH] state: LocalState allows file to not exist --- state/local.go | 6 ++++++ state/local_test.go | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/state/local.go b/state/local.go index ce0effe980..a18a5b5c6e 100644 --- a/state/local.go +++ b/state/local.go @@ -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() diff --git a/state/local_test.go b/state/local_test.go index 631c979486..edf8595a10 100644 --- a/state/local_test.go +++ b/state/local_test.go @@ -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)