main_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 2020 Docker, Inc.
  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 main
  14. import (
  15. "io/ioutil"
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. "gotest.tools/v3/assert"
  20. "github.com/docker/compose-cli/cli/cmd"
  21. "github.com/docker/compose-cli/cli/cmd/context"
  22. "github.com/docker/compose-cli/cli/cmd/login"
  23. "github.com/docker/compose-cli/cli/cmd/run"
  24. "github.com/docker/compose-cli/config"
  25. )
  26. var contextSetConfig = []byte(`{
  27. "currentContext": "some-context"
  28. }`)
  29. func TestDetermineCurrentContext(t *testing.T) {
  30. d, err := ioutil.TempDir("", "")
  31. // nolint errcheck
  32. defer os.RemoveAll(d)
  33. assert.NilError(t, err)
  34. err = ioutil.WriteFile(filepath.Join(d, config.ConfigFileName), contextSetConfig, 0644)
  35. assert.NilError(t, err)
  36. // If nothing set, fallback to default
  37. c := determineCurrentContext("", "")
  38. assert.Equal(t, c, "default")
  39. // If context flag set, use that
  40. c = determineCurrentContext("other-context", "")
  41. assert.Equal(t, c, "other-context")
  42. // If no context flag, use config
  43. c = determineCurrentContext("", d)
  44. assert.Equal(t, c, "some-context")
  45. // Ensure context flag overrides config
  46. c = determineCurrentContext("other-context", d)
  47. assert.Equal(t, "other-context", c)
  48. }
  49. func TestCheckOwnCommand(t *testing.T) {
  50. assert.Assert(t, isOwnCommand(login.Command()))
  51. assert.Assert(t, isOwnCommand(context.Command()))
  52. assert.Assert(t, isOwnCommand(cmd.ServeCommand()))
  53. assert.Assert(t, !isOwnCommand(run.Command()))
  54. assert.Assert(t, !isOwnCommand(cmd.ExecCommand()))
  55. assert.Assert(t, !isOwnCommand(cmd.LogsCommand()))
  56. assert.Assert(t, !isOwnCommand(cmd.PsCommand()))
  57. }