瀏覽代碼

Add tests for source file searching. (#337)

* Add tests for source file searching.

* Fix slash handling in tests on Windows.

* Fix test cleanup, it was leaving stale files.

* Fix number of args in Fatalf call
Alexey Alexandrov 7 年之前
父節點
當前提交
63f29708e4
共有 2 個文件被更改,包括 81 次插入1 次删除
  1. 1
    1
      internal/report/source.go
  2. 80
    0
      internal/report/source_test.go

+ 1
- 1
internal/report/source.go 查看文件

@@ -576,7 +576,7 @@ func openSourceFile(path, searchPath string) (*os.File, error) {
576 576
 	}
577 577
 
578 578
 	// Scan each component of the path
579
-	for _, dir := range strings.Split(searchPath, ":") {
579
+	for _, dir := range filepath.SplitList(searchPath) {
580 580
 		// Search up for every parent of each possible path.
581 581
 		for {
582 582
 			filename := filepath.Join(dir, path)

+ 80
- 0
internal/report/source_test.go 查看文件

@@ -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