mirror of
https://github.com/hashicorp/packer.git
synced 2026-06-09 00:32:09 -04:00
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.
This commit is contained in:
parent
2d7f523bd3
commit
ba4b8b2b68
1 changed files with 38 additions and 0 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue