|
@@ -2,6 +2,7 @@ package report
|
2
|
2
|
|
3
|
3
|
import (
|
4
|
4
|
"bytes"
|
|
5
|
+ "io/ioutil"
|
5
|
6
|
"os"
|
6
|
7
|
"path/filepath"
|
7
|
8
|
"regexp"
|
|
@@ -38,6 +39,85 @@ func TestWebList(t *testing.T) {
|
38
|
39
|
}
|
39
|
40
|
}
|
40
|
41
|
|
|
42
|
+func TestOpenSourceFile(t *testing.T) {
|
|
43
|
+ tempdir, err := ioutil.TempDir("", "")
|
|
44
|
+ if err != nil {
|
|
45
|
+ t.Fatalf("failed to create temp dir: %v", err)
|
|
46
|
+ }
|
|
47
|
+ const lsep = string(filepath.ListSeparator)
|
|
48
|
+ for _, tc := range []struct {
|
|
49
|
+ desc string
|
|
50
|
+ searchPath string
|
|
51
|
+ fs []string
|
|
52
|
+ path string
|
|
53
|
+ wantPath string // If empty, error is wanted.
|
|
54
|
+ }{
|
|
55
|
+ {
|
|
56
|
+ desc: "exact absolute path is found",
|
|
57
|
+ fs: []string{"foo/bar.txt"},
|
|
58
|
+ path: "$dir/foo/bar.txt",
|
|
59
|
+ wantPath: "$dir/foo/bar.txt",
|
|
60
|
+ },
|
|
61
|
+ {
|
|
62
|
+ desc: "exact relative path is found",
|
|
63
|
+ searchPath: "$dir",
|
|
64
|
+ fs: []string{"foo/bar.txt"},
|
|
65
|
+ path: "foo/bar.txt",
|
|
66
|
+ wantPath: "$dir/foo/bar.txt",
|
|
67
|
+ },
|
|
68
|
+ {
|
|
69
|
+ desc: "multiple search path",
|
|
70
|
+ searchPath: "some/path" + lsep + "$dir",
|
|
71
|
+ fs: []string{"foo/bar.txt"},
|
|
72
|
+ path: "foo/bar.txt",
|
|
73
|
+ wantPath: "$dir/foo/bar.txt",
|
|
74
|
+ },
|
|
75
|
+ {
|
|
76
|
+ desc: "relative path is found in parent dir",
|
|
77
|
+ searchPath: "$dir/foo/bar",
|
|
78
|
+ fs: []string{"bar.txt", "foo/bar/baz.txt"},
|
|
79
|
+ path: "bar.txt",
|
|
80
|
+ wantPath: "$dir/bar.txt",
|
|
81
|
+ },
|
|
82
|
+ {
|
|
83
|
+ desc: "error when not found",
|
|
84
|
+ path: "foo.txt",
|
|
85
|
+ },
|
|
86
|
+ } {
|
|
87
|
+ t.Run(tc.desc, func(t *testing.T) {
|
|
88
|
+ defer func() {
|
|
89
|
+ if err := os.RemoveAll(tempdir); err != nil {
|
|
90
|
+ t.Fatalf("failed to remove dir %q: %v", tempdir, err)
|
|
91
|
+ }
|
|
92
|
+ }()
|
|
93
|
+ for _, f := range tc.fs {
|
|
94
|
+ path := filepath.Join(tempdir, filepath.FromSlash(f))
|
|
95
|
+ dir := filepath.Dir(path)
|
|
96
|
+ if err := os.MkdirAll(dir, 0755); err != nil {
|
|
97
|
+ t.Fatalf("failed to create dir %q: %v", dir, err)
|
|
98
|
+ }
|
|
99
|
+ if err := ioutil.WriteFile(path, nil, 0644); err != nil {
|
|
100
|
+ t.Fatalf("failed to create file %q: %v", path, err)
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+ tc.searchPath = filepath.FromSlash(strings.Replace(tc.searchPath, "$dir", tempdir, -1))
|
|
104
|
+ tc.path = filepath.FromSlash(strings.Replace(tc.path, "$dir", tempdir, 1))
|
|
105
|
+ tc.wantPath = filepath.FromSlash(strings.Replace(tc.wantPath, "$dir", tempdir, 1))
|
|
106
|
+ if file, err := openSourceFile(tc.path, tc.searchPath); err != nil && tc.wantPath != "" {
|
|
107
|
+ t.Errorf("openSourceFile(%q, %q) = err %v, want path %q", tc.path, tc.searchPath, err, tc.wantPath)
|
|
108
|
+ } else if err == nil {
|
|
109
|
+ defer file.Close()
|
|
110
|
+ gotPath := file.Name()
|
|
111
|
+ if tc.wantPath == "" {
|
|
112
|
+ t.Errorf("openSourceFile(%q, %q) = %q, want error", tc.path, tc.searchPath, gotPath)
|
|
113
|
+ } else if gotPath != tc.wantPath {
|
|
114
|
+ t.Errorf("openSourceFile(%q, %q) = %q, want path %q", tc.path, tc.searchPath, gotPath, tc.wantPath)
|
|
115
|
+ }
|
|
116
|
+ }
|
|
117
|
+ })
|
|
118
|
+ }
|
|
119
|
+}
|
|
120
|
+
|
41
|
121
|
func TestIndentation(t *testing.T) {
|
42
|
122
|
for _, c := range []struct {
|
43
|
123
|
str string
|