| 1 | /* |
| 2 | Copyright The containerd 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 "testing" |
| 20 | |
| 21 | func TestMkcmd(t *testing.T) { |
| 22 | testcases := []struct { |
| 23 | name string |
| 24 | cmd protocCmd |
| 25 | expectedV1 string |
| 26 | expectedV2 string |
| 27 | }{ |
| 28 | { |
| 29 | name: "basic", |
| 30 | cmd: protocCmd{Names: []string{"go"}, Generators: []generator{{Name: "go"}}}, |
| 31 | expectedV1: "protoc -I --go_out=import_path=:", |
| 32 | expectedV2: "protoc -I --go_out=", |
| 33 | }, |
| 34 | { |
| 35 | name: "plugin", |
| 36 | cmd: protocCmd{Names: []string{"go"}, Plugins: []string{"grpc"}, Generators: []generator{{Name: "go"}}}, |
| 37 | expectedV1: "protoc -I --go_out=plugins=grpc,import_path=:", |
| 38 | expectedV2: "protoc -I --go_out=", |
| 39 | }, |
| 40 | { |
| 41 | name: "use protoc-gen-go-grpc instead of plugins", |
| 42 | cmd: protocCmd{Names: []string{"go", "go-grpc"}, Generators: []generator{{Name: "go"}, {Name: "go-grpc"}}}, |
| 43 | expectedV1: "protoc -I --go_out=import_path=: --go-grpc_out=import_path=:", |
| 44 | expectedV2: "protoc -I --go_out= --go-grpc_out=", |
| 45 | }, |
| 46 | { |
| 47 | name: "use custom parameters", |
| 48 | cmd: protocCmd{ |
| 49 | Names: []string{"go", "go-ttrpc"}, |
| 50 | Generators: []generator{ |
| 51 | { |
| 52 | Name: "go", |
| 53 | Parameters: map[string]string{ |
| 54 | "Mpath": "newpath", |
| 55 | }, |
| 56 | }, |
| 57 | { |
| 58 | Name: "go-ttrpc", |
| 59 | Parameters: map[string]string{ |
| 60 | "prefix": "TTRPC", |
| 61 | }, |
| 62 | }, |
| 63 | }, |
| 64 | }, |
| 65 | expectedV1: "protoc -I --go_out=import_path=: --go-ttrpc_out=import_path=:", |
| 66 | expectedV2: "protoc -I --go_out= --go_opt=Mpath=newpath --go-ttrpc_out= --go-ttrpc_opt=prefix=TTRPC", |
| 67 | }, |
| 68 | } |
| 69 | for _, tc := range testcases { |
| 70 | t.Run(tc.name+"V1", func(t *testing.T) { |
| 71 | cmd := &tc.cmd |
| 72 | cmd.Version = 1 |
| 73 | |
| 74 | s, err := cmd.mkcmd() |
| 75 | if err != nil { |
| 76 | t.Fatalf("err must be nil but %+v", err) |
| 77 | } |
| 78 | |
| 79 | if s != tc.expectedV1 { |
| 80 | t.Fatalf(`s must be %q, but %q`, tc.expectedV1, s) |
| 81 | } |
| 82 | }) |
| 83 | t.Run(tc.name+"V2", func(t *testing.T) { |
| 84 | cmd := &tc.cmd |
| 85 | cmd.Version = 2 |
| 86 | |
| 87 | s, err := cmd.mkcmd() |
| 88 | if err != nil { |
| 89 | t.Fatalf("err must be nil but %+v", err) |
| 90 | } |
| 91 | |
| 92 | if s != tc.expectedV2 { |
| 93 | t.Fatalf(`s must be %q, but %q`, tc.expectedV2, s) |
| 94 | } |
| 95 | }) |
| 96 | } |
| 97 | } |