-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils_test.go
96 lines (76 loc) · 2.19 KB
/
utils_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
package utils
import (
"os/exec"
"path/filepath"
"testing"
"go.viam.com/test"
)
func TestDecompressFile(t *testing.T) {
MockViamDirs(t)
td := t.TempDir()
if _, err := exec.LookPath("xz"); err != nil {
t.Skip("no xz command")
}
orig := filepath.Join(td, "plaintext")
// compress an empty file
Touch(t, orig)
_, err := exec.Command("xz", orig).Output()
test.That(t, err, test.ShouldBeNil)
// decompress
path, err := DecompressFile(orig + ".xz")
test.That(t, err, test.ShouldBeNil)
test.That(t, path, test.ShouldResemble, filepath.Join(ViamDirs["cache"], "plaintext"))
}
func TestGetFileSum(t *testing.T) {
td := t.TempDir()
path := filepath.Join(td, "checkme")
Touch(t, path)
_, err := GetFileSum(path)
test.That(t, err, test.ShouldBeNil)
}
func TestCheckIfSame(t *testing.T) {
td := t.TempDir()
path1 := filepath.Join(td, "path1")
path2 := filepath.Join(td, "path2")
Touch(t, path1)
Touch(t, path2)
link := filepath.Join(td, "link")
test.That(t, ForceSymlink(path1, link), test.ShouldBeNil)
check := func(path1, path2 string, expected bool) {
same, err := CheckIfSame(path1, path2)
test.That(t, err, test.ShouldBeNil)
test.That(t, same == expected, test.ShouldBeTrue)
}
check(path1, path1, true)
check(path1, path2, false)
check(link, link, true)
check(link, path1, true)
check(link, path2, false)
}
func TestForceSymlink(t *testing.T) {
td := t.TempDir()
path := filepath.Join(td, "link")
target := filepath.Join(td, "target")
// test initial case
err := ForceSymlink(target, path)
test.That(t, err, test.ShouldBeNil)
// test already-exists case
err = ForceSymlink(target, path)
test.That(t, err, test.ShouldBeNil)
}
func TestWriteFileIfNew(t *testing.T) {
contents := []byte("hello")
path := filepath.Join(t.TempDir(), "writeme")
// write new
written, err := WriteFileIfNew(path, contents)
test.That(t, err, test.ShouldBeNil)
test.That(t, written, test.ShouldBeTrue)
// unchanged
written, err = WriteFileIfNew(path, contents)
test.That(t, err, test.ShouldBeNil)
test.That(t, written, test.ShouldBeFalse)
// changed
written, err = WriteFileIfNew(path, []byte("other contents"))
test.That(t, err, test.ShouldBeNil)
test.That(t, written, test.ShouldBeTrue)
}