command_line_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2018 Twitch Interactive, Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"). You may not
  4. // use this file except in compliance with the License. A copy of the License is
  5. // located at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // or in the "license" file accompanying this file. This file is distributed on
  10. // an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  11. // express or implied. See the License for the specific language governing
  12. // permissions and limitations under the License.
  13. package main
  14. import (
  15. "errors"
  16. "reflect"
  17. "testing"
  18. )
  19. func TestParseCommandLineParams(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. parameter string
  23. params *commandLineParams
  24. err error
  25. }{
  26. {
  27. "no parameters",
  28. "",
  29. &commandLineParams{
  30. importMap: map[string]string{},
  31. },
  32. nil,
  33. },
  34. {
  35. "unknown parameter",
  36. "k=v",
  37. nil,
  38. errors.New(`unknown parameter "k"`),
  39. },
  40. {
  41. "empty parameter value - no equals sign",
  42. "import_prefix",
  43. nil,
  44. errors.New(`invalid parameter "import_prefix": expected format of parameter to be k=v`),
  45. },
  46. {
  47. "empty parameter value - no value",
  48. "import_prefix=",
  49. nil,
  50. errors.New(`invalid parameter "import_prefix": expected format of parameter to be k=v`),
  51. },
  52. {
  53. "import_prefix parameter",
  54. "import_prefix=github.com/example/repo",
  55. &commandLineParams{
  56. importMap: map[string]string{},
  57. importPrefix: "github.com/example/repo",
  58. },
  59. nil,
  60. },
  61. {
  62. "single import parameter starting with 'M'",
  63. "Mrpcutil/empty.proto=github.com/example/rpcutil",
  64. &commandLineParams{
  65. importMap: map[string]string{
  66. "rpcutil/empty.proto": "github.com/example/rpcutil",
  67. },
  68. },
  69. nil,
  70. },
  71. {
  72. "multiple import parameters starting with 'M'",
  73. "Mrpcutil/empty.proto=github.com/example/rpcutil,Mrpc/haberdasher/service.proto=github.com/example/rpc/haberdasher",
  74. &commandLineParams{
  75. importMap: map[string]string{
  76. "rpcutil/empty.proto": "github.com/example/rpcutil",
  77. "rpc/haberdasher/service.proto": "github.com/example/rpc/haberdasher",
  78. },
  79. },
  80. nil,
  81. },
  82. {
  83. "single import parameter starting with 'go_import_mapping@'",
  84. "go_import_mapping@rpcutil/empty.proto=github.com/example/rpcutil",
  85. &commandLineParams{
  86. importMap: map[string]string{
  87. "rpcutil/empty.proto": "github.com/example/rpcutil",
  88. },
  89. },
  90. nil,
  91. },
  92. {
  93. "multiple import parameters starting with 'go_import_mapping@'",
  94. "go_import_mapping@rpcutil/empty.proto=github.com/example/rpcutil,go_import_mapping@rpc/haberdasher/service.proto=github.com/example/rpc/haberdasher",
  95. &commandLineParams{
  96. importMap: map[string]string{
  97. "rpcutil/empty.proto": "github.com/example/rpcutil",
  98. "rpc/haberdasher/service.proto": "github.com/example/rpc/haberdasher",
  99. },
  100. },
  101. nil,
  102. },
  103. }
  104. for _, tt := range tests {
  105. t.Run(tt.name, func(t *testing.T) {
  106. params, err := parseCommandLineParams(tt.parameter)
  107. switch {
  108. case err != nil:
  109. if tt.err == nil {
  110. t.Fatal(err)
  111. }
  112. if err.Error() != tt.err.Error() {
  113. t.Errorf("got error = %v, want %v", err, tt.err)
  114. }
  115. case err == nil:
  116. if tt.err != nil {
  117. t.Errorf("got error = %v, want %v", err, tt.err)
  118. }
  119. }
  120. if !reflect.DeepEqual(params, tt.params) {
  121. t.Errorf("got params = %v, want %v", params, tt.params)
  122. }
  123. })
  124. }
  125. }