mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-21 07:21:40 -04:00
test: add WriteFile convenience function
The WriteFile function creates a new file to the specified location, and writes some contents to it.
This commit is contained in:
committed by
Lucas Bajolet
parent
da41272df5
commit
df9012dfbe
@@ -246,3 +246,19 @@ func CopyFile(t *testing.T, dest, src string) {
|
||||
t.Fatalf("failed to copy from %q -> %q: %s", src, dest, err)
|
||||
}
|
||||
}
|
||||
|
||||
// WriteFile writes `content` to a file `dest`
|
||||
//
|
||||
// The default permissions of that file is 0644
|
||||
func WriteFile(t *testing.T, dest string, content string) {
|
||||
outFile, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open/create %q: %s", dest, err)
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
_, err = fmt.Fprintf(outFile, content)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to write to file %q: %s", dest, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user