1
0

env_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. // Copyright 2017 Faissal Elamraoui. All Rights Reserved.
  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. package env
  16. import (
  17. "os"
  18. "testing"
  19. )
  20. func TestEnvGet(t *testing.T) {
  21. gopath := Get("GOPATH", "")
  22. if gopath != os.Getenv("GOPATH") {
  23. t.Error("expected GOPATH not empty.")
  24. }
  25. noExistVar := Get("NOEXISTVAR", "foo")
  26. if noExistVar != "foo" {
  27. t.Errorf("expected NOEXISTVAR to equal foo, got %s.", noExistVar)
  28. }
  29. }
  30. func TestEnvMustGet(t *testing.T) {
  31. gopath, err := MustGet("GOPATH")
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. if gopath != os.Getenv("GOPATH") {
  36. t.Errorf("expected GOPATH to be the same, got %s.", gopath)
  37. }
  38. _, err = MustGet("NOEXISTVAR")
  39. if err == nil {
  40. t.Error("expected error to be non-nil")
  41. }
  42. }
  43. func TestEnvSet(t *testing.T) {
  44. Set("MYVAR", "foo")
  45. myVar := Get("MYVAR", "bar")
  46. if myVar != "foo" {
  47. t.Errorf("expected MYVAR to equal foo, got %s.", myVar)
  48. }
  49. }
  50. func TestEnvMustSet(t *testing.T) {
  51. err := MustSet("FOO", "bar")
  52. if err != nil {
  53. t.Error(err)
  54. }
  55. fooVar := os.Getenv("FOO")
  56. if fooVar != "bar" {
  57. t.Errorf("expected FOO variable to equal bar, got %s.", fooVar)
  58. }
  59. }
  60. func TestEnvGetAll(t *testing.T) {
  61. envMap := GetAll()
  62. if len(envMap) == 0 {
  63. t.Error("expected environment not empty.")
  64. }
  65. }