From 44a94911da8a462f5055db9accff0ee8b0783c50 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 1 Oct 2024 10:55:32 -0400 Subject: [PATCH] packer_test: add FileExists checker Some tests will create files and directories as part of the execution path for Packer, and we need a way to check this, so this commit adds a new file gadget to do those checks after a command executes. --- packer_test/common/check/file_gadgets.go | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 packer_test/common/check/file_gadgets.go diff --git a/packer_test/common/check/file_gadgets.go b/packer_test/common/check/file_gadgets.go new file mode 100644 index 000000000..cb15f94e0 --- /dev/null +++ b/packer_test/common/check/file_gadgets.go @@ -0,0 +1,35 @@ +package check + +import ( + "fmt" + "os" +) + +type fileExists struct { + filepath string + isDir bool +} + +func (fe fileExists) Check(_, _ string, _ error) error { + st, err := os.Stat(fe.filepath) + if err != nil { + return fmt.Errorf("failed to stat %q: %s", fe.filepath, err) + } + + if st.IsDir() && !fe.isDir { + return fmt.Errorf("file %q is a directory, wasn't supposed to be", fe.filepath) + } + + if !st.IsDir() && fe.isDir { + return fmt.Errorf("file %q is not a directory, was supposed to be", fe.filepath) + } + + return nil +} + +func FileExists(filePath string, isDir bool) Checker { + return fileExists{ + filepath: filePath, + isDir: isDir, + } +}