| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package tools |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"path" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
) |
| 13 |
|
| 14 |
func TestCheckCloneFileSupported(t *testing.T) { |
| 15 |
as := assert.New(t) |
| 16 |
|
| 17 |
|
| 18 |
ok, err := CheckCloneFileSupported(os.TempDir()) |
| 19 |
|
| 20 |
|
| 21 |
t.Logf("ok = %v, err = %v", ok, err) |
| 22 |
|
| 23 |
if !checkCloneFileSupported() { |
| 24 |
as.EqualError(err, "unsupported OS version. >= 10.12.x Sierra required") |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func TestCloneFile(t *testing.T) { |
| 29 |
as := assert.New(t) |
| 30 |
|
| 31 |
|
| 32 |
ok, err := CloneFile(nil, nil) |
| 33 |
|
| 34 |
|
| 35 |
as.NoError(err) |
| 36 |
as.False(ok) |
| 37 |
} |
| 38 |
|
| 39 |
func TestCloneFileByPath(t *testing.T) { |
| 40 |
if !cloneFileSupported { |
| 41 |
t.Skip("clone not supported on this platform") |
| 42 |
} |
| 43 |
|
| 44 |
src := path.Join(os.TempDir(), "src") |
| 45 |
t.Logf("src = %s", src) |
| 46 |
|
| 47 |
dst := path.Join(os.TempDir(), "dst") |
| 48 |
t.Logf("dst = %s", dst) |
| 49 |
|
| 50 |
as := assert.New(t) |
| 51 |
|
| 52 |
|
| 53 |
err := os.WriteFile(src, []byte("TEST"), 0666) |
| 54 |
as.NoError(err) |
| 55 |
|
| 56 |
|
| 57 |
ok, err := CloneFileByPath(dst, src) |
| 58 |
if err != nil { |
| 59 |
if cloneFileError, ok := err.(*CloneFileError); ok && cloneFileError.Unsupported { |
| 60 |
t.Log(err) |
| 61 |
t.Skip("tmp file is not support clonefile in this os installation.") |
| 62 |
} |
| 63 |
t.Error(err) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
as.NoError(err) |
| 68 |
as.True(ok) |
| 69 |
|
| 70 |
dstContents, err := os.ReadFile(dst) |
| 71 |
as.NoError(err) |
| 72 |
as.Equal("TEST", string(dstContents)) |
| 73 |
} |