From 24ca48104f796a85d32e9f406c39e0467d5c0cfb Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sun, 18 Aug 2024 00:13:10 -0600 Subject: [PATCH] test: handle fs.Stat errors to prevent panics --- internal/assertions/assertions.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/assertions/assertions.go b/internal/assertions/assertions.go index a959434..a2b5096 100644 --- a/internal/assertions/assertions.go +++ b/internal/assertions/assertions.go @@ -983,8 +983,13 @@ func FileExistsFS(system fs.FS, file string) (s string) { s += bullet("error: %s\n", err) return } + if err != nil { + s = "got an unexpected error\n" + s += bullet("name: %s\n", file) + s += bullet("error: %s\n", err) + return + } - // other errors - file probably exists but cannot be read if info.IsDir() { s = "expected file but is a directory\n" s += bullet("name: %s\n", file) @@ -1016,7 +1021,12 @@ func DirExistsFS(system fs.FS, directory string) (s string) { s += bullet("error: %s\n", err) return } - // other errors - directory probably exists but cannot be read + if err != nil { + s = "got an unexpected error\n" + s += bullet("name: %s\n", directory) + s += bullet("error: %s\n", err) + return + } if !info.IsDir() { s = "expected directory but is a file\n" s += bullet("name: %s\n", directory)