1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package appconfig
16
17 import (
18 "testing"
19 )
20
21 func TestYAMLRemoteRefParser(t *testing.T) {
22 tests := map[string]struct {
23 Input string
24 Output *RemoteRef
25 Error bool
26 }{
27 "complete": {
28 Input: "{remote: test/test, path: test.yaml, ref: main}",
29 Output: &RemoteRef{
30 Remote: "test/test",
31 Path: "test.yaml",
32 Ref: "main",
33 },
34 },
35 "missingRemote": {
36 Input: "{path: test.yaml, ref: main}",
37 Output: nil,
38 },
39 "missingPath": {
40 Input: "{remote: test/test, ref: main}",
41 Output: &RemoteRef{
42 Remote: "test/test",
43 Ref: "main",
44 },
45 },
46 "missingRef": {
47 Input: "{remote: test/test, path: test.yaml}",
48 Output: &RemoteRef{
49 Remote: "test/test",
50 Path: "test.yaml",
51 },
52 },
53 "emptyRemote": {
54 Input: "{remote: ''}",
55 Error: true,
56 },
57 "empty": {
58 Input: "",
59 Output: nil,
60 },
61 "commentsOnly": {
62 Input: "# the existence of this file enables the app\n",
63 Output: nil,
64 },
65 "extraFields": {
66 Input: "{remote: test/test, path: test.yaml, ref: main, key: value}",
67 Output: nil,
68 },
69 "onlyUnknownFields": {
70 Input: "{key: value}",
71 Output: nil,
72 },
73 }
74
75 for name, test := range tests {
76 t.Run(name, func(t *testing.T) {
77 ref, err := YAMLRemoteRefParser("test.yml", []byte(test.Input))
78 if test.Error {
79 if err == nil {
80 t.Fatal("expected error parsing ref, but got nil")
81 }
82 return
83 }
84 if err != nil {
85 t.Fatalf("unexpected error parsing ref: %v", err)
86 }
87
88 switch {
89 case test.Output == nil && ref != nil:
90 t.Errorf("expected nil ref, but got: %+v", *ref)
91 case test.Output != nil && ref == nil:
92 t.Errorf("expected %+v, but got nil", *test.Output)
93 case test.Output != nil && ref != nil:
94 if *test.Output != *ref {
95 t.Errorf("expected %+v, but got %+v", *test.Output, *ref)
96 }
97 }
98 })
99 }
100 }
101
View as plain text