|
| 1 | +/* |
| 2 | +Copyright 2026 The Crossplane Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/google/go-cmp/cmp" |
| 23 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 24 | +) |
| 25 | + |
| 26 | +func TestConfigFlag(t *testing.T) { |
| 27 | + type want struct { |
| 28 | + path string |
| 29 | + err error |
| 30 | + } |
| 31 | + |
| 32 | + cases := map[string]struct { |
| 33 | + reason string |
| 34 | + args []string |
| 35 | + want want |
| 36 | + }{ |
| 37 | + "Empty": { |
| 38 | + reason: "No args should yield no path and no error.", |
| 39 | + args: nil, |
| 40 | + }, |
| 41 | + "NoConfigFlag": { |
| 42 | + reason: "Argv without --config should yield no path and no error.", |
| 43 | + args: []string{"version"}, |
| 44 | + }, |
| 45 | + "EqualsForm": { |
| 46 | + reason: "--config=PATH should return PATH.", |
| 47 | + args: []string{"--config=/tmp/config.yaml"}, |
| 48 | + want: want{path: "/tmp/config.yaml"}, |
| 49 | + }, |
| 50 | + "SpaceForm": { |
| 51 | + reason: "--config PATH should return PATH from the next argv.", |
| 52 | + args: []string{"--config", "/tmp/config.yaml"}, |
| 53 | + want: want{path: "/tmp/config.yaml"}, |
| 54 | + }, |
| 55 | + "EmptyEquals": { |
| 56 | + reason: "--config= is an explicitly empty value and should return an error.", |
| 57 | + args: []string{"--config="}, |
| 58 | + want: want{err: cmpopts.AnyError}, |
| 59 | + }, |
| 60 | + "MissingValue": { |
| 61 | + reason: "Trailing --config with no following argv should return an error.", |
| 62 | + args: []string{"--config"}, |
| 63 | + want: want{err: cmpopts.AnyError}, |
| 64 | + }, |
| 65 | + "FirstWins": { |
| 66 | + reason: "If --config appears more than once, the first occurrence wins.", |
| 67 | + args: []string{"--config=/first.yaml", "--config=/second.yaml"}, |
| 68 | + want: want{path: "/first.yaml"}, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + for name, tc := range cases { |
| 73 | + t.Run(name, func(t *testing.T) { |
| 74 | + got, err := configFlag(tc.args) |
| 75 | + if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" { |
| 76 | + t.Errorf("\n%s\nconfigFlag(%v): -want err, +got err:\n%s", tc.reason, tc.args, diff) |
| 77 | + } |
| 78 | + if diff := cmp.Diff(tc.want.path, got); diff != "" { |
| 79 | + t.Errorf("\n%s\nconfigFlag(%v): -want, +got:\n%s", tc.reason, tc.args, diff) |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
0 commit comments