Debian Codegraph @

dir/fs_test.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package dir
15
16 import (
17 "bytes"
18 "path/filepath"
19 "testing"
20 )
21
22 func TestSysFS_SysPath(t *testing.T) {
23 wantPath := filepath.FromSlash("/path/notation/config.json")
24 fsys := NewSysFS("/path/notation")
25 path, err := fsys.SysPath(PathConfigFile)
26 if err != nil {
27 t.Fatalf("SysPath() failed. err = %v", err)
28 }
29 if path != wantPath {
30 t.Fatalf(`SysPath() failed. got: %q, want: %q`, path, wantPath)
31 }
32 }
33
34 func TestOsFs(t *testing.T) {
35 wantData := []byte("data")
36 fsys := NewSysFS("./testdata")
37
38 // read test file
39 path, err := fsys.Open("data.txt")
40 if err != nil {
41 t.Fatalf("Open() failed. err = %v", err)
42 }
43 data := make([]byte, 4)
44 _, err = path.Read(data)
45 if err != nil {
46 t.Fatalf("Read() failed. err = %v", err)
47 }
48 if !bytes.Equal(data, wantData) {
49 t.Fatalf("SysFS read failed. got data = %v, want %v", data, wantData)
50 }
51 }
52
53 func TestConfigFS(t *testing.T) {
54 configFS := ConfigFS()
55 path, err := configFS.SysPath(PathConfigFile)
56 if err != nil {
57 t.Fatalf("SysPath() failed. err = %v", err)
58 }
59 if path != filepath.Join(UserConfigDir, PathConfigFile) {
60 t.Fatalf(`SysPath() failed. got: %q, want: %q`, path, filepath.Join(UserConfigDir, PathConfigFile))
61 }
62 }
63
64 func TestPluginFS(t *testing.T) {
65 pluginFS := PluginFS()
66 path, err := pluginFS.SysPath("plugin")
67 if err != nil {
68 t.Fatalf("SysPath() failed. err = %v", err)
69 }
70 if path != filepath.Join(userLibexecDirPath(), PathPlugins, "plugin") {
71 t.Fatalf(`SysPath() failed. got: %q, want: %q`, path, filepath.Join(userLibexecDirPath(), PathPlugins, "plugin"))
72 }
73 }
74
75 func TestCRLFileCacheFS(t *testing.T) {
76 cacheFS := CacheFS()
77 path, err := cacheFS.SysPath(PathCRLCache)
78 if err != nil {
79 t.Fatalf("SysPath() failed. err = %v", err)
80 }
81 if path != filepath.Join(UserCacheDir, PathCRLCache) {
82 t.Fatalf(`SysPath() failed. got: %q, want: %q`, path, UserConfigDir)
83 }
84 }