| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package yaml |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
) |
| 11 |
|
| 12 |
func TestDeriveSeqIndentStyle(t *testing.T) { |
| 13 |
type testCase struct { |
| 14 |
name string |
| 15 |
input string |
| 16 |
expectedOutput string |
| 17 |
} |
| 18 |
|
| 19 |
testCases := []testCase{ |
| 20 |
{ |
| 21 |
name: "detect simple wide indent", |
| 22 |
input: `apiVersion: apps/v1 |
| 23 |
kind: Deployment |
| 24 |
spec: |
| 25 |
- foo |
| 26 |
- bar |
| 27 |
- baz |
| 28 |
`, |
| 29 |
expectedOutput: `wide`, |
| 30 |
}, |
| 31 |
{ |
| 32 |
name: "detect simple compact indent", |
| 33 |
input: `apiVersion: apps/v1 |
| 34 |
kind: Deployment |
| 35 |
spec: |
| 36 |
- foo |
| 37 |
- bar |
| 38 |
- baz |
| 39 |
`, |
| 40 |
expectedOutput: `compact`, |
| 41 |
}, |
| 42 |
{ |
| 43 |
name: "read with mixed indentation, wide first", |
| 44 |
input: `apiVersion: apps/v1 |
| 45 |
kind: Deployment |
| 46 |
spec: |
| 47 |
- foo |
| 48 |
- bar |
| 49 |
- baz |
| 50 |
env: |
| 51 |
- foo |
| 52 |
- bar |
| 53 |
`, |
| 54 |
expectedOutput: `wide`, |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "read with mixed indentation, compact first", |
| 58 |
input: `apiVersion: apps/v1 |
| 59 |
kind: Deployment |
| 60 |
spec: |
| 61 |
- foo |
| 62 |
- bar |
| 63 |
- baz |
| 64 |
env: |
| 65 |
- foo |
| 66 |
- bar |
| 67 |
`, |
| 68 |
expectedOutput: `compact`, |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "read with mixed indentation, compact first with less elements", |
| 72 |
input: `apiVersion: apps/v1 |
| 73 |
kind: Deployment |
| 74 |
spec: |
| 75 |
- foo |
| 76 |
- bar |
| 77 |
env: |
| 78 |
- foo |
| 79 |
- bar |
| 80 |
- baz |
| 81 |
`, |
| 82 |
expectedOutput: `compact`, |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: "skip wrapped sequence strings, pipe hyphen", |
| 86 |
input: `apiVersion: apps/v1 |
| 87 |
kind: Deployment |
| 88 |
spec: |- |
| 89 |
- foo |
| 90 |
- bar |
| 91 |
`, |
| 92 |
expectedOutput: `compact`, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "skip wrapped sequence strings, pipe", |
| 96 |
input: `apiVersion: apps/v1 |
| 97 |
kind: Deployment |
| 98 |
spec: | |
| 99 |
- foo |
| 100 |
- bar |
| 101 |
`, |
| 102 |
expectedOutput: `compact`, |
| 103 |
}, |
| 104 |
{ |
| 105 |
name: "skip wrapped sequence strings, right angle bracket", |
| 106 |
input: `apiVersion: apps/v1 |
| 107 |
kind: Deployment |
| 108 |
spec: > |
| 109 |
- foo |
| 110 |
- bar |
| 111 |
`, |
| 112 |
expectedOutput: `compact`, |
| 113 |
}, |
| 114 |
{ |
| 115 |
name: "skip wrapped sequence strings, plus", |
| 116 |
input: `apiVersion: apps/v1 |
| 117 |
kind: Deployment |
| 118 |
spec: + |
| 119 |
- foo |
| 120 |
- bar |
| 121 |
`, |
| 122 |
expectedOutput: `compact`, |
| 123 |
}, |
| 124 |
{ |
| 125 |
name: "handle comments", |
| 126 |
input: `apiVersion: v1 |
| 127 |
kind: Service |
| 128 |
spec: |
| 129 |
ports: # comment 1 |
| 130 |
# comment 2 |
| 131 |
- name: etcd-server-ssl |
| 132 |
port: 2380 |
| 133 |
# comment 3 |
| 134 |
- name: etcd-client-ssl |
| 135 |
port: 2379 |
| 136 |
`, |
| 137 |
expectedOutput: `wide`, |
| 138 |
}, |
| 139 |
{ |
| 140 |
name: "nested wide vs compact", |
| 141 |
input: `apiVersion: apps/v1 |
| 142 |
kind: Deployment |
| 143 |
spec: |
| 144 |
foo: |
| 145 |
bar: |
| 146 |
baz: |
| 147 |
bor: |
| 148 |
- a |
| 149 |
- b |
| 150 |
abc: |
| 151 |
- a |
| 152 |
- b |
| 153 |
`, |
| 154 |
expectedOutput: `wide`, |
| 155 |
}, |
| 156 |
{ |
| 157 |
name: "invalid resource but valid yaml sequence", |
| 158 |
input: ` - foo`, |
| 159 |
expectedOutput: `compact`, |
| 160 |
}, |
| 161 |
{ |
| 162 |
name: "invalid resource but valid yaml sequence with comments", |
| 163 |
input: ` |
| 164 |
# comment 1 |
| 165 |
# comment 2 |
| 166 |
- foo |
| 167 |
`, |
| 168 |
expectedOutput: `compact`, |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "- within sequence element", |
| 172 |
input: `apiVersion: apps/v1 |
| 173 |
kind: Deployment |
| 174 |
spec: |
| 175 |
foo: |
| 176 |
- - a`, |
| 177 |
expectedOutput: `wide`, |
| 178 |
}, |
| 179 |
{ |
| 180 |
name: "- within non sequence element", |
| 181 |
input: `apiVersion: apps/v1 |
| 182 |
kind: Deployment |
| 183 |
spec: |
| 184 |
foo: |
| 185 |
a: - b`, |
| 186 |
expectedOutput: `compact`, |
| 187 |
}, |
| 188 |
} |
| 189 |
|
| 190 |
for i := range testCases { |
| 191 |
tc := testCases[i] |
| 192 |
t.Run(tc.name, func(t *testing.T) { |
| 193 |
assert.Equal(t, tc.expectedOutput, DeriveSeqIndentStyle(tc.input)) |
| 194 |
}) |
| 195 |
} |
| 196 |
} |