From b57de004645959771fb20f9fc16c7202f3765d5b Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Sun, 24 Mar 2024 22:06:08 -0700 Subject: [PATCH] test file not exist error --- internal/resource/file.go | 3 ++- internal/resource/file_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/resource/file.go b/internal/resource/file.go index 690538e..b4547c2 100644 --- a/internal/resource/file.go +++ b/internal/resource/file.go @@ -160,7 +160,8 @@ func (f *File) Read(ctx context.Context) ([]byte, error) { info, e := os.Stat(f.Path) if e != nil { - panic(e) + f.State = "absent" + return nil, e } f.Mtime = info.ModTime() diff --git a/internal/resource/file_test.go b/internal/resource/file_test.go index 1a73ddd..7d1294c 100644 --- a/internal/resource/file_test.go +++ b/internal/resource/file_test.go @@ -77,6 +77,18 @@ func TestReadFile(t *testing.T) { assert.YAMLEq(t, expected, string(r)) } +func TestReadFileError(t *testing.T) { + ctx := context.Background() + file, _ := filepath.Abs(filepath.Join(TempDir, "missingfile.txt")) + + f := NewFile() + assert.NotEqual(t, nil, f) + f.Path = file + _, e := f.Read(ctx) + assert.True(t, os.IsNotExist(e)) + assert.Equal(t, "absent", f.State) +} + func TestCreateFile(t *testing.T) { file, _ := filepath.Abs(filepath.Join(TempDir, "foo.txt"))