From df9012dfbe337d7f8fd2fa99c5e0af97d41728a7 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 3 May 2024 16:38:08 -0400 Subject: [PATCH] test: add WriteFile convenience function The WriteFile function creates a new file to the specified location, and writes some contents to it. --- test/plugin_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/plugin_test.go b/test/plugin_test.go index e97c0c21f..744a91afb 100644 --- a/test/plugin_test.go +++ b/test/plugin_test.go @@ -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) + } +}