|
1 | 1 | package clicommand_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
4 | 6 | "slices" |
5 | 7 | "strings" |
6 | 8 | "testing" |
7 | 9 |
|
8 | 10 | "github.com/buildkite/agent/v3/clicommand" |
9 | 11 | "github.com/buildkite/agent/v3/logger" |
10 | | - "gotest.tools/v3/assert" |
| 12 | + "github.com/google/go-cmp/cmp" |
11 | 13 | ) |
12 | 14 |
|
13 | 15 | func TestParseSecrets(t *testing.T) { |
14 | 16 | t.Parallel() |
15 | 17 |
|
16 | 18 | for _, tc := range []struct { |
17 | | - name string |
18 | | - inputData string |
19 | | - formatString string |
20 | | - expectedSecrets []string |
21 | | - errorTextContains string |
| 19 | + name string |
| 20 | + inputData string |
| 21 | + formatString string |
| 22 | + applyVarsFilter bool |
| 23 | + wantSecrets []string |
22 | 24 | }{ |
23 | 25 | { |
24 | | - name: "json", |
25 | | - inputData: `{"hello": "world", "password": "hunter2"}`, |
26 | | - formatString: clicommand.FormatStringJSON, |
27 | | - expectedSecrets: []string{"world", "hunter2"}, |
| 26 | + name: "json", |
| 27 | + inputData: `{"hello": "world", "password": "hunter2"}`, |
| 28 | + formatString: clicommand.FormatStringJSON, |
| 29 | + wantSecrets: []string{"world", "hunter2"}, |
28 | 30 | }, |
29 | 31 | { |
30 | | - name: "plaintext", |
31 | | - inputData: "hunter2\n", |
32 | | - formatString: clicommand.FormatStringNone, |
33 | | - expectedSecrets: []string{"hunter2"}, |
| 32 | + name: "plaintext", |
| 33 | + inputData: "hunter2\n", |
| 34 | + formatString: clicommand.FormatStringNone, |
| 35 | + wantSecrets: []string{"hunter2"}, |
34 | 36 | }, |
| 37 | + |
35 | 38 | { |
36 | | - name: "invalid_json", |
37 | | - inputData: `{"hello": 1, "password": "hunter2"}`, |
38 | | - formatString: clicommand.FormatStringJSON, |
39 | | - errorTextContains: "failed to parse as string valued JSON", |
| 39 | + name: "vars filter", |
| 40 | + inputData: `{"HELLO": "1", "MY_PASSWORD": "hunter2"}`, |
| 41 | + applyVarsFilter: true, |
| 42 | + formatString: clicommand.FormatStringJSON, |
| 43 | + wantSecrets: []string{"hunter2"}, |
40 | 44 | }, |
41 | 45 | } { |
42 | 46 | t.Run(tc.name, func(t *testing.T) { |
43 | 47 | t.Parallel() |
44 | 48 |
|
45 | 49 | input := strings.NewReader(tc.inputData) |
46 | | - secrets, err := clicommand.ParseSecrets(logger.Discard, clicommand.RedactorAddConfig{Format: tc.formatString}, input) |
47 | | - if tc.errorTextContains != "" { |
48 | | - assert.ErrorContains(t, err, tc.errorTextContains) |
49 | | - return |
| 50 | + secrets, err := clicommand.ParseSecrets( |
| 51 | + logger.Discard, |
| 52 | + clicommand.RedactorAddConfig{ |
| 53 | + Format: tc.formatString, |
| 54 | + ApplyVarsFilter: tc.applyVarsFilter, |
| 55 | + RedactedVars: *clicommand.RedactedVars.Value, |
| 56 | + }, |
| 57 | + input, |
| 58 | + ) |
| 59 | + if err != nil { |
| 60 | + t.Errorf("clicommand.ParseSecrets(logger, cfg, %q) error = %v", input, err) |
50 | 61 | } |
51 | | - assert.NilError(t, err) |
52 | 62 |
|
53 | 63 | slices.Sort(secrets) |
54 | | - slices.Sort(tc.expectedSecrets) |
55 | | - assert.DeepEqual(t, secrets, tc.expectedSecrets) |
| 64 | + slices.Sort(tc.wantSecrets) |
| 65 | + if diff := cmp.Diff(secrets, tc.wantSecrets); diff != "" { |
| 66 | + t.Errorf("clicommand.ParseSecrets(logger, cfg, %q) secrets diff (-got +want):\n%s", input, diff) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestParseSecrets_JSON(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + |
| 75 | + for _, tc := range []struct { |
| 76 | + name string |
| 77 | + inputData string |
| 78 | + wantError any |
| 79 | + }{ |
| 80 | + { |
| 81 | + name: "type mismatch", |
| 82 | + inputData: `{"hello": 1, "password": "hunter2"}`, |
| 83 | + wantError: new(*json.UnmarshalTypeError), |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "syntax error", |
| 87 | + inputData: `}}{"hello": , "pas: "hun'}`, |
| 88 | + wantError: new(*json.SyntaxError), |
| 89 | + }, |
| 90 | + } { |
| 91 | + t.Run(tc.name, func(t *testing.T) { |
| 92 | + t.Parallel() |
| 93 | + |
| 94 | + input := strings.NewReader(tc.inputData) |
| 95 | + |
| 96 | + _, err := clicommand.ParseSecrets( |
| 97 | + logger.Discard, |
| 98 | + clicommand.RedactorAddConfig{ |
| 99 | + Format: clicommand.FormatStringJSON, |
| 100 | + }, |
| 101 | + input, |
| 102 | + ) |
| 103 | + if !errors.As(err, tc.wantError) { |
| 104 | + t.Errorf("clicommand.ParseSecrets(logger, cfg, %q) error = %v, want error wrapping %T", input, err, tc.wantError) |
| 105 | + } |
56 | 106 | }) |
57 | 107 | } |
58 | 108 | } |
0 commit comments