github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/test/fileutil.go (about)

     1  // Copyright (c) 2015 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package test
     6  
     7  import (
     8  	"io"
     9  	"io/ioutil"
    10  	"os"
    11  	"testing"
    12  )
    13  
    14  // CopyFile copies a file
    15  func CopyFile(t *testing.T, srcPath, dstPath string) {
    16  	t.Helper()
    17  	src, err := os.Open(srcPath)
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	defer src.Close()
    22  	dst, err := os.Create(dstPath)
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	defer dst.Close()
    27  	_, err = io.Copy(dst, src)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  }
    32  
    33  // TempDir creates a temporary directory under the default directory for temporary files (see
    34  // os.TempDir) and returns the path of the new directory or fails the test trying.
    35  func TempDir(t *testing.T, dirName string) string {
    36  	t.Helper()
    37  	tempDir, err := ioutil.TempDir("", dirName)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	return tempDir
    42  }