42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
|
|
package writer
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestWriteImageWithProgressBranches(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
src := filepath.Join(dir, "src.img")
|
||
|
|
if err := os.WriteFile(src, []byte("writer-test"), 0o644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
dest := filepath.Join(dir, "out", "dest.img")
|
||
|
|
var calls int
|
||
|
|
if err := WriteImageWithProgress(context.Background(), src, dest, func(written, total int64) {
|
||
|
|
calls++
|
||
|
|
if written == 0 || total == 0 {
|
||
|
|
t.Fatalf("unexpected progress: %d/%d", written, total)
|
||
|
|
}
|
||
|
|
}); err != nil {
|
||
|
|
t.Fatalf("WriteImageWithProgress: %v", err)
|
||
|
|
}
|
||
|
|
if calls == 0 {
|
||
|
|
t.Fatal("expected progress callback")
|
||
|
|
}
|
||
|
|
if got, err := os.ReadFile(dest); err != nil || string(got) != "writer-test" {
|
||
|
|
t.Fatalf("write result = %q err=%v", got, err)
|
||
|
|
}
|
||
|
|
if !isDevicePath("/dev/sdz") || isDevicePath(dest) {
|
||
|
|
t.Fatal("isDevicePath helper failed")
|
||
|
|
}
|
||
|
|
if err := WriteImageWithProgress(context.Background(), src, "", nil); err == nil {
|
||
|
|
t.Fatal("expected empty destination error")
|
||
|
|
}
|
||
|
|
if err := WriteImageWithProgress(context.Background(), filepath.Join(dir, "missing"), dest, nil); err == nil {
|
||
|
|
t.Fatal("expected missing source error")
|
||
|
|
}
|
||
|
|
}
|