cmd_span_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmdtrace
  14. import (
  15. "reflect"
  16. "testing"
  17. commands "github.com/docker/compose/v2/cmd/compose"
  18. "github.com/spf13/cobra"
  19. flag "github.com/spf13/pflag"
  20. )
  21. func TestGetFlags(t *testing.T) {
  22. // Initialize flagSet with flags
  23. fs := flag.NewFlagSet("up", flag.ContinueOnError)
  24. var (
  25. detach string
  26. timeout string
  27. )
  28. fs.StringVar(&detach, "detach", "d", "")
  29. fs.StringVar(&timeout, "timeout", "t", "")
  30. _ = fs.Set("detach", "detach")
  31. _ = fs.Set("timeout", "timeout")
  32. tests := []struct {
  33. name string
  34. input *flag.FlagSet
  35. expected []string
  36. }{
  37. {
  38. name: "NoFlags",
  39. input: flag.NewFlagSet("NoFlags", flag.ContinueOnError),
  40. expected: nil,
  41. },
  42. {
  43. name: "Flags",
  44. input: fs,
  45. expected: []string{"detach", "timeout"},
  46. },
  47. }
  48. for _, test := range tests {
  49. t.Run(test.name, func(t *testing.T) {
  50. result := getFlags(test.input)
  51. if !reflect.DeepEqual(result, test.expected) {
  52. t.Errorf("Expected %v, but got %v", test.expected, result)
  53. }
  54. })
  55. }
  56. }
  57. func TestCommandName(t *testing.T) {
  58. tests := []struct {
  59. name string
  60. setupCmd func() *cobra.Command
  61. want []string
  62. }{
  63. {
  64. name: "docker compose alpha watch -> [watch, alpha]",
  65. setupCmd: func() *cobra.Command {
  66. dockerCmd := &cobra.Command{Use: "docker"}
  67. composeCmd := &cobra.Command{Use: commands.PluginName}
  68. alphaCmd := &cobra.Command{Use: "alpha"}
  69. watchCmd := &cobra.Command{Use: "watch"}
  70. dockerCmd.AddCommand(composeCmd)
  71. composeCmd.AddCommand(alphaCmd)
  72. alphaCmd.AddCommand(watchCmd)
  73. return watchCmd
  74. },
  75. want: []string{"watch", "alpha"},
  76. },
  77. {
  78. name: "docker-compose up -> [up]",
  79. setupCmd: func() *cobra.Command {
  80. dockerComposeCmd := &cobra.Command{Use: commands.PluginName}
  81. upCmd := &cobra.Command{Use: "up"}
  82. dockerComposeCmd.AddCommand(upCmd)
  83. return upCmd
  84. },
  85. want: []string{"up"},
  86. },
  87. }
  88. for _, tt := range tests {
  89. t.Run(tt.name, func(t *testing.T) {
  90. cmd := tt.setupCmd()
  91. got := commandName(cmd)
  92. if !reflect.DeepEqual(got, tt.want) {
  93. t.Errorf("commandName() = %v, want %v", got, tt.want)
  94. }
  95. })
  96. }
  97. }