-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
all_test.go
121 lines (99 loc) · 2.11 KB
/
all_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2017 The go-darwin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin
package apfs
import (
"io"
"log"
"os"
"os/exec"
"path/filepath"
"testing"
)
var (
sparseImage = filepath.Join("testdata", "apfs.sparseimage")
mountPoint = filepath.Join("testdata", "apfs")
testFileGolden = filepath.Join("testdata", "testfile.txt")
testFile = filepath.Join(mountPoint, "testfile.txt")
)
func setupTest() {
log.SetFlags(log.Lshortfile)
if err := createAPFSSparseImage(); err != nil {
log.Fatal(err)
}
if err := mountAPFSSparseImage(); err != nil {
log.Fatal(err)
}
if err := copyFile(testFile, testFileGolden); err != nil {
log.Fatal(err)
}
}
func cleanupTest() {
if err := unmountAPFSSparseImage(); err != nil {
log.Fatal(err)
}
if err := os.Remove(sparseImage); err != nil {
log.Fatal(err)
}
}
func TestMain(m *testing.M) {
setupTest()
err := m.Run()
cleanupTest()
os.Exit(err)
}
var hdiutl = filepath.Join("/usr", "bin", "hdiutil")
func createAPFSSparseImage() error {
cmd := exec.Command(hdiutl, []string{"create", "-fs", "-quiet", "APFS", "-size", "1GB", sparseImage}...)
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func mountAPFSSparseImage() error {
cmd := exec.Command(hdiutl, []string{"mount", "-mountpoint", mountPoint, sparseImage}...)
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func unmountAPFSSparseImage() error {
cmd := exec.Command(hdiutl, []string{"unmount", mountPoint}...)
if err := cmd.Run(); err != nil {
return err
}
return nil
}
// copyFile copies a file from src to dst.
func copyFile(dst, src string) error {
s, err := os.Open(src)
if err != nil {
return err
}
defer func() {
e := s.Close()
if err == nil {
err = e
}
}()
d, err := os.Create(dst)
if err != nil {
return err
}
defer func() {
e := d.Close()
if err == nil {
err = e
}
}()
_, err = io.Copy(d, s)
if err != nil {
return err
}
i, err := os.Stat(src)
if err != nil {
return err
}
return os.Chmod(dst, i.Mode())
}