Skip to content

Commit

Permalink
packer_test: add file checker
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lbajolet-hashicorp committed Oct 1, 2024
1 parent 69ec781 commit 6096a38
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packer_test/common/check/file_gadgets.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 6096a38

Please sign in to comment.