Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

acctest: Gadgets for testing files #13231

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions packer_test/common/check/file_gadgets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package check

import (
"fmt"
"os"
"path/filepath"
)

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,
}
}

type fileGlob struct {
filepath string
}

func (fe fileGlob) Check(_, _ string, _ error) error {
matches, err := filepath.Glob(fe.filepath)
if err != nil {
return fmt.Errorf("error evaluating file glob pattern %q: %v", fe.filepath, err)
}

if len(matches) == 0 {
return fmt.Errorf("no matches found for file glob pattern %q", fe.filepath)
}

return nil
}

func FileGlob(filename string) Checker {
return fileGlob{
filepath: filename,
}
}
Loading