| 1 |
package dots |
| 2 |
|
| 3 |
import ( |
| 4 |
"path/filepath" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func TestResolveNoArgs(t *testing.T) { |
| 10 |
result, err := Resolve([]string{}, []string{}) |
| 11 |
if err != nil { |
| 12 |
t.Fatal(err) |
| 13 |
} |
| 14 |
if len(result) != 0 { |
| 15 |
t.Errorf("Matched different number of files: got=%v, want=0", len(result)) |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
func TestResolve(t *testing.T) { |
| 20 |
result, err := Resolve([]string{"fixtures/dummy/..."}, []string{"fixtures/dummy/foo", "fixtures/dummy/UNKNOWN"}) |
| 21 |
|
| 22 |
files := []string{ |
| 23 |
filepath.FromSlash("fixtures/dummy/bar/bar1.go"), |
| 24 |
filepath.FromSlash("fixtures/dummy/bar/bar2.go"), |
| 25 |
filepath.FromSlash("fixtures/dummy/baz/baz1.go"), |
| 26 |
filepath.FromSlash("fixtures/dummy/baz/baz2.go"), |
| 27 |
filepath.FromSlash("fixtures/dummy/baz/baz3.go"), |
| 28 |
} |
| 29 |
|
| 30 |
if err != nil { |
| 31 |
t.Fatal(err) |
| 32 |
} |
| 33 |
if len(result) != len(files) { |
| 34 |
t.Fatalf("Matched different number of files: got=%v, want=%v", len(result), len(files)) |
| 35 |
} |
| 36 |
for _, r := range result { |
| 37 |
matched := false |
| 38 |
for _, e := range files { |
| 39 |
matched = matched || strings.HasSuffix(r, e) |
| 40 |
} |
| 41 |
if !matched { |
| 42 |
t.Errorf("Not supposed to match: %v", r) |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func TestResolveTests(t *testing.T) { |
| 48 |
result, err := Resolve([]string{"fixtures/withtests/..."}, []string{}) |
| 49 |
|
| 50 |
files := []string{ |
| 51 |
filepath.FromSlash("fixtures/withtests/gofile.go"), |
| 52 |
filepath.FromSlash("fixtures/withtests/testgo_test.go"), |
| 53 |
filepath.FromSlash("fixtures/withtests/xtestgo_test.go"), |
| 54 |
} |
| 55 |
|
| 56 |
if err != nil { |
| 57 |
t.Fatal(err) |
| 58 |
} |
| 59 |
if len(result) != len(files) { |
| 60 |
t.Fatalf("Matched different number of files: got=%v, want=%v", len(result), len(files)) |
| 61 |
} |
| 62 |
for _, r := range result { |
| 63 |
matched := false |
| 64 |
for _, e := range files { |
| 65 |
matched = matched || strings.HasSuffix(r, e) |
| 66 |
} |
| 67 |
if !matched { |
| 68 |
t.Errorf("Not supposed to match: %v", r) |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestPackageResolve(t *testing.T) { |
| 74 |
result, err := Resolve([]string{"github.com/mgechev/dots"}, []string{"resolve_test.go"}) |
| 75 |
|
| 76 |
files := []string{ |
| 77 |
"resolve.go", |
| 78 |
} |
| 79 |
|
| 80 |
if err != nil { |
| 81 |
t.Fatal(err) |
| 82 |
} |
| 83 |
if len(result) != len(files) { |
| 84 |
t.Fatalf("Matched different number of files: got=%v, want=%v", len(result), len(files)) |
| 85 |
} |
| 86 |
for _, r := range result { |
| 87 |
matched := false |
| 88 |
for _, e := range files { |
| 89 |
matched = matched || strings.HasSuffix(r, e) |
| 90 |
} |
| 91 |
if !matched { |
| 92 |
t.Errorf("Not supposed to match: %v", r) |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestSkipWildcard(t *testing.T) { |
| 98 |
result, err := Resolve([]string{"fixtures/dummy/"}, []string{"fixtures/dummy/..."}) |
| 99 |
|
| 100 |
files := map[string]bool{} |
| 101 |
|
| 102 |
if err != nil { |
| 103 |
t.Fatal(err) |
| 104 |
} |
| 105 |
if len(result) != len(files) { |
| 106 |
t.Fatalf("Matched different number of files: got=%v, want=%v", len(result), len(files)) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func TestPackageWildcard(t *testing.T) { |
| 111 |
result, err := Resolve([]string{"github.com/mgechev/dots/fixtures/pkg/foo/...", "github.com/mgechev/dots/fixtures/pkg/baz"}, []string{}) |
| 112 |
files := []string{ |
| 113 |
"baz1.go", |
| 114 |
"baz2.go", |
| •115 |
"foo1.go", |
| 116 |
"foo2.go", |
| 117 |
"bar1.go", |
| 118 |
} |
| 119 |
|
| 120 |
if err != nil { |
| 121 |
t.Fatal(err) |
| 122 |
} |
| 123 |
if len(result) != len(files) { |
| 124 |
t.Fatalf("Matched different number of files: got=%v, want=%v", len(result), len(files)) |
| 125 |
} |
| 126 |
for _, r := range result { |
| 127 |
matched := false |
| 128 |
for _, e := range files { |
| 129 |
matched = matched || strings.HasSuffix(r, e) |
| 130 |
} |
| 131 |
if !matched { |
| 132 |
t.Errorf("Not supposed to match: %v", r) |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func TestPackageWildcardWithSkip(t *testing.T) { |
| 138 |
result, err := Resolve([]string{"github.com/mgechev/dots/fixtures/pkg/baz"}, []string{"github.com/mgechev/dots/fixtures/pkg/foo/..."}) |
| 139 |
|
| 140 |
files := []string{ |
| 141 |
"baz1.go", |
| 142 |
"baz2.go", |
| 143 |
} |
| 144 |
|
| 145 |
if err != nil { |
| 146 |
t.Fatal(err) |
| 147 |
} |
| 148 |
if len(result) != len(files) { |
| 149 |
t.Fatalf("Matched different number of files: got=%v, want=%v", len(result), len(files)) |
| 150 |
} |
| 151 |
for _, r := range result { |
| 152 |
matched := false |
| 153 |
for _, e := range files { |
| 154 |
matched = matched || strings.HasSuffix(r, e) |
| 155 |
} |
| 156 |
if !matched { |
| 157 |
t.Errorf("Not supposed to match: %v", r) |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func TestComplainForMissingDirectories(t *testing.T) { |
| 163 |
_, err := Resolve([]string{"./fixturess"}, []string{}) |
| 164 |
|
| 165 |
if err == nil { |
| 166 |
t.Error("Should get an error") |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func TestComplainForMissingPackages(t *testing.T) { |
| 171 |
_, err := Resolve([]string{"github.com/mgechev/bazbaz"}, []string{}) |
| 172 |
|
| 173 |
if err == nil { |
| 174 |
t.Error("Should get an error") |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
func TestResolvePackages(t *testing.T) { |
| 179 |
result, err := ResolvePackages([]string{"github.com/mgechev/dots/fixtures/pkg/foo/...", "github.com/mgechev/dots/fixtures/pkg/baz"}, []string{}) |
| 180 |
if err != nil { |
| 181 |
t.Fatal(err) |
| 182 |
} |
| •183 |
if len(result) != 3 { |
| 184 |
t.Fatalf("Matched different number of files: got=%v, want=%v", len(result), 1) |
| 185 |
} |
| 186 |
for _, pkg := range result { |
| 187 |
if len(pkg) == 0 { |
| 188 |
t.Error("Empty package") |
| 189 |
} |
| 190 |
} |
| 191 |
} |