forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_test.go
40 lines (33 loc) · 994 Bytes
/
file_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
package utils
import (
"bytes"
"os"
"testing"
"github.com/pkg/errors"
"go.viam.com/test"
)
func TestResolveFile(t *testing.T) {
sentinel := "great"
_ = sentinel
resolved := ResolveFile("utils/file_test.go")
rd, err := os.ReadFile(resolved)
test.That(t, err, test.ShouldBeNil)
test.That(t, bytes.Contains(rd, []byte(`sentinel := "great"`)), test.ShouldBeTrue)
}
func TestSafeJoinDir(t *testing.T) {
parentDir := "/some/parent"
validate := func(in, expectedOut string, expectedErr error) {
t.Helper()
out, err := SafeJoinDir(parentDir, in)
if expectedErr == nil {
test.That(t, err, test.ShouldBeNil)
test.That(t, out, test.ShouldEqual, expectedOut)
} else {
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, expectedErr.Error())
}
}
validate("sub/dir", "/some/parent/sub/dir", nil)
validate("/other/parent", "/some/parent/other/parent", nil)
validate("../../../root", "", errors.New("unsafe path join"))
}