-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil_test.go
57 lines (48 loc) · 1.52 KB
/
util_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
// Copyright 2017 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package testutil_test
import (
"os"
"path/filepath"
"testing"
"github.com/grailbio/testutil"
"github.com/stretchr/testify/require"
)
func setEnvWithReset(t *testing.T, key string, value string) func() {
origValue, hasValue := os.LookupEnv(key)
err := os.Setenv(key, value)
require.NoError(t, err)
return func() {
if hasValue {
os.Setenv(key, origValue)
} else {
os.Unsetenv(key)
}
}
}
func clearEnvWithReset(t *testing.T, key string) func() {
origValue, hasValue := os.LookupEnv(key)
err := os.Unsetenv(key)
require.NoError(t, err)
return func() {
if hasValue {
os.Setenv(key, origValue)
}
}
}
func TestGetFilePathInGrailEnv(t *testing.T) {
defer clearEnvWithReset(t, "TEST_SRCDIR")()
grailPath := "/grail"
defer setEnvWithReset(t, "GRAIL", grailPath)()
require.Equal(t, filepath.Join(grailPath, "foo/bar/baz"), testutil.GetFilePath("foo/bar/baz"))
}
func TestGetFilePathInBazelEnv(t *testing.T) {
defer clearEnvWithReset(t, "TEST_SRCDIR")()
bazelSrc := "/bazel_src"
defer setEnvWithReset(t, "TEST_SRCDIR", bazelSrc)()
bazelSpace := "/bazel_space"
defer setEnvWithReset(t, "TEST_WORKSPACE", bazelSpace)()
require.Equal(t, filepath.Join(bazelSrc, bazelSpace, "//foo/bar/baz"), testutil.GetFilePath("//foo/bar/baz"))
require.Equal(t, filepath.Join(bazelSrc, bazelSpace, "external/ws", "//foo/bar/baz"), testutil.GetFilePath("@ws//foo/bar/baz"))
}