A command line tool for DigitalOcean services

doit_test.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright 2016 The Doctl Authors All rights reserved.
  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 doit
  14. import (
  15. "os"
  16. "testing"
  17. )
  18. func TestMain(m *testing.M) {
  19. os.Exit(m.Run())
  20. }
  21. func TestVersion(t *testing.T) {
  22. slr1 := &stubLatestRelease{version: "0.1.0"}
  23. slr2 := &stubLatestRelease{version: "1.0.0"}
  24. cases := []struct {
  25. v Version
  26. s string
  27. ver string
  28. slr LatestVersioner
  29. }{
  30. // version with no label
  31. {
  32. v: Version{Major: 0, Minor: 1, Patch: 2},
  33. s: `doctl version 0.1.2`,
  34. ver: "0.1.2",
  35. slr: slr1,
  36. },
  37. // version with label
  38. {
  39. v: Version{Major: 0, Minor: 1, Patch: 2, Label: "dev"},
  40. s: `doctl version 0.1.2-dev`,
  41. ver: "0.1.2-dev",
  42. slr: slr1,
  43. },
  44. // version with label and build
  45. {
  46. v: Version{Major: 0, Minor: 1, Patch: 2, Label: "dev", Build: "12345"},
  47. s: "doctl version 0.1.2-dev\nGit commit hash: 12345",
  48. ver: "0.1.2-dev",
  49. slr: slr1,
  50. },
  51. // version with no label and higher released version
  52. {
  53. v: Version{Major: 0, Minor: 1, Patch: 2},
  54. s: "doctl version 0.1.2\n\"1.0.0\" is a newer release than \"0.1.2\"",
  55. ver: `0.1.2`,
  56. slr: slr2,
  57. },
  58. }
  59. for _, c := range cases {
  60. if got, want := c.v.String(), c.ver; got != want {
  61. t.Errorf("version string for %#v = %q; want = %q", c.v, got, want)
  62. }
  63. if got, want := c.v.Complete(c.slr), c.s; got != want {
  64. t.Errorf("complete version string for %#v = %q; want = %q", c.v, got, want)
  65. }
  66. }
  67. }
  68. type stubLatestRelease struct {
  69. version string
  70. }
  71. func (slr stubLatestRelease) LatestVersion() (string, error) {
  72. return slr.version, nil
  73. }