From ba4b8b2b68ecbbd73a89baac17f6d07178eeec88 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 3 May 2024 11:30:15 -0400 Subject: [PATCH] test: add CopyFile convenience function to pkg The CopyFile function is essentially a go recreation of the `cp' command, which copies one file from a source path to a destination directory or file. This can be used for several tests in the future. --- test/plugin_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/plugin_test.go b/test/plugin_test.go index 53d5193a6..c4b06bd31 100644 --- a/test/plugin_test.go +++ b/test/plugin_test.go @@ -196,3 +196,41 @@ func (ts *PackerTestSuite) MakePluginDir(t *testing.T, pluginVersions ...string) } } } + +// CopyFile essentially replicates the `cp` command, for a file only. +// +// # Permissions are copied over from the source to destination +// +// The function detects if destination is a directory or a file (existent or not). +// +// If this is the former, we append the source file's basename to the +// directory and create the file from that inferred path. +func CopyFile(t *testing.T, dest, src string) { + st, err := os.Stat(src) + if err != nil { + t.Fatalf("failed to stat origin file %q: %s", src, err) + } + + // If the stat call fails, we assume dest is the destination file. + dstStat, err := os.Stat(dest) + if err == nil && dstStat.IsDir() { + dest = filepath.Join(dest, filepath.Base(src)) + } + + destFD, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, st.Mode().Perm()) + if err != nil { + t.Fatalf("failed to create cp destination file %q: %s", dest, err) + } + defer destFD.Close() + + srcFD, err := os.Open(src) + if err != nil { + t.Fatalf("failed to open source file to copy: %s", err) + } + defer srcFD.Close() + + _, err = io.Copy(destFD, srcFD) + if err != nil { + t.Fatalf("failed to copy from %q -> %q: %s", src, dest, err) + } +}