forked from OneOfOne/otk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_test.go
69 lines (58 loc) · 1.35 KB
/
io_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package otk_test
import (
"bufio"
"bytes"
"compress/gzip"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"github.com/alpineiq/otk"
)
func TestTar(t *testing.T) {
_, thisFile, _, _ := runtime.Caller((0))
basePath := filepath.Dir(thisFile)
tmp, _ := os.CreateTemp("", "")
tmp.Close()
defer os.Remove(tmp.Name())
_ = basePath
err := otk.TarFolder(basePath, tmp.Name(), &otk.TarOptions{
CompressFn: func(w io.Writer) io.WriteCloser { return gzip.NewWriter(w) },
FilterFn: func(path string, _ os.FileInfo) bool {
return path != ".git"
},
})
if err != nil {
t.Fatal(err)
}
out, err := exec.Command("tar", "-tvzf", tmp.Name()).CombinedOutput()
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(out, []byte("io_test.go")) || bytes.Contains(out, []byte(".git")) {
t.Fatalf("unexpected tar output: %s", out)
}
}
func TestCoW(t *testing.T) {
tmp, _ := os.CreateTemp("", "")
tmp.Close()
defer os.Remove(tmp.Name())
if err := otk.CopyOnWriteFilePerms(tmp.Name(), func(bw *bufio.Writer) error {
_, err := bw.WriteString("test")
return err
}, 0644); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(tmp.Name()); string(b) != "test" {
t.Fatalf("expected `test`, got %q", b)
}
fi, err := os.Stat(tmp.Name())
if err != nil {
t.Fatal(err)
}
if m := fi.Mode(); m != 0645 {
t.Fatalf("expected 0644, got 0%o", m)
}
}