58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package file
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestCopy(t *testing.T) {
|
|
contents := "COPY TEST"
|
|
|
|
tmpDir, err := os.MkdirTemp(os.TempDir(), "go-common")
|
|
if err != nil {
|
|
t.Logf("could not create temp dir: %v", err)
|
|
t.FailNow()
|
|
}
|
|
defer func() {
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
t.Logf("could not remove temp dir at %s: %v", tmpDir, err)
|
|
}
|
|
}()
|
|
|
|
tmp1, err := setupCopy(tmpDir, contents)
|
|
if err != nil {
|
|
t.Log(err)
|
|
t.FailNow()
|
|
}
|
|
|
|
tmp2 := filepath.Join(tmpDir, "tmp2")
|
|
if err := Copy(tmp1, tmp2); err != nil {
|
|
t.Logf("could not copy file: %v", err)
|
|
t.FailNow()
|
|
}
|
|
|
|
b, err := os.ReadFile(tmp2)
|
|
if err != nil {
|
|
t.Logf("could not read tmp2: %v", err)
|
|
t.FailNow()
|
|
}
|
|
|
|
if string(b) != contents {
|
|
t.Logf("contents did not match")
|
|
t.FailNow()
|
|
}
|
|
}
|
|
|
|
func setupCopy(tmpDir, contents string) (string, error) {
|
|
tmp, err := os.CreateTemp(tmpDir, "go-common")
|
|
if err != nil {
|
|
return "", fmt.Errorf("could not create temp file 1: %v", err)
|
|
}
|
|
if _, err := tmp.WriteString(contents); err != nil {
|
|
return "", fmt.Errorf("could not write to temp file 1: %v", err)
|
|
}
|
|
return tmp.Name(), tmp.Close()
|
|
}
|