|
1 | 1 | package tcurls |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "io/ioutil" |
6 | 5 | "strings" |
7 | 6 | "testing" |
8 | 7 |
|
9 | 8 | "gopkg.in/yaml.v2" |
10 | 9 | ) |
11 | 10 |
|
12 | | -const rootURL = "https://taskcluster.example.com" |
13 | | - |
14 | | -type spec struct { |
15 | | - FunctionType string `yaml:"type"` |
16 | | - ExpectedURL string `yaml:"expectedUrl"` |
17 | | - OldExpectedURL string `yaml:"oldExpectedUrl"` |
18 | | - ArgSets [][]string `yaml:"argSets"` |
| 11 | +type TestCase struct { |
| 12 | + Function string `yaml:"function"` |
| 13 | + Expected map[string]string `yaml:"expected"` |
| 14 | + ArgSets [][]string `yaml:"argSets"` |
19 | 15 | } |
20 | 16 |
|
21 | | -type document struct { |
22 | | - Specs []spec `yaml:"specs"` |
| 17 | +type TestsSpecification struct { |
| 18 | + RootURLs map[string][]string `yaml:"rootURLs"` |
| 19 | + Tests []TestCase `yaml:"tests"` |
23 | 20 | } |
24 | 21 |
|
25 | | -func testFunc(t *testing.T, functionType string, root string, args ...string) (string, error) { |
26 | | - switch functionType { |
| 22 | +func testFunc(t *testing.T, function string, expectedURL string, root string, args ...string) { |
| 23 | + var actualURL string |
| 24 | + switch function { |
27 | 25 | case "api": |
28 | | - return API(root, args[0], args[1], args[2]), nil |
| 26 | + actualURL = API(root, args[0], args[1], args[2]) |
29 | 27 | case "apiReference": |
30 | | - return APIReference(root, args[0], args[1]), nil |
| 28 | + actualURL = APIReference(root, args[0], args[1]) |
31 | 29 | case "docs": |
32 | | - return Docs(root, args[0]), nil |
| 30 | + actualURL = Docs(root, args[0]) |
33 | 31 | case "exchangeReference": |
34 | | - return ExchangeReference(root, args[0], args[1]), nil |
| 32 | + actualURL = ExchangeReference(root, args[0], args[1]) |
35 | 33 | case "schema": |
36 | | - return Schema(root, args[0], args[1]), nil |
| 34 | + actualURL = Schema(root, args[0], args[1]) |
37 | 35 | case "ui": |
38 | | - return UI(root, args[0]), nil |
| 36 | + actualURL = UI(root, args[0]) |
39 | 37 | case "servicesManifest": |
40 | | - return ServicesManifest(root), nil |
| 38 | + actualURL = ServicesManifest(root) |
41 | 39 | default: |
42 | | - return "", fmt.Errorf("Unknown function type: %s", functionType) |
| 40 | + t.Errorf("Unknown function type: %s", function) |
| 41 | + return |
| 42 | + } |
| 43 | + if expectedURL != actualURL { |
| 44 | + t.Errorf("%v %v(%v) = `%v` but should be `%v`", redCross(), function, quotedList(root, args), actualURL, expectedURL) |
| 45 | + return |
43 | 46 | } |
| 47 | + t.Logf("%v %v(%v) = `%v`", greenTick(), function, quotedList(root, args), actualURL) |
44 | 48 | } |
45 | 49 |
|
46 | 50 | func TestURLs(t *testing.T) { |
47 | | - data, err := ioutil.ReadFile("specification.yml") |
| 51 | + data, err := ioutil.ReadFile("tests.yml") |
48 | 52 | if err != nil { |
49 | 53 | t.Error(err) |
50 | 54 | } |
51 | | - var specs document |
52 | | - err = yaml.Unmarshal([]byte(data), &specs) |
| 55 | + var spec TestsSpecification |
| 56 | + err = yaml.Unmarshal([]byte(data), &spec) |
53 | 57 | if err != nil { |
54 | 58 | t.Error(err) |
55 | 59 | } |
56 | 60 |
|
57 | | - for _, test := range specs.Specs { |
| 61 | + for _, test := range spec.Tests { |
58 | 62 | for _, argSet := range test.ArgSets { |
59 | | - |
60 | | - // Test "new" URLs |
61 | | - result, err := testFunc(t, test.FunctionType, rootURL, argSet...) |
62 | | - if err != nil { |
63 | | - t.Error(err) |
64 | | - continue |
65 | | - } |
66 | | - if result != test.ExpectedURL { |
67 | | - t.Errorf("URL is not correct. Got %q wanted %q", result, test.ExpectedURL) |
68 | | - continue |
| 63 | + for cluster, rootURLs := range spec.RootURLs { |
| 64 | + for _, rootURL := range rootURLs { |
| 65 | + testFunc(t, test.Function, test.Expected[cluster], rootURL, argSet...) |
| 66 | + } |
69 | 67 | } |
70 | | - result, err = testFunc(t, test.FunctionType, fmt.Sprintf("%s/", rootURL), argSet...) |
71 | | - if err != nil { |
72 | | - t.Error(err) |
73 | | - } |
74 | | - if result != test.ExpectedURL { |
75 | | - t.Errorf("URL is not correct. Got %q wanted %q", result, test.ExpectedURL) |
76 | | - continue |
77 | | - } |
78 | | - t.Logf(`%v %v(%v) = %q`, greenTick(), test.FunctionType, quotedList(rootURL, argSet), result) |
79 | | - |
80 | | - // Test "old" URLs |
81 | | - result, err = testFunc(t, test.FunctionType, oldRootURL, argSet...) |
82 | | - if err != nil { |
83 | | - t.Error(err) |
84 | | - } |
85 | | - if result != test.OldExpectedURL { |
86 | | - t.Errorf("URL is not correct. Got %q wanted %q", result, test.OldExpectedURL) |
87 | | - } |
88 | | - result, err = testFunc(t, test.FunctionType, fmt.Sprintf("%s/", oldRootURL), argSet...) |
89 | | - if err != nil { |
90 | | - t.Error(err) |
91 | | - } |
92 | | - if result != test.OldExpectedURL { |
93 | | - t.Errorf("URL is not correct. Got %q wanted %q", result, test.OldExpectedURL) |
94 | | - } |
95 | | - t.Logf(`%v %v(%v) = %q`, greenTick(), test.FunctionType, quotedList(oldRootURL, argSet), result) |
96 | 68 | } |
97 | 69 | } |
98 | 70 | } |
99 | 71 |
|
100 | | -// quotedList returns a quoted list of the arguments passed in |
| 72 | +// quotedList returns a backtick-quoted list of the arguments passed in |
101 | 73 | func quotedList(url string, args []string) string { |
102 | 74 | all := append([]string{url}, args...) |
103 | | - return `'` + strings.Join(all, `', '`) + `'` |
| 75 | + return "`" + strings.Join(all, "`, `") + "`" |
104 | 76 | } |
105 | 77 |
|
106 | 78 | // greenTick returns an ANSI string including escape codes to render a light |
107 | 79 | // green tick (✓) in a color console |
108 | 80 | func greenTick() string { |
109 | 81 | return string([]byte{0x1b, 0x5b, 0x33, 0x32, 0x6d, 0xe2, 0x9c, 0x93, 0x1b, 0x5b, 0x30, 0x6d}) |
110 | 82 | } |
| 83 | + |
| 84 | +func redCross() string { |
| 85 | + return "❌" |
| 86 | +} |
0 commit comments