state: LocalState allows file to not exist
This commit is contained in:
parent
579f102f37
commit
34864a64a5
|
@ -44,6 +44,12 @@ func (s *LocalState) PersistState() error {
|
||||||
func (s *LocalState) RefreshState() error {
|
func (s *LocalState) RefreshState() error {
|
||||||
f, err := os.Open(s.Path)
|
f, err := os.Open(s.Path)
|
||||||
if err != nil {
|
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
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
|
@ -14,6 +14,17 @@ func TestLocalState(t *testing.T) {
|
||||||
TestState(t, ls)
|
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) {
|
func TestLocalState_impl(t *testing.T) {
|
||||||
var _ StateReader = new(LocalState)
|
var _ StateReader = new(LocalState)
|
||||||
var _ StateWriter = new(LocalState)
|
var _ StateWriter = new(LocalState)
|
||||||
|
|
Loading…
Reference in New Issue