1package secrets
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7)
8
9func TestConvertToSopsExtractPath(t *testing.T) {
10 tests := []struct {
11 name string
12 input string
13 expected string
14 }{
15 {
16 name: "empty string",
17 input: "",
18 expected: "",
19 },
20 {
21 name: "single key",
22 input: "password",
23 expected: `["password"]`,
24 },
25 {
26 name: "two level nested",
27 input: "app.secret",
28 expected: `["app"]["secret"]`,
29 },
30 {
31 name: "three level nested",
32 input: "app.database.password",
33 expected: `["app"]["database"]["password"]`,
34 },
35 {
36 name: "key with underscores",
37 input: "app.database_password",
38 expected: `["app"]["database_password"]`,
39 },
40 {
41 name: "key with hyphens",
42 input: "app.api-key",
43 expected: `["app"]["api-key"]`,
44 },
45 {
46 name: "array index only",
47 input: "[0]",
48 expected: `[0]`,
49 },
50 {
51 name: "key with array index",
52 input: "items[0]",
53 expected: `["items"][0]`,
54 },
55 {
56 name: "nested key with array index",
57 input: "app.items[0]",
58 expected: `["app"]["items"][0]`,
59 },
60 {
61 name: "nested key with array then key",
62 input: "app.items[0].name",
63 expected: `["app"]["items"][0]["name"]`,
64 },
65 {
66 name: "multiple array indices",
67 input: "matrix[0][1]",
68 expected: `["matrix"][0][1]`,
69 },
70 {
71 name: "deeply nested with array",
72 input: "config.databases[0].credentials.password",
73 expected: `["config"]["databases"][0]["credentials"]["password"]`,
74 },
75 }
76
77 for _, tt := range tests {
78 t.Run(tt.name, func(t *testing.T) {
79 result := convertToSopsExtractPath(tt.input)
80 assert.Equal(t, tt.expected, result)
81 })
82 }
83}