|
1 | 1 | package lib |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "github.com/aliyun/aliyun-cli/cli" |
| 7 | + "github.com/stretchr/testify/assert" |
4 | 8 | "testing" |
5 | 9 | ) |
6 | 10 |
|
7 | 11 | func TestCliBridge(t *testing.T) { |
8 | 12 | NewCommandBridge(configCommand.command) |
9 | 13 | } |
| 14 | + |
| 15 | +func TestParseAndGetEndpoint(t *testing.T) { |
| 16 | + type args struct { |
| 17 | + ctx *cli.Context |
| 18 | + args []string |
| 19 | + } |
| 20 | + w := new(bytes.Buffer) |
| 21 | + stderr := new(bytes.Buffer) |
| 22 | + context := cli.NewCommandContext(w, stderr) |
| 23 | + flag := cli.Flag{ |
| 24 | + Name: "endpoint", |
| 25 | + } |
| 26 | + flag.SetValue("oss-cn-hangzhou.aliyuncs.com") |
| 27 | + context.Flags().Add(&flag) |
| 28 | + |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + args args |
| 32 | + want string |
| 33 | + wantErr assert.ErrorAssertionFunc |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "Valid endpoint from args", |
| 37 | + args: args{ |
| 38 | + ctx: new(cli.Context), |
| 39 | + args: []string{"--endpoint", "oss-cn-shenzhen.aliyuncs.com"}, |
| 40 | + }, |
| 41 | + want: "oss-cn-shenzhen.aliyuncs.com", |
| 42 | + wantErr: assert.NoError, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Valid region from args", |
| 46 | + args: args{ |
| 47 | + ctx: new(cli.Context), |
| 48 | + args: []string{"--region", "cn-shenzhen"}, |
| 49 | + }, |
| 50 | + want: "oss-cn-shenzhen.aliyuncs.com", |
| 51 | + wantErr: assert.NoError, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "Fetch endpoint flag from context", |
| 55 | + args: args{ |
| 56 | + ctx: context, |
| 57 | + }, |
| 58 | + want: "oss-cn-hangzhou.aliyuncs.com", |
| 59 | + wantErr: assert.NoError, |
| 60 | + }, |
| 61 | + } |
| 62 | + for _, tt := range tests { |
| 63 | + t.Run(tt.name, func(t *testing.T) { |
| 64 | + got, err := ParseAndGetEndpoint(tt.args.ctx, tt.args.args) |
| 65 | + if !tt.wantErr(t, err, fmt.Sprintf("ParseAndGetEndpoint(%v, %v)", tt.args.ctx, tt.args.args)) { |
| 66 | + return |
| 67 | + } |
| 68 | + assert.Equalf(t, tt.want, got, "ParseAndGetEndpoint(%v, %v)", tt.args.ctx, tt.args.args) |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
0 commit comments